mem_alloc.c 4.5 KB

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