ems_alloc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "ems_gc_internal.h"
  6. #if !defined(NVALGRIND)
  7. #include <valgrind/memcheck.h>
  8. #endif
  9. static int hmu_is_in_heap(gc_heap_t* heap, hmu_t* hmu)
  10. {
  11. return heap && hmu && (gc_uint8*) hmu >= heap->base_addr
  12. && (gc_uint8*) hmu < heap->base_addr + heap->current_size;
  13. }
  14. /* Remove a node from the tree it belongs to*/
  15. /* @p can not be NULL*/
  16. /* @p can not be the ROOT node*/
  17. /* Node @p will be removed from the tree and left,right,parent pointers of node @p will be*/
  18. /* set to be NULL. Other fields will not be touched.*/
  19. /* The tree will be re-organized so that the order conditions are still satisified.*/
  20. BH_STATIC void remove_tree_node(hmu_tree_node_t *p)
  21. {
  22. hmu_tree_node_t *q = NULL, **slot = NULL;
  23. bh_assert(p);
  24. bh_assert(p->parent); /* @p can not be the ROOT node*/
  25. /* get the slot which holds pointer to node p*/
  26. if (p == p->parent->right) {
  27. slot = &p->parent->right;
  28. } else {
  29. bh_assert(p == p->parent->left); /* @p should be a child of its parent*/
  30. slot = &p->parent->left;
  31. }
  32. /* algorithms used to remove node p*/
  33. /* case 1: if p has no left child, replace p with its right child*/
  34. /* case 2: if p has no right child, replace p with its left child*/
  35. /* case 3: otherwise, find p's predecessor, remove it from the tree and replace p with it.*/
  36. /* use predecessor can keep the left <= root < right condition.*/
  37. if (!p->left) {
  38. /* move right child up*/
  39. *slot = p->right;
  40. if (p->right)
  41. p->right->parent = p->parent;
  42. p->left = p->right = p->parent = NULL;
  43. return;
  44. }
  45. if (!p->right) {
  46. /* move left child up*/
  47. *slot = p->left;
  48. p->left->parent = p->parent; /* p->left can never be NULL.*/
  49. p->left = p->right = p->parent = NULL;
  50. return;
  51. }
  52. /* both left & right exist, find p's predecessor at first*/
  53. q = p->left;
  54. while (q->right)
  55. q = q->right;
  56. remove_tree_node(q); /* remove from the tree*/
  57. *slot = q;
  58. q->parent = p->parent;
  59. q->left = p->left;
  60. q->right = p->right;
  61. if (q->left)
  62. q->left->parent = q;
  63. if (q->right)
  64. q->right->parent = q;
  65. p->left = p->right = p->parent = NULL;
  66. }
  67. static void unlink_hmu(gc_heap_t *heap, hmu_t *hmu)
  68. {
  69. gc_size_t size;
  70. bh_assert(gci_is_heap_valid(heap));
  71. bh_assert(
  72. hmu && (gc_uint8*) hmu >= heap->base_addr
  73. && (gc_uint8*) hmu < heap->base_addr + heap->current_size);
  74. bh_assert(hmu_get_ut(hmu) == HMU_FC);
  75. size = hmu_get_size(hmu);
  76. if (HMU_IS_FC_NORMAL(size)) {
  77. uint32 node_idx = size >> 3;
  78. hmu_normal_node_t* node = heap->kfc_normal_list[node_idx].next;
  79. hmu_normal_node_t** p = &(heap->kfc_normal_list[node_idx].next);
  80. while (node) {
  81. if ((hmu_t*) node == hmu) {
  82. *p = node->next;
  83. break;
  84. }
  85. p = &(node->next);
  86. node = node->next;
  87. }
  88. if (!node) {
  89. bh_printf("[GC_ERROR]couldn't find the node in the normal list");
  90. }
  91. } else {
  92. remove_tree_node((hmu_tree_node_t *) hmu);
  93. }
  94. }
  95. static void hmu_set_free_size(hmu_t *hmu)
  96. {
  97. gc_size_t size;
  98. bh_assert(hmu && hmu_get_ut(hmu) == HMU_FC);
  99. size = hmu_get_size(hmu);
  100. *((uint32*) ((char*) hmu + size) - 1) = size;
  101. }
  102. /* Add free chunk back to KFC*/
  103. /* @heap should not be NULL and it should be a valid heap*/
  104. /* @hmu should not be NULL and it should be a HMU of length @size inside @heap*/
  105. /* @hmu should be aligned to 8*/
  106. /* @size should be positive and multiple of 8*/
  107. /* @hmu with size @size will be added into KFC as a new FC.*/
  108. void gci_add_fc(gc_heap_t *heap, hmu_t *hmu, gc_size_t size)
  109. {
  110. hmu_normal_node_t *np = NULL;
  111. hmu_tree_node_t *root = NULL, *tp = NULL, *node = NULL;
  112. uint32 node_idx;
  113. bh_assert(gci_is_heap_valid(heap));
  114. bh_assert(
  115. hmu && (gc_uint8*) hmu >= heap->base_addr
  116. && (gc_uint8*) hmu < heap->base_addr + heap->current_size);
  117. bh_assert(((gc_uint32)(uintptr_t)hmu_to_obj(hmu) & 7) == 0);
  118. bh_assert(
  119. size > 0
  120. && ((gc_uint8*) hmu) + size
  121. <= heap->base_addr + heap->current_size);
  122. bh_assert(!(size & 7));
  123. hmu_set_ut(hmu, HMU_FC);
  124. hmu_set_size(hmu, size);
  125. hmu_set_free_size(hmu);
  126. if (HMU_IS_FC_NORMAL(size)) {
  127. np = (hmu_normal_node_t*) hmu;
  128. node_idx = size >> 3;
  129. np->next = heap->kfc_normal_list[node_idx].next;
  130. heap->kfc_normal_list[node_idx].next = np;
  131. return;
  132. }
  133. /* big block*/
  134. node = (hmu_tree_node_t*) hmu;
  135. node->size = size;
  136. node->left = node->right = node->parent = NULL;
  137. /* find proper node to link this new node to*/
  138. root = &heap->kfc_tree_root;
  139. tp = root;
  140. bh_assert(tp->size < size);
  141. while (1) {
  142. if (tp->size < size) {
  143. if (!tp->right) {
  144. tp->right = node;
  145. node->parent = tp;
  146. break;
  147. }
  148. tp = tp->right;
  149. } else /* tp->size >= size*/
  150. {
  151. if (!tp->left) {
  152. tp->left = node;
  153. node->parent = tp;
  154. break;
  155. }
  156. tp = tp->left;
  157. }
  158. }
  159. }
  160. /* Find a proper hmu for required memory size*/
  161. /* @heap should not be NULL and it should be a valid heap*/
  162. /* @size should cover the header and it should be 8 bytes aligned*/
  163. /* GC will not be performed here.*/
  164. /* Heap extension will not be performed here.*/
  165. /* A proper HMU will be returned. This HMU can include the header and given size. The returned HMU will be aligned to 8 bytes.*/
  166. /* NULL will be returned if there are no proper HMU.*/
  167. BH_STATIC hmu_t *alloc_hmu(gc_heap_t *heap, gc_size_t size)
  168. {
  169. hmu_normal_node_t *node = NULL, *p = NULL;
  170. uint32 node_idx = 0, init_node_idx = 0;
  171. hmu_tree_node_t *root = NULL, *tp = NULL, *last_tp = NULL;
  172. hmu_t *next, *rest;
  173. bh_assert(gci_is_heap_valid(heap));
  174. bh_assert(size > 0 && !(size & 7));
  175. if (size < GC_SMALLEST_SIZE)
  176. size = GC_SMALLEST_SIZE;
  177. /* check normal list at first*/
  178. if (HMU_IS_FC_NORMAL(size)) {
  179. /* find a non-empty slot in normal_node_list with good size*/
  180. init_node_idx = (size >> 3);
  181. for (node_idx = init_node_idx; node_idx < HMU_NORMAL_NODE_CNT;
  182. node_idx++) {
  183. node = heap->kfc_normal_list + node_idx;
  184. if (node->next)
  185. break;
  186. node = NULL;
  187. }
  188. /* not found in normal list*/
  189. if (node) {
  190. bh_assert(node_idx >= init_node_idx);
  191. p = node->next;
  192. node->next = p->next;
  193. bh_assert(((gc_int32)(uintptr_t)hmu_to_obj(p) & 7) == 0);
  194. if ((gc_size_t)node_idx != (uint32)init_node_idx
  195. && ((gc_size_t)node_idx << 3) >= size + GC_SMALLEST_SIZE) { /* with bigger size*/
  196. rest = (hmu_t*) (((char *) p) + size);
  197. gci_add_fc(heap, rest, (node_idx << 3) - size);
  198. hmu_mark_pinuse(rest);
  199. } else {
  200. size = node_idx << 3;
  201. next = (hmu_t*) ((char*) p + size);
  202. if (hmu_is_in_heap(heap, next))
  203. hmu_mark_pinuse(next);
  204. }
  205. #if GC_STAT_DATA != 0
  206. heap->total_free_size -= size;
  207. if ((heap->current_size - heap->total_free_size)
  208. > heap->highmark_size)
  209. heap->highmark_size = heap->current_size
  210. - heap->total_free_size;
  211. #endif
  212. hmu_set_size((hmu_t* ) p, size);
  213. return (hmu_t*) p;
  214. }
  215. }
  216. /* need to find a node in tree*/
  217. root = &heap->kfc_tree_root;
  218. /* find the best node*/
  219. bh_assert(root);
  220. tp = root->right;
  221. while (tp) {
  222. if (tp->size < size) {
  223. tp = tp->right;
  224. continue;
  225. }
  226. /* record the last node with size equal to or bigger than given size*/
  227. last_tp = tp;
  228. tp = tp->left;
  229. }
  230. if (last_tp) {
  231. bh_assert(last_tp->size >= size);
  232. /* alloc in last_p*/
  233. /* remove node last_p from tree*/
  234. remove_tree_node(last_tp);
  235. if (last_tp->size >= size + GC_SMALLEST_SIZE) {
  236. rest = (hmu_t*) ((char*) last_tp + size);
  237. gci_add_fc(heap, rest, last_tp->size - size);
  238. hmu_mark_pinuse(rest);
  239. } else {
  240. size = last_tp->size;
  241. next = (hmu_t*) ((char*) last_tp + size);
  242. if (hmu_is_in_heap(heap, next))
  243. hmu_mark_pinuse(next);
  244. }
  245. #if GC_STAT_DATA != 0
  246. heap->total_free_size -= size;
  247. if ((heap->current_size - heap->total_free_size) > heap->highmark_size)
  248. heap->highmark_size = heap->current_size - heap->total_free_size;
  249. #endif
  250. hmu_set_size((hmu_t* ) last_tp, size);
  251. return (hmu_t*) last_tp;
  252. }
  253. return NULL;
  254. }
  255. /* Find a proper HMU for given size*/
  256. /* @heap should not be NULL and it should be a valid heap*/
  257. /* @size should cover the header and it should be 8 bytes aligned*/
  258. /* This function will try several ways to satisfy the allocation request.*/
  259. /* 1. Find a proper on available HMUs.*/
  260. /* 2. GC will be triggered if 1 failed.*/
  261. /* 3. Find a proper on available HMUS.*/
  262. /* 4. Return NULL if 3 failed*/
  263. /* A proper HMU will be returned. This HMU can include the header and given size. The returned HMU will be aligned to 8 bytes.*/
  264. /* NULL will be returned if there are no proper HMU.*/
  265. BH_STATIC hmu_t* alloc_hmu_ex(gc_heap_t *heap, gc_size_t size)
  266. {
  267. hmu_t *ret = NULL;
  268. bh_assert(gci_is_heap_valid(heap));
  269. bh_assert(size > 0 && !(size & 7));
  270. #ifdef GC_IN_EVERY_ALLOCATION
  271. gci_gc_heap(heap);
  272. ret = alloc_hmu(heap, size);
  273. #else
  274. # if GC_STAT_DATA != 0
  275. if (heap->gc_threshold < heap->total_free_size)
  276. ret = alloc_hmu(heap, size);
  277. # else
  278. ret = alloc_hmu(heap, size);
  279. # endif
  280. if (ret)
  281. return ret;
  282. /*gci_gc_heap(heap);*//* disable gc claim currently */
  283. ret = alloc_hmu(heap, size);
  284. #endif
  285. return ret;
  286. }
  287. unsigned long g_total_malloc = 0;
  288. unsigned long g_total_free = 0;
  289. gc_object_t _gc_alloc_vo_i_heap(void *vheap,
  290. gc_size_t size ALLOC_EXTRA_PARAMETERS)
  291. {
  292. gc_heap_t* heap = (gc_heap_t*) vheap;
  293. hmu_t *hmu = NULL;
  294. gc_object_t ret = (gc_object_t) NULL;
  295. gc_size_t tot_size = 0;
  296. /* align size*/
  297. tot_size = GC_ALIGN_8(size + HMU_SIZE + OBJ_PREFIX_SIZE + OBJ_SUFFIX_SIZE); /* hmu header, prefix, suffix*/
  298. if (tot_size < size)
  299. return NULL;
  300. gct_vm_mutex_lock(&heap->lock);
  301. hmu = alloc_hmu_ex(heap, tot_size);
  302. if (!hmu)
  303. goto FINISH;
  304. g_total_malloc += tot_size;
  305. hmu_set_ut(hmu, HMU_VO);
  306. hmu_unfree_vo(hmu);
  307. #if defined(GC_VERIFY)
  308. hmu_init_prefix_and_suffix(hmu, tot_size, file_name, line_number);
  309. #endif
  310. ret = hmu_to_obj(hmu);
  311. #if BH_ENABLE_MEMORY_PROFILING != 0
  312. bh_printf("HEAP.ALLOC: heap: %p, size: %u", heap, size);
  313. #endif
  314. FINISH:
  315. gct_vm_mutex_unlock(&heap->lock);
  316. return ret;
  317. }
  318. /* see ems_gc.h for description*/
  319. gc_object_t _gc_alloc_jo_i_heap(void *vheap,
  320. gc_size_t size ALLOC_EXTRA_PARAMETERS)
  321. {
  322. gc_heap_t* heap = (gc_heap_t*) vheap;
  323. gc_object_t ret = (gc_object_t) NULL;
  324. hmu_t *hmu = NULL;
  325. gc_size_t tot_size = 0;
  326. bh_assert(gci_is_heap_valid(heap));
  327. /* align size*/
  328. tot_size = GC_ALIGN_8(size + HMU_SIZE + OBJ_PREFIX_SIZE + OBJ_SUFFIX_SIZE); /* hmu header, prefix, suffix*/
  329. if (tot_size < size)
  330. return NULL;
  331. hmu = alloc_hmu_ex(heap, tot_size);
  332. if (!hmu)
  333. goto FINISH;
  334. /* reset all fields*/
  335. memset((char*) hmu + sizeof(*hmu), 0, tot_size - sizeof(*hmu));
  336. /* hmu->header = 0; */
  337. hmu_set_ut(hmu, HMU_JO);
  338. hmu_unmark_jo(hmu);
  339. #if defined(GC_VERIFY)
  340. hmu_init_prefix_and_suffix(hmu, tot_size, file_name, line_number);
  341. #endif
  342. ret = hmu_to_obj(hmu);
  343. #if BH_ENABLE_MEMORY_PROFILING != 0
  344. bh_printf("HEAP.ALLOC: heap: %p, size: %u", heap, size);
  345. #endif
  346. FINISH:
  347. return ret;
  348. }
  349. /* Do some checking to see if given pointer is a possible valid heap*/
  350. /* Return GC_TRUE if all checking passed*/
  351. /* Return GC_FALSE otherwise*/
  352. int gci_is_heap_valid(gc_heap_t *heap)
  353. {
  354. if (!heap)
  355. return GC_FALSE;
  356. if (heap->heap_id != (gc_handle_t) heap)
  357. return GC_FALSE;
  358. return GC_TRUE;
  359. }
  360. int gc_free_i_heap(void *vheap, gc_object_t obj ALLOC_EXTRA_PARAMETERS)
  361. {
  362. gc_heap_t* heap = (gc_heap_t*) vheap;
  363. hmu_t *hmu = NULL;
  364. hmu_t *prev = NULL;
  365. hmu_t *next = NULL;
  366. gc_size_t size = 0;
  367. hmu_type_t ut;
  368. int ret = GC_SUCCESS;
  369. if (!obj) {
  370. return GC_SUCCESS;
  371. }
  372. hmu = obj_to_hmu(obj);
  373. gct_vm_mutex_lock(&heap->lock);
  374. if ((gc_uint8 *) hmu >= heap->base_addr
  375. && (gc_uint8 *) hmu < heap->base_addr + heap->current_size) {
  376. #ifdef GC_VERIFY
  377. hmu_verify(hmu);
  378. #endif
  379. ut = hmu_get_ut(hmu);
  380. if (ut == HMU_VO) {
  381. if (hmu_is_vo_freed(hmu)) {
  382. bh_assert(0);
  383. ret = GC_ERROR;
  384. goto out;
  385. }
  386. size = hmu_get_size(hmu);
  387. g_total_free += size;
  388. #if GC_STAT_DATA != 0
  389. heap->total_free_size += size;
  390. #endif
  391. #if BH_ENABLE_MEMORY_PROFILING != 0
  392. bh_printf("HEAP.FREE, heap: %p, size: %u\n",heap, size);
  393. #endif
  394. if (!hmu_get_pinuse(hmu)) {
  395. prev = (hmu_t*) ((char*) hmu - *((int*) hmu - 1));
  396. if (hmu_is_in_heap(heap, prev) && hmu_get_ut(prev) == HMU_FC) {
  397. size += hmu_get_size(prev);
  398. hmu = prev;
  399. unlink_hmu(heap, prev);
  400. }
  401. }
  402. next = (hmu_t*) ((char*) hmu + size);
  403. if (hmu_is_in_heap(heap, next)) {
  404. if (hmu_get_ut(next) == HMU_FC) {
  405. size += hmu_get_size(next);
  406. unlink_hmu(heap, next);
  407. next = (hmu_t*) ((char*) hmu + size);
  408. }
  409. }
  410. gci_add_fc(heap, hmu, size);
  411. if (hmu_is_in_heap(heap, next)) {
  412. hmu_unmark_pinuse(next);
  413. }
  414. } else {
  415. ret = GC_ERROR;
  416. goto out;
  417. }
  418. ret = GC_SUCCESS;
  419. goto out;
  420. }
  421. out:
  422. gct_vm_mutex_unlock(&heap->lock);
  423. return ret;
  424. }
  425. void gc_dump_heap_stats(gc_heap_t *heap)
  426. {
  427. bh_printf("heap: %p, heap start: %p\n", heap, heap->base_addr);
  428. bh_printf(
  429. "total malloc: totalfree: %u, current: %u, highmark: %u, gc cnt: %u\n",
  430. heap->total_free_size, heap->current_size, heap->highmark_size,
  431. heap->total_gc_count);
  432. bh_printf("g_total_malloc=%lu, g_total_free=%lu, occupied=%lu\n",
  433. g_total_malloc, g_total_free, g_total_malloc - g_total_free);
  434. }
  435. #ifdef GC_TEST
  436. void gci_dump(char* buf, gc_heap_t *heap)
  437. {
  438. hmu_t *cur = NULL, *end = NULL;
  439. hmu_type_t ut;
  440. gc_size_t size;
  441. int i = 0;
  442. int p;
  443. char inuse;
  444. int mark;
  445. cur = (hmu_t*)heap->base_addr;
  446. end = (hmu_t*)((char*)heap->base_addr + heap->current_size);
  447. while(cur < end)
  448. {
  449. ut = hmu_get_ut(cur);
  450. size = hmu_get_size(cur);
  451. p = hmu_get_pinuse(cur);
  452. mark = hmu_is_jo_marked (cur);
  453. if(ut == HMU_VO)
  454. inuse = 'V';
  455. else if(ut == HMU_JO)
  456. inuse = hmu_is_jo_marked(cur) ? 'J' : 'j';
  457. else if(ut == HMU_FC)
  458. inuse = 'F';
  459. bh_assert(size > 0);
  460. buf += sprintf(buf, "#%d %08x %x %x %d %c %d\n", i, (char*) cur - (char*) heap->base_addr, ut, p, mark, inuse, hmu_obj_size(size));
  461. cur = (hmu_t*)((char *)cur + size);
  462. i++;
  463. }
  464. bh_assert(cur == end);
  465. }
  466. #endif