mem_alloc.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef __MEM_ALLOC_H
  6. #define __MEM_ALLOC_H
  7. #include "bh_platform.h"
  8. #if WASM_ENABLE_GC != 0
  9. #include "../../common/gc/gc_object.h"
  10. #endif
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. typedef void *mem_allocator_t;
  15. #ifndef GC_FINALIZER_T_DEFINED
  16. #define GC_FINALIZER_T_DEFINED
  17. typedef void (*gc_finalizer_t)(void *obj, void *data);
  18. #endif
  19. mem_allocator_t
  20. mem_allocator_create(void *mem, uint32_t size);
  21. mem_allocator_t
  22. mem_allocator_create_with_struct_and_pool(void *struct_buf,
  23. uint32_t struct_buf_size,
  24. void *pool_buf,
  25. uint32_t pool_buf_size);
  26. int
  27. mem_allocator_destroy(mem_allocator_t allocator);
  28. uint32
  29. mem_allocator_get_heap_struct_size(void);
  30. void *
  31. mem_allocator_malloc(mem_allocator_t allocator, uint32_t size);
  32. void *
  33. mem_allocator_realloc(mem_allocator_t allocator, void *ptr, uint32_t size);
  34. void
  35. mem_allocator_free(mem_allocator_t allocator, void *ptr);
  36. /* Aligned allocation support */
  37. #ifndef GC_MIN_ALIGNMENT
  38. #define GC_MIN_ALIGNMENT 8
  39. #endif
  40. #if BH_ENABLE_GC_VERIFY == 0
  41. void *
  42. mem_allocator_malloc_aligned(mem_allocator_t allocator, uint32_t size,
  43. uint32_t alignment);
  44. #define mem_allocator_malloc_aligned_internal(allocator, size, alignment, \
  45. file, line) \
  46. mem_allocator_malloc_aligned(allocator, size, alignment)
  47. #else /* BH_ENABLE_GC_VERIFY != 0 */
  48. void *
  49. mem_allocator_malloc_aligned_internal(mem_allocator_t allocator, uint32_t size,
  50. uint32_t alignment, const char *file,
  51. int line);
  52. #define mem_allocator_malloc_aligned(allocator, size, alignment) \
  53. mem_allocator_malloc_aligned_internal(allocator, size, alignment, \
  54. __FILE__, __LINE__)
  55. #endif /* end of BH_ENABLE_GC_VERIFY */
  56. int
  57. mem_allocator_migrate(mem_allocator_t allocator, char *pool_buf_new,
  58. uint32 pool_buf_size);
  59. bool
  60. mem_allocator_is_heap_corrupted(mem_allocator_t allocator);
  61. #if WASM_ENABLE_GC != 0
  62. void *
  63. mem_allocator_malloc_with_gc(mem_allocator_t allocator, uint32_t size);
  64. #if WASM_GC_MANUALLY != 0
  65. void
  66. mem_allocator_free_with_gc(mem_allocator_t allocator, void *ptr);
  67. #endif
  68. #if WASM_ENABLE_THREAD_MGR == 0
  69. void
  70. mem_allocator_enable_gc_reclaim(mem_allocator_t allocator, void *exec_env);
  71. #else
  72. void
  73. mem_allocator_enable_gc_reclaim(mem_allocator_t allocator, void *cluster);
  74. #endif
  75. int
  76. mem_allocator_add_root(mem_allocator_t allocator, WASMObjectRef obj);
  77. bool
  78. mem_allocator_set_gc_finalizer(mem_allocator_t allocator, void *obj,
  79. gc_finalizer_t cb, void *data);
  80. void
  81. mem_allocator_unset_gc_finalizer(mem_allocator_t allocator, void *obj);
  82. #if WASM_ENABLE_GC_PERF_PROFILING != 0
  83. void
  84. mem_allocator_dump_perf_profiling(mem_allocator_t allocator);
  85. #endif
  86. #endif /* end of WASM_ENABLE_GC != 0 */
  87. bool
  88. mem_allocator_get_alloc_info(mem_allocator_t allocator, void *mem_alloc_info);
  89. #ifdef __cplusplus
  90. }
  91. #endif
  92. #endif /* #ifndef __MEM_ALLOC_H */