ems_kfc.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 gc_handle_t
  7. gc_init_internal(gc_heap_t *heap, char *base_addr, gc_size_t heap_max_size)
  8. {
  9. hmu_tree_node_t *root = NULL, *q = NULL;
  10. int ret;
  11. memset(heap, 0, sizeof *heap);
  12. memset(base_addr, 0, heap_max_size);
  13. ret = os_mutex_init(&heap->lock);
  14. if (ret != BHT_OK) {
  15. os_printf("[GC_ERROR]failed to init lock\n");
  16. return NULL;
  17. }
  18. /* init all data structures*/
  19. heap->current_size = heap_max_size;
  20. heap->base_addr = (gc_uint8*)base_addr;
  21. heap->heap_id = (gc_handle_t)heap;
  22. heap->total_free_size = heap->current_size;
  23. heap->highmark_size = 0;
  24. root = &heap->kfc_tree_root;
  25. memset(root, 0, sizeof *root);
  26. root->size = sizeof *root;
  27. hmu_set_ut(&root->hmu_header, HMU_FC);
  28. hmu_set_size(&root->hmu_header, sizeof *root);
  29. q = (hmu_tree_node_t *) heap->base_addr;
  30. memset(q, 0, sizeof *q);
  31. hmu_set_ut(&q->hmu_header, HMU_FC);
  32. hmu_set_size(&q->hmu_header, heap->current_size);
  33. hmu_mark_pinuse(&q->hmu_header);
  34. root->right = q;
  35. q->parent = root;
  36. q->size = heap->current_size;
  37. bh_assert(root->size <= HMU_FC_NORMAL_MAX_SIZE);
  38. #if WASM_ENABLE_MEMORY_TRACING != 0
  39. os_printf("Heap created, total size: %u\n", buf_size);
  40. os_printf(" heap struct size: %u\n", sizeof(gc_heap_t));
  41. os_printf(" actual heap size: %u\n", heap_max_size);
  42. os_printf(" padding bytes: %u\n",
  43. buf_size - sizeof(gc_heap_t) - heap_max_size);
  44. #endif
  45. return heap;
  46. }
  47. gc_handle_t
  48. gc_init_with_pool(char *buf, gc_size_t buf_size)
  49. {
  50. char *buf_end = buf + buf_size;
  51. char *buf_aligned = (char*)(((uintptr_t) buf + 7) & (uintptr_t)~7);
  52. char *base_addr = buf_aligned + sizeof(gc_heap_t);
  53. gc_heap_t *heap = (gc_heap_t*)buf_aligned;
  54. gc_size_t heap_max_size;
  55. if (buf_size < APP_HEAP_SIZE_MIN) {
  56. os_printf("[GC_ERROR]heap init buf size (%u) < %u\n",
  57. buf_size, APP_HEAP_SIZE_MIN);
  58. return NULL;
  59. }
  60. base_addr = (char*) (((uintptr_t) base_addr + 7) & (uintptr_t)~7) + GC_HEAD_PADDING;
  61. heap_max_size = (uint32)(buf_end - base_addr) & (uint32)~7;
  62. return gc_init_internal(heap, base_addr, heap_max_size);
  63. }
  64. gc_handle_t
  65. gc_init_with_struct_and_pool(char *struct_buf, gc_size_t struct_buf_size,
  66. char *pool_buf, gc_size_t pool_buf_size)
  67. {
  68. gc_heap_t *heap = (gc_heap_t*)struct_buf;
  69. char *base_addr = pool_buf + GC_HEAD_PADDING;
  70. char *pool_buf_end = pool_buf + pool_buf_size;
  71. gc_size_t heap_max_size;
  72. if ((((uintptr_t)struct_buf) & 7) != 0) {
  73. os_printf("[GC_ERROR]heap init struct buf not 8-byte aligned\n");
  74. return NULL;
  75. }
  76. if (struct_buf_size < sizeof(gc_handle_t)) {
  77. os_printf("[GC_ERROR]heap init struct buf size (%u) < %u\n",
  78. struct_buf_size, sizeof(gc_handle_t));
  79. return NULL;
  80. }
  81. if ((((uintptr_t)pool_buf) & 7) != 0) {
  82. os_printf("[GC_ERROR]heap init pool buf not 8-byte aligned\n");
  83. return NULL;
  84. }
  85. if (pool_buf_size < APP_HEAP_SIZE_MIN) {
  86. os_printf("[GC_ERROR]heap init buf size (%u) < %u\n",
  87. pool_buf_size, APP_HEAP_SIZE_MIN);
  88. return NULL;
  89. }
  90. heap_max_size = (uint32)(pool_buf_end - base_addr) & (uint32)~7;
  91. return gc_init_internal(heap, base_addr, heap_max_size);
  92. }
  93. int
  94. gc_destroy_with_pool(gc_handle_t handle)
  95. {
  96. gc_heap_t *heap = (gc_heap_t *) handle;
  97. #if BH_ENABLE_GC_VERIFY != 0
  98. hmu_t *cur = (hmu_t*)heap->base_addr;
  99. hmu_t *end = (hmu_t*)((char*)heap->base_addr + heap->current_size);
  100. if (!heap->is_heap_corrupted
  101. && (hmu_t*)((char *)cur + hmu_get_size(cur)) != end) {
  102. os_printf("Memory leak detected:\n");
  103. gci_dump(heap);
  104. #if WASM_ENABLE_SPEC_TEST != 0
  105. while (1);
  106. #endif
  107. }
  108. #endif
  109. os_mutex_destroy(&heap->lock);
  110. memset(heap->base_addr, 0, heap->current_size);
  111. memset(heap, 0, sizeof(gc_heap_t));
  112. return GC_SUCCESS;
  113. }
  114. uint32
  115. gc_get_heap_struct_size()
  116. {
  117. return sizeof(gc_heap_t);
  118. }
  119. static void
  120. adjust_ptr(uint8 **p_ptr, intptr_t offset)
  121. {
  122. if (*p_ptr)
  123. *p_ptr += offset;
  124. }
  125. int
  126. gc_migrate(gc_handle_t handle,
  127. char *pool_buf_new, gc_size_t pool_buf_size)
  128. {
  129. gc_heap_t *heap = (gc_heap_t *)handle;
  130. char *base_addr_new = pool_buf_new + GC_HEAD_PADDING;
  131. char *pool_buf_end = pool_buf_new + pool_buf_size;
  132. intptr_t offset = (uint8*)base_addr_new - (uint8*)heap->base_addr;
  133. hmu_t *cur = NULL, *end = NULL;
  134. hmu_tree_node_t *tree_node;
  135. gc_size_t heap_max_size, size;
  136. if ((((uintptr_t)pool_buf_new) & 7) != 0) {
  137. os_printf("[GC_ERROR]heap migrate pool buf not 8-byte aligned\n");
  138. return GC_ERROR;
  139. }
  140. heap_max_size = (uint32)(pool_buf_end - base_addr_new) & (uint32)~7;
  141. if (pool_buf_end < base_addr_new
  142. || heap_max_size < heap->current_size) {
  143. os_printf("[GC_ERROR]heap migrate invlaid pool buf size\n");
  144. return GC_ERROR;
  145. }
  146. if (offset == 0)
  147. return 0;
  148. heap->base_addr = (uint8*)base_addr_new;
  149. adjust_ptr((uint8**)&heap->kfc_tree_root.left, offset);
  150. adjust_ptr((uint8**)&heap->kfc_tree_root.right, offset);
  151. adjust_ptr((uint8**)&heap->kfc_tree_root.parent, offset);
  152. cur = (hmu_t*)heap->base_addr;
  153. end = (hmu_t*)((char*)heap->base_addr + heap->current_size);
  154. while (cur < end) {
  155. size = hmu_get_size(cur);
  156. bh_assert(size > 0);
  157. if (hmu_get_ut(cur) == HMU_FC && !HMU_IS_FC_NORMAL(size)) {
  158. tree_node = (hmu_tree_node_t *)cur;
  159. adjust_ptr((uint8**)&tree_node->left, offset);
  160. adjust_ptr((uint8**)&tree_node->right, offset);
  161. if (tree_node->parent != &heap->kfc_tree_root)
  162. /* The root node belongs to heap structure,
  163. it is fixed part and isn't changed. */
  164. adjust_ptr((uint8**)&tree_node->parent, offset);
  165. }
  166. cur = (hmu_t*)((char *)cur + size);
  167. }
  168. bh_assert(cur == end);
  169. return 0;
  170. }
  171. void
  172. gc_destroy_lock(gc_handle_t handle)
  173. {
  174. gc_heap_t *heap = (gc_heap_t *) handle;
  175. os_mutex_destroy(&heap->lock);
  176. }
  177. #if BH_ENABLE_GC_VERIFY != 0
  178. void
  179. gci_verify_heap(gc_heap_t *heap)
  180. {
  181. hmu_t *cur = NULL, *end = NULL;
  182. bh_assert(heap && gci_is_heap_valid(heap));
  183. cur = (hmu_t *)heap->base_addr;
  184. end = (hmu_t *)(heap->base_addr + heap->current_size);
  185. while(cur < end) {
  186. hmu_verify(heap, cur);
  187. cur = (hmu_t *)((gc_uint8*)cur + hmu_get_size(cur));
  188. }
  189. bh_assert(cur == end);
  190. }
  191. #endif
  192. void *
  193. gc_heap_stats(void *heap_arg, uint32* stats, int size)
  194. {
  195. int i;
  196. gc_heap_t *heap = (gc_heap_t *) heap_arg;
  197. for (i = 0; i < size; i++) {
  198. switch (i) {
  199. case GC_STAT_TOTAL:
  200. stats[i] = heap->current_size;
  201. break;
  202. case GC_STAT_FREE:
  203. stats[i] = heap->total_free_size;
  204. break;
  205. case GC_STAT_HIGHMARK:
  206. stats[i] = heap->highmark_size;
  207. break;
  208. default:
  209. break;
  210. }
  211. }
  212. return heap;
  213. }