ems_alloc.c 22 KB

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