mem_alloc.c 5.5 KB

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