mem_alloc.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #include "config.h"
  7. #if DEFAULT_MEM_ALLOCATOR == MEM_ALLOCATOR_EMS
  8. #include "ems/ems_gc.h"
  9. mem_allocator_t mem_allocator_create(void *mem, uint32_t size)
  10. {
  11. return gc_init_with_pool((char *) mem, size);
  12. }
  13. void mem_allocator_destroy(mem_allocator_t allocator)
  14. {
  15. gc_destroy_with_pool((gc_handle_t) allocator);
  16. }
  17. void *
  18. mem_allocator_malloc(mem_allocator_t allocator, uint32_t size)
  19. {
  20. return gc_alloc_vo_h((gc_handle_t) allocator, size);
  21. }
  22. void *
  23. mem_allocator_realloc(mem_allocator_t allocator, void *ptr, uint32_t size)
  24. {
  25. return gc_realloc_vo_h((gc_handle_t) allocator, ptr, size);
  26. }
  27. void mem_allocator_free(mem_allocator_t allocator, void *ptr)
  28. {
  29. if (ptr)
  30. gc_free_h((gc_handle_t) allocator, ptr);
  31. }
  32. #else /* else of DEFAULT_MEM_ALLOCATOR */
  33. #include "tlsf/tlsf.h"
  34. #include "bh_thread.h"
  35. typedef struct mem_allocator_tlsf {
  36. tlsf_t tlsf;
  37. korp_mutex lock;
  38. }mem_allocator_tlsf;
  39. mem_allocator_t
  40. mem_allocator_create(void *mem, uint32_t size)
  41. {
  42. mem_allocator_tlsf *allocator_tlsf;
  43. tlsf_t tlsf;
  44. char *mem_aligned = (char*)(((uintptr_t)mem + 3) & ~3);
  45. if (size < 1024) {
  46. printf("Create mem allocator failed: pool size must be "
  47. "at least 1024 bytes.\n");
  48. return NULL;
  49. }
  50. size -= mem_aligned - (char*)mem;
  51. mem = (void*)mem_aligned;
  52. tlsf = tlsf_create_with_pool(mem, size);
  53. if (!tlsf) {
  54. printf("Create mem allocator failed: tlsf_create_with_pool failed.\n");
  55. return NULL;
  56. }
  57. allocator_tlsf = tlsf_malloc(tlsf, sizeof(mem_allocator_tlsf));
  58. if (!allocator_tlsf) {
  59. printf("Create mem allocator failed: tlsf_malloc failed.\n");
  60. tlsf_destroy(tlsf);
  61. return NULL;
  62. }
  63. allocator_tlsf->tlsf = tlsf;
  64. if (vm_mutex_init(&allocator_tlsf->lock)) {
  65. printf("Create mem allocator failed: tlsf_malloc failed.\n");
  66. tlsf_free(tlsf, allocator_tlsf);
  67. tlsf_destroy(tlsf);
  68. return NULL;
  69. }
  70. return allocator_tlsf;
  71. }
  72. void
  73. mem_allocator_destroy(mem_allocator_t allocator)
  74. {
  75. mem_allocator_tlsf *allocator_tlsf = (mem_allocator_tlsf *)allocator;
  76. tlsf_t tlsf = allocator_tlsf->tlsf;
  77. vm_mutex_destroy(&allocator_tlsf->lock);
  78. tlsf_free(tlsf, allocator_tlsf);
  79. tlsf_destroy(tlsf);
  80. }
  81. void *
  82. mem_allocator_malloc(mem_allocator_t allocator, uint32_t size)
  83. {
  84. void *ret;
  85. mem_allocator_tlsf *allocator_tlsf = (mem_allocator_tlsf *)allocator;
  86. if (size == 0)
  87. /* tlsf doesn't allow to allocate 0 byte */
  88. size = 1;
  89. vm_mutex_lock(&allocator_tlsf->lock);
  90. ret = tlsf_malloc(allocator_tlsf->tlsf, size);
  91. vm_mutex_unlock(&allocator_tlsf->lock);
  92. return ret;
  93. }
  94. void *
  95. mem_allocator_realloc(mem_allocator_t allocator, void *ptr, uint32_t size)
  96. {
  97. void *ret;
  98. mem_allocator_tlsf *allocator_tlsf = (mem_allocator_tlsf *)allocator;
  99. if (size == 0)
  100. /* tlsf doesn't allow to allocate 0 byte */
  101. size = 1;
  102. vm_mutex_lock(&allocator_tlsf->lock);
  103. ret = tlsf_realloc(allocator_tlsf->tlsf, ptr, size);
  104. vm_mutex_unlock(&allocator_tlsf->lock);
  105. return ret;
  106. }
  107. void
  108. mem_allocator_free(mem_allocator_t allocator, void *ptr)
  109. {
  110. if (ptr) {
  111. mem_allocator_tlsf *allocator_tlsf = (mem_allocator_tlsf *)allocator;
  112. vm_mutex_lock(&allocator_tlsf->lock);
  113. tlsf_free(allocator_tlsf->tlsf, ptr);
  114. vm_mutex_unlock(&allocator_tlsf->lock);
  115. }
  116. }
  117. #endif /* end of DEFAULT_MEM_ALLOCATOR */