ems_kfc.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. gc_handle_t
  7. gc_init_with_pool(char *buf, gc_size_t buf_size)
  8. {
  9. char *buf_end = buf + buf_size;
  10. char *buf_aligned = (char*) (((uintptr_t) buf + 7) & (uintptr_t)~7);
  11. char *base_addr = buf_aligned + sizeof(gc_heap_t);
  12. gc_heap_t *heap = (gc_heap_t*) buf_aligned;
  13. gc_size_t heap_max_size;
  14. hmu_normal_node_t *p = NULL;
  15. hmu_tree_node_t *root = NULL, *q = NULL;
  16. int i = 0, ret;
  17. if (buf_size < 1024) {
  18. os_printf("[GC_ERROR]heap_init_size(%d) < 1024\n", buf_size);
  19. return NULL;
  20. }
  21. base_addr = (char*) (((uintptr_t) base_addr + 7) & (uintptr_t)~7) + GC_HEAD_PADDING;
  22. heap_max_size = (uint32)(buf_end - base_addr) & (uint32)~7;
  23. memset(heap, 0, sizeof *heap);
  24. memset(base_addr, 0, heap_max_size);
  25. ret = os_mutex_init(&heap->lock);
  26. if (ret != BHT_OK) {
  27. os_printf("[GC_ERROR]failed to init lock\n");
  28. return NULL;
  29. }
  30. /* init all data structures*/
  31. heap->current_size = heap_max_size;
  32. heap->base_addr = (gc_uint8*)base_addr;
  33. heap->heap_id = (gc_handle_t)heap;
  34. heap->total_free_size = heap->current_size;
  35. heap->highmark_size = 0;
  36. for (i = 0; i < HMU_NORMAL_NODE_CNT; i++) {
  37. /* make normal node look like a FC*/
  38. p = &heap->kfc_normal_list[i];
  39. memset(p, 0, sizeof *p);
  40. hmu_set_ut(&p->hmu_header, HMU_FC);
  41. hmu_set_size(&p->hmu_header, sizeof *p);
  42. }
  43. root = &heap->kfc_tree_root;
  44. memset(root, 0, sizeof *root);
  45. root->size = sizeof *root;
  46. hmu_set_ut(&root->hmu_header, HMU_FC);
  47. hmu_set_size(&root->hmu_header, sizeof *root);
  48. q = (hmu_tree_node_t *) heap->base_addr;
  49. memset(q, 0, sizeof *q);
  50. hmu_set_ut(&q->hmu_header, HMU_FC);
  51. hmu_set_size(&q->hmu_header, heap->current_size);
  52. hmu_mark_pinuse(&q->hmu_header);
  53. root->right = q;
  54. q->parent = root;
  55. q->size = heap->current_size;
  56. bh_assert(root->size <= HMU_FC_NORMAL_MAX_SIZE
  57. && HMU_FC_NORMAL_MAX_SIZE < q->size);
  58. #if BH_ENABLE_MEMORY_PROFILING != 0
  59. os_printf("heap is successfully initialized with max_size=%u.\n",
  60. heap_max_size);
  61. #endif
  62. return heap;
  63. }
  64. int
  65. gc_destroy_with_pool(gc_handle_t handle)
  66. {
  67. gc_heap_t *heap = (gc_heap_t *) handle;
  68. #if BH_ENABLE_GC_VERIFY != 0
  69. hmu_t *cur = (hmu_t*)heap->base_addr;
  70. hmu_t *end = (hmu_t*)((char*)heap->base_addr + heap->current_size);
  71. if ((hmu_t*)((char *)cur + hmu_get_size(cur)) != end) {
  72. os_printf("Memory leak detected:\n");
  73. gci_dump(heap);
  74. #if WASM_ENABLE_SPEC_TEST != 0
  75. while (1);
  76. #endif
  77. }
  78. #endif
  79. os_mutex_destroy(&heap->lock);
  80. memset(heap->base_addr, 0, heap->current_size);
  81. memset(heap, 0, sizeof(gc_heap_t));
  82. return GC_SUCCESS;
  83. }
  84. static void
  85. adjust_ptr(uint8 **p_ptr, intptr_t offset)
  86. {
  87. if (*p_ptr)
  88. *p_ptr += offset;
  89. }
  90. int
  91. gc_migrate(gc_handle_t handle, gc_handle_t handle_old)
  92. {
  93. gc_heap_t *heap = (gc_heap_t *) handle;
  94. intptr_t offset = (uint8*)handle - (uint8*)handle_old;
  95. hmu_t *cur = NULL, *end = NULL;
  96. hmu_tree_node_t *tree_node;
  97. gc_size_t size;
  98. os_mutex_init(&heap->lock);
  99. if (offset == 0)
  100. return 0;
  101. heap->heap_id = (gc_handle_t)heap;
  102. heap->base_addr += offset;
  103. adjust_ptr((uint8**)&heap->kfc_tree_root.left, offset);
  104. adjust_ptr((uint8**)&heap->kfc_tree_root.right, offset);
  105. adjust_ptr((uint8**)&heap->kfc_tree_root.parent, offset);
  106. cur = (hmu_t*)heap->base_addr;
  107. end = (hmu_t*)((char*)heap->base_addr + heap->current_size);
  108. while (cur < end) {
  109. size = hmu_get_size(cur);
  110. bh_assert(size > 0);
  111. if (!HMU_IS_FC_NORMAL(size)) {
  112. tree_node = (hmu_tree_node_t *)cur;
  113. adjust_ptr((uint8**)&tree_node->left, offset);
  114. adjust_ptr((uint8**)&tree_node->right, offset);
  115. adjust_ptr((uint8**)&tree_node->parent, offset);
  116. }
  117. cur = (hmu_t*)((char *)cur + size);
  118. }
  119. bh_assert(cur == end);
  120. return 0;
  121. }
  122. int
  123. gc_reinit_lock(gc_handle_t handle)
  124. {
  125. gc_heap_t *heap = (gc_heap_t *) handle;
  126. return os_mutex_init(&heap->lock);
  127. }
  128. void
  129. gc_destroy_lock(gc_handle_t handle)
  130. {
  131. gc_heap_t *heap = (gc_heap_t *) handle;
  132. os_mutex_destroy(&heap->lock);
  133. }
  134. #if BH_ENABLE_GC_VERIFY != 0
  135. void
  136. gci_verify_heap(gc_heap_t *heap)
  137. {
  138. hmu_t *cur = NULL, *end = NULL;
  139. bh_assert(heap && gci_is_heap_valid(heap));
  140. cur = (hmu_t *)heap->base_addr;
  141. end = (hmu_t *)(heap->base_addr + heap->current_size);
  142. while(cur < end)
  143. {
  144. hmu_verify(cur);
  145. cur = (hmu_t *)((gc_uint8*)cur + hmu_get_size(cur));
  146. }
  147. bh_assert(cur == end);
  148. }
  149. #endif
  150. void *
  151. gc_heap_stats(void *heap_arg, uint32* stats, int size)
  152. {
  153. int i;
  154. gc_heap_t *heap = (gc_heap_t *) heap_arg;
  155. for (i = 0; i < size; i++) {
  156. switch (i) {
  157. case GC_STAT_TOTAL:
  158. stats[i] = heap->current_size;
  159. break;
  160. case GC_STAT_FREE:
  161. stats[i] = heap->total_free_size;
  162. break;
  163. case GC_STAT_HIGHMARK:
  164. stats[i] = heap->highmark_size;
  165. break;
  166. default:
  167. break;
  168. }
  169. }
  170. return heap;
  171. }