mem_alloc.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "mem_alloc.h"
  17. #include "config.h"
  18. #if DEFAULT_MEM_ALLOCATOR == MEM_ALLOCATOR_EMS
  19. #include "ems/ems_gc.h"
  20. mem_allocator_t mem_allocator_create(void *mem, uint32_t size)
  21. {
  22. return gc_init_with_pool((char *) mem, size);
  23. }
  24. void mem_allocator_destroy(mem_allocator_t allocator)
  25. {
  26. gc_destroy_with_pool((gc_handle_t) allocator);
  27. }
  28. void *
  29. mem_allocator_malloc(mem_allocator_t allocator, uint32_t size)
  30. {
  31. return gc_alloc_vo_h((gc_handle_t) allocator, size);
  32. }
  33. void mem_allocator_free(mem_allocator_t allocator, void *ptr)
  34. {
  35. if (ptr)
  36. gc_free_h((gc_handle_t) allocator, ptr);
  37. }
  38. #else /* else of DEFAULT_MEM_ALLOCATOR */
  39. #include "tlsf/tlsf.h"
  40. #include "bh_thread.h"
  41. typedef struct mem_allocator_tlsf {
  42. tlsf_t tlsf;
  43. korp_mutex lock;
  44. }mem_allocator_tlsf;
  45. mem_allocator_t
  46. mem_allocator_create(void *mem, uint32_t size)
  47. {
  48. mem_allocator_tlsf *allocator_tlsf;
  49. tlsf_t tlsf;
  50. char *mem_aligned = (char*)(((uintptr_t)mem + 3) & ~3);
  51. if (size < 1024) {
  52. printf("Create mem allocator failed: pool size must be "
  53. "at least 1024 bytes.\n");
  54. return NULL;
  55. }
  56. size -= mem_aligned - (char*)mem;
  57. mem = (void*)mem_aligned;
  58. tlsf = tlsf_create_with_pool(mem, size);
  59. if (!tlsf) {
  60. printf("Create mem allocator failed: tlsf_create_with_pool failed.\n");
  61. return NULL;
  62. }
  63. allocator_tlsf = tlsf_malloc(tlsf, sizeof(mem_allocator_tlsf));
  64. if (!allocator_tlsf) {
  65. printf("Create mem allocator failed: tlsf_malloc failed.\n");
  66. tlsf_destroy(tlsf);
  67. return NULL;
  68. }
  69. allocator_tlsf->tlsf = tlsf;
  70. if (vm_mutex_init(&allocator_tlsf->lock)) {
  71. printf("Create mem allocator failed: tlsf_malloc failed.\n");
  72. tlsf_free(tlsf, allocator_tlsf);
  73. tlsf_destroy(tlsf);
  74. return NULL;
  75. }
  76. return allocator_tlsf;
  77. }
  78. void
  79. mem_allocator_destroy(mem_allocator_t allocator)
  80. {
  81. mem_allocator_tlsf *allocator_tlsf = (mem_allocator_tlsf *)allocator;
  82. tlsf_t tlsf = allocator_tlsf->tlsf;
  83. vm_mutex_destroy(&allocator_tlsf->lock);
  84. tlsf_free(tlsf, allocator_tlsf);
  85. tlsf_destroy(tlsf);
  86. }
  87. void *
  88. mem_allocator_malloc(mem_allocator_t allocator, uint32_t size)
  89. {
  90. void *ret;
  91. mem_allocator_tlsf *allocator_tlsf = (mem_allocator_tlsf *)allocator;
  92. if (size == 0)
  93. /* tlsf doesn't allow to allocate 0 byte */
  94. size = 1;
  95. vm_mutex_lock(&allocator_tlsf->lock);
  96. ret = tlsf_malloc(allocator_tlsf->tlsf, size);
  97. vm_mutex_unlock(&allocator_tlsf->lock);
  98. return ret;
  99. }
  100. void
  101. mem_allocator_free(mem_allocator_t allocator, void *ptr)
  102. {
  103. if (ptr) {
  104. mem_allocator_tlsf *allocator_tlsf = (mem_allocator_tlsf *)allocator;
  105. vm_mutex_lock(&allocator_tlsf->lock);
  106. tlsf_free(allocator_tlsf->tlsf, ptr);
  107. vm_mutex_unlock(&allocator_tlsf->lock);
  108. }
  109. }
  110. #endif /* end of DEFAULT_MEM_ALLOCATOR */