wasm_memory.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_runtime_common.h"
  6. #include "bh_platform.h"
  7. #include "mem_alloc.h"
  8. typedef enum Memory_Mode {
  9. MEMORY_MODE_UNKNOWN = 0,
  10. MEMORY_MODE_POOL,
  11. MEMORY_MODE_ALLOCATOR
  12. } Memory_Mode;
  13. static Memory_Mode memory_mode = MEMORY_MODE_UNKNOWN;
  14. static mem_allocator_t pool_allocator = NULL;
  15. static void *(*malloc_func)(unsigned int size) = NULL;
  16. static void *(*realloc_func)(void *ptr, unsigned int size) = NULL;
  17. static void (*free_func)(void *ptr) = NULL;
  18. static unsigned int global_pool_size;
  19. static bool
  20. wasm_memory_init_with_pool(void *mem, unsigned int bytes)
  21. {
  22. mem_allocator_t _allocator = mem_allocator_create(mem, bytes);
  23. if (_allocator) {
  24. memory_mode = MEMORY_MODE_POOL;
  25. pool_allocator = _allocator;
  26. global_pool_size = bytes;
  27. return true;
  28. }
  29. LOG_ERROR("Init memory with pool (%p, %u) failed.\n", mem, bytes);
  30. return false;
  31. }
  32. static bool
  33. wasm_memory_init_with_allocator(void *_malloc_func, void *_realloc_func,
  34. void *_free_func)
  35. {
  36. if (_malloc_func && _free_func && _malloc_func != _free_func) {
  37. memory_mode = MEMORY_MODE_ALLOCATOR;
  38. malloc_func = _malloc_func;
  39. realloc_func = _realloc_func;
  40. free_func = _free_func;
  41. return true;
  42. }
  43. LOG_ERROR("Init memory with allocator (%p, %p, %p) failed.\n", _malloc_func,
  44. _realloc_func, _free_func);
  45. return false;
  46. }
  47. bool
  48. wasm_runtime_memory_init(mem_alloc_type_t mem_alloc_type,
  49. const MemAllocOption *alloc_option)
  50. {
  51. if (mem_alloc_type == Alloc_With_Pool)
  52. return wasm_memory_init_with_pool(alloc_option->pool.heap_buf,
  53. alloc_option->pool.heap_size);
  54. else if (mem_alloc_type == Alloc_With_Allocator)
  55. return wasm_memory_init_with_allocator(
  56. alloc_option->allocator.malloc_func,
  57. alloc_option->allocator.realloc_func,
  58. alloc_option->allocator.free_func);
  59. else if (mem_alloc_type == Alloc_With_System_Allocator)
  60. return wasm_memory_init_with_allocator(os_malloc, os_realloc, os_free);
  61. else
  62. return false;
  63. }
  64. void
  65. wasm_runtime_memory_destroy()
  66. {
  67. if (memory_mode == MEMORY_MODE_POOL) {
  68. #if BH_ENABLE_GC_VERIFY == 0
  69. (void)mem_allocator_destroy(pool_allocator);
  70. #else
  71. int ret = mem_allocator_destroy(pool_allocator);
  72. if (ret != 0) {
  73. /* Memory leak detected */
  74. exit(-1);
  75. }
  76. #endif
  77. }
  78. memory_mode = MEMORY_MODE_UNKNOWN;
  79. }
  80. unsigned
  81. wasm_runtime_memory_pool_size()
  82. {
  83. if (memory_mode == MEMORY_MODE_POOL)
  84. return global_pool_size;
  85. else
  86. return UINT32_MAX;
  87. }
  88. static inline void *
  89. wasm_runtime_malloc_internal(unsigned int size)
  90. {
  91. if (memory_mode == MEMORY_MODE_UNKNOWN) {
  92. LOG_WARNING(
  93. "wasm_runtime_malloc failed: memory hasn't been initialize.\n");
  94. return NULL;
  95. }
  96. else if (memory_mode == MEMORY_MODE_POOL) {
  97. return mem_allocator_malloc(pool_allocator, size);
  98. }
  99. else {
  100. return malloc_func(size);
  101. }
  102. }
  103. static inline void *
  104. wasm_runtime_realloc_internal(void *ptr, unsigned int size)
  105. {
  106. if (memory_mode == MEMORY_MODE_UNKNOWN) {
  107. LOG_WARNING(
  108. "wasm_runtime_realloc failed: memory hasn't been initialize.\n");
  109. return NULL;
  110. }
  111. else if (memory_mode == MEMORY_MODE_POOL) {
  112. return mem_allocator_realloc(pool_allocator, ptr, size);
  113. }
  114. else {
  115. if (realloc_func)
  116. return realloc_func(ptr, size);
  117. else
  118. return NULL;
  119. }
  120. }
  121. static inline void
  122. wasm_runtime_free_internal(void *ptr)
  123. {
  124. if (!ptr) {
  125. LOG_WARNING("warning: wasm_runtime_free with NULL pointer\n");
  126. return;
  127. }
  128. if (memory_mode == MEMORY_MODE_UNKNOWN) {
  129. LOG_WARNING("warning: wasm_runtime_free failed: "
  130. "memory hasn't been initialize.\n");
  131. }
  132. else if (memory_mode == MEMORY_MODE_POOL) {
  133. mem_allocator_free(pool_allocator, ptr);
  134. }
  135. else {
  136. free_func(ptr);
  137. }
  138. }
  139. void *
  140. wasm_runtime_malloc(unsigned int size)
  141. {
  142. if (size == 0) {
  143. LOG_WARNING("warning: wasm_runtime_malloc with size zero\n");
  144. /* At lease alloc 1 byte to avoid malloc failed */
  145. size = 1;
  146. }
  147. return wasm_runtime_malloc_internal(size);
  148. }
  149. void *
  150. wasm_runtime_realloc(void *ptr, unsigned int size)
  151. {
  152. return wasm_runtime_realloc_internal(ptr, size);
  153. }
  154. void
  155. wasm_runtime_free(void *ptr)
  156. {
  157. wasm_runtime_free_internal(ptr);
  158. }
  159. bool
  160. wasm_runtime_get_mem_alloc_info(mem_alloc_info_t *mem_alloc_info)
  161. {
  162. if (memory_mode == MEMORY_MODE_POOL) {
  163. return mem_allocator_get_alloc_info(pool_allocator, mem_alloc_info);
  164. }
  165. return false;
  166. }