mem_alloc.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #else /* else of DEFAULT_MEM_ALLOCATOR */
  32. #include "tlsf/tlsf.h"
  33. typedef struct mem_allocator_tlsf {
  34. tlsf_t tlsf;
  35. korp_mutex lock;
  36. }mem_allocator_tlsf;
  37. mem_allocator_t
  38. mem_allocator_create(void *mem, uint32_t size)
  39. {
  40. mem_allocator_tlsf *allocator_tlsf;
  41. tlsf_t tlsf;
  42. char *mem_aligned = (char*)(((uintptr_t)mem + 3) & ~3);
  43. if (size < 1024) {
  44. printf("Create mem allocator failed: pool size must be "
  45. "at least 1024 bytes.\n");
  46. return NULL;
  47. }
  48. size -= mem_aligned - (char*)mem;
  49. mem = (void*)mem_aligned;
  50. tlsf = tlsf_create_with_pool(mem, size);
  51. if (!tlsf) {
  52. printf("Create mem allocator failed: tlsf_create_with_pool failed.\n");
  53. return NULL;
  54. }
  55. allocator_tlsf = tlsf_malloc(tlsf, sizeof(mem_allocator_tlsf));
  56. if (!allocator_tlsf) {
  57. printf("Create mem allocator failed: tlsf_malloc failed.\n");
  58. tlsf_destroy(tlsf);
  59. return NULL;
  60. }
  61. allocator_tlsf->tlsf = tlsf;
  62. if (os_mutex_init(&allocator_tlsf->lock)) {
  63. printf("Create mem allocator failed: tlsf_malloc failed.\n");
  64. tlsf_free(tlsf, allocator_tlsf);
  65. tlsf_destroy(tlsf);
  66. return NULL;
  67. }
  68. return allocator_tlsf;
  69. }
  70. void
  71. mem_allocator_destroy(mem_allocator_t allocator)
  72. {
  73. mem_allocator_tlsf *allocator_tlsf = (mem_allocator_tlsf *)allocator;
  74. tlsf_t tlsf = allocator_tlsf->tlsf;
  75. os_mutex_destroy(&allocator_tlsf->lock);
  76. tlsf_free(tlsf, allocator_tlsf);
  77. tlsf_destroy(tlsf);
  78. }
  79. void *
  80. mem_allocator_malloc(mem_allocator_t allocator, uint32_t size)
  81. {
  82. void *ret;
  83. mem_allocator_tlsf *allocator_tlsf = (mem_allocator_tlsf *)allocator;
  84. if (size == 0)
  85. /* tlsf doesn't allow to allocate 0 byte */
  86. size = 1;
  87. os_mutex_lock(&allocator_tlsf->lock);
  88. ret = tlsf_malloc(allocator_tlsf->tlsf, size);
  89. os_mutex_unlock(&allocator_tlsf->lock);
  90. return ret;
  91. }
  92. void *
  93. mem_allocator_realloc(mem_allocator_t allocator, void *ptr, uint32_t size)
  94. {
  95. void *ret;
  96. mem_allocator_tlsf *allocator_tlsf = (mem_allocator_tlsf *)allocator;
  97. if (size == 0)
  98. /* tlsf doesn't allow to allocate 0 byte */
  99. size = 1;
  100. os_mutex_lock(&allocator_tlsf->lock);
  101. ret = tlsf_realloc(allocator_tlsf->tlsf, ptr, size);
  102. os_mutex_unlock(&allocator_tlsf->lock);
  103. return ret;
  104. }
  105. void
  106. mem_allocator_free(mem_allocator_t allocator, void *ptr)
  107. {
  108. if (ptr) {
  109. mem_allocator_tlsf *allocator_tlsf = (mem_allocator_tlsf *)allocator;
  110. os_mutex_lock(&allocator_tlsf->lock);
  111. tlsf_free(allocator_tlsf->tlsf, ptr);
  112. os_mutex_unlock(&allocator_tlsf->lock);
  113. }
  114. }
  115. #endif /* end of DEFAULT_MEM_ALLOCATOR */