mem0mem.h
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:12k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /******************************************************
  2. The memory management
  3. (c) 1994, 1995 Innobase Oy
  4. Created 6/9/1994 Heikki Tuuri
  5. *******************************************************/
  6. #ifndef mem0mem_h
  7. #define mem0mem_h
  8. #include "univ.i"
  9. #include "ut0mem.h"
  10. #include "ut0byte.h"
  11. #include "ut0ut.h"
  12. #include "ut0rnd.h"
  13. #include "sync0sync.h"
  14. #include "ut0lst.h"
  15. #include "mach0data.h"
  16. /* -------------------- MEMORY HEAPS ----------------------------- */
  17. /* The info structure stored at the beginning of a heap block */
  18. typedef struct mem_block_info_struct mem_block_info_t;
  19. /* A block of a memory heap consists of the info structure
  20. followed by an area of memory */
  21. typedef mem_block_info_t mem_block_t;
  22. /* A memory heap is a nonempty linear list of memory blocks */
  23. typedef mem_block_t mem_heap_t;
  24. /* Types of allocation for memory heaps: DYNAMIC means allocation from the
  25. dynamic memory pool of the C compiler, BUFFER means allocation from the index
  26. page buffer pool; the latter method is used for very big heaps */
  27. #define MEM_HEAP_DYNAMIC 0 /* the most common type */
  28. #define MEM_HEAP_BUFFER 1
  29. #define MEM_HEAP_BTR_SEARCH 2 /* this flag can be ORed to the
  30. previous */
  31. /* The following start size is used for the first block in the memory heap if
  32. the size is not specified, i.e., 0 is given as the parameter in the call of
  33. create. The standard size is the maximum size of the blocks used for
  34. allocations of small buffers. */
  35. #define MEM_BLOCK_START_SIZE            64
  36. #define MEM_BLOCK_STANDARD_SIZE         8192
  37. /* If a memory heap is allowed to grow into the buffer pool, the following
  38. is the maximum size for a single allocated buffer: */
  39. #define MEM_MAX_ALLOC_IN_BUF (UNIV_PAGE_SIZE - 200)
  40. /**********************************************************************
  41. Initializes the memory system. */
  42. void
  43. mem_init(
  44. /*=====*/
  45. ulint size); /* in: common pool size in bytes */
  46. /******************************************************************
  47. Use this macro instead of the corresponding function! Macro for memory
  48. heap creation. */
  49. #ifdef UNIV_MEM_DEBUG
  50. #define mem_heap_create(N)    mem_heap_create_func(
  51. (N), NULL, MEM_HEAP_DYNAMIC,
  52. IB__FILE__, __LINE__)
  53. #else
  54. #define mem_heap_create(N)    mem_heap_create_func(N, NULL, MEM_HEAP_DYNAMIC)
  55. #endif
  56. /******************************************************************
  57. Use this macro instead of the corresponding function! Macro for memory
  58. heap creation. */
  59. #ifdef UNIV_MEM_DEBUG
  60. #define mem_heap_create_in_buffer(N) mem_heap_create_func(
  61. (N), NULL, MEM_HEAP_BUFFER,
  62. IB__FILE__, __LINE__)
  63. #else
  64. #define mem_heap_create_in_buffer(N) mem_heap_create_func(N, NULL,
  65. MEM_HEAP_BUFFER)
  66. #endif
  67. /******************************************************************
  68. Use this macro instead of the corresponding function! Macro for memory
  69. heap creation. */
  70. #ifdef UNIV_MEM_DEBUG
  71. #define mem_heap_create_in_btr_search(N) mem_heap_create_func(
  72. (N), NULL, MEM_HEAP_BTR_SEARCH |
  73. MEM_HEAP_BUFFER,
  74. IB__FILE__, __LINE__)
  75. #else
  76. #define mem_heap_create_in_btr_search(N) mem_heap_create_func(N, NULL,
  77. MEM_HEAP_BTR_SEARCH | MEM_HEAP_BUFFER)
  78. #endif
  79. /******************************************************************
  80. Use this macro instead of the corresponding function! Macro for fast
  81. memory heap creation. An initial block of memory B is given by the
  82. caller, N is its size, and this memory block is not freed by
  83. mem_heap_free. See the parameter comment in mem_heap_create_func below. */
  84. #ifdef UNIV_MEM_DEBUG
  85. #define mem_heap_fast_create(N, B) mem_heap_create_func(
  86. (N), (B), MEM_HEAP_DYNAMIC,
  87. IB__FILE__, __LINE__)
  88. #else
  89. #define mem_heap_fast_create(N, B)     mem_heap_create_func(N, (B),
  90. MEM_HEAP_DYNAMIC)
  91. #endif
  92. /******************************************************************
  93. Use this macro instead of the corresponding function! Macro for memory
  94. heap freeing. */
  95. #ifdef  UNIV_MEM_DEBUG
  96. #define mem_heap_free(heap) mem_heap_free_func(
  97.   (heap), IB__FILE__, __LINE__)
  98. #else
  99. #define mem_heap_free(heap) mem_heap_free_func(heap)
  100. #endif
  101. /*********************************************************************
  102. NOTE: Use the corresponding macros instead of this function. Creates a
  103. memory heap which allocates memory from dynamic space. For debugging
  104. purposes, takes also the file name and line as argument in the debug
  105. version. */
  106. UNIV_INLINE
  107. mem_heap_t*
  108. mem_heap_create_func(
  109. /*=================*/
  110. /* out, own: memory heap */
  111. ulint n, /* in: desired start block size,
  112. this means that a single user buffer
  113. of size n will fit in the block, 
  114. 0 creates a default size block;
  115. if init_block is not NULL, n tells
  116. its size in bytes */
  117. void* init_block, /* in: if very fast creation is
  118. wanted, the caller can reserve some
  119. memory from its stack, for example,
  120. and pass it as the the initial block
  121. to the heap: then no OS call of malloc
  122. is needed at the creation. CAUTION:
  123. the caller must make sure the initial
  124. block is not unintentionally erased
  125. (if allocated in the stack), before
  126. the memory heap is explicitly freed. */
  127. ulint type /* in: MEM_HEAP_DYNAMIC or MEM_HEAP_BUFFER */ 
  128. #ifdef UNIV_MEM_DEBUG
  129. ,char*  file_name, /* in: file name where created */
  130. ulint line /* in: line where created */
  131. #endif
  132. );
  133. /*********************************************************************
  134. NOTE: Use the corresponding macro instead of this function.
  135. Frees the space occupied by a memory heap. */
  136. UNIV_INLINE
  137. void
  138. mem_heap_free_func(
  139. /*===============*/
  140. mem_heap_t*   heap   /* in, own: heap to be freed */
  141. #ifdef UNIV_MEM_DEBUG
  142. ,char*  file_name,      /* in: file name where freed */
  143. ulint   line            /* in: line where freed */
  144. #endif
  145. );
  146. /*******************************************************************
  147. Allocates n bytes of memory from a memory heap. */
  148. UNIV_INLINE
  149. void*
  150. mem_heap_alloc(
  151. /*===========*/
  152. /* out: allocated storage, NULL if
  153. did not succeed */
  154. mem_heap_t*    heap,  /* in: memory heap */
  155. ulint           n); /* in: number of bytes; if the heap is allowed
  156. to grow into the buffer pool, this must be
  157. <= MEM_MAX_ALLOC_IN_BUF */
  158. /*********************************************************************
  159. Returns a pointer to the heap top. */
  160. UNIV_INLINE
  161. byte*
  162. mem_heap_get_heap_top(
  163. /*==================*/     
  164. /* out: pointer to the heap top */
  165. mem_heap_t*    heap);  /* in: memory heap */
  166. /*********************************************************************
  167. Frees the space in a memory heap exceeding the pointer given. The
  168. pointer must have been acquired from mem_heap_get_heap_top. The first
  169. memory block of the heap is not freed. */
  170. UNIV_INLINE
  171. void
  172. mem_heap_free_heap_top(
  173. /*===================*/
  174. mem_heap_t*    heap, /* in: heap from which to free */
  175. byte* old_top);/* in: pointer to old top of heap */
  176. /*********************************************************************
  177. Empties a memory heap. The first memory block of the heap is not freed. */
  178. UNIV_INLINE
  179. void
  180. mem_heap_empty(
  181. /*===========*/
  182. mem_heap_t*    heap); /* in: heap to empty */
  183. /*********************************************************************
  184. Returns a pointer to the topmost element in a memory heap.
  185. The size of the element must be given. */
  186. UNIV_INLINE
  187. void*
  188. mem_heap_get_top(
  189. /*=============*/     
  190. /* out: pointer to the topmost element */
  191. mem_heap_t*    heap,  /* in: memory heap */
  192. ulint           n);     /* in: size of the topmost element */
  193. /*********************************************************************
  194. Frees the topmost element in a memory heap.
  195. The size of the element must be given. */
  196. UNIV_INLINE
  197. void
  198. mem_heap_free_top(
  199. /*==============*/     
  200. mem_heap_t*    heap,  /* in: memory heap */
  201. ulint           n);     /* in: size of the topmost element */
  202. /*********************************************************************
  203. Returns the space in bytes occupied by a memory heap. */
  204. UNIV_INLINE
  205. ulint
  206. mem_heap_get_size(
  207. /*==============*/
  208. mem_heap_t*   heap);   /* in: heap */
  209. /******************************************************************
  210. Use this macro instead of the corresponding function!
  211. Macro for memory buffer allocation */
  212. #ifdef UNIV_MEM_DEBUG
  213. #define mem_alloc(N)    mem_alloc_func(
  214.   (N), IB__FILE__, __LINE__)
  215. #else
  216. #define mem_alloc(N)    mem_alloc_func(N)
  217. #endif
  218. /******************************************************************
  219. Use this macro instead of the corresponding function!
  220. Macro for memory buffer allocation */
  221. #ifdef UNIV_MEM_DEBUG
  222. #define mem_alloc_noninline(N)    mem_alloc_func_noninline(
  223.   (N), IB__FILE__, __LINE__)
  224. #else
  225. #define mem_alloc_noninline(N)    mem_alloc_func_noninline(N)
  226. #endif
  227. /*******************************************************************
  228. NOTE: Use the corresponding macro instead of this function.
  229. Allocates a single buffer of memory from the dynamic memory of
  230. the C compiler. Is like malloc of C. The buffer must be freed 
  231. with mem_free. */
  232. UNIV_INLINE
  233. void*
  234. mem_alloc_func(
  235. /*===========*/
  236. /* out, own: free storage, NULL
  237. if did not succeed */
  238. ulint    n              /* in: desired number of bytes */
  239. #ifdef UNIV_MEM_DEBUG
  240. ,char*  file_name,      /* in: file name where created */
  241. ulint    line            /* in: line where created */
  242. #endif
  243. );
  244. /*******************************************************************
  245. NOTE: Use the corresponding macro instead of this function.
  246. Allocates a single buffer of memory from the dynamic memory of
  247. the C compiler. Is like malloc of C. The buffer must be freed 
  248. with mem_free. */
  249. void*
  250. mem_alloc_func_noninline(
  251. /*=====================*/
  252. /* out, own: free storage, NULL if did not
  253. succeed */
  254. ulint   n               /* in: desired number of bytes */
  255. #ifdef UNIV_MEM_DEBUG
  256. ,char*  file_name, /* in: file name where created */
  257. ulint   line /* in: line where created */
  258. #endif
  259. );
  260. /******************************************************************
  261. Use this macro instead of the corresponding function!
  262. Macro for memory buffer freeing */
  263. #ifdef  UNIV_MEM_DEBUG
  264. #define mem_free(PTR)   mem_free_func(
  265.   (PTR), IB__FILE__, __LINE__)
  266. #else
  267. #define mem_free(PTR)   mem_free_func(PTR)
  268. #endif
  269. /*******************************************************************
  270. NOTE: Use the corresponding macro instead of this function.
  271. Frees a single buffer of storage from
  272. the dynamic memory of C compiler. Similar to free of C. */
  273. UNIV_INLINE
  274. void
  275. mem_free_func(
  276. /*==========*/
  277. void*   ptr             /* in, own: buffer to be freed */
  278. #ifdef UNIV_MEM_DEBUG
  279. ,char*  file_name,      /* in: file name where created */
  280. ulint    line            /* in: line where created */
  281. #endif
  282. );
  283. /*******************************************************************
  284. Implements realloc. */
  285. UNIV_INLINE
  286. void*
  287. mem_realloc(
  288. /*========*/
  289. /* out, own: free storage, NULL if did not succeed */
  290. void* buf, /* in: pointer to an old buffer */
  291. ulint   n); /* in: desired number of bytes */
  292. /*#######################################################################*/
  293. /* The info header of a block in a memory heap */
  294. struct mem_block_info_struct {
  295. UT_LIST_BASE_NODE_T(mem_block_t) base; /* In the first block in the
  296. the list this is the base node of the list of blocks;
  297. in subsequent blocks this is undefined */
  298. UT_LIST_NODE_T(mem_block_t) list; /* This contains pointers to next
  299. and prev in the list. The first block allocated
  300. to the heap is also the first block in this list,
  301. though it also contains the base node of the list. */
  302. ulint   len;    /* physical length of this block in bytes */
  303. ulint  type;  /* type of heap: MEM_HEAP_DYNAMIC, or
  304. MEM_HEAP_BUF possibly ORed to MEM_HEAP_BTR_SEARCH */
  305. ibool init_block; /* TRUE if this is the first block used in fast
  306. creation of a heap: the memory will be freed
  307. by the creator, not by mem_heap_free */
  308. ulint   free;   /* offset in bytes of the first free position for
  309. user data in the block */
  310. ulint   start;  /* the value of the struct field 'free' at the 
  311. creation of the block */
  312. byte*  free_block;
  313. /* if the MEM_HEAP_BTR_SEARCH bit is set in type,
  314. and this is the heap root, this can contain an
  315. allocated buffer frame, which can be appended as a
  316. free block to the heap, if we need more space;
  317. otherwise, this is NULL */
  318. ulint   magic_n;/* magic number for debugging */
  319. };
  320. /* Header size for a memory heap block */
  321. #define MEM_BLOCK_HEADER_SIZE   ut_calc_align(sizeof(mem_block_info_t),
  322. UNIV_MEM_ALIGNMENT)
  323. #include "mem0dbg.h"
  324. #ifndef UNIV_NONINL
  325. #include "mem0mem.ic"
  326. #endif
  327. #endif