mem_alloc.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 mem_allocator_free(mem_allocator_t allocator, void *ptr)
  23. {
  24. if (ptr)
  25. gc_free_h((gc_handle_t) allocator, ptr);
  26. }
  27. #else /* else of DEFAULT_MEM_ALLOCATOR */
  28. #include "tlsf/tlsf.h"
  29. #include "bh_thread.h"
  30. typedef struct mem_allocator_tlsf {
  31. tlsf_t tlsf;
  32. korp_mutex lock;
  33. }mem_allocator_tlsf;
  34. mem_allocator_t
  35. mem_allocator_create(void *mem, uint32_t size)
  36. {
  37. mem_allocator_tlsf *allocator_tlsf;
  38. tlsf_t tlsf;
  39. char *mem_aligned = (char*)(((uintptr_t)mem + 3) & ~3);
  40. if (size < 1024) {
  41. printf("Create mem allocator failed: pool size must be "
  42. "at least 1024 bytes.\n");
  43. return NULL;
  44. }
  45. size -= mem_aligned - (char*)mem;
  46. mem = (void*)mem_aligned;
  47. tlsf = tlsf_create_with_pool(mem, size);
  48. if (!tlsf) {
  49. printf("Create mem allocator failed: tlsf_create_with_pool failed.\n");
  50. return NULL;
  51. }
  52. allocator_tlsf = tlsf_malloc(tlsf, sizeof(mem_allocator_tlsf));
  53. if (!allocator_tlsf) {
  54. printf("Create mem allocator failed: tlsf_malloc failed.\n");
  55. tlsf_destroy(tlsf);
  56. return NULL;
  57. }
  58. allocator_tlsf->tlsf = tlsf;
  59. if (vm_mutex_init(&allocator_tlsf->lock)) {
  60. printf("Create mem allocator failed: tlsf_malloc failed.\n");
  61. tlsf_free(tlsf, allocator_tlsf);
  62. tlsf_destroy(tlsf);
  63. return NULL;
  64. }
  65. return allocator_tlsf;
  66. }
  67. void
  68. mem_allocator_destroy(mem_allocator_t allocator)
  69. {
  70. mem_allocator_tlsf *allocator_tlsf = (mem_allocator_tlsf *)allocator;
  71. tlsf_t tlsf = allocator_tlsf->tlsf;
  72. vm_mutex_destroy(&allocator_tlsf->lock);
  73. tlsf_free(tlsf, allocator_tlsf);
  74. tlsf_destroy(tlsf);
  75. }
  76. void *
  77. mem_allocator_malloc(mem_allocator_t allocator, uint32_t size)
  78. {
  79. void *ret;
  80. mem_allocator_tlsf *allocator_tlsf = (mem_allocator_tlsf *)allocator;
  81. if (size == 0)
  82. /* tlsf doesn't allow to allocate 0 byte */
  83. size = 1;
  84. vm_mutex_lock(&allocator_tlsf->lock);
  85. ret = tlsf_malloc(allocator_tlsf->tlsf, size);
  86. vm_mutex_unlock(&allocator_tlsf->lock);
  87. return ret;
  88. }
  89. void
  90. mem_allocator_free(mem_allocator_t allocator, void *ptr)
  91. {
  92. if (ptr) {
  93. mem_allocator_tlsf *allocator_tlsf = (mem_allocator_tlsf *)allocator;
  94. vm_mutex_lock(&allocator_tlsf->lock);
  95. tlsf_free(allocator_tlsf->tlsf, ptr);
  96. vm_mutex_unlock(&allocator_tlsf->lock);
  97. }
  98. }
  99. #endif /* end of DEFAULT_MEM_ALLOCATOR */