ems_alloc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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. gc_object_t _gc_realloc_vo_i_heap(void *vheap, void *ptr,
  316. gc_size_t size ALLOC_EXTRA_PARAMETERS)
  317. {
  318. gc_heap_t* heap = (gc_heap_t*) vheap;
  319. hmu_t *hmu = NULL, *hmu_old = NULL;
  320. gc_object_t ret = (gc_object_t) NULL, obj_old = (gc_object_t)ptr;
  321. gc_size_t tot_size = 0, size_old = 0;
  322. if (obj_old) {
  323. hmu_old = obj_to_hmu(obj_old);
  324. size_old = hmu_get_size(hmu_old);
  325. size_old -= HMU_SIZE + OBJ_PREFIX_SIZE + OBJ_SUFFIX_SIZE;
  326. if (size < size_old)
  327. return NULL;
  328. if (size == size_old)
  329. return obj_old;
  330. }
  331. /* align size*/
  332. tot_size = GC_ALIGN_8(size + HMU_SIZE + OBJ_PREFIX_SIZE + OBJ_SUFFIX_SIZE); /* hmu header, prefix, suffix*/
  333. if (tot_size < size)
  334. return NULL;
  335. gct_vm_mutex_lock(&heap->lock);
  336. hmu = alloc_hmu_ex(heap, tot_size);
  337. if (!hmu)
  338. goto FINISH;
  339. g_total_malloc += tot_size;
  340. hmu_set_ut(hmu, HMU_VO);
  341. hmu_unfree_vo(hmu);
  342. #if defined(GC_VERIFY)
  343. hmu_init_prefix_and_suffix(hmu, tot_size, file_name, line_number);
  344. #endif
  345. ret = hmu_to_obj(hmu);
  346. #if BH_ENABLE_MEMORY_PROFILING != 0
  347. bh_printf("HEAP.ALLOC: heap: %p, size: %u", heap, size);
  348. #endif
  349. FINISH:
  350. gct_vm_mutex_unlock(&heap->lock);
  351. if (ret) {
  352. memset(ret, 0, size);
  353. if (obj_old) {
  354. memcpy(ret, obj_old, size_old);
  355. gc_free_h(vheap, obj_old);
  356. }
  357. }
  358. return ret;
  359. }
  360. /* see ems_gc.h for description*/
  361. gc_object_t _gc_alloc_jo_i_heap(void *vheap,
  362. gc_size_t size ALLOC_EXTRA_PARAMETERS)
  363. {
  364. gc_heap_t* heap = (gc_heap_t*) vheap;
  365. gc_object_t ret = (gc_object_t) NULL;
  366. hmu_t *hmu = NULL;
  367. gc_size_t tot_size = 0;
  368. bh_assert(gci_is_heap_valid(heap));
  369. /* align size*/
  370. tot_size = GC_ALIGN_8(size + HMU_SIZE + OBJ_PREFIX_SIZE + OBJ_SUFFIX_SIZE); /* hmu header, prefix, suffix*/
  371. if (tot_size < size)
  372. return NULL;
  373. hmu = alloc_hmu_ex(heap, tot_size);
  374. if (!hmu)
  375. goto FINISH;
  376. /* reset all fields*/
  377. memset((char*) hmu + sizeof(*hmu), 0, tot_size - sizeof(*hmu));
  378. /* hmu->header = 0; */
  379. hmu_set_ut(hmu, HMU_JO);
  380. hmu_unmark_jo(hmu);
  381. #if defined(GC_VERIFY)
  382. hmu_init_prefix_and_suffix(hmu, tot_size, file_name, line_number);
  383. #endif
  384. ret = hmu_to_obj(hmu);
  385. #if BH_ENABLE_MEMORY_PROFILING != 0
  386. bh_printf("HEAP.ALLOC: heap: %p, size: %u", heap, size);
  387. #endif
  388. FINISH:
  389. return ret;
  390. }
  391. /* Do some checking to see if given pointer is a possible valid heap*/
  392. /* Return GC_TRUE if all checking passed*/
  393. /* Return GC_FALSE otherwise*/
  394. int gci_is_heap_valid(gc_heap_t *heap)
  395. {
  396. if (!heap)
  397. return GC_FALSE;
  398. if (heap->heap_id != (gc_handle_t) heap)
  399. return GC_FALSE;
  400. return GC_TRUE;
  401. }
  402. int gc_free_i_heap(void *vheap, gc_object_t obj ALLOC_EXTRA_PARAMETERS)
  403. {
  404. gc_heap_t* heap = (gc_heap_t*) vheap;
  405. hmu_t *hmu = NULL;
  406. hmu_t *prev = NULL;
  407. hmu_t *next = NULL;
  408. gc_size_t size = 0;
  409. hmu_type_t ut;
  410. int ret = GC_SUCCESS;
  411. if (!obj) {
  412. return GC_SUCCESS;
  413. }
  414. hmu = obj_to_hmu(obj);
  415. gct_vm_mutex_lock(&heap->lock);
  416. if ((gc_uint8 *) hmu >= heap->base_addr
  417. && (gc_uint8 *) hmu < heap->base_addr + heap->current_size) {
  418. #ifdef GC_VERIFY
  419. hmu_verify(hmu);
  420. #endif
  421. ut = hmu_get_ut(hmu);
  422. if (ut == HMU_VO) {
  423. if (hmu_is_vo_freed(hmu)) {
  424. bh_assert(0);
  425. ret = GC_ERROR;
  426. goto out;
  427. }
  428. size = hmu_get_size(hmu);
  429. g_total_free += size;
  430. #if GC_STAT_DATA != 0
  431. heap->total_free_size += size;
  432. #endif
  433. #if BH_ENABLE_MEMORY_PROFILING != 0
  434. bh_printf("HEAP.FREE, heap: %p, size: %u\n",heap, size);
  435. #endif
  436. if (!hmu_get_pinuse(hmu)) {
  437. prev = (hmu_t*) ((char*) hmu - *((int*) hmu - 1));
  438. if (hmu_is_in_heap(heap, prev) && hmu_get_ut(prev) == HMU_FC) {
  439. size += hmu_get_size(prev);
  440. hmu = prev;
  441. unlink_hmu(heap, prev);
  442. }
  443. }
  444. next = (hmu_t*) ((char*) hmu + size);
  445. if (hmu_is_in_heap(heap, next)) {
  446. if (hmu_get_ut(next) == HMU_FC) {
  447. size += hmu_get_size(next);
  448. unlink_hmu(heap, next);
  449. next = (hmu_t*) ((char*) hmu + size);
  450. }
  451. }
  452. gci_add_fc(heap, hmu, size);
  453. if (hmu_is_in_heap(heap, next)) {
  454. hmu_unmark_pinuse(next);
  455. }
  456. } else {
  457. ret = GC_ERROR;
  458. goto out;
  459. }
  460. ret = GC_SUCCESS;
  461. goto out;
  462. }
  463. out:
  464. gct_vm_mutex_unlock(&heap->lock);
  465. return ret;
  466. }
  467. void gc_dump_heap_stats(gc_heap_t *heap)
  468. {
  469. bh_printf("heap: %p, heap start: %p\n", heap, heap->base_addr);
  470. bh_printf(
  471. "total malloc: totalfree: %u, current: %u, highmark: %u, gc cnt: %u\n",
  472. heap->total_free_size, heap->current_size, heap->highmark_size,
  473. heap->total_gc_count);
  474. bh_printf("g_total_malloc=%lu, g_total_free=%lu, occupied=%lu\n",
  475. g_total_malloc, g_total_free, g_total_malloc - g_total_free);
  476. }
  477. #ifdef GC_TEST
  478. void gci_dump(char* buf, gc_heap_t *heap)
  479. {
  480. hmu_t *cur = NULL, *end = NULL;
  481. hmu_type_t ut;
  482. gc_size_t size;
  483. int i = 0;
  484. int p;
  485. char inuse;
  486. int mark;
  487. cur = (hmu_t*)heap->base_addr;
  488. end = (hmu_t*)((char*)heap->base_addr + heap->current_size);
  489. while(cur < end)
  490. {
  491. ut = hmu_get_ut(cur);
  492. size = hmu_get_size(cur);
  493. p = hmu_get_pinuse(cur);
  494. mark = hmu_is_jo_marked (cur);
  495. if(ut == HMU_VO)
  496. inuse = 'V';
  497. else if(ut == HMU_JO)
  498. inuse = hmu_is_jo_marked(cur) ? 'J' : 'j';
  499. else if(ut == HMU_FC)
  500. inuse = 'F';
  501. bh_assert(size > 0);
  502. 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));
  503. cur = (hmu_t*)((char *)cur + size);
  504. i++;
  505. }
  506. bh_assert(cur == end);
  507. }
  508. #endif