ems_gc.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. *
  12. */
  13. #ifndef _EMS_GC_H
  14. #define _EMS_GC_H
  15. #include "bh_platform.h"
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. /*Pre-compile configuration can be done here or on Makefiles*/
  20. /*#define GC_EMBEDDED or GC_STANDALONE*/
  21. /*#define GC_DEBUG*/
  22. /*#define GC_TEST // TEST mode is a sub-mode of STANDALONE*/
  23. /* #define GC_ALLOC_TRACE */
  24. /* #define GC_STAT */
  25. #ifndef GC_STAT_DATA
  26. #define GC_STAT_DATA 1
  27. #endif
  28. #define GC_HEAD_PADDING 4
  29. /* Standalone GC is used for testing.*/
  30. #ifndef GC_EMBEDDED
  31. # ifndef GC_STANDALONE
  32. # define GC_STANDALONE
  33. # endif
  34. #endif
  35. #if defined(GC_EMBEDDED) && defined(GC_STANDALONE)
  36. # error "Can not define GC_EMBEDDED and GC_STANDALONE at the same time"
  37. #endif
  38. #ifdef BH_TEST
  39. # ifndef GC_TEST
  40. # define GC_TEST
  41. # endif
  42. #endif
  43. #ifdef BH_DEBUG
  44. /*instrument mode ignore GC_DEBUG feature, for instrument testing gc_alloc_vo_i_heap only has func_name parameter*/
  45. #if !defined INSTRUMENT_TEST_ENABLED && !defined GC_DEBUG
  46. # define GC_DEBUG
  47. #endif
  48. #endif
  49. #if defined(GC_EMBEDDED) && defined(GC_TEST)
  50. # error "Can not defined GC_EMBEDDED and GC_TEST at the same time"
  51. #endif
  52. typedef void *gc_handle_t;
  53. typedef void *gc_object_t;
  54. #define NULL_REF ((gc_object_t)NULL)
  55. #define GC_SUCCESS (0)
  56. #define GC_ERROR (-1)
  57. #define GC_TRUE (1)
  58. #define GC_FALSE (0)
  59. #define GC_MAX_HEAP_SIZE (256 * BH_KB)
  60. typedef int64 gc_int64;
  61. typedef unsigned int gc_uint32;
  62. typedef signed int gc_int32;
  63. typedef unsigned short gc_uint16;
  64. typedef signed short gc_int16;
  65. typedef unsigned char gc_uint8;
  66. typedef signed char gc_int8;
  67. typedef gc_uint32 gc_size_t;
  68. typedef enum {
  69. MMT_SHARED = 0,
  70. MMT_INSTANCE = 1,
  71. MMT_APPMANAGER = MMT_SHARED,
  72. MMT_VERIFIER = MMT_SHARED,
  73. MMT_JHI = MMT_SHARED,
  74. MMT_LOADER = MMT_SHARED,
  75. MMT_APPLET = MMT_INSTANCE,
  76. MMT_INTERPRETER = MMT_INSTANCE
  77. } gc_mm_t;
  78. #ifdef GC_STAT
  79. #define GC_HEAP_STAT_SIZE (128 / 4)
  80. typedef struct {
  81. int usage;
  82. int usage_block;
  83. int vo_usage;
  84. int jo_usage;
  85. int free;
  86. int free_block;
  87. int vo_free;
  88. int jo_free;
  89. int usage_sizes[GC_HEAP_STAT_SIZE];
  90. int free_sizes[GC_HEAP_STAT_SIZE];
  91. }gc_stat_t;
  92. extern void gc_heap_stat(void* heap, gc_stat_t* gc_stat);
  93. extern void __gc_print_stat(void *heap, int verbose);
  94. #define gc_print_stat __gc_print_stat
  95. #else
  96. #define gc_print_stat(heap, verbose)
  97. #endif
  98. #if GC_STAT_DATA != 0
  99. typedef enum {
  100. GC_STAT_TOTAL = 0,
  101. GC_STAT_FREE,
  102. GC_STAT_HIGHMARK,
  103. GC_STAT_COUNT,
  104. GC_STAT_TIME,
  105. GC_STAT_MAX_1,
  106. GC_STAT_MAX_2,
  107. GC_STAT_MAX_3,
  108. GC_STAT_MAX
  109. } GC_STAT_INDEX;
  110. #endif
  111. /*////////////// Exported APIs*/
  112. /**
  113. * GC initialization from a buffer
  114. *
  115. * @param buf the buffer to be initialized to a heap
  116. * @param buf_size the size of buffer
  117. *
  118. * @return gc handle if success, NULL otherwise
  119. */
  120. extern gc_handle_t gc_init_with_pool(char *buf, gc_size_t buf_size);
  121. /**
  122. * Destroy heap which is initilized from a buffer
  123. *
  124. * @param handle handle to heap needed destroy
  125. *
  126. * @return GC_SUCCESS if success
  127. * GC_ERROR for bad parameters or failed system resource freeing.
  128. */
  129. extern int gc_destroy_with_pool(gc_handle_t handle);
  130. #if GC_STAT_DATA != 0
  131. /**
  132. * Get Heap Stats
  133. *
  134. * @param stats [out] integer array to save heap stats
  135. * @param size [in] the size of stats
  136. * @param mmt [in] type of heap, MMT_SHARED or MMT_INSTANCE
  137. */
  138. extern void* gc_heap_stats(void *heap, uint32* stats, int size, gc_mm_t mmt);
  139. /**
  140. * Set GC threshold factor
  141. *
  142. * @param heap [in] the heap to set
  143. * @param factor [in] the threshold size is free_size * factor / 1000
  144. *
  145. * @return GC_SUCCESS if success.
  146. */
  147. extern int gc_set_threshold_factor(void *heap, unsigned int factor);
  148. #endif
  149. /*////// Allocate heap object*/
  150. /* There are two versions of allocate functions. The functions with _i suffix should be only used*/
  151. /* internally. Functions without _i suffix are just wrappers with the corresponded functions with*/
  152. /* _i suffix. Allocation operation code position are record under DEBUG model for debugging.*/
  153. #ifdef GC_DEBUG
  154. # define ALLOC_EXTRA_PARAMETERS ,const char*file_name,int line_number
  155. # define ALLOC_EXTRA_ARGUMENTS , __FILE__, __LINE__
  156. # define ALLOC_PASSDOWN_EXTRA_ARGUMENTS , file_name, line_number
  157. # define gc_alloc_vo_h(heap, size) gc_alloc_vo_i_heap(heap, size, __FILE__, __LINE__)
  158. # define gc_free_h(heap, obj) gc_free_i_heap(heap, obj, __FILE__, __LINE__)
  159. #else
  160. # define ALLOC_EXTRA_PARAMETERS
  161. # define ALLOC_EXTRA_ARGUMENTS
  162. # define ALLOC_PASSDOWN_EXTRA_ARGUMENTS
  163. # define gc_alloc_vo_h gc_alloc_vo_i_heap
  164. # define gc_free_h gc_free_i_heap
  165. #endif
  166. /**
  167. * Invoke a GC
  168. *
  169. * @param heap
  170. *
  171. * @return GC_SUCCESS if success
  172. */
  173. extern int gci_gc_heap(void *heap);
  174. /**
  175. * Allocate VM Object in specific heap.
  176. *
  177. * @param heap heap to allocate.
  178. * @param size bytes to allocate.
  179. *
  180. * @return pointer to VM object allocated
  181. * NULL if failed.
  182. */
  183. extern gc_object_t _gc_alloc_vo_i_heap(void *heap,
  184. gc_size_t size ALLOC_EXTRA_PARAMETERS);
  185. extern gc_object_t _gc_alloc_jo_i_heap(void *heap,
  186. gc_size_t size ALLOC_EXTRA_PARAMETERS);
  187. #ifdef INSTRUMENT_TEST_ENABLED
  188. extern gc_object_t gc_alloc_vo_i_heap_instr(void *heap, gc_size_t size, const char* func_name );
  189. extern gc_object_t gc_alloc_jo_i_heap_instr(void *heap, gc_size_t size, const char* func_name);
  190. # define gc_alloc_vo_i_heap(heap, size) gc_alloc_vo_i_heap_instr(heap, size, __FUNCTION__)
  191. # define gc_alloc_jo_i_heap(heap, size) gc_alloc_jo_i_heap_instr(heap, size, __FUNCTION__)
  192. #else
  193. # define gc_alloc_vo_i_heap _gc_alloc_vo_i_heap
  194. # define gc_alloc_jo_i_heap _gc_alloc_jo_i_heap
  195. #endif
  196. /**
  197. * Allocate Java object in specific heap.
  198. *
  199. * @param heap heap to allocate.
  200. * @param size bytes to allocate.
  201. *
  202. * @return pointer to Java object allocated
  203. * NULL if failed.
  204. */
  205. extern gc_object_t _gc_alloc_jo_i_heap(void *heap,
  206. gc_size_t size ALLOC_EXTRA_PARAMETERS);
  207. /**
  208. * Free VM object
  209. *
  210. * @param heap heap to free.
  211. * @param obj pointer to object need free.
  212. *
  213. * @return GC_SUCCESS if success
  214. */
  215. extern int gc_free_i_heap(void *heap, gc_object_t obj ALLOC_EXTRA_PARAMETERS);
  216. /**
  217. * Add ref to rootset of gc for current instance.
  218. *
  219. * @param obj pointer to real load of a valid Java object managed by gc for current instance.
  220. *
  221. * @return GC_SUCCESS if success.
  222. * GC_ERROR for invalid parameters.
  223. */
  224. extern int gc_add_root(void* heap, gc_object_t obj);
  225. /*////////////// Imported APIs which should be implemented in other components*/
  226. /*////// Java object layout related APIs*/
  227. /**
  228. * Get Java object size from corresponding VM module
  229. *
  230. * @param obj pointer to the real load of a Java object.
  231. *
  232. * @return size of java object.
  233. */
  234. extern gc_size_t vm_get_java_object_size(gc_object_t obj);
  235. /**
  236. * Get reference list of this object
  237. *
  238. * @param obj [in] pointer to java object.
  239. * @param is_compact_mode [in] indicate the java object mode. GC_TRUE or GC_FALSE.
  240. * @param ref_num [out] the size of ref_list.
  241. * @param ref_list [out] if is_compact_mode is GC_FALSE, this parameter will be set to a list of offset.
  242. * @param ref_start_offset [out] If is_compact_mode is GC_TRUE, this parameter will be set to the start offset of the references in this object.
  243. *
  244. * @return GC_SUCCESS if success.
  245. * GC_ERROR when error occurs.
  246. */
  247. extern int vm_get_java_object_ref_list(gc_object_t obj, int *is_compact_mode,
  248. gc_size_t *ref_num, gc_uint16 **ref_list, gc_uint32 *ref_start_offset);
  249. /**
  250. * Get gc handle for current instance
  251. *
  252. *
  253. * @return instance heap handle.
  254. */
  255. extern gc_handle_t app_manager_get_cur_applet_heap(void);
  256. /**
  257. * Begin current instance heap rootset enumeration
  258. *
  259. *
  260. * @return GC_SUCCESS if success.
  261. * GC_ERROR when error occurs.
  262. */
  263. extern int vm_begin_rootset_enumeration(void *heap);
  264. #ifdef _INSTRUMENT_TEST_ENABLED
  265. extern int vm_begin_rootset_enumeration_instr(void *heap, const char*func_name);
  266. #define vm_begin_rootset_enumeration(heap) vm_begin_rootset_enumeration_instr(heap, __FUNCTION__)
  267. #else
  268. #define vm_begin_rootset_enumeration _vm_begin_rootset_enumeration
  269. #endif /* INSTUMENT_TEST_ENABLED*/
  270. #ifndef offsetof
  271. #define offsetof(Type, field) ((size_t)(&((Type *)0)->field))
  272. #endif
  273. #ifdef __cplusplus
  274. }
  275. #endif
  276. #endif