You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

392 lines
14 KiB

  1. /*****************************************************************************
  2. Copyright (c) 1994, 2009, Innobase Oy. All Rights Reserved.
  3. This program is free software; you can redistribute it and/or modify it under
  4. the terms of the GNU General Public License as published by the Free Software
  5. Foundation; version 2 of the License.
  6. This program is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along with
  10. this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  11. Place, Suite 330, Boston, MA 02111-1307 USA
  12. *****************************************************************************/
  13. /**************************************************//**
  14. @file include/mem0mem.h
  15. The memory management
  16. Created 6/9/1994 Heikki Tuuri
  17. *******************************************************/
  18. #ifndef mem0mem_h
  19. #define mem0mem_h
  20. #include "univ.i"
  21. #include "ut0mem.h"
  22. #include "ut0byte.h"
  23. #include "ut0rnd.h"
  24. #ifndef UNIV_HOTBACKUP
  25. # include "sync0sync.h"
  26. #endif /* UNIV_HOTBACKUP */
  27. #include "ut0lst.h"
  28. #include "mach0data.h"
  29. /* -------------------- MEMORY HEAPS ----------------------------- */
  30. /* The info structure stored at the beginning of a heap block */
  31. typedef struct mem_block_info_struct mem_block_info_t;
  32. /* A block of a memory heap consists of the info structure
  33. followed by an area of memory */
  34. typedef mem_block_info_t mem_block_t;
  35. /* A memory heap is a nonempty linear list of memory blocks */
  36. typedef mem_block_t mem_heap_t;
  37. /* Types of allocation for memory heaps: DYNAMIC means allocation from the
  38. dynamic memory pool of the C compiler, BUFFER means allocation from the
  39. buffer pool; the latter method is used for very big heaps */
  40. #define MEM_HEAP_DYNAMIC 0 /* the most common type */
  41. #define MEM_HEAP_BUFFER 1
  42. #define MEM_HEAP_BTR_SEARCH 2 /* this flag can optionally be
  43. ORed to MEM_HEAP_BUFFER, in which
  44. case heap->free_block is used in
  45. some cases for memory allocations,
  46. and if it's NULL, the memory
  47. allocation functions can return
  48. NULL. */
  49. /* The following start size is used for the first block in the memory heap if
  50. the size is not specified, i.e., 0 is given as the parameter in the call of
  51. create. The standard size is the maximum (payload) size of the blocks used for
  52. allocations of small buffers. */
  53. #define MEM_BLOCK_START_SIZE 64
  54. #define MEM_BLOCK_STANDARD_SIZE \
  55. (UNIV_PAGE_SIZE >= 16384 ? 8000 : MEM_MAX_ALLOC_IN_BUF)
  56. /* If a memory heap is allowed to grow into the buffer pool, the following
  57. is the maximum size for a single allocated buffer: */
  58. #define MEM_MAX_ALLOC_IN_BUF (UNIV_PAGE_SIZE - 200)
  59. /******************************************************************//**
  60. Initializes the memory system. */
  61. UNIV_INTERN
  62. void
  63. mem_init(
  64. /*=====*/
  65. ulint size); /*!< in: common pool size in bytes */
  66. /**************************************************************//**
  67. Use this macro instead of the corresponding function! Macro for memory
  68. heap creation. */
  69. #define mem_heap_create(N) mem_heap_create_func(\
  70. (N), MEM_HEAP_DYNAMIC, __FILE__, __LINE__)
  71. /**************************************************************//**
  72. Use this macro instead of the corresponding function! Macro for memory
  73. heap creation. */
  74. #define mem_heap_create_in_buffer(N) mem_heap_create_func(\
  75. (N), MEM_HEAP_BUFFER, __FILE__, __LINE__)
  76. /**************************************************************//**
  77. Use this macro instead of the corresponding function! Macro for memory
  78. heap creation. */
  79. #define mem_heap_create_in_btr_search(N) mem_heap_create_func(\
  80. (N), MEM_HEAP_BTR_SEARCH | MEM_HEAP_BUFFER,\
  81. __FILE__, __LINE__)
  82. /**************************************************************//**
  83. Use this macro instead of the corresponding function! Macro for memory
  84. heap freeing. */
  85. #define mem_heap_free(heap) mem_heap_free_func(\
  86. (heap), __FILE__, __LINE__)
  87. /*****************************************************************//**
  88. NOTE: Use the corresponding macros instead of this function. Creates a
  89. memory heap. For debugging purposes, takes also the file name and line as
  90. arguments.
  91. @return own: memory heap, NULL if did not succeed (only possible for
  92. MEM_HEAP_BTR_SEARCH type heaps) */
  93. UNIV_INLINE
  94. mem_heap_t*
  95. mem_heap_create_func(
  96. /*=================*/
  97. ulint n, /*!< in: desired start block size,
  98. this means that a single user buffer
  99. of size n will fit in the block,
  100. 0 creates a default size block */
  101. ulint type, /*!< in: heap type */
  102. const char* file_name, /*!< in: file name where created */
  103. ulint line); /*!< in: line where created */
  104. /*****************************************************************//**
  105. NOTE: Use the corresponding macro instead of this function. Frees the space
  106. occupied by a memory heap. In the debug version erases the heap memory
  107. blocks. */
  108. UNIV_INLINE
  109. void
  110. mem_heap_free_func(
  111. /*===============*/
  112. mem_heap_t* heap, /*!< in, own: heap to be freed */
  113. const char* file_name, /*!< in: file name where freed */
  114. ulint line); /*!< in: line where freed */
  115. /***************************************************************//**
  116. Allocates and zero-fills n bytes of memory from a memory heap.
  117. @return allocated, zero-filled storage */
  118. UNIV_INLINE
  119. void*
  120. mem_heap_zalloc(
  121. /*============*/
  122. mem_heap_t* heap, /*!< in: memory heap */
  123. ulint n); /*!< in: number of bytes; if the heap is allowed
  124. to grow into the buffer pool, this must be
  125. <= MEM_MAX_ALLOC_IN_BUF */
  126. /***************************************************************//**
  127. Allocates n bytes of memory from a memory heap.
  128. @return allocated storage, NULL if did not succeed (only possible for
  129. MEM_HEAP_BTR_SEARCH type heaps) */
  130. UNIV_INLINE
  131. void*
  132. mem_heap_alloc(
  133. /*===========*/
  134. mem_heap_t* heap, /*!< in: memory heap */
  135. ulint n); /*!< in: number of bytes; if the heap is allowed
  136. to grow into the buffer pool, this must be
  137. <= MEM_MAX_ALLOC_IN_BUF */
  138. /*****************************************************************//**
  139. Returns a pointer to the heap top.
  140. @return pointer to the heap top */
  141. UNIV_INLINE
  142. byte*
  143. mem_heap_get_heap_top(
  144. /*==================*/
  145. mem_heap_t* heap); /*!< in: memory heap */
  146. /*****************************************************************//**
  147. Frees the space in a memory heap exceeding the pointer given. The
  148. pointer must have been acquired from mem_heap_get_heap_top. The first
  149. memory block of the heap is not freed. */
  150. UNIV_INLINE
  151. void
  152. mem_heap_free_heap_top(
  153. /*===================*/
  154. mem_heap_t* heap, /*!< in: heap from which to free */
  155. byte* old_top);/*!< in: pointer to old top of heap */
  156. /*****************************************************************//**
  157. Empties a memory heap. The first memory block of the heap is not freed. */
  158. UNIV_INLINE
  159. void
  160. mem_heap_empty(
  161. /*===========*/
  162. mem_heap_t* heap); /*!< in: heap to empty */
  163. /*****************************************************************//**
  164. Returns a pointer to the topmost element in a memory heap.
  165. The size of the element must be given.
  166. @return pointer to the topmost element */
  167. UNIV_INLINE
  168. void*
  169. mem_heap_get_top(
  170. /*=============*/
  171. mem_heap_t* heap, /*!< in: memory heap */
  172. ulint n); /*!< in: size of the topmost element */
  173. /*****************************************************************//**
  174. Frees the topmost element in a memory heap.
  175. The size of the element must be given. */
  176. UNIV_INLINE
  177. void
  178. mem_heap_free_top(
  179. /*==============*/
  180. mem_heap_t* heap, /*!< in: memory heap */
  181. ulint n); /*!< in: size of the topmost element */
  182. /*****************************************************************//**
  183. Returns the space in bytes occupied by a memory heap. */
  184. UNIV_INLINE
  185. ulint
  186. mem_heap_get_size(
  187. /*==============*/
  188. mem_heap_t* heap); /*!< in: heap */
  189. /**************************************************************//**
  190. Use this macro instead of the corresponding function!
  191. Macro for memory buffer allocation */
  192. #define mem_zalloc(N) memset(mem_alloc(N), 0, (N));
  193. #define mem_alloc(N) mem_alloc_func((N), NULL, __FILE__, __LINE__)
  194. #define mem_alloc2(N,S) mem_alloc_func((N), (S), __FILE__, __LINE__)
  195. /***************************************************************//**
  196. NOTE: Use the corresponding macro instead of this function.
  197. Allocates a single buffer of memory from the dynamic memory of
  198. the C compiler. Is like malloc of C. The buffer must be freed
  199. with mem_free.
  200. @return own: free storage */
  201. UNIV_INLINE
  202. void*
  203. mem_alloc_func(
  204. /*===========*/
  205. ulint n, /*!< in: requested size in bytes */
  206. ulint* size, /*!< out: allocated size in bytes,
  207. or NULL */
  208. const char* file_name, /*!< in: file name where created */
  209. ulint line); /*!< in: line where created */
  210. /**************************************************************//**
  211. Use this macro instead of the corresponding function!
  212. Macro for memory buffer freeing */
  213. #define mem_free(PTR) mem_free_func((PTR), __FILE__, __LINE__)
  214. /***************************************************************//**
  215. NOTE: Use the corresponding macro instead of this function.
  216. Frees a single buffer of storage from
  217. the dynamic memory of C compiler. Similar to free of C. */
  218. UNIV_INLINE
  219. void
  220. mem_free_func(
  221. /*==========*/
  222. void* ptr, /*!< in, own: buffer to be freed */
  223. const char* file_name, /*!< in: file name where created */
  224. ulint line); /*!< in: line where created */
  225. /**********************************************************************//**
  226. Duplicates a NUL-terminated string.
  227. @return own: a copy of the string, must be deallocated with mem_free */
  228. UNIV_INLINE
  229. char*
  230. mem_strdup(
  231. /*=======*/
  232. const char* str); /*!< in: string to be copied */
  233. /**********************************************************************//**
  234. Makes a NUL-terminated copy of a nonterminated string.
  235. @return own: a copy of the string, must be deallocated with mem_free */
  236. UNIV_INLINE
  237. char*
  238. mem_strdupl(
  239. /*========*/
  240. const char* str, /*!< in: string to be copied */
  241. ulint len); /*!< in: length of str, in bytes */
  242. /**********************************************************************//**
  243. Duplicates a NUL-terminated string, allocated from a memory heap.
  244. @return own: a copy of the string */
  245. UNIV_INTERN
  246. char*
  247. mem_heap_strdup(
  248. /*============*/
  249. mem_heap_t* heap, /*!< in: memory heap where string is allocated */
  250. const char* str); /*!< in: string to be copied */
  251. /**********************************************************************//**
  252. Makes a NUL-terminated copy of a nonterminated string,
  253. allocated from a memory heap.
  254. @return own: a copy of the string */
  255. UNIV_INLINE
  256. char*
  257. mem_heap_strdupl(
  258. /*=============*/
  259. mem_heap_t* heap, /*!< in: memory heap where string is allocated */
  260. const char* str, /*!< in: string to be copied */
  261. ulint len); /*!< in: length of str, in bytes */
  262. /**********************************************************************//**
  263. Concatenate two strings and return the result, using a memory heap.
  264. @return own: the result */
  265. UNIV_INTERN
  266. char*
  267. mem_heap_strcat(
  268. /*============*/
  269. mem_heap_t* heap, /*!< in: memory heap where string is allocated */
  270. const char* s1, /*!< in: string 1 */
  271. const char* s2); /*!< in: string 2 */
  272. /**********************************************************************//**
  273. Duplicate a block of data, allocated from a memory heap.
  274. @return own: a copy of the data */
  275. UNIV_INTERN
  276. void*
  277. mem_heap_dup(
  278. /*=========*/
  279. mem_heap_t* heap, /*!< in: memory heap where copy is allocated */
  280. const void* data, /*!< in: data to be copied */
  281. ulint len); /*!< in: length of data, in bytes */
  282. /****************************************************************//**
  283. A simple (s)printf replacement that dynamically allocates the space for the
  284. formatted string from the given heap. This supports a very limited set of
  285. the printf syntax: types 's' and 'u' and length modifier 'l' (which is
  286. required for the 'u' type).
  287. @return heap-allocated formatted string */
  288. UNIV_INTERN
  289. char*
  290. mem_heap_printf(
  291. /*============*/
  292. mem_heap_t* heap, /*!< in: memory heap */
  293. const char* format, /*!< in: format string */
  294. ...) __attribute__ ((format (printf, 2, 3)));
  295. #ifdef MEM_PERIODIC_CHECK
  296. /******************************************************************//**
  297. Goes through the list of all allocated mem blocks, checks their magic
  298. numbers, and reports possible corruption. */
  299. UNIV_INTERN
  300. void
  301. mem_validate_all_blocks(void);
  302. /*=========================*/
  303. #endif
  304. /*#######################################################################*/
  305. /* The info header of a block in a memory heap */
  306. struct mem_block_info_struct {
  307. ulint magic_n;/* magic number for debugging */
  308. char file_name[8];/* file name where the mem heap was created */
  309. ulint line; /*!< line number where the mem heap was created */
  310. UT_LIST_BASE_NODE_T(mem_block_t) base; /* In the first block in the
  311. the list this is the base node of the list of blocks;
  312. in subsequent blocks this is undefined */
  313. UT_LIST_NODE_T(mem_block_t) list; /* This contains pointers to next
  314. and prev in the list. The first block allocated
  315. to the heap is also the first block in this list,
  316. though it also contains the base node of the list. */
  317. ulint len; /*!< physical length of this block in bytes */
  318. ulint type; /*!< type of heap: MEM_HEAP_DYNAMIC, or
  319. MEM_HEAP_BUF possibly ORed to MEM_HEAP_BTR_SEARCH */
  320. ulint free; /*!< offset in bytes of the first free position for
  321. user data in the block */
  322. ulint start; /*!< the value of the struct field 'free' at the
  323. creation of the block */
  324. #ifndef UNIV_HOTBACKUP
  325. void* free_block;
  326. /* if the MEM_HEAP_BTR_SEARCH bit is set in type,
  327. and this is the heap root, this can contain an
  328. allocated buffer frame, which can be appended as a
  329. free block to the heap, if we need more space;
  330. otherwise, this is NULL */
  331. void* buf_block;
  332. /* if this block has been allocated from the buffer
  333. pool, this contains the buf_block_t handle;
  334. otherwise, this is NULL */
  335. #endif /* !UNIV_HOTBACKUP */
  336. #ifdef MEM_PERIODIC_CHECK
  337. UT_LIST_NODE_T(mem_block_t) mem_block_list;
  338. /* List of all mem blocks allocated; protected
  339. by the mem_comm_pool mutex */
  340. #endif
  341. };
  342. #define MEM_BLOCK_MAGIC_N 764741555
  343. #define MEM_FREED_BLOCK_MAGIC_N 547711122
  344. /* Header size for a memory heap block */
  345. #define MEM_BLOCK_HEADER_SIZE ut_calc_align(sizeof(mem_block_info_t),\
  346. UNIV_MEM_ALIGNMENT)
  347. #include "mem0dbg.h"
  348. #ifndef UNIV_NONINL
  349. #include "mem0mem.ic"
  350. #endif
  351. #endif