ems_alloc.c 27 KB

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