ems_gc.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. #ifndef GC_FINALIZER_T_DEFINED
  58. #define GC_FINALIZER_T_DEFINED
  59. typedef void (*gc_finalizer_t)(void *obj, void *data);
  60. #endif
  61. #ifndef EXTRA_INFO_NORMAL_NODE_CNT
  62. #define EXTRA_INFO_NORMAL_NODE_CNT 32
  63. #endif
  64. /* extra information attached to specific object */
  65. typedef struct extra_info_node {
  66. gc_object_t obj;
  67. gc_finalizer_t finalizer;
  68. void *data;
  69. } extra_info_node_t;
  70. /**
  71. * GC initialization from a buffer, which is separated into
  72. * two parts: the beginning of the buffer is used to create
  73. * the heap structure, and the left is used to create the
  74. * actual pool data
  75. *
  76. * @param buf the buffer to be initialized to a heap
  77. * @param buf_size the size of buffer
  78. *
  79. * @return gc handle if success, NULL otherwise
  80. */
  81. gc_handle_t
  82. gc_init_with_pool(char *buf, gc_size_t buf_size);
  83. /**
  84. * GC initialization from heap struct buffer and pool buffer
  85. *
  86. * @param struct_buf the struct buffer to create the heap structure
  87. * @param struct_buf_size the size of struct buffer
  88. * @param pool_buf the pool buffer to create pool data
  89. * @param pool_buf_size the size of poll buffer
  90. *
  91. * @return gc handle if success, NULL otherwise
  92. */
  93. gc_handle_t
  94. gc_init_with_struct_and_pool(char *struct_buf, gc_size_t struct_buf_size,
  95. char *pool_buf, gc_size_t pool_buf_size);
  96. /**
  97. * Destroy heap which is initialized from a buffer
  98. *
  99. * @param handle handle to heap needed destroy
  100. *
  101. * @return GC_SUCCESS if success
  102. * GC_ERROR for bad parameters or failed system resource freeing.
  103. */
  104. int
  105. gc_destroy_with_pool(gc_handle_t handle);
  106. #if WASM_ENABLE_GC != 0
  107. /**
  108. * Enable or disable GC reclaim for a heap
  109. *
  110. * @param handle handle of the heap
  111. * @param exec_env the exec_env of current module instance
  112. */
  113. #if WASM_ENABLE_THREAD_MGR == 0
  114. void
  115. gc_enable_gc_reclaim(gc_handle_t handle, void *exec_env);
  116. #else
  117. /**
  118. * Enable or disable GC reclaim for a heap
  119. *
  120. * @param handle handle of the heap
  121. * @param cluster the tread cluster of current module instance
  122. */
  123. void
  124. gc_enable_gc_reclaim(gc_handle_t handle, void *cluster);
  125. #endif
  126. #endif
  127. /**
  128. * Return heap struct size
  129. */
  130. uint32
  131. gc_get_heap_struct_size(void);
  132. /**
  133. * Migrate heap from one pool buf to another pool buf
  134. *
  135. * @param handle handle of the new heap
  136. * @param pool_buf_new the new pool buffer
  137. * @param pool_buf_size the size of new pool buffer
  138. *
  139. * @return GC_SUCCESS if success, GC_ERROR otherwise
  140. */
  141. int
  142. gc_migrate(gc_handle_t handle, char *pool_buf_new, gc_size_t pool_buf_size);
  143. /**
  144. * Check whether the heap is corrupted
  145. *
  146. * @param handle handle of the heap
  147. *
  148. * @return true if success, false otherwise
  149. */
  150. bool
  151. gc_is_heap_corrupted(gc_handle_t handle);
  152. /**
  153. * Get Heap Stats
  154. *
  155. * @param stats [out] integer array to save heap stats
  156. * @param size [in] the size of stats
  157. * @param mmt [in] type of heap, MMT_SHARED or MMT_INSTANCE
  158. */
  159. void *
  160. gc_heap_stats(void *heap, uint32 *stats, int size);
  161. #if BH_ENABLE_GC_VERIFY == 0
  162. gc_object_t
  163. gc_alloc_vo(void *heap, gc_size_t size);
  164. gc_object_t
  165. gc_realloc_vo(void *heap, void *ptr, gc_size_t size);
  166. int
  167. gc_free_vo(void *heap, gc_object_t obj);
  168. #if WASM_ENABLE_GC != 0
  169. gc_object_t
  170. gc_alloc_wo(void *heap, gc_size_t size);
  171. void
  172. gc_free_wo(void *vheap, void *ptr);
  173. #endif
  174. #else /* else of BH_ENABLE_GC_VERIFY */
  175. gc_object_t
  176. gc_alloc_vo_internal(void *heap, gc_size_t size, const char *file, int line);
  177. gc_object_t
  178. gc_realloc_vo_internal(void *heap, void *ptr, gc_size_t size, const char *file,
  179. int line);
  180. int
  181. gc_free_vo_internal(void *heap, gc_object_t obj, const char *file, int line);
  182. #if WASM_ENABLE_GC != 0
  183. gc_object_t
  184. gc_alloc_wo_internal(void *heap, gc_size_t size, const char *file, int line);
  185. void
  186. gc_free_wo_internal(void *vheap, void *ptr, const char *file, int line);
  187. #endif
  188. /* clang-format off */
  189. #define gc_alloc_vo(heap, size) \
  190. gc_alloc_vo_internal(heap, size, __FILE__, __LINE__)
  191. #define gc_realloc_vo(heap, ptr, size) \
  192. gc_realloc_vo_internal(heap, ptr, size, __FILE__, __LINE__)
  193. #define gc_free_vo(heap, obj) \
  194. gc_free_vo_internal(heap, obj, __FILE__, __LINE__)
  195. #if WASM_ENABLE_GC != 0
  196. #define gc_alloc_wo(heap, size) \
  197. gc_alloc_wo_internal(heap, size, __FILE__, __LINE__)
  198. #define gc_free_wo(heap, obj) \
  199. gc_free_wo_internal(heap, obj, __FILE__, __LINE__)
  200. #endif
  201. /* clang-format on */
  202. #endif /* end of BH_ENABLE_GC_VERIFY */
  203. #if WASM_ENABLE_GC != 0
  204. /**
  205. * Add gc object ref to the rootset of a gc heap.
  206. *
  207. * @param heap the heap to add the gc object to its rootset
  208. * @param obj pointer to a valid WASM object managed by the gc heap.
  209. *
  210. * @return GC_SUCCESS if success, GC_ERROR otherwise
  211. */
  212. int
  213. gc_add_root(void *heap, gc_object_t obj);
  214. int
  215. gci_gc_heap(void *heap);
  216. extra_info_node_t *
  217. gc_search_extra_info_node(gc_handle_t handle, gc_object_t obj,
  218. gc_size_t *p_index);
  219. /**
  220. * Set finalizer to the given object, if another finalizer is set to the same
  221. * object, the previous one will be cancelled
  222. *
  223. * @param handle handle of the heap
  224. * @param obj object to set finalizer
  225. * @param cb finalizer function to be called before this object is freed
  226. * @param data custom data to be passed to finalizer function
  227. *
  228. * @return true if success, false otherwise
  229. */
  230. bool
  231. gc_set_finalizer(gc_handle_t handle, gc_object_t obj, gc_finalizer_t cb,
  232. void *data);
  233. /**
  234. * Unset finalizer to the given object
  235. *
  236. * @param handle handle of the heap
  237. * @param obj object to unset finalizer
  238. */
  239. void
  240. gc_unset_finalizer(gc_handle_t handle, gc_object_t obj);
  241. #if WASM_ENABLE_THREAD_MGR == 0
  242. bool
  243. wasm_runtime_traverse_gc_rootset(void *exec_env, void *heap);
  244. #else
  245. bool
  246. wasm_runtime_traverse_gc_rootset(void *cluster, void *heap);
  247. #endif
  248. bool
  249. wasm_runtime_get_wasm_object_ref_list(gc_object_t obj, bool *p_is_compact_mode,
  250. gc_uint32 *p_ref_num,
  251. gc_uint16 **p_ref_list,
  252. gc_uint32 *p_ref_start_offset);
  253. bool
  254. wasm_runtime_get_wasm_object_extra_info_flag(gc_object_t obj);
  255. void
  256. wasm_runtime_set_wasm_object_extra_info_flag(gc_object_t obj, bool set);
  257. void
  258. wasm_runtime_gc_prepare(void *exec_env);
  259. void
  260. wasm_runtime_gc_finalize(void *exec_env);
  261. #endif /* end of WASM_ENABLE_GC != 0 */
  262. #define GC_HEAP_STAT_SIZE (128 / 4)
  263. typedef struct {
  264. int usage;
  265. int usage_block;
  266. int vo_usage;
  267. int wo_usage;
  268. int free;
  269. int free_block;
  270. int vo_free;
  271. int wo_free;
  272. int usage_sizes[GC_HEAP_STAT_SIZE];
  273. int free_sizes[GC_HEAP_STAT_SIZE];
  274. } gc_stat_t;
  275. void
  276. gc_show_stat(gc_handle_t handle);
  277. #if WASM_ENABLE_GC != 0
  278. void
  279. gc_show_fragment(gc_handle_t handle);
  280. #if WASM_ENABLE_GC_PERF_PROFILING != 0
  281. void
  282. gc_dump_perf_profiling(gc_handle_t *handle);
  283. #endif
  284. #endif
  285. #ifdef __cplusplus
  286. }
  287. #endif
  288. #endif