ems_kfc.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 !defined(NVALGRIND)
  7. #include <valgrind/memcheck.h>
  8. #endif
  9. #define HEAP_INC_FACTOR 1
  10. /* Check if current platform is compatible with current GC design*/
  11. /* Return GC_ERROR if not;*/
  12. /* Return GC_SUCCESS otherwise.*/
  13. int gci_check_platform()
  14. {
  15. #define CHECK(x, y) do { \
  16. if((x) != (y)) { \
  17. bh_printf("Platform checking failed on LINE %d at FILE %s.",\
  18. __LINE__, __FILE__); \
  19. return GC_ERROR; \
  20. } \
  21. } while(0)
  22. CHECK(8, sizeof(gc_int64));
  23. CHECK(4, sizeof(gc_uint32));
  24. CHECK(4, sizeof(gc_int32));
  25. CHECK(2, sizeof(gc_uint16));
  26. CHECK(2, sizeof(gc_int16));
  27. CHECK(1, sizeof(gc_int8));
  28. CHECK(1, sizeof(gc_uint8));
  29. CHECK(4, sizeof(gc_size_t));
  30. /*CHECK(4, sizeof(void *));*/
  31. return GC_SUCCESS;
  32. }
  33. gc_handle_t gc_init_with_pool(char *buf, gc_size_t buf_size)
  34. {
  35. char *buf_end = buf + buf_size;
  36. char *buf_aligned = (char*) (((uintptr_t) buf + 7) & (uintptr_t)~7);
  37. char *base_addr = buf_aligned + sizeof(gc_heap_t);
  38. gc_heap_t *heap = (gc_heap_t*) buf_aligned;
  39. gc_size_t heap_max_size;
  40. hmu_normal_node_t *p = NULL;
  41. hmu_tree_node_t *root = NULL, *q = NULL;
  42. int i = 0, ret;
  43. /* check system compatibility*/
  44. if (gci_check_platform() == GC_ERROR) {
  45. bh_printf("Check platform compatibility failed");
  46. return NULL;
  47. }
  48. if (buf_size < 1024) {
  49. bh_printf("[GC_ERROR]heap_init_size(%d) < 1024", buf_size);
  50. return NULL;
  51. }
  52. base_addr = (char*) (((uintptr_t) base_addr + 7) & (uintptr_t)~7) + GC_HEAD_PADDING;
  53. heap_max_size = (uint32)(buf_end - base_addr) & (uint32)~7;
  54. memset(heap, 0, sizeof *heap);
  55. memset(base_addr, 0, heap_max_size);
  56. ret = gct_vm_mutex_init(&heap->lock);
  57. if (ret != BHT_OK) {
  58. bh_printf("[GC_ERROR]failed to init lock ");
  59. return NULL;
  60. }
  61. #ifdef BH_FOOTPRINT
  62. bh_printf("\nINIT HEAP 0x%08x %d\n", base_addr, heap_max_size);
  63. #endif
  64. /* init all data structures*/
  65. heap->max_size = heap_max_size;
  66. heap->current_size = heap_max_size;
  67. heap->base_addr = (gc_uint8*) base_addr;
  68. heap->heap_id = (gc_handle_t) heap;
  69. #if GC_STAT_DATA != 0
  70. heap->total_free_size = heap->current_size;
  71. heap->highmark_size = 0;
  72. heap->total_gc_count = 0;
  73. heap->total_gc_time = 0;
  74. heap->gc_threshold_factor = GC_DEFAULT_THRESHOLD_FACTOR;
  75. gc_update_threshold(heap);
  76. #endif
  77. for (i = 0; i < HMU_NORMAL_NODE_CNT; i++) {
  78. /* make normal node look like a FC*/
  79. p = &heap->kfc_normal_list[i];
  80. memset(p, 0, sizeof *p);
  81. hmu_set_ut(&p->hmu_header, HMU_FC);
  82. hmu_set_size(&p->hmu_header, sizeof *p);
  83. }
  84. root = &heap->kfc_tree_root;
  85. memset(root, 0, sizeof *root);
  86. root->size = sizeof *root;
  87. hmu_set_ut(&root->hmu_header, HMU_FC);
  88. hmu_set_size(&root->hmu_header, sizeof *root);
  89. q = (hmu_tree_node_t *) heap->base_addr;
  90. memset(q, 0, sizeof *q);
  91. hmu_set_ut(&q->hmu_header, HMU_FC);
  92. hmu_set_size(&q->hmu_header, heap->current_size);
  93. hmu_mark_pinuse(&q->hmu_header);
  94. root->right = q;
  95. q->parent = root;
  96. q->size = heap->current_size;
  97. bh_assert(
  98. root->size <= HMU_FC_NORMAL_MAX_SIZE
  99. && HMU_FC_NORMAL_MAX_SIZE < q->size); /*@NOTIFY*/
  100. #if BH_ENABLE_MEMORY_PROFILING != 0
  101. bh_printf("heap is successfully initialized with max_size=%u.",
  102. heap_max_size);
  103. #endif
  104. return heap;
  105. }
  106. int gc_destroy_with_pool(gc_handle_t handle)
  107. {
  108. gc_heap_t *heap = (gc_heap_t *) handle;
  109. gct_vm_mutex_destroy(&heap->lock);
  110. memset(heap->base_addr, 0, heap->max_size);
  111. memset(heap, 0, sizeof(gc_heap_t));
  112. return GC_SUCCESS;
  113. }
  114. #if defined(GC_VERIFY)
  115. /* Verify heap integrity*/
  116. /* @heap should not be NULL and it should be a valid heap*/
  117. void gci_verify_heap(gc_heap_t *heap)
  118. {
  119. hmu_t *cur = NULL, *end = NULL;
  120. bh_assert(heap && gci_is_heap_valid(heap));
  121. cur = (hmu_t *)heap->base_addr;
  122. end = (hmu_t *)(heap->base_addr + heap->current_size);
  123. while(cur < end)
  124. {
  125. hmu_verify(cur);
  126. cur = (hmu_t *)((gc_uint8*)cur + hmu_get_size(cur));
  127. }
  128. bh_assert(cur == end);
  129. }
  130. #endif
  131. void* gc_heap_stats(void *heap_arg, uint32* stats, int size, gc_mm_t mmt)
  132. {
  133. (void) mmt;
  134. int i;
  135. gc_heap_t *heap = (gc_heap_t *) heap_arg;
  136. for (i = 0; i < size; i++) {
  137. switch (i) {
  138. case GC_STAT_TOTAL:
  139. stats[i] = heap->current_size;
  140. break;
  141. case GC_STAT_FREE:
  142. stats[i] = heap->total_free_size;
  143. break;
  144. case GC_STAT_HIGHMARK:
  145. stats[i] = heap->highmark_size;
  146. break;
  147. case GC_STAT_COUNT:
  148. stats[i] = heap->total_gc_count;
  149. break;
  150. case GC_STAT_TIME:
  151. stats[i] = (uint32)heap->total_gc_time;
  152. break;
  153. default:
  154. break;
  155. }
  156. }
  157. return heap;
  158. }