ems_alloc.c 24 KB

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