bh_memory.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 "bh_memory.h"
  17. #include "mem_alloc.h"
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #ifndef MALLOC_MEMORY_FROM_SYSTEM
  21. typedef enum Memory_Mode {
  22. MEMORY_MODE_UNKNOWN = 0, MEMORY_MODE_POOL, MEMORY_MODE_ALLOCATOR
  23. } Memory_Mode;
  24. static Memory_Mode memory_mode = MEMORY_MODE_UNKNOWN;
  25. static mem_allocator_t pool_allocator = NULL;
  26. static void *(*malloc_func)(unsigned int size) = NULL;
  27. static void (*free_func)(void *ptr) = NULL;
  28. int bh_memory_init_with_pool(void *mem, unsigned int bytes)
  29. {
  30. mem_allocator_t _allocator = mem_allocator_create(mem, bytes);
  31. if (_allocator) {
  32. memory_mode = MEMORY_MODE_POOL;
  33. pool_allocator = _allocator;
  34. return 0;
  35. }
  36. printf("Init memory with pool (%p, %u) failed.\n", mem, bytes);
  37. return -1;
  38. }
  39. int bh_memory_init_with_allocator(void *_malloc_func, void *_free_func)
  40. {
  41. if (_malloc_func && _free_func && _malloc_func != _free_func) {
  42. memory_mode = MEMORY_MODE_ALLOCATOR;
  43. malloc_func = _malloc_func;
  44. free_func = _free_func;
  45. return 0;
  46. }
  47. printf("Init memory with allocator (%p, %p) failed.\n", _malloc_func,
  48. _free_func);
  49. return -1;
  50. }
  51. void bh_memory_destroy()
  52. {
  53. if (memory_mode == MEMORY_MODE_POOL)
  54. mem_allocator_destroy(pool_allocator);
  55. memory_mode = MEMORY_MODE_UNKNOWN;
  56. }
  57. void* bh_malloc(unsigned int size)
  58. {
  59. if (memory_mode == MEMORY_MODE_UNKNOWN) {
  60. printf("bh_malloc failed: memory hasn't been initialize.\n");
  61. return NULL;
  62. } else if (memory_mode == MEMORY_MODE_POOL) {
  63. return mem_allocator_malloc(pool_allocator, size);
  64. } else {
  65. return malloc_func(size);
  66. }
  67. }
  68. void bh_free(void *ptr)
  69. {
  70. if (memory_mode == MEMORY_MODE_UNKNOWN) {
  71. printf("bh_free failed: memory hasn't been initialize.\n");
  72. } else if (memory_mode == MEMORY_MODE_POOL) {
  73. mem_allocator_free(pool_allocator, ptr);
  74. } else {
  75. free_func(ptr);
  76. }
  77. }
  78. #else /* else of MALLOC_MEMORY_FROM_SYSTEM */
  79. void* bh_malloc(unsigned int size)
  80. {
  81. return malloc(size);
  82. }
  83. void bh_free(void *ptr)
  84. {
  85. if (ptr)
  86. free(ptr);
  87. }
  88. #endif /* end of MALLOC_MEMORY_FROM_SYSTEM*/