bh_memory.h 2.8 KB

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