ems_kfc.c 5.1 KB

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