mem_alloc.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. return 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. return 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. #endif
  112. #else /* else of DEFAULT_MEM_ALLOCATOR */
  113. #include "tlsf/tlsf.h"
  114. typedef struct mem_allocator_tlsf {
  115. tlsf_t tlsf;
  116. korp_mutex lock;
  117. } mem_allocator_tlsf;
  118. mem_allocator_t
  119. mem_allocator_create(void *mem, uint32_t size)
  120. {
  121. mem_allocator_tlsf *allocator_tlsf;
  122. tlsf_t tlsf;
  123. char *mem_aligned = (char *)(((uintptr_t)mem + 3) & ~3);
  124. if (size < 1024) {
  125. printf("Create mem allocator failed: pool size must be "
  126. "at least 1024 bytes.\n");
  127. return NULL;
  128. }
  129. size -= mem_aligned - (char *)mem;
  130. mem = (void *)mem_aligned;
  131. tlsf = tlsf_create_with_pool(mem, size);
  132. if (!tlsf) {
  133. printf("Create mem allocator failed: tlsf_create_with_pool failed.\n");
  134. return NULL;
  135. }
  136. allocator_tlsf = tlsf_malloc(tlsf, sizeof(mem_allocator_tlsf));
  137. if (!allocator_tlsf) {
  138. printf("Create mem allocator failed: tlsf_malloc failed.\n");
  139. tlsf_destroy(tlsf);
  140. return NULL;
  141. }
  142. allocator_tlsf->tlsf = tlsf;
  143. if (os_mutex_init(&allocator_tlsf->lock)) {
  144. printf("Create mem allocator failed: tlsf_malloc failed.\n");
  145. tlsf_free(tlsf, allocator_tlsf);
  146. tlsf_destroy(tlsf);
  147. return NULL;
  148. }
  149. return allocator_tlsf;
  150. }
  151. void
  152. mem_allocator_destroy(mem_allocator_t allocator)
  153. {
  154. mem_allocator_tlsf *allocator_tlsf = (mem_allocator_tlsf *)allocator;
  155. tlsf_t tlsf = allocator_tlsf->tlsf;
  156. os_mutex_destroy(&allocator_tlsf->lock);
  157. tlsf_free(tlsf, allocator_tlsf);
  158. tlsf_destroy(tlsf);
  159. }
  160. void *
  161. mem_allocator_malloc(mem_allocator_t allocator, uint32_t size)
  162. {
  163. void *ret;
  164. mem_allocator_tlsf *allocator_tlsf = (mem_allocator_tlsf *)allocator;
  165. if (size == 0)
  166. /* tlsf doesn't allow to allocate 0 byte */
  167. size = 1;
  168. os_mutex_lock(&allocator_tlsf->lock);
  169. ret = tlsf_malloc(allocator_tlsf->tlsf, size);
  170. os_mutex_unlock(&allocator_tlsf->lock);
  171. return ret;
  172. }
  173. void *
  174. mem_allocator_realloc(mem_allocator_t allocator, void *ptr, uint32_t size)
  175. {
  176. void *ret;
  177. mem_allocator_tlsf *allocator_tlsf = (mem_allocator_tlsf *)allocator;
  178. if (size == 0)
  179. /* tlsf doesn't allow to allocate 0 byte */
  180. size = 1;
  181. os_mutex_lock(&allocator_tlsf->lock);
  182. ret = tlsf_realloc(allocator_tlsf->tlsf, ptr, size);
  183. os_mutex_unlock(&allocator_tlsf->lock);
  184. return ret;
  185. }
  186. void
  187. mem_allocator_free(mem_allocator_t allocator, void *ptr)
  188. {
  189. if (ptr) {
  190. mem_allocator_tlsf *allocator_tlsf = (mem_allocator_tlsf *)allocator;
  191. os_mutex_lock(&allocator_tlsf->lock);
  192. tlsf_free(allocator_tlsf->tlsf, ptr);
  193. os_mutex_unlock(&allocator_tlsf->lock);
  194. }
  195. }
  196. int
  197. mem_allocator_migrate(mem_allocator_t allocator, mem_allocator_t allocator_old)
  198. {
  199. return tlsf_migrate((mem_allocator_tlsf *)allocator,
  200. (mem_allocator_tlsf *)allocator_old);
  201. }
  202. #endif /* end of DEFAULT_MEM_ALLOCATOR */