ems_alloc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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, *hmu_next;
  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, tot_size_next;
  349. gc_size_t obj_size, obj_size_old;
  350. hmu_type_t ut;
  351. /* hmu header + prefix + obj + suffix */
  352. tot_size_unaligned = HMU_SIZE + OBJ_PREFIX_SIZE + size + OBJ_SUFFIX_SIZE;
  353. /* aligned size*/
  354. tot_size = GC_ALIGN_8(tot_size_unaligned);
  355. if (tot_size < size)
  356. /* integer overflow */
  357. return NULL;
  358. if (obj_old) {
  359. hmu_old = obj_to_hmu(obj_old);
  360. tot_size_old = hmu_get_size(hmu_old);
  361. if (tot_size <= tot_size_old)
  362. /* current node alreay meets requirement */
  363. return obj_old;
  364. }
  365. os_mutex_lock(&heap->lock);
  366. if (hmu_old) {
  367. hmu_next = (hmu_t*)((char *)hmu_old + tot_size_old);
  368. if (hmu_is_in_heap(heap, hmu_next)) {
  369. ut = hmu_get_ut(hmu_next);
  370. tot_size_next = hmu_get_size(hmu_next);
  371. if (ut == HMU_FC
  372. && tot_size <= tot_size_old + tot_size_next) {
  373. /* current node and next node meets requirement */
  374. unlink_hmu(heap, hmu_next);
  375. hmu_set_size(hmu_old, tot_size);
  376. memset((char*)hmu_old + tot_size_old, 0, tot_size - tot_size_old);
  377. #if BH_ENABLE_GC_VERIFY != 0
  378. hmu_init_prefix_and_suffix(hmu_old, tot_size, file, line);
  379. #endif
  380. if (tot_size < tot_size_old + tot_size_next) {
  381. hmu_next = (hmu_t*)((char*)hmu_old + tot_size);
  382. tot_size_next = tot_size_old + tot_size_next - tot_size;
  383. gci_add_fc(heap, hmu_next, tot_size_next);
  384. }
  385. os_mutex_unlock(&heap->lock);
  386. return obj_old;
  387. }
  388. }
  389. }
  390. hmu = alloc_hmu_ex(heap, tot_size);
  391. if (!hmu)
  392. goto finish;
  393. g_total_malloc += tot_size;
  394. hmu_set_ut(hmu, HMU_VO);
  395. hmu_unfree_vo(hmu);
  396. #if BH_ENABLE_GC_VERIFY != 0
  397. hmu_init_prefix_and_suffix(hmu, tot_size, file, line);
  398. #endif
  399. ret = hmu_to_obj(hmu);
  400. #if BH_ENABLE_MEMORY_PROFILING != 0
  401. os_printf("HEAP.ALLOC: heap: %p, size: %u\n", heap, size);
  402. #endif
  403. finish:
  404. os_mutex_unlock(&heap->lock);
  405. if (ret) {
  406. obj_size = tot_size - HMU_SIZE - OBJ_PREFIX_SIZE - OBJ_SUFFIX_SIZE;
  407. memset(ret, 0, obj_size);
  408. if (obj_old) {
  409. obj_size_old = tot_size_old - HMU_SIZE
  410. - OBJ_PREFIX_SIZE - OBJ_SUFFIX_SIZE;
  411. bh_memcpy_s(ret, obj_size, obj_old, obj_size_old);
  412. gc_free_vo(vheap, obj_old);
  413. }
  414. }
  415. return ret;
  416. }
  417. /**
  418. * Do some checking to see if given pointer is a possible valid heap
  419. * @return GC_TRUE if all checking passed, GC_FALSE otherwise
  420. */
  421. int
  422. gci_is_heap_valid(gc_heap_t *heap)
  423. {
  424. if (!heap)
  425. return GC_FALSE;
  426. if (heap->heap_id != (gc_handle_t) heap)
  427. return GC_FALSE;
  428. return GC_TRUE;
  429. }
  430. #if BH_ENABLE_GC_VERIFY == 0
  431. int
  432. gc_free_vo(void *vheap, gc_object_t obj)
  433. #else
  434. int
  435. gc_free_vo_internal(void *vheap, gc_object_t obj,
  436. const char *file, int line)
  437. #endif
  438. {
  439. gc_heap_t* heap = (gc_heap_t*) vheap;
  440. hmu_t *hmu = NULL;
  441. hmu_t *prev = NULL;
  442. hmu_t *next = NULL;
  443. gc_size_t size = 0;
  444. hmu_type_t ut;
  445. int ret = GC_SUCCESS;
  446. if (!obj) {
  447. return GC_SUCCESS;
  448. }
  449. hmu = obj_to_hmu(obj);
  450. os_mutex_lock(&heap->lock);
  451. if ((gc_uint8 *)hmu >= heap->base_addr
  452. && (gc_uint8 *)hmu < heap->base_addr + heap->current_size) {
  453. #if BH_ENABLE_GC_VERIFY != 0
  454. hmu_verify(hmu);
  455. #endif
  456. ut = hmu_get_ut(hmu);
  457. if (ut == HMU_VO) {
  458. if (hmu_is_vo_freed(hmu)) {
  459. bh_assert(0);
  460. ret = GC_ERROR;
  461. goto out;
  462. }
  463. size = hmu_get_size(hmu);
  464. g_total_free += size;
  465. heap->total_free_size += size;
  466. #if BH_ENABLE_MEMORY_PROFILING != 0
  467. os_printf("HEAP.FREE, heap: %p, size: %u\n", heap, size);
  468. #endif
  469. if (!hmu_get_pinuse(hmu)) {
  470. prev = (hmu_t*) ((char*) hmu - *((int*) hmu - 1));
  471. if (hmu_is_in_heap(heap, prev) && hmu_get_ut(prev) == HMU_FC) {
  472. size += hmu_get_size(prev);
  473. hmu = prev;
  474. unlink_hmu(heap, prev);
  475. }
  476. }
  477. next = (hmu_t*) ((char*) hmu + size);
  478. if (hmu_is_in_heap(heap, next)) {
  479. if (hmu_get_ut(next) == HMU_FC) {
  480. size += hmu_get_size(next);
  481. unlink_hmu(heap, next);
  482. next = (hmu_t*) ((char*) hmu + size);
  483. }
  484. }
  485. gci_add_fc(heap, hmu, size);
  486. if (hmu_is_in_heap(heap, next)) {
  487. hmu_unmark_pinuse(next);
  488. }
  489. } else {
  490. ret = GC_ERROR;
  491. goto out;
  492. }
  493. ret = GC_SUCCESS;
  494. goto out;
  495. }
  496. out:
  497. os_mutex_unlock(&heap->lock);
  498. return ret;
  499. }
  500. void
  501. gc_dump_heap_stats(gc_heap_t *heap)
  502. {
  503. os_printf("heap: %p, heap start: %p\n", heap, heap->base_addr);
  504. os_printf("total free: %u, current: %u, highmark: %u\n",
  505. heap->total_free_size, heap->current_size, heap->highmark_size);
  506. os_printf("g_total_malloc=%lu, g_total_free=%lu, occupied=%lu\n",
  507. g_total_malloc, g_total_free, g_total_malloc - g_total_free);
  508. }
  509. void
  510. gci_dump(gc_heap_t *heap)
  511. {
  512. hmu_t *cur = NULL, *end = NULL;
  513. hmu_type_t ut;
  514. gc_size_t size;
  515. int i = 0, p, mark;
  516. char inuse = 'U';
  517. cur = (hmu_t*)heap->base_addr;
  518. end = (hmu_t*)((char*)heap->base_addr + heap->current_size);
  519. while(cur < end) {
  520. ut = hmu_get_ut(cur);
  521. size = hmu_get_size(cur);
  522. p = hmu_get_pinuse(cur);
  523. mark = hmu_is_jo_marked (cur);
  524. if (ut == HMU_VO)
  525. inuse = 'V';
  526. else if (ut == HMU_JO)
  527. inuse = hmu_is_jo_marked(cur) ? 'J' : 'j';
  528. else if (ut == HMU_FC)
  529. inuse = 'F';
  530. bh_assert(size > 0);
  531. os_printf("#%d %08x %x %x %d %c %d\n",
  532. i, (int32)((char*) cur - (char*) heap->base_addr),
  533. ut, p, mark, inuse, (int32)hmu_obj_size(size));
  534. #if BH_ENABLE_GC_VERIFY != 0
  535. if (inuse == 'V') {
  536. gc_object_prefix_t *prefix = (gc_object_prefix_t *)(cur + 1);
  537. os_printf("#%s:%d\n", prefix->file_name, prefix->line_no);
  538. }
  539. #endif
  540. cur = (hmu_t*)((char *)cur + size);
  541. i++;
  542. }
  543. bh_assert(cur == end);
  544. }