ems_alloc.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  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. hmu_mark_pinuse(hmu_next);
  550. }
  551. UNLOCK_HEAP(heap);
  552. return obj_old;
  553. }
  554. }
  555. }
  556. hmu = alloc_hmu_ex(heap, tot_size);
  557. if (!hmu)
  558. goto finish;
  559. bh_assert(hmu_get_size(hmu) >= tot_size);
  560. /* the total size allocated may be larger than
  561. the required size, reset it here */
  562. tot_size = hmu_get_size(hmu);
  563. #if GC_STAT_DATA != 0
  564. heap->total_size_allocated += tot_size;
  565. #endif
  566. hmu_set_ut(hmu, HMU_VO);
  567. hmu_unfree_vo(hmu);
  568. #if BH_ENABLE_GC_VERIFY != 0
  569. hmu_init_prefix_and_suffix(hmu, tot_size, file, line);
  570. #endif
  571. ret = hmu_to_obj(hmu);
  572. finish:
  573. if (ret) {
  574. obj_size = tot_size - HMU_SIZE - OBJ_PREFIX_SIZE - OBJ_SUFFIX_SIZE;
  575. memset(ret, 0, obj_size);
  576. if (obj_old) {
  577. obj_size_old =
  578. tot_size_old - HMU_SIZE - OBJ_PREFIX_SIZE - OBJ_SUFFIX_SIZE;
  579. bh_memcpy_s(ret, obj_size, obj_old, obj_size_old);
  580. }
  581. }
  582. UNLOCK_HEAP(heap);
  583. if (ret && obj_old)
  584. gc_free_vo(vheap, obj_old);
  585. return ret;
  586. }
  587. #if GC_MANUALLY != 0
  588. void
  589. gc_free_wo(void *vheap, void *ptr)
  590. {
  591. gc_heap_t *heap = (gc_heap_t *)vheap;
  592. gc_object_t *obj = (gc_object_t *)ptr;
  593. hmu_t *hmu = obj_to_hmu(obj);
  594. bh_assert(gci_is_heap_valid(heap));
  595. bh_assert(obj);
  596. bh_assert((gc_uint8 *)hmu >= heap->base_addr
  597. && (gc_uint8 *)hmu < heap->base_addr + heap->current_size);
  598. bh_assert(hmu_get_ut(hmu) == HMU_WO);
  599. hmu_unmark_wo(hmu);
  600. (void)heap;
  601. }
  602. #endif
  603. /* see ems_gc.h for description*/
  604. #if BH_ENABLE_GC_VERIFY == 0
  605. gc_object_t
  606. gc_alloc_wo(void *vheap, gc_size_t size)
  607. #else
  608. gc_object_t
  609. gc_alloc_wo_internal(void *vheap, gc_size_t size, const char *file, int line)
  610. #endif
  611. {
  612. gc_heap_t *heap = (gc_heap_t *)vheap;
  613. hmu_t *hmu = NULL;
  614. gc_object_t ret = (gc_object_t)NULL;
  615. gc_size_t tot_size = 0, tot_size_unaligned;
  616. /* hmu header + prefix + obj + suffix */
  617. tot_size_unaligned = HMU_SIZE + OBJ_PREFIX_SIZE + size + OBJ_SUFFIX_SIZE;
  618. /* aligned size*/
  619. tot_size = GC_ALIGN_8(tot_size_unaligned);
  620. if (tot_size < size)
  621. /* integer overflow */
  622. return NULL;
  623. if (heap->is_heap_corrupted) {
  624. os_printf("[GC_ERROR]Heap is corrupted, allocate memory failed.\n");
  625. return NULL;
  626. }
  627. LOCK_HEAP(heap);
  628. hmu = alloc_hmu_ex(heap, tot_size);
  629. if (!hmu)
  630. goto finish;
  631. /* Do we need to memset the memory to 0? */
  632. /* memset((char *)hmu + sizeof(*hmu), 0, tot_size - sizeof(*hmu)); */
  633. bh_assert(hmu_get_size(hmu) >= tot_size);
  634. /* the total size allocated may be larger than
  635. the required size, reset it here */
  636. tot_size = hmu_get_size(hmu);
  637. #if GC_STAT_DATA != 0
  638. heap->total_size_allocated += tot_size;
  639. #endif
  640. hmu_set_ut(hmu, HMU_WO);
  641. #if GC_MANUALLY != 0
  642. hmu_mark_wo(hmu);
  643. #else
  644. hmu_unmark_wo(hmu);
  645. #endif
  646. #if BH_ENABLE_GC_VERIFY != 0
  647. hmu_init_prefix_and_suffix(hmu, tot_size, file, line);
  648. #endif
  649. ret = hmu_to_obj(hmu);
  650. if (tot_size > tot_size_unaligned)
  651. /* clear buffer appended by GC_ALIGN_8() */
  652. memset((uint8 *)ret + size, 0, tot_size - tot_size_unaligned);
  653. finish:
  654. UNLOCK_HEAP(heap);
  655. return ret;
  656. }
  657. /**
  658. * Do some checking to see if given pointer is a possible valid heap
  659. * @return GC_TRUE if all checking passed, GC_FALSE otherwise
  660. */
  661. int
  662. gci_is_heap_valid(gc_heap_t *heap)
  663. {
  664. if (!heap)
  665. return GC_FALSE;
  666. if (heap->heap_id != (gc_handle_t)heap)
  667. return GC_FALSE;
  668. return GC_TRUE;
  669. }
  670. #if BH_ENABLE_GC_VERIFY == 0
  671. int
  672. gc_free_vo(void *vheap, gc_object_t obj)
  673. #else
  674. int
  675. gc_free_vo_internal(void *vheap, gc_object_t obj, const char *file, int line)
  676. #endif
  677. {
  678. gc_heap_t *heap = (gc_heap_t *)vheap;
  679. gc_uint8 *base_addr, *end_addr;
  680. hmu_t *hmu = NULL;
  681. hmu_t *prev = NULL;
  682. hmu_t *next = NULL;
  683. gc_size_t size = 0;
  684. hmu_type_t ut;
  685. int ret = GC_SUCCESS;
  686. if (!obj) {
  687. return GC_SUCCESS;
  688. }
  689. if (heap->is_heap_corrupted) {
  690. os_printf("[GC_ERROR]Heap is corrupted, free memory failed.\n");
  691. return GC_ERROR;
  692. }
  693. hmu = obj_to_hmu(obj);
  694. base_addr = heap->base_addr;
  695. end_addr = base_addr + heap->current_size;
  696. LOCK_HEAP(heap);
  697. if (hmu_is_in_heap(hmu, base_addr, end_addr)) {
  698. #if BH_ENABLE_GC_VERIFY != 0
  699. hmu_verify(heap, hmu);
  700. #endif
  701. ut = hmu_get_ut(hmu);
  702. if (ut == HMU_VO) {
  703. if (hmu_is_vo_freed(hmu)) {
  704. bh_assert(0);
  705. ret = GC_ERROR;
  706. goto out;
  707. }
  708. size = hmu_get_size(hmu);
  709. heap->total_free_size += size;
  710. #if GC_STAT_DATA != 0
  711. heap->total_size_freed += size;
  712. #endif
  713. if (!hmu_get_pinuse(hmu)) {
  714. prev = (hmu_t *)((char *)hmu - *((int *)hmu - 1));
  715. if (hmu_is_in_heap(prev, base_addr, end_addr)
  716. && hmu_get_ut(prev) == HMU_FC) {
  717. size += hmu_get_size(prev);
  718. hmu = prev;
  719. if (!unlink_hmu(heap, prev)) {
  720. ret = GC_ERROR;
  721. goto out;
  722. }
  723. }
  724. }
  725. next = (hmu_t *)((char *)hmu + size);
  726. if (hmu_is_in_heap(next, base_addr, end_addr)) {
  727. if (hmu_get_ut(next) == HMU_FC) {
  728. size += hmu_get_size(next);
  729. if (!unlink_hmu(heap, next)) {
  730. ret = GC_ERROR;
  731. goto out;
  732. }
  733. next = (hmu_t *)((char *)hmu + size);
  734. }
  735. }
  736. if (!gci_add_fc(heap, hmu, size)) {
  737. ret = GC_ERROR;
  738. goto out;
  739. }
  740. if (hmu_is_in_heap(next, base_addr, end_addr)) {
  741. hmu_unmark_pinuse(next);
  742. }
  743. }
  744. else {
  745. ret = GC_ERROR;
  746. goto out;
  747. }
  748. ret = GC_SUCCESS;
  749. goto out;
  750. }
  751. out:
  752. UNLOCK_HEAP(heap);
  753. return ret;
  754. }
  755. void
  756. gc_dump_heap_stats(gc_heap_t *heap)
  757. {
  758. os_printf("heap: %p, heap start: %p\n", heap, heap->base_addr);
  759. os_printf("total free: %" PRIu32 ", current: %" PRIu32
  760. ", highmark: %" PRIu32 "\n",
  761. heap->total_free_size, heap->current_size, heap->highmark_size);
  762. #if GC_STAT_DATA != 0
  763. os_printf("total size allocated: %" PRIu64 ", total size freed: %" PRIu64
  764. ", total occupied: %" PRIu64 "\n",
  765. heap->total_size_allocated, heap->total_size_freed,
  766. heap->total_size_allocated - heap->total_size_freed);
  767. #endif
  768. }
  769. uint32
  770. gc_get_heap_highmark_size(gc_heap_t *heap)
  771. {
  772. return heap->highmark_size;
  773. }
  774. void
  775. gci_dump(gc_heap_t *heap)
  776. {
  777. hmu_t *cur = NULL, *end = NULL;
  778. hmu_type_t ut;
  779. gc_size_t size;
  780. int i = 0, p, mark;
  781. char inuse = 'U';
  782. cur = (hmu_t *)heap->base_addr;
  783. end = (hmu_t *)((char *)heap->base_addr + heap->current_size);
  784. while (cur < end) {
  785. ut = hmu_get_ut(cur);
  786. size = hmu_get_size(cur);
  787. p = hmu_get_pinuse(cur);
  788. mark = hmu_is_wo_marked(cur);
  789. if (ut == HMU_VO)
  790. inuse = 'V';
  791. else if (ut == HMU_WO)
  792. inuse = hmu_is_wo_marked(cur) ? 'W' : 'w';
  793. else if (ut == HMU_FC)
  794. inuse = 'F';
  795. if (size == 0 || size > (uint32)((uint8 *)end - (uint8 *)cur)) {
  796. os_printf("[GC_ERROR]Heap is corrupted, heap dump failed.\n");
  797. heap->is_heap_corrupted = true;
  798. return;
  799. }
  800. os_printf("#%d %08" PRIx32 " %" PRIx32 " %d %d"
  801. " %c %" PRId32 "\n",
  802. i, (int32)((char *)cur - (char *)heap->base_addr), (int32)ut,
  803. p, mark, inuse, (int32)hmu_obj_size(size));
  804. #if BH_ENABLE_GC_VERIFY != 0
  805. if (inuse == 'V') {
  806. gc_object_prefix_t *prefix = (gc_object_prefix_t *)(cur + 1);
  807. os_printf("#%s:%d\n", prefix->file_name, prefix->line_no);
  808. }
  809. #endif
  810. cur = (hmu_t *)((char *)cur + size);
  811. i++;
  812. }
  813. if (cur != end) {
  814. os_printf("[GC_ERROR]Heap is corrupted, heap dump failed.\n");
  815. heap->is_heap_corrupted = true;
  816. }
  817. }