ems_gc.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. #define GC_HEAD_PADDING 4
  18. #define NULL_REF ((gc_object_t)NULL)
  19. #define GC_SUCCESS (0)
  20. #define GC_ERROR (-1)
  21. #define GC_TRUE (1)
  22. #define GC_FALSE (0)
  23. #define GC_MAX_HEAP_SIZE (256 * BH_KB)
  24. typedef void *gc_handle_t;
  25. typedef void *gc_object_t;
  26. typedef int64 gc_int64;
  27. typedef uint32 gc_uint32;
  28. typedef int32 gc_int32;
  29. typedef uint16 gc_uint16;
  30. typedef int16 gc_int16;
  31. typedef uint8 gc_uint8;
  32. typedef int8 gc_int8;
  33. typedef uint32 gc_size_t;
  34. typedef enum {
  35. GC_STAT_TOTAL = 0,
  36. GC_STAT_FREE,
  37. GC_STAT_HIGHMARK,
  38. } GC_STAT_INDEX;
  39. /**
  40. * GC initialization from a buffer, which is separated into
  41. * two parts: the beginning of the buffer is used to create
  42. * the heap structure, and the left is used to create the
  43. * actual pool data
  44. *
  45. * @param buf the buffer to be initialized to a heap
  46. * @param buf_size the size of buffer
  47. *
  48. * @return gc handle if success, NULL otherwise
  49. */
  50. gc_handle_t
  51. gc_init_with_pool(char *buf, gc_size_t buf_size);
  52. /**
  53. * GC initialization from heap struct buffer and pool buffer
  54. *
  55. * @param struct_buf the struct buffer to create the heap structure
  56. * @param struct_buf_size the size of struct buffer
  57. * @param pool_buf the pool buffer to create pool data
  58. * @param pool_buf_size the size of poll buffer
  59. *
  60. * @return gc handle if success, NULL otherwise
  61. */
  62. gc_handle_t
  63. gc_init_with_struct_and_pool(char *struct_buf, gc_size_t struct_buf_size,
  64. char *pool_buf, gc_size_t pool_buf_size);
  65. /**
  66. * Destroy heap which is initilized from a buffer
  67. *
  68. * @param handle handle to heap needed destroy
  69. *
  70. * @return GC_SUCCESS if success
  71. * GC_ERROR for bad parameters or failed system resource freeing.
  72. */
  73. int
  74. gc_destroy_with_pool(gc_handle_t handle);
  75. /**
  76. * Return heap struct size
  77. */
  78. uint32
  79. gc_get_heap_struct_size(void);
  80. /**
  81. * Migrate heap from one pool buf to another pool buf
  82. *
  83. * @param handle handle of the new heap
  84. * @param pool_buf_new the new pool buffer
  85. * @param pool_buf_size the size of new pool buffer
  86. *
  87. * @return GC_SUCCESS if success, GC_ERROR otherwise
  88. */
  89. int
  90. gc_migrate(gc_handle_t handle, char *pool_buf_new, gc_size_t pool_buf_size);
  91. /**
  92. * Check whether the heap is corrupted
  93. *
  94. * @param handle handle of the heap
  95. *
  96. * @return true if success, false otherwise
  97. */
  98. bool
  99. gc_is_heap_corrupted(gc_handle_t handle);
  100. /**
  101. * Get Heap Stats
  102. *
  103. * @param stats [out] integer array to save heap stats
  104. * @param size [in] the size of stats
  105. * @param mmt [in] type of heap, MMT_SHARED or MMT_INSTANCE
  106. */
  107. void *
  108. gc_heap_stats(void *heap, uint32 *stats, int size);
  109. #if BH_ENABLE_GC_VERIFY == 0
  110. gc_object_t
  111. gc_alloc_vo(void *heap, gc_size_t size);
  112. gc_object_t
  113. gc_realloc_vo(void *heap, void *ptr, gc_size_t size);
  114. int
  115. gc_free_vo(void *heap, gc_object_t obj);
  116. #else /* else of BH_ENABLE_GC_VERIFY */
  117. gc_object_t
  118. gc_alloc_vo_internal(void *heap, gc_size_t size, const char *file, int line);
  119. gc_object_t
  120. gc_realloc_vo_internal(void *heap, void *ptr, gc_size_t size, const char *file,
  121. int line);
  122. int
  123. gc_free_vo_internal(void *heap, gc_object_t obj, const char *file, int line);
  124. /* clang-format off */
  125. #define gc_alloc_vo(heap, size) \
  126. gc_alloc_vo_internal(heap, size, __FILE__, __LINE__)
  127. #define gc_realloc_vo(heap, ptr, size) \
  128. gc_realloc_vo_internal(heap, ptr, size, __FILE__, __LINE__)
  129. #define gc_free_vo(heap, obj) \
  130. gc_free_vo_internal(heap, obj, __FILE__, __LINE__)
  131. /* clang-format on */
  132. #endif /* end of BH_ENABLE_GC_VERIFY */
  133. #ifdef __cplusplus
  134. }
  135. #endif
  136. #endif