ems_alloc.c 22 KB

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