wasm_memory.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. mem_allocator_destroy(pool_allocator);
  69. memory_mode = MEMORY_MODE_UNKNOWN;
  70. }
  71. unsigned
  72. wasm_runtime_memory_pool_size()
  73. {
  74. if (memory_mode == MEMORY_MODE_POOL)
  75. return global_pool_size;
  76. else
  77. return 1 * BH_GB;
  78. }
  79. static inline void *
  80. wasm_runtime_malloc_internal(unsigned int size)
  81. {
  82. if (memory_mode == MEMORY_MODE_UNKNOWN) {
  83. LOG_WARNING(
  84. "wasm_runtime_malloc failed: memory hasn't been initialize.\n");
  85. return NULL;
  86. }
  87. else if (memory_mode == MEMORY_MODE_POOL) {
  88. return mem_allocator_malloc(pool_allocator, size);
  89. }
  90. else {
  91. return malloc_func(size);
  92. }
  93. }
  94. static inline void *
  95. wasm_runtime_realloc_internal(void *ptr, unsigned int size)
  96. {
  97. if (memory_mode == MEMORY_MODE_UNKNOWN) {
  98. LOG_WARNING(
  99. "wasm_runtime_realloc failed: memory hasn't been initialize.\n");
  100. return NULL;
  101. }
  102. else if (memory_mode == MEMORY_MODE_POOL) {
  103. return mem_allocator_realloc(pool_allocator, ptr, size);
  104. }
  105. else {
  106. if (realloc_func)
  107. return realloc_func(ptr, size);
  108. else
  109. return NULL;
  110. }
  111. }
  112. static inline void
  113. wasm_runtime_free_internal(void *ptr)
  114. {
  115. if (!ptr) {
  116. LOG_WARNING("warning: wasm_runtime_free with NULL pointer\n");
  117. return;
  118. }
  119. if (memory_mode == MEMORY_MODE_UNKNOWN) {
  120. LOG_WARNING("warning: wasm_runtime_free failed: "
  121. "memory hasn't been initialize.\n");
  122. }
  123. else if (memory_mode == MEMORY_MODE_POOL) {
  124. mem_allocator_free(pool_allocator, ptr);
  125. }
  126. else {
  127. free_func(ptr);
  128. }
  129. }
  130. void *
  131. wasm_runtime_malloc(unsigned int size)
  132. {
  133. if (size == 0) {
  134. LOG_WARNING("warning: wasm_runtime_malloc with size zero\n");
  135. /* At lease alloc 1 byte to avoid malloc failed */
  136. size = 1;
  137. }
  138. return wasm_runtime_malloc_internal(size);
  139. }
  140. void *
  141. wasm_runtime_realloc(void *ptr, unsigned int size)
  142. {
  143. return wasm_runtime_realloc_internal(ptr, size);
  144. }
  145. void
  146. wasm_runtime_free(void *ptr)
  147. {
  148. wasm_runtime_free_internal(ptr);
  149. }