wasm_memory.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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,
  34. void *_realloc_func,
  35. void *_free_func)
  36. {
  37. if (_malloc_func && _free_func && _malloc_func != _free_func) {
  38. memory_mode = MEMORY_MODE_ALLOCATOR;
  39. malloc_func = _malloc_func;
  40. realloc_func = _realloc_func;
  41. free_func = _free_func;
  42. return true;
  43. }
  44. LOG_ERROR("Init memory with allocator (%p, %p, %p) failed.\n",
  45. _malloc_func, _realloc_func, _free_func);
  46. return false;
  47. }
  48. bool
  49. wasm_runtime_memory_init(mem_alloc_type_t mem_alloc_type,
  50. const MemAllocOption *alloc_option)
  51. {
  52. if (mem_alloc_type == Alloc_With_Pool)
  53. return wasm_memory_init_with_pool(alloc_option->pool.heap_buf,
  54. alloc_option->pool.heap_size);
  55. else if (mem_alloc_type == Alloc_With_Allocator)
  56. return wasm_memory_init_with_allocator(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("wasm_runtime_malloc failed: memory hasn't been initialize.\n");
  84. return NULL;
  85. }
  86. else if (memory_mode == MEMORY_MODE_POOL) {
  87. return mem_allocator_malloc(pool_allocator, size);
  88. }
  89. else {
  90. return malloc_func(size);
  91. }
  92. }
  93. static inline void *
  94. wasm_runtime_realloc_internal(void *ptr, unsigned int size)
  95. {
  96. if (memory_mode == MEMORY_MODE_UNKNOWN) {
  97. LOG_WARNING("wasm_runtime_realloc failed: memory hasn't been initialize.\n");
  98. return NULL;
  99. }
  100. else if (memory_mode == MEMORY_MODE_POOL) {
  101. return mem_allocator_realloc(pool_allocator, ptr, size);
  102. }
  103. else {
  104. if (realloc_func)
  105. return realloc_func(ptr, size);
  106. else
  107. return NULL;
  108. }
  109. }
  110. static inline void
  111. wasm_runtime_free_internal(void *ptr)
  112. {
  113. if (!ptr) {
  114. LOG_WARNING("warning: wasm_runtime_free with NULL pointer\n");
  115. return;
  116. }
  117. if (memory_mode == MEMORY_MODE_UNKNOWN) {
  118. LOG_WARNING("warning: wasm_runtime_free failed: "
  119. "memory hasn't been initialize.\n");
  120. }
  121. else if (memory_mode == MEMORY_MODE_POOL) {
  122. mem_allocator_free(pool_allocator, ptr);
  123. }
  124. else {
  125. free_func(ptr);
  126. }
  127. }
  128. void *
  129. wasm_runtime_malloc(unsigned int size)
  130. {
  131. if (size == 0) {
  132. LOG_WARNING("warning: wasm_runtime_malloc with size zero\n");
  133. /* At lease alloc 1 byte to avoid malloc failed */
  134. size = 1;
  135. }
  136. return wasm_runtime_malloc_internal(size);
  137. }
  138. void *
  139. wasm_runtime_realloc(void *ptr, unsigned int size)
  140. {
  141. return wasm_runtime_realloc_internal(ptr, size);
  142. }
  143. void
  144. wasm_runtime_free(void *ptr)
  145. {
  146. wasm_runtime_free_internal(ptr);
  147. }