ems_gc.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. /**
  6. * @file ems_gc.h
  7. * @date Wed Aug 3 10:46:38 2011
  8. *
  9. * @brief This file defines GC modules types and interfaces.
  10. */
  11. #ifndef _EMS_GC_H
  12. #define _EMS_GC_H
  13. #include "bh_platform.h"
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. #ifndef GC_STAT_DATA
  18. #define GC_STAT_DATA 0
  19. #endif
  20. #ifndef GC_STAT_SHOW
  21. #define GC_STAT_SHOW 0
  22. #endif
  23. #ifndef GC_IN_EVERY_ALLOCATION
  24. #define GC_IN_EVERY_ALLOCATION 0
  25. #endif
  26. #ifndef GC_MANUALLY
  27. #define GC_MANUALLY 0
  28. #endif
  29. #define GC_HEAD_PADDING 4
  30. #ifndef NULL_REF
  31. #define NULL_REF ((gc_object_t)NULL)
  32. #endif
  33. #define GC_SUCCESS (0)
  34. #define GC_ERROR (-1)
  35. #define GC_TRUE (1)
  36. #define GC_FALSE (0)
  37. #define GC_MAX_HEAP_SIZE (256 * BH_KB)
  38. typedef void *gc_handle_t;
  39. typedef void *gc_object_t;
  40. typedef uint64 gc_uint64;
  41. typedef int64 gc_int64;
  42. typedef uint32 gc_uint32;
  43. typedef int32 gc_int32;
  44. typedef uint16 gc_uint16;
  45. typedef int16 gc_int16;
  46. typedef uint8 gc_uint8;
  47. typedef int8 gc_int8;
  48. typedef uint32 gc_size_t;
  49. typedef enum {
  50. GC_STAT_TOTAL = 0,
  51. GC_STAT_FREE,
  52. GC_STAT_HIGHMARK,
  53. GC_STAT_COUNT,
  54. GC_STAT_TIME,
  55. GC_STAT_MAX
  56. } GC_STAT_INDEX;
  57. /**
  58. * GC initialization from a buffer, which is separated into
  59. * two parts: the beginning of the buffer is used to create
  60. * the heap structure, and the left is used to create the
  61. * actual pool data
  62. *
  63. * @param buf the buffer to be initialized to a heap
  64. * @param buf_size the size of buffer
  65. *
  66. * @return gc handle if success, NULL otherwise
  67. */
  68. gc_handle_t
  69. gc_init_with_pool(char *buf, gc_size_t buf_size);
  70. /**
  71. * GC initialization from heap struct buffer and pool buffer
  72. *
  73. * @param struct_buf the struct buffer to create the heap structure
  74. * @param struct_buf_size the size of struct buffer
  75. * @param pool_buf the pool buffer to create pool data
  76. * @param pool_buf_size the size of poll buffer
  77. *
  78. * @return gc handle if success, NULL otherwise
  79. */
  80. gc_handle_t
  81. gc_init_with_struct_and_pool(char *struct_buf, gc_size_t struct_buf_size,
  82. char *pool_buf, gc_size_t pool_buf_size);
  83. /**
  84. * Destroy heap which is initilized from a buffer
  85. *
  86. * @param handle handle to heap needed destroy
  87. *
  88. * @return GC_SUCCESS if success
  89. * GC_ERROR for bad parameters or failed system resource freeing.
  90. */
  91. int
  92. gc_destroy_with_pool(gc_handle_t handle);
  93. #if WASM_ENABLE_GC != 0
  94. /**
  95. * Enable or disable GC reclaim for a heap
  96. *
  97. * @param handle handle of the heap
  98. * @param exec_env the exec_env of current module instance
  99. */
  100. #if WASM_ENABLE_THREAD_MGR == 0
  101. void
  102. gc_enable_gc_reclaim(gc_handle_t handle, void *exec_env);
  103. #else
  104. /**
  105. * Enable or disable GC reclaim for a heap
  106. *
  107. * @param handle handle of the heap
  108. * @param cluster the tread cluster of current module instance
  109. */
  110. void
  111. gc_enable_gc_reclaim(gc_handle_t handle, void *cluster);
  112. #endif
  113. #endif
  114. /**
  115. * Return heap struct size
  116. */
  117. uint32
  118. gc_get_heap_struct_size(void);
  119. /**
  120. * Migrate heap from one pool buf to another pool buf
  121. *
  122. * @param handle handle of the new heap
  123. * @param pool_buf_new the new pool buffer
  124. * @param pool_buf_size the size of new pool buffer
  125. *
  126. * @return GC_SUCCESS if success, GC_ERROR otherwise
  127. */
  128. int
  129. gc_migrate(gc_handle_t handle, char *pool_buf_new, gc_size_t pool_buf_size);
  130. /**
  131. * Check whether the heap is corrupted
  132. *
  133. * @param handle handle of the heap
  134. *
  135. * @return true if success, false otherwise
  136. */
  137. bool
  138. gc_is_heap_corrupted(gc_handle_t handle);
  139. /**
  140. * Get Heap Stats
  141. *
  142. * @param stats [out] integer array to save heap stats
  143. * @param size [in] the size of stats
  144. * @param mmt [in] type of heap, MMT_SHARED or MMT_INSTANCE
  145. */
  146. void *
  147. gc_heap_stats(void *heap, uint32 *stats, int size);
  148. #if BH_ENABLE_GC_VERIFY == 0
  149. gc_object_t
  150. gc_alloc_vo(void *heap, gc_size_t size);
  151. gc_object_t
  152. gc_realloc_vo(void *heap, void *ptr, gc_size_t size);
  153. int
  154. gc_free_vo(void *heap, gc_object_t obj);
  155. #if WASM_ENABLE_GC != 0
  156. gc_object_t
  157. gc_alloc_wo(void *heap, gc_size_t size);
  158. void
  159. gc_free_wo(void *vheap, void *ptr);
  160. #endif
  161. #else /* else of BH_ENABLE_GC_VERIFY */
  162. gc_object_t
  163. gc_alloc_vo_internal(void *heap, gc_size_t size, const char *file, int line);
  164. gc_object_t
  165. gc_realloc_vo_internal(void *heap, void *ptr, gc_size_t size, const char *file,
  166. int line);
  167. int
  168. gc_free_vo_internal(void *heap, gc_object_t obj, const char *file, int line);
  169. #if WASM_ENABLE_GC != 0
  170. gc_object_t
  171. gc_alloc_wo_internal(void *heap, gc_size_t size, const char *file, int line);
  172. void
  173. gc_free_wo_internal(void *vheap, void *ptr, const char *file, int line);
  174. #endif
  175. /* clang-format off */
  176. #define gc_alloc_vo(heap, size) \
  177. gc_alloc_vo_internal(heap, size, __FILE__, __LINE__)
  178. #define gc_realloc_vo(heap, ptr, size) \
  179. gc_realloc_vo_internal(heap, ptr, size, __FILE__, __LINE__)
  180. #define gc_free_vo(heap, obj) \
  181. gc_free_vo_internal(heap, obj, __FILE__, __LINE__)
  182. #if WASM_ENABLE_GC != 0
  183. #define gc_alloc_wo(heap, size) \
  184. gc_alloc_wo_internal(heap, size, __FILE__, __LINE__)
  185. #define gc_free_wo(heap, obj) \
  186. gc_free_wo_internal(heap, obj, __FILE__, __LINE__)
  187. #endif
  188. /* clang-format on */
  189. #endif /* end of BH_ENABLE_GC_VERIFY */
  190. #if WASM_ENABLE_GC != 0
  191. /**
  192. * Add gc object ref to the rootset of a gc heap.
  193. *
  194. * @param heap the heap to add the gc object to its rootset
  195. * @param obj pointer to a valid WASM object managed by the gc heap.
  196. *
  197. * @return GC_SUCCESS if success, GC_ERROR otherwise
  198. */
  199. int
  200. gc_add_root(void *heap, gc_object_t obj);
  201. int
  202. gci_gc_heap(void *heap);
  203. #if WASM_ENABLE_THREAD_MGR == 0
  204. bool
  205. wasm_runtime_traverse_gc_rootset(void *exec_env, void *heap);
  206. #else
  207. bool
  208. wasm_runtime_traverse_gc_rootset(void *cluster, void *heap);
  209. #endif
  210. bool
  211. wasm_runtime_get_wasm_object_ref_list(gc_object_t obj, bool *p_is_compact_mode,
  212. gc_uint32 *p_ref_num,
  213. gc_uint16 **p_ref_list,
  214. gc_uint32 *p_ref_start_offset);
  215. void
  216. wasm_runtime_gc_prepare();
  217. void
  218. wasm_runtime_gc_finalize();
  219. #endif /* end of WASM_ENABLE_GC != 0 */
  220. #define GC_HEAP_STAT_SIZE (128 / 4)
  221. typedef struct {
  222. int usage;
  223. int usage_block;
  224. int vo_usage;
  225. int wo_usage;
  226. int free;
  227. int free_block;
  228. int vo_free;
  229. int wo_free;
  230. int usage_sizes[GC_HEAP_STAT_SIZE];
  231. int free_sizes[GC_HEAP_STAT_SIZE];
  232. } gc_stat_t;
  233. void
  234. gc_show_stat(gc_handle_t handle);
  235. #if WASM_ENABLE_GC != 0
  236. void
  237. gc_show_fragment(gc_handle_t handle);
  238. #endif
  239. #ifdef __cplusplus
  240. }
  241. #endif
  242. #endif