mem_alloc.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. int
  49. mem_allocator_migrate(mem_allocator_t allocator, char *pool_buf_new,
  50. uint32 pool_buf_size)
  51. {
  52. return gc_migrate((gc_handle_t)allocator, pool_buf_new, pool_buf_size);
  53. }
  54. bool
  55. mem_allocator_is_heap_corrupted(mem_allocator_t allocator)
  56. {
  57. return gc_is_heap_corrupted((gc_handle_t)allocator);
  58. }
  59. bool
  60. mem_allocator_get_alloc_info(mem_allocator_t allocator, void *mem_alloc_info)
  61. {
  62. gc_heap_stats((gc_handle_t)allocator, mem_alloc_info, 3);
  63. return true;
  64. }
  65. #else /* else of DEFAULT_MEM_ALLOCATOR */
  66. #include "tlsf/tlsf.h"
  67. typedef struct mem_allocator_tlsf {
  68. tlsf_t tlsf;
  69. korp_mutex lock;
  70. } mem_allocator_tlsf;
  71. mem_allocator_t
  72. mem_allocator_create(void *mem, uint32_t size)
  73. {
  74. mem_allocator_tlsf *allocator_tlsf;
  75. tlsf_t tlsf;
  76. char *mem_aligned = (char *)(((uintptr_t)mem + 3) & ~3);
  77. if (size < 1024) {
  78. printf("Create mem allocator failed: pool size must be "
  79. "at least 1024 bytes.\n");
  80. return NULL;
  81. }
  82. size -= mem_aligned - (char *)mem;
  83. mem = (void *)mem_aligned;
  84. tlsf = tlsf_create_with_pool(mem, size);
  85. if (!tlsf) {
  86. printf("Create mem allocator failed: tlsf_create_with_pool failed.\n");
  87. return NULL;
  88. }
  89. allocator_tlsf = tlsf_malloc(tlsf, sizeof(mem_allocator_tlsf));
  90. if (!allocator_tlsf) {
  91. printf("Create mem allocator failed: tlsf_malloc failed.\n");
  92. tlsf_destroy(tlsf);
  93. return NULL;
  94. }
  95. allocator_tlsf->tlsf = tlsf;
  96. if (os_mutex_init(&allocator_tlsf->lock)) {
  97. printf("Create mem allocator failed: tlsf_malloc failed.\n");
  98. tlsf_free(tlsf, allocator_tlsf);
  99. tlsf_destroy(tlsf);
  100. return NULL;
  101. }
  102. return allocator_tlsf;
  103. }
  104. void
  105. mem_allocator_destroy(mem_allocator_t allocator)
  106. {
  107. mem_allocator_tlsf *allocator_tlsf = (mem_allocator_tlsf *)allocator;
  108. tlsf_t tlsf = allocator_tlsf->tlsf;
  109. os_mutex_destroy(&allocator_tlsf->lock);
  110. tlsf_free(tlsf, allocator_tlsf);
  111. tlsf_destroy(tlsf);
  112. }
  113. void *
  114. mem_allocator_malloc(mem_allocator_t allocator, uint32_t size)
  115. {
  116. void *ret;
  117. mem_allocator_tlsf *allocator_tlsf = (mem_allocator_tlsf *)allocator;
  118. if (size == 0)
  119. /* tlsf doesn't allow to allocate 0 byte */
  120. size = 1;
  121. os_mutex_lock(&allocator_tlsf->lock);
  122. ret = tlsf_malloc(allocator_tlsf->tlsf, size);
  123. os_mutex_unlock(&allocator_tlsf->lock);
  124. return ret;
  125. }
  126. void *
  127. mem_allocator_realloc(mem_allocator_t allocator, void *ptr, uint32_t size)
  128. {
  129. void *ret;
  130. mem_allocator_tlsf *allocator_tlsf = (mem_allocator_tlsf *)allocator;
  131. if (size == 0)
  132. /* tlsf doesn't allow to allocate 0 byte */
  133. size = 1;
  134. os_mutex_lock(&allocator_tlsf->lock);
  135. ret = tlsf_realloc(allocator_tlsf->tlsf, ptr, size);
  136. os_mutex_unlock(&allocator_tlsf->lock);
  137. return ret;
  138. }
  139. void
  140. mem_allocator_free(mem_allocator_t allocator, void *ptr)
  141. {
  142. if (ptr) {
  143. mem_allocator_tlsf *allocator_tlsf = (mem_allocator_tlsf *)allocator;
  144. os_mutex_lock(&allocator_tlsf->lock);
  145. tlsf_free(allocator_tlsf->tlsf, ptr);
  146. os_mutex_unlock(&allocator_tlsf->lock);
  147. }
  148. }
  149. int
  150. mem_allocator_migrate(mem_allocator_t allocator, mem_allocator_t allocator_old)
  151. {
  152. return tlsf_migrate((mem_allocator_tlsf *)allocator,
  153. (mem_allocator_tlsf *)allocator_old);
  154. }
  155. #endif /* end of DEFAULT_MEM_ALLOCATOR */