bh_memory.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #ifndef _BH_MEMORY_H
  17. #define _BH_MEMORY_H
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. #define BH_KB (1024)
  22. #define BH_MB ((BH_KB)*1024)
  23. #define BH_GB ((BH_MB)*1024)
  24. /**
  25. * Initialize memory allocator with a pool, the bh_malloc/bh_free function
  26. * will malloc/free memory from the pool
  27. *
  28. * @param mem the pool buffer
  29. * @param bytes the size bytes of the buffer
  30. *
  31. * @return 0 if success, -1 otherwise
  32. */
  33. int bh_memory_init_with_pool(void *mem, unsigned int bytes);
  34. /**
  35. * Initialize memory allocator with memory allocator, the bh_malloc/bh_free
  36. * function will malloc/free memory with the allocator passed
  37. *
  38. * @param malloc_func the malloc function
  39. * @param free_func the free function
  40. *
  41. * @return 0 if success, -1 otherwise
  42. */
  43. int bh_memory_init_with_allocator(void *malloc_func, void *free_func);
  44. /**
  45. * Destroy memory
  46. */
  47. void bh_memory_destroy();
  48. /**
  49. * This function allocates a memory chunk from system
  50. *
  51. * @param size bytes need allocate
  52. *
  53. * @return the pointer to memory allocated
  54. */
  55. void* bh_malloc(unsigned int size);
  56. /**
  57. * This function frees memory chunk
  58. *
  59. * @param ptr the pointer to memory need free
  60. */
  61. void bh_free(void *ptr);
  62. #ifdef __cplusplus
  63. }
  64. #endif
  65. #endif /* #ifndef _BH_MEMORY_H */