ems_alloc.c 16 KB

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