mem_alloc.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 mem_allocator_create(void *mem, uint32_t size)
  9. {
  10. return gc_init_with_pool((char *) mem, size);
  11. }
  12. void mem_allocator_destroy(mem_allocator_t allocator)
  13. {
  14. gc_destroy_with_pool((gc_handle_t) allocator);
  15. }
  16. void *
  17. mem_allocator_malloc(mem_allocator_t allocator, uint32_t size)
  18. {
  19. return gc_alloc_vo((gc_handle_t) allocator, size);
  20. }
  21. void *
  22. mem_allocator_realloc(mem_allocator_t allocator, void *ptr, uint32_t size)
  23. {
  24. return gc_realloc_vo((gc_handle_t) allocator, ptr, size);
  25. }
  26. void mem_allocator_free(mem_allocator_t allocator, void *ptr)
  27. {
  28. if (ptr)
  29. gc_free_vo((gc_handle_t) allocator, ptr);
  30. }
  31. int
  32. mem_allocator_migrate(mem_allocator_t allocator,
  33. mem_allocator_t allocator_old)
  34. {
  35. return gc_migrate((gc_handle_t) allocator,
  36. (gc_handle_t) allocator_old);
  37. }
  38. int
  39. mem_allocator_reinit_lock(mem_allocator_t allocator)
  40. {
  41. return gc_reinit_lock((gc_handle_t) allocator);
  42. }
  43. void
  44. mem_allocator_destroy_lock(mem_allocator_t allocator)
  45. {
  46. gc_destroy_lock((gc_handle_t) allocator);
  47. }
  48. #else /* else of DEFAULT_MEM_ALLOCATOR */
  49. #include "tlsf/tlsf.h"
  50. typedef struct mem_allocator_tlsf {
  51. tlsf_t tlsf;
  52. korp_mutex lock;
  53. }mem_allocator_tlsf;
  54. mem_allocator_t
  55. mem_allocator_create(void *mem, uint32_t size)
  56. {
  57. mem_allocator_tlsf *allocator_tlsf;
  58. tlsf_t tlsf;
  59. char *mem_aligned = (char*)(((uintptr_t)mem + 3) & ~3);
  60. if (size < 1024) {
  61. printf("Create mem allocator failed: pool size must be "
  62. "at least 1024 bytes.\n");
  63. return NULL;
  64. }
  65. size -= mem_aligned - (char*)mem;
  66. mem = (void*)mem_aligned;
  67. tlsf = tlsf_create_with_pool(mem, size);
  68. if (!tlsf) {
  69. printf("Create mem allocator failed: tlsf_create_with_pool failed.\n");
  70. return NULL;
  71. }
  72. allocator_tlsf = tlsf_malloc(tlsf, sizeof(mem_allocator_tlsf));
  73. if (!allocator_tlsf) {
  74. printf("Create mem allocator failed: tlsf_malloc failed.\n");
  75. tlsf_destroy(tlsf);
  76. return NULL;
  77. }
  78. allocator_tlsf->tlsf = tlsf;
  79. if (os_mutex_init(&allocator_tlsf->lock)) {
  80. printf("Create mem allocator failed: tlsf_malloc failed.\n");
  81. tlsf_free(tlsf, allocator_tlsf);
  82. tlsf_destroy(tlsf);
  83. return NULL;
  84. }
  85. return allocator_tlsf;
  86. }
  87. void
  88. mem_allocator_destroy(mem_allocator_t allocator)
  89. {
  90. mem_allocator_tlsf *allocator_tlsf = (mem_allocator_tlsf *)allocator;
  91. tlsf_t tlsf = allocator_tlsf->tlsf;
  92. os_mutex_destroy(&allocator_tlsf->lock);
  93. tlsf_free(tlsf, allocator_tlsf);
  94. tlsf_destroy(tlsf);
  95. }
  96. void *
  97. mem_allocator_malloc(mem_allocator_t allocator, uint32_t size)
  98. {
  99. void *ret;
  100. mem_allocator_tlsf *allocator_tlsf = (mem_allocator_tlsf *)allocator;
  101. if (size == 0)
  102. /* tlsf doesn't allow to allocate 0 byte */
  103. size = 1;
  104. os_mutex_lock(&allocator_tlsf->lock);
  105. ret = tlsf_malloc(allocator_tlsf->tlsf, size);
  106. os_mutex_unlock(&allocator_tlsf->lock);
  107. return ret;
  108. }
  109. void *
  110. mem_allocator_realloc(mem_allocator_t allocator, void *ptr, uint32_t size)
  111. {
  112. void *ret;
  113. mem_allocator_tlsf *allocator_tlsf = (mem_allocator_tlsf *)allocator;
  114. if (size == 0)
  115. /* tlsf doesn't allow to allocate 0 byte */
  116. size = 1;
  117. os_mutex_lock(&allocator_tlsf->lock);
  118. ret = tlsf_realloc(allocator_tlsf->tlsf, ptr, size);
  119. os_mutex_unlock(&allocator_tlsf->lock);
  120. return ret;
  121. }
  122. void
  123. mem_allocator_free(mem_allocator_t allocator, void *ptr)
  124. {
  125. if (ptr) {
  126. mem_allocator_tlsf *allocator_tlsf = (mem_allocator_tlsf *)allocator;
  127. os_mutex_lock(&allocator_tlsf->lock);
  128. tlsf_free(allocator_tlsf->tlsf, ptr);
  129. os_mutex_unlock(&allocator_tlsf->lock);
  130. }
  131. }
  132. int
  133. mem_allocator_migrate(mem_allocator_t allocator,
  134. mem_allocator_t allocator_old)
  135. {
  136. return tlsf_migrate((mem_allocator_tlsf *) allocator,
  137. (mem_allocator_tlsf *) allocator_old);
  138. }
  139. int
  140. mem_allocator_init_lock(mem_allocator_t allocator)
  141. {
  142. mem_allocator_tlsf *allocator_tlsf = (mem_allocator_tlsf *)allocator;
  143. return os_mutex_init(&allocator_tlsf->lock);
  144. }
  145. void
  146. mem_allocator_destroy_lock(mem_allocator_t allocator)
  147. {
  148. mem_allocator_tlsf *allocator_tlsf = (mem_allocator_tlsf *)allocator;
  149. os_mutex_destroy(&allocator_tlsf->lock);
  150. }
  151. #endif /* end of DEFAULT_MEM_ALLOCATOR */