ems_alloc.c 17 KB

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