/CEDAR_0.2/src/common/ob_malloc.h

https://github.com/daseECNU/Cedar · C Header · 233 lines · 173 code · 27 blank · 33 comment · 10 complexity · 00ec3920307b325359bb502b33625106 MD5 · raw file

  1. /*
  2. * (C) 2007-2010 Taobao Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * ob_malloc.h is for what ...
  9. *
  10. * Version: $id: ob_malloc.h,v 0.1 8/19/2010 9:57a wushi Exp $
  11. *
  12. * Authors:
  13. * wushi <wushi.ly@taobao.com>
  14. * 为了便于调试内存错误的bug,如果定义了名为__OB_MALLOC_DIRECT__的环境变量,
  15. * 内存池将直接调用new和delete分配和释放内存
  16. *
  17. */
  18. #ifndef OCEANBASE_COMMON_OB_MALLOC_H_
  19. #define OCEANBASE_COMMON_OB_MALLOC_H_
  20. #include <stdint.h>
  21. #include "ob_define.h"
  22. #include "ob_mod_define.h"
  23. #include "ob_allocator.h"
  24. namespace oceanbase
  25. {
  26. namespace common
  27. {
  28. class ObTSIBlockAllocator;
  29. /// @fn int oceanbase/common::ob_init_memory_pool(int64_t block_size)
  30. /// 初始化全局的内存池,在调用ob_malloc分配内存前必须调用该函数进行初始化
  31. ///
  32. /// @param block_size 每次从系统分配的内存块的大小
  33. int ob_init_memory_pool(int64_t block_size = OB_MALLOC_BLOCK_SIZE);
  34. void ob_mod_usage_update(const int64_t delta, const int32_t mod_id);
  35. /// @fn 从全局内存池中分配nbyte的缓冲区
  36. void *ob_malloc(void *ptr, size_t size); //alloc mem for libonev onev_pool
  37. void *ob_malloc(const int64_t nbyte, const int32_t mod_id, int64_t *got_size = NULL);
  38. void *ob_malloc_emergency(const int64_t nbyte, const int32_t mod_id = 0, int64_t *got_size = NULL);
  39. /// @fn 释放通过ob_malloc获取的内存
  40. void ob_free(void *ptr, const int32_t mod_id = 0);
  41. void ob_safe_free(void *&ptr, const int32_t mod_id = 0);
  42. ObTSIBlockAllocator& get_global_tsi_block_allocator();
  43. void *ob_tc_malloc(const int64_t nbyte, const int32_t mod_id = 0);
  44. void ob_tc_free(void *ptr, const int32_t mod_id = 0);
  45. void *ob_tc_realloc(void *ptr, const int64_t nbyte, const int32_t mod_id = 0);
  46. class ObMalloc : public ObIAllocator
  47. {
  48. public:
  49. ObMalloc() : mod_id_(0) {};
  50. explicit ObMalloc(int32_t mod_id) : mod_id_(mod_id) {};
  51. ~ObMalloc() {};
  52. public:
  53. void set_mod_id(int32_t mod_id) {mod_id_ = mod_id;};
  54. void *alloc(const int64_t sz) {return ob_malloc(sz, mod_id_);};
  55. void free(void *ptr) {ob_free(ptr);};
  56. void* mod_alloc(const int64_t size, const int32_t mod_id){ return ob_malloc(size, mod_id); }
  57. void mod_free(void* p, const int32_t mod_id) { return ob_free(p, mod_id); }
  58. private:
  59. int32_t mod_id_;
  60. };
  61. class ObTCMalloc : public ObIAllocator
  62. {
  63. public:
  64. ObTCMalloc() : mod_id_(0) {};
  65. ~ObTCMalloc() {};
  66. public:
  67. void set_mod_id(int32_t mod_id) {mod_id_ = mod_id;};
  68. void *alloc(const int64_t sz) {return ob_tc_malloc(sz, mod_id_);};
  69. void free(void *ptr) {ob_tc_free(ptr, mod_id_);};
  70. private:
  71. int32_t mod_id_;
  72. };
  73. /// @fn print memory usage of each module
  74. void ob_print_mod_memory_usage(bool print_to_std = false);
  75. int64_t ob_get_mod_memory_usage(int32_t mod_id);
  76. int64_t ob_get_memory_size_direct_allocated();
  77. int64_t ob_set_memory_size_limit(const int64_t mem_size_limit);
  78. int64_t ob_get_memory_size_limit();
  79. int64_t ob_get_memory_size_handled();
  80. int64_t ob_get_memory_size_used();
  81. /// @class handle buffer allocated form ObBaseMemPool
  82. /// @author wushi(wushi.ly@taobao.com) (8/16/2010)
  83. class ObMemBuffer
  84. {
  85. private:
  86. DISALLOW_COPY_AND_ASSIGN(ObMemBuffer);
  87. public:
  88. /// @fn default constructor
  89. ObMemBuffer();
  90. /// @fn allocate memory from given mempool
  91. explicit ObMemBuffer(const int64_t nbyte);
  92. /// @fn free memory allocated from given mempool
  93. virtual ~ObMemBuffer();
  94. /// @fn 重新分配内存
  95. void *malloc(const int64_t nbyte, const int32_t mod_id = 0);
  96. /// @fn return memory allocated
  97. void *get_buffer();
  98. /// @fn get buffer size
  99. int64_t get_buffer_size();
  100. private:
  101. /// @property memory ptr pointing to memory allocated from memory
  102. void *buf_ptr_;
  103. int64_t buf_size_;
  104. int32_t mod_id_;
  105. };
  106. class ObMemBuf
  107. {
  108. public:
  109. ObMemBuf() : buf_ptr_(NULL), buf_size_(DEFAULT_MEMBUF_SIZE)
  110. {
  111. }
  112. explicit ObMemBuf(const int64_t default_size)
  113. : buf_ptr_(NULL), buf_size_(default_size)
  114. {
  115. }
  116. ~ObMemBuf()
  117. {
  118. if (NULL != buf_ptr_)
  119. {
  120. ob_free(buf_ptr_, mod_id_);
  121. buf_ptr_ = NULL;
  122. }
  123. }
  124. inline char* get_buffer()
  125. {
  126. return buf_ptr_;
  127. }
  128. int64_t get_buffer_size() const
  129. {
  130. return buf_size_;
  131. }
  132. int ensure_space(const int64_t size, const int32_t mod_id = 0);
  133. private:
  134. static const int64_t DEFAULT_MEMBUF_SIZE = 1024 * 1024; //1M
  135. private:
  136. char* buf_ptr_;
  137. int64_t buf_size_;
  138. int32_t mod_id_;
  139. };
  140. class ObMemBufAllocatorWrapper
  141. {
  142. public:
  143. ObMemBufAllocatorWrapper(ObMemBuf& mem_buf, const int32_t mod_id=0)
  144. : mem_buf_(mem_buf), mod_id_(mod_id) {}
  145. public:
  146. inline char* alloc(int64_t sz)
  147. {
  148. char* ptr = NULL;
  149. if (OB_SUCCESS == mem_buf_.ensure_space(sz, mod_id_))
  150. {
  151. ptr = mem_buf_.get_buffer();
  152. }
  153. return ptr;
  154. }
  155. inline void free(char* ptr)
  156. {
  157. UNUSED(ptr);
  158. }
  159. private:
  160. ObMemBuf& mem_buf_;
  161. int32_t mod_id_;
  162. };
  163. class ObRawBufAllocatorWrapper
  164. {
  165. public:
  166. ObRawBufAllocatorWrapper(char *mem_buf, int64_t mem_buf_len) : mem_buf_(mem_buf),mem_buf_len_(mem_buf_len) {}
  167. public:
  168. inline char* alloc(int64_t sz)
  169. {
  170. char* ptr = NULL;
  171. if (mem_buf_len_ >= sz)
  172. {
  173. ptr = mem_buf_;
  174. }
  175. return ptr;
  176. }
  177. inline void free(char* ptr)
  178. {
  179. UNUSED(ptr);
  180. }
  181. private:
  182. char * mem_buf_;
  183. int64_t mem_buf_len_;
  184. };
  185. }
  186. }
  187. #define OB_NEW(T, mod_id, ...) \
  188. ({ \
  189. T* ret = NULL; \
  190. void *buf = ob_malloc(sizeof(T), mod_id); \
  191. if (NULL != buf) \
  192. { \
  193. ret = new(buf) T(__VA_ARGS__); \
  194. } \
  195. ret; \
  196. })
  197. #define OB_DELETE(T, mod_id, ptr) \
  198. do{ \
  199. if (NULL != ptr) \
  200. { \
  201. ptr->~T(); \
  202. ob_free(ptr, mod_id); \
  203. ptr = NULL; \
  204. } \
  205. } while(0)
  206. #endif /* OCEANBASE_SRC_COMMON_OB_MALLOC_H_ */