ems_gc.h 7.7 KB

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