mem_alloc.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "mem_alloc.h"
  6. #include <stdbool.h>
  7. #if DEFAULT_MEM_ALLOCATOR == MEM_ALLOCATOR_EMS
  8. #include "ems/ems_gc.h"
  9. mem_allocator_t
  10. mem_allocator_create(void *mem, uint32_t size)
  11. {
  12. return gc_init_with_pool((char *)mem, size);
  13. }
  14. mem_allocator_t
  15. mem_allocator_create_with_struct_and_pool(void *struct_buf,
  16. uint32_t struct_buf_size,
  17. void *pool_buf,
  18. uint32_t pool_buf_size)
  19. {
  20. return gc_init_with_struct_and_pool((char *)struct_buf, struct_buf_size,
  21. pool_buf, pool_buf_size);
  22. }
  23. int
  24. mem_allocator_destroy(mem_allocator_t allocator)
  25. {
  26. return gc_destroy_with_pool((gc_handle_t)allocator);
  27. }
  28. uint32
  29. mem_allocator_get_heap_struct_size()
  30. {
  31. return gc_get_heap_struct_size();
  32. }
  33. void *
  34. mem_allocator_malloc(mem_allocator_t allocator, uint32_t size)
  35. {
  36. return gc_alloc_vo((gc_handle_t)allocator, size);
  37. }
  38. void *
  39. mem_allocator_realloc(mem_allocator_t allocator, void *ptr, uint32_t size)
  40. {
  41. return gc_realloc_vo((gc_handle_t)allocator, ptr, size);
  42. }
  43. void
  44. mem_allocator_free(mem_allocator_t allocator, void *ptr)
  45. {
  46. if (ptr)
  47. gc_free_vo((gc_handle_t)allocator, ptr);
  48. }
  49. #if WASM_ENABLE_GC != 0
  50. void *
  51. mem_allocator_malloc_with_gc(mem_allocator_t allocator, uint32_t size)
  52. {
  53. return gc_alloc_wo((gc_handle_t)allocator, size);
  54. }
  55. #if WASM_GC_MANUALLY != 0
  56. void
  57. mem_allocator_free_with_gc(mem_allocator_t allocator, void *ptr)
  58. {
  59. if (ptr)
  60. gc_free_wo((gc_handle_t)allocator, ptr);
  61. }
  62. #endif
  63. #if WASM_ENABLE_THREAD_MGR == 0
  64. void
  65. mem_allocator_enable_gc_reclaim(mem_allocator_t allocator, void *exec_env)
  66. {
  67. gc_enable_gc_reclaim((gc_handle_t)allocator, exec_env);
  68. }
  69. #else
  70. void
  71. mem_allocator_enable_gc_reclaim(mem_allocator_t allocator, void *cluster)
  72. {
  73. gc_enable_gc_reclaim((gc_handle_t)allocator, cluster);
  74. }
  75. #endif
  76. int
  77. mem_allocator_add_root(mem_allocator_t allocator, WASMObjectRef obj)
  78. {
  79. return gc_add_root((gc_handle_t)allocator, (gc_object_t)obj);
  80. }
  81. #endif
  82. int
  83. mem_allocator_migrate(mem_allocator_t allocator, char *pool_buf_new,
  84. uint32 pool_buf_size)
  85. {
  86. return gc_migrate((gc_handle_t)allocator, pool_buf_new, pool_buf_size);
  87. }
  88. bool
  89. mem_allocator_is_heap_corrupted(mem_allocator_t allocator)
  90. {
  91. return gc_is_heap_corrupted((gc_handle_t)allocator);
  92. }
  93. bool
  94. mem_allocator_get_alloc_info(mem_allocator_t allocator, void *mem_alloc_info)
  95. {
  96. gc_heap_stats((gc_handle_t)allocator, mem_alloc_info, 3);
  97. return true;
  98. }
  99. #if WASM_ENABLE_GC != 0
  100. bool
  101. mem_allocator_set_gc_finalizer(mem_allocator_t allocator, void *obj,
  102. gc_finalizer_t cb, void *data)
  103. {
  104. return gc_set_finalizer((gc_handle_t)allocator, (gc_object_t)obj, cb, data);
  105. }
  106. void
  107. mem_allocator_unset_gc_finalizer(mem_allocator_t allocator, void *obj)
  108. {
  109. gc_unset_finalizer((gc_handle_t)allocator, (gc_object_t)obj);
  110. }
  111. #if WASM_ENABLE_GC_PERF_PROFILING != 0
  112. void
  113. mem_allocator_dump_perf_profiling(mem_allocator_t allocator)
  114. {
  115. gc_dump_perf_profiling((gc_handle_t)allocator);
  116. }
  117. #endif
  118. #endif
  119. #else /* else of DEFAULT_MEM_ALLOCATOR */
  120. #include "tlsf/tlsf.h"
  121. typedef struct mem_allocator_tlsf {
  122. tlsf_t tlsf;
  123. korp_mutex lock;
  124. } mem_allocator_tlsf;
  125. mem_allocator_t
  126. mem_allocator_create(void *mem, uint32_t size)
  127. {
  128. mem_allocator_tlsf *allocator_tlsf;
  129. tlsf_t tlsf;
  130. char *mem_aligned = (char *)(((uintptr_t)mem + 3) & ~3);
  131. if (size < 1024) {
  132. printf("Create mem allocator failed: pool size must be "
  133. "at least 1024 bytes.\n");
  134. return NULL;
  135. }
  136. size -= mem_aligned - (char *)mem;
  137. mem = (void *)mem_aligned;
  138. tlsf = tlsf_create_with_pool(mem, size);
  139. if (!tlsf) {
  140. printf("Create mem allocator failed: tlsf_create_with_pool failed.\n");
  141. return NULL;
  142. }
  143. allocator_tlsf = tlsf_malloc(tlsf, sizeof(mem_allocator_tlsf));
  144. if (!allocator_tlsf) {
  145. printf("Create mem allocator failed: tlsf_malloc failed.\n");
  146. tlsf_destroy(tlsf);
  147. return NULL;
  148. }
  149. allocator_tlsf->tlsf = tlsf;
  150. if (os_mutex_init(&allocator_tlsf->lock)) {
  151. printf("Create mem allocator failed: tlsf_malloc failed.\n");
  152. tlsf_free(tlsf, allocator_tlsf);
  153. tlsf_destroy(tlsf);
  154. return NULL;
  155. }
  156. return allocator_tlsf;
  157. }
  158. void
  159. mem_allocator_destroy(mem_allocator_t allocator)
  160. {
  161. mem_allocator_tlsf *allocator_tlsf = (mem_allocator_tlsf *)allocator;
  162. tlsf_t tlsf = allocator_tlsf->tlsf;
  163. os_mutex_destroy(&allocator_tlsf->lock);
  164. tlsf_free(tlsf, allocator_tlsf);
  165. tlsf_destroy(tlsf);
  166. }
  167. void *
  168. mem_allocator_malloc(mem_allocator_t allocator, uint32_t size)
  169. {
  170. void *ret;
  171. mem_allocator_tlsf *allocator_tlsf = (mem_allocator_tlsf *)allocator;
  172. if (size == 0)
  173. /* tlsf doesn't allow to allocate 0 byte */
  174. size = 1;
  175. os_mutex_lock(&allocator_tlsf->lock);
  176. ret = tlsf_malloc(allocator_tlsf->tlsf, size);
  177. os_mutex_unlock(&allocator_tlsf->lock);
  178. return ret;
  179. }
  180. void *
  181. mem_allocator_realloc(mem_allocator_t allocator, void *ptr, uint32_t size)
  182. {
  183. void *ret;
  184. mem_allocator_tlsf *allocator_tlsf = (mem_allocator_tlsf *)allocator;
  185. if (size == 0)
  186. /* tlsf doesn't allow to allocate 0 byte */
  187. size = 1;
  188. os_mutex_lock(&allocator_tlsf->lock);
  189. ret = tlsf_realloc(allocator_tlsf->tlsf, ptr, size);
  190. os_mutex_unlock(&allocator_tlsf->lock);
  191. return ret;
  192. }
  193. void
  194. mem_allocator_free(mem_allocator_t allocator, void *ptr)
  195. {
  196. if (ptr) {
  197. mem_allocator_tlsf *allocator_tlsf = (mem_allocator_tlsf *)allocator;
  198. os_mutex_lock(&allocator_tlsf->lock);
  199. tlsf_free(allocator_tlsf->tlsf, ptr);
  200. os_mutex_unlock(&allocator_tlsf->lock);
  201. }
  202. }
  203. int
  204. mem_allocator_migrate(mem_allocator_t allocator, mem_allocator_t allocator_old)
  205. {
  206. return tlsf_migrate((mem_allocator_tlsf *)allocator,
  207. (mem_allocator_tlsf *)allocator_old);
  208. }
  209. #endif /* end of DEFAULT_MEM_ALLOCATOR */