ems_gc.h 8.9 KB

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