bh_memory.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #if BEIHAI_ENABLE_MEMORY_PROFILING == 0
  49. /**
  50. * This function allocates a memory chunk from system
  51. *
  52. * @param size bytes need allocate
  53. *
  54. * @return the pointer to memory allocated
  55. */
  56. void* bh_malloc(unsigned int size);
  57. /**
  58. * This function frees memory chunk
  59. *
  60. * @param ptr the pointer to memory need free
  61. */
  62. void bh_free(void *ptr);
  63. #else
  64. void* bh_malloc_profile(const char *file, int line, const char *func, unsigned int size);
  65. void bh_free_profile(const char *file, int line, const char *func, void *ptr);
  66. #define bh_malloc(size) bh_malloc_profile(__FILE__, __LINE__, __func__, size)
  67. #define bh_free(ptr) bh_free_profile(__FILE__, __LINE__, __func__, ptr)
  68. /**
  69. * Print current memory profiling data
  70. *
  71. * @param file file name of the caller
  72. * @param line line of the file of the caller
  73. * @param func function name of the caller
  74. */
  75. void memory_profile_print(const char *file, int line, const char *func, int alloc);
  76. /**
  77. * Summarize memory usage and print it out
  78. * Can use awk to analyze the output like below:
  79. * awk -F: '{print $2,$4,$6,$8,$9}' OFS="\t" ./out.txt | sort -n -r -k 1
  80. */
  81. void memory_usage_summarize();
  82. #endif
  83. #ifdef __cplusplus
  84. }
  85. #endif
  86. #endif /* #ifndef _BH_MEMORY_H */