ems_kfc.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "ems_gc_internal.h"
  17. #if !defined(NVALGRIND)
  18. #include <valgrind/memcheck.h>
  19. #endif
  20. #define HEAP_INC_FACTOR 1
  21. /* Check if current platform is compatible with current GC design*/
  22. /* Return GC_ERROR if not;*/
  23. /* Return GC_SUCCESS otherwise.*/
  24. int gci_check_platform()
  25. {
  26. #define CHECK(x, y) do { \
  27. if((x) != (y)) { \
  28. printf("Platform checking failed on LINE %d at FILE %s.", \
  29. __LINE__, __FILE__); \
  30. return GC_ERROR; \
  31. } \
  32. } while(0)
  33. CHECK(8, sizeof(gc_int64));
  34. CHECK(4, sizeof(gc_uint32));
  35. CHECK(4, sizeof(gc_int32));
  36. CHECK(2, sizeof(gc_uint16));
  37. CHECK(2, sizeof(gc_int16));
  38. CHECK(1, sizeof(gc_int8));
  39. CHECK(1, sizeof(gc_uint8));
  40. CHECK(4, sizeof(gc_size_t));
  41. CHECK(4, sizeof(void *));
  42. return GC_SUCCESS;
  43. }
  44. gc_handle_t gc_init_with_pool(char *buf, gc_size_t buf_size)
  45. {
  46. char *buf_end = buf + buf_size;
  47. char *buf_aligned = (char*) (((uintptr_t) buf + 7) & ~7);
  48. char *base_addr = buf_aligned + sizeof(gc_heap_t);
  49. gc_heap_t *heap = (gc_heap_t*) buf_aligned;
  50. gc_size_t heap_max_size;
  51. hmu_normal_node_t *p = NULL;
  52. hmu_tree_node_t *root = NULL, *q = NULL;
  53. int i = 0, ret;
  54. /* check system compatibility*/
  55. if (gci_check_platform() == GC_ERROR) {
  56. printf("Check platform compatibility failed");
  57. return NULL;
  58. }
  59. if (buf_size < 1024) {
  60. printf("[GC_ERROR]heap_init_size(%d) < 1024", buf_size);
  61. return NULL;
  62. }
  63. base_addr = (char*) (((uintptr_t) base_addr + 7) & ~7) + GC_HEAD_PADDING;
  64. heap_max_size = (buf_end - base_addr) & ~7;
  65. memset(heap, 0, sizeof *heap);
  66. memset(base_addr, 0, heap_max_size);
  67. ret = gct_vm_mutex_init(&heap->lock);
  68. if (ret != BHT_OK) {
  69. printf("[GC_ERROR]failed to init lock ");
  70. return NULL;
  71. }
  72. #ifdef BH_FOOTPRINT
  73. printf("\nINIT HEAP 0x%08x %d\n", base_addr, heap_max_size);
  74. #endif
  75. /* init all data structures*/
  76. heap->max_size = heap_max_size;
  77. heap->current_size = heap_max_size;
  78. heap->base_addr = (gc_uint8*) base_addr;
  79. heap->heap_id = (gc_handle_t) heap;
  80. #if GC_STAT_DATA != 0
  81. heap->total_free_size = heap->current_size;
  82. heap->highmark_size = 0;
  83. heap->total_gc_count = 0;
  84. heap->total_gc_time = 0;
  85. heap->gc_threshold_factor = GC_DEFAULT_THRESHOLD_FACTOR;
  86. gc_update_threshold(heap);
  87. #endif
  88. for (i = 0; i < HMU_NORMAL_NODE_CNT; i++) {
  89. /* make normal node look like a FC*/
  90. p = &heap->kfc_normal_list[i];
  91. memset(p, 0, sizeof *p);
  92. hmu_set_ut(&p->hmu_header, HMU_FC);
  93. hmu_set_size(&p->hmu_header, sizeof *p);
  94. }
  95. root = &heap->kfc_tree_root;
  96. memset(root, 0, sizeof *root);
  97. root->size = sizeof *root;
  98. hmu_set_ut(&root->hmu_header, HMU_FC);
  99. hmu_set_size(&root->hmu_header, sizeof *root);
  100. q = (hmu_tree_node_t *) heap->base_addr;
  101. memset(q, 0, sizeof *q);
  102. hmu_set_ut(&q->hmu_header, HMU_FC);
  103. hmu_set_size(&q->hmu_header, heap->current_size);
  104. hmu_mark_pinuse(&q->hmu_header);
  105. root->right = q;
  106. q->parent = root;
  107. q->size = heap->current_size;
  108. bh_assert(
  109. root->size <= HMU_FC_NORMAL_MAX_SIZE
  110. && HMU_FC_NORMAL_MAX_SIZE < q->size); /*@NOTIFY*/
  111. #if BH_ENABLE_MEMORY_PROFILING != 0
  112. printf("heap is successfully initialized with max_size=%u.",
  113. heap_max_size);
  114. #endif
  115. return heap;
  116. }
  117. int gc_destroy_with_pool(gc_handle_t handle)
  118. {
  119. gc_heap_t *heap = (gc_heap_t *) handle;
  120. gct_vm_mutex_destroy(&heap->lock);
  121. memset(heap->base_addr, 0, heap->max_size);
  122. memset(heap, 0, sizeof(gc_heap_t));
  123. return GC_SUCCESS;
  124. }
  125. #if defined(GC_VERIFY)
  126. /* Verify heap integrity*/
  127. /* @heap should not be NULL and it should be a valid heap*/
  128. void gci_verify_heap(gc_heap_t *heap)
  129. {
  130. hmu_t *cur = NULL, *end = NULL;
  131. bh_assert(heap && gci_is_heap_valid(heap));
  132. cur = (hmu_t *)heap->base_addr;
  133. end = (hmu_t *)(heap->base_addr + heap->current_size);
  134. while(cur < end)
  135. {
  136. hmu_verify(cur);
  137. cur = (hmu_t *)((gc_uint8*)cur + hmu_get_size(cur));
  138. }
  139. bh_assert(cur == end);
  140. }
  141. #endif
  142. void* gc_heap_stats(void *heap_arg, int* stats, int size, gc_mm_t mmt)
  143. {
  144. (void) mmt;
  145. int i;
  146. gc_heap_t *heap = (gc_heap_t *) heap_arg;
  147. for (i = 0; i < size; i++) {
  148. switch (i) {
  149. case GC_STAT_TOTAL:
  150. stats[i] = heap->current_size;
  151. break;
  152. case GC_STAT_FREE:
  153. stats[i] = heap->total_free_size;
  154. break;
  155. case GC_STAT_HIGHMARK:
  156. stats[i] = heap->highmark_size;
  157. break;
  158. case GC_STAT_COUNT:
  159. stats[i] = heap->total_gc_count;
  160. break;
  161. case GC_STAT_TIME:
  162. stats[i] = (int) heap->total_gc_time;
  163. break;
  164. default:
  165. break;
  166. }
  167. }
  168. return heap;
  169. }