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.

427 lines
15 KiB

20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
  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
  26. 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 optionally be
  30. ORed to MEM_HEAP_BUFFER, in which
  31. case heap->free_block is used in
  32. some cases for memory allocations,
  33. and if it's NULL, the memory
  34. allocation functions can return
  35. NULL. */
  36. /* The following start size is used for the first block in the memory heap if
  37. the size is not specified, i.e., 0 is given as the parameter in the call of
  38. create. The standard size is the maximum (payload) size of the blocks used for
  39. allocations of small buffers. */
  40. #define MEM_BLOCK_START_SIZE 64
  41. #define MEM_BLOCK_STANDARD_SIZE 8000
  42. /* If a memory heap is allowed to grow into the buffer pool, the following
  43. is the maximum size for a single allocated buffer: */
  44. #define MEM_MAX_ALLOC_IN_BUF (UNIV_PAGE_SIZE - 200)
  45. /**********************************************************************
  46. Initializes the memory system. */
  47. void
  48. mem_init(
  49. /*=====*/
  50. ulint size); /* in: common pool size in bytes */
  51. /******************************************************************
  52. Use this macro instead of the corresponding function! Macro for memory
  53. heap creation. */
  54. #define mem_heap_create(N) mem_heap_create_func(\
  55. (N), NULL, MEM_HEAP_DYNAMIC, __FILE__, __LINE__)
  56. /******************************************************************
  57. Use this macro instead of the corresponding function! Macro for memory
  58. heap creation. */
  59. #define mem_heap_create_in_buffer(N) mem_heap_create_func(\
  60. (N), NULL, MEM_HEAP_BUFFER, __FILE__, __LINE__)
  61. /******************************************************************
  62. Use this macro instead of the corresponding function! Macro for memory
  63. heap creation. */
  64. #define mem_heap_create_in_btr_search(N) mem_heap_create_func(\
  65. (N), NULL, MEM_HEAP_BTR_SEARCH | MEM_HEAP_BUFFER,\
  66. __FILE__, __LINE__)
  67. /******************************************************************
  68. Use this macro instead of the corresponding function! Macro for fast
  69. memory heap creation. An initial block of memory B is given by the
  70. caller, N is its size, and this memory block is not freed by
  71. mem_heap_free. See the parameter comment in mem_heap_create_func below. */
  72. #define mem_heap_fast_create(N, B) mem_heap_create_func(\
  73. (N), (B), MEM_HEAP_DYNAMIC, __FILE__, __LINE__)
  74. /******************************************************************
  75. Use this macro instead of the corresponding function! Macro for memory
  76. heap freeing. */
  77. #define mem_heap_free(heap) mem_heap_free_func(\
  78. (heap), __FILE__, __LINE__)
  79. /*********************************************************************
  80. NOTE: Use the corresponding macros instead of this function. Creates a
  81. memory heap. For debugging purposes, takes also the file name and line as
  82. arguments. */
  83. UNIV_INLINE
  84. mem_heap_t*
  85. mem_heap_create_func(
  86. /*=================*/
  87. /* out, own: memory heap, NULL if
  88. did not succeed (only possible for
  89. MEM_HEAP_BTR_SEARCH type heaps)*/
  90. ulint n, /* in: desired start block size,
  91. this means that a single user buffer
  92. of size n will fit in the block,
  93. 0 creates a default size block;
  94. if init_block is not NULL, n tells
  95. its size in bytes */
  96. void* init_block, /* in: if very fast creation is
  97. wanted, the caller can reserve some
  98. memory from its stack, for example,
  99. and pass it as the the initial block
  100. to the heap: then no OS call of malloc
  101. is needed at the creation. CAUTION:
  102. the caller must make sure the initial
  103. block is not unintentionally erased
  104. (if allocated in the stack), before
  105. the memory heap is explicitly freed. */
  106. ulint type, /* in: heap type */
  107. const char* file_name, /* in: file name where created */
  108. ulint line); /* in: line where created */
  109. /*********************************************************************
  110. NOTE: Use the corresponding macro instead of this function. Frees the space
  111. occupied by a memory heap. In the debug version erases the heap memory
  112. blocks. */
  113. UNIV_INLINE
  114. void
  115. mem_heap_free_func(
  116. /*===============*/
  117. mem_heap_t* heap, /* in, own: heap to be freed */
  118. const char* file_name, /* in: file name where freed */
  119. ulint line); /* in: line where freed */
  120. /*******************************************************************
  121. Allocates n bytes of memory from a memory heap. */
  122. UNIV_INLINE
  123. void*
  124. mem_heap_alloc(
  125. /*===========*/
  126. /* out: allocated storage, NULL if did not
  127. succeed (only possible for
  128. MEM_HEAP_BTR_SEARCH type heaps) */
  129. mem_heap_t* heap, /* in: memory heap */
  130. ulint n); /* in: number of bytes; if the heap is allowed
  131. to grow into the buffer pool, this must be
  132. <= MEM_MAX_ALLOC_IN_BUF */
  133. /*********************************************************************
  134. Returns a pointer to the heap top. */
  135. UNIV_INLINE
  136. byte*
  137. mem_heap_get_heap_top(
  138. /*==================*/
  139. /* out: pointer to the heap top */
  140. mem_heap_t* heap); /* in: memory heap */
  141. /*********************************************************************
  142. Frees the space in a memory heap exceeding the pointer given. The
  143. pointer must have been acquired from mem_heap_get_heap_top. The first
  144. memory block of the heap is not freed. */
  145. UNIV_INLINE
  146. void
  147. mem_heap_free_heap_top(
  148. /*===================*/
  149. mem_heap_t* heap, /* in: heap from which to free */
  150. byte* old_top);/* in: pointer to old top of heap */
  151. /*********************************************************************
  152. Empties a memory heap. The first memory block of the heap is not freed. */
  153. UNIV_INLINE
  154. void
  155. mem_heap_empty(
  156. /*===========*/
  157. mem_heap_t* heap); /* in: heap to empty */
  158. /*********************************************************************
  159. Returns a pointer to the topmost element in a memory heap.
  160. The size of the element must be given. */
  161. UNIV_INLINE
  162. void*
  163. mem_heap_get_top(
  164. /*=============*/
  165. /* out: pointer to the topmost element */
  166. mem_heap_t* heap, /* in: memory heap */
  167. ulint n); /* in: size of the topmost element */
  168. /*********************************************************************
  169. Frees the topmost element in a memory heap.
  170. The size of the element must be given. */
  171. UNIV_INLINE
  172. void
  173. mem_heap_free_top(
  174. /*==============*/
  175. mem_heap_t* heap, /* in: memory heap */
  176. ulint n); /* in: size of the topmost element */
  177. /*********************************************************************
  178. Returns the space in bytes occupied by a memory heap. */
  179. UNIV_INLINE
  180. ulint
  181. mem_heap_get_size(
  182. /*==============*/
  183. mem_heap_t* heap); /* in: heap */
  184. /******************************************************************
  185. Use this macro instead of the corresponding function!
  186. Macro for memory buffer allocation */
  187. #define mem_alloc(N) mem_alloc_func((N), __FILE__, __LINE__)
  188. /******************************************************************
  189. Use this macro instead of the corresponding function!
  190. Macro for memory buffer allocation */
  191. #define mem_alloc_noninline(N) mem_alloc_func_noninline(\
  192. (N), __FILE__, __LINE__)
  193. /*******************************************************************
  194. NOTE: Use the corresponding macro instead of this function.
  195. Allocates a single buffer of memory from the dynamic memory of
  196. the C compiler. Is like malloc of C. The buffer must be freed
  197. with mem_free. */
  198. UNIV_INLINE
  199. void*
  200. mem_alloc_func(
  201. /*===========*/
  202. /* out, own: free storage */
  203. ulint n, /* in: desired number of bytes */
  204. const char* file_name, /* in: file name where created */
  205. ulint line /* in: line where created */
  206. );
  207. /*******************************************************************
  208. NOTE: Use the corresponding macro instead of this function.
  209. Allocates a single buffer of memory from the dynamic memory of
  210. the C compiler. Is like malloc of C. The buffer must be freed
  211. with mem_free. */
  212. void*
  213. mem_alloc_func_noninline(
  214. /*=====================*/
  215. /* out, own: free storage */
  216. ulint n, /* in: desired number of bytes */
  217. const char* file_name, /* in: file name where created */
  218. ulint line /* in: line where created */
  219. );
  220. /******************************************************************
  221. Use this macro instead of the corresponding function!
  222. Macro for memory buffer freeing */
  223. #define mem_free(PTR) mem_free_func((PTR), __FILE__, __LINE__)
  224. /*******************************************************************
  225. NOTE: Use the corresponding macro instead of this function.
  226. Frees a single buffer of storage from
  227. the dynamic memory of C compiler. Similar to free of C. */
  228. UNIV_INLINE
  229. void
  230. mem_free_func(
  231. /*==========*/
  232. void* ptr, /* in, own: buffer to be freed */
  233. const char* file_name, /* in: file name where created */
  234. ulint line /* in: line where created */
  235. );
  236. /**************************************************************************
  237. Duplicates a NUL-terminated string. */
  238. UNIV_INLINE
  239. char*
  240. mem_strdup(
  241. /*=======*/
  242. /* out, own: a copy of the string,
  243. must be deallocated with mem_free */
  244. const char* str); /* in: string to be copied */
  245. /**************************************************************************
  246. Makes a NUL-terminated copy of a nonterminated string. */
  247. UNIV_INLINE
  248. char*
  249. mem_strdupl(
  250. /*========*/
  251. /* out, own: a copy of the string,
  252. must be deallocated with mem_free */
  253. const char* str, /* in: string to be copied */
  254. ulint len); /* in: length of str, in bytes */
  255. /**************************************************************************
  256. Makes a NUL-terminated quoted copy of a NUL-terminated string. */
  257. UNIV_INLINE
  258. char*
  259. mem_strdupq(
  260. /*========*/
  261. /* out, own: a quoted copy of the string,
  262. must be deallocated with mem_free */
  263. const char* str, /* in: string to be copied */
  264. char q); /* in: quote character */
  265. /**************************************************************************
  266. Duplicates a NUL-terminated string, allocated from a memory heap. */
  267. char*
  268. mem_heap_strdup(
  269. /*============*/
  270. /* out, own: a copy of the string */
  271. mem_heap_t* heap, /* in: memory heap where string is allocated */
  272. const char* str); /* in: string to be copied */
  273. /**************************************************************************
  274. Makes a NUL-terminated copy of a nonterminated string,
  275. allocated from a memory heap. */
  276. UNIV_INLINE
  277. char*
  278. mem_heap_strdupl(
  279. /*=============*/
  280. /* out, own: a copy of the string */
  281. mem_heap_t* heap, /* in: memory heap where string is allocated */
  282. const char* str, /* in: string to be copied */
  283. ulint len); /* in: length of str, in bytes */
  284. /**************************************************************************
  285. Concatenate two strings and return the result, using a memory heap. */
  286. char*
  287. mem_heap_strcat(
  288. /*============*/
  289. /* out, own: the result */
  290. mem_heap_t* heap, /* in: memory heap where string is allocated */
  291. const char* s1, /* in: string 1 */
  292. const char* s2); /* in: string 2 */
  293. /**************************************************************************
  294. Duplicate a block of data, allocated from a memory heap. */
  295. void*
  296. mem_heap_dup(
  297. /*=========*/
  298. /* out, own: a copy of the data */
  299. mem_heap_t* heap, /* in: memory heap where copy is allocated */
  300. const void* data, /* in: data to be copied */
  301. ulint len); /* in: length of data, in bytes */
  302. /**************************************************************************
  303. Concatenate two memory blocks and return the result, using a memory heap. */
  304. void*
  305. mem_heap_cat(
  306. /*=========*/
  307. /* out, own: the result */
  308. mem_heap_t* heap, /* in: memory heap where result is allocated */
  309. const void* b1, /* in: block 1 */
  310. ulint len1, /* in: length of b1, in bytes */
  311. const void* b2, /* in: block 2 */
  312. ulint len2); /* in: length of b2, in bytes */
  313. /********************************************************************
  314. A simple (s)printf replacement that dynamically allocates the space for the
  315. formatted string from the given heap. This supports a very limited set of
  316. the printf syntax: types 's' and 'u' and length modifier 'l' (which is
  317. required for the 'u' type). */
  318. char*
  319. mem_heap_printf(
  320. /*============*/
  321. /* out: heap-allocated formatted string */
  322. mem_heap_t* heap, /* in: memory heap */
  323. const char* format, /* in: format string */
  324. ...) __attribute__ ((format (printf, 2, 3)));
  325. #ifdef MEM_PERIODIC_CHECK
  326. /**********************************************************************
  327. Goes through the list of all allocated mem blocks, checks their magic
  328. numbers, and reports possible corruption. */
  329. void
  330. mem_validate_all_blocks(void);
  331. /*=========================*/
  332. #endif
  333. /*#######################################################################*/
  334. /* The info header of a block in a memory heap */
  335. struct mem_block_info_struct {
  336. ulint magic_n;/* magic number for debugging */
  337. char file_name[8];/* file name where the mem heap was created */
  338. ulint line; /* line number where the mem heap was created */
  339. UT_LIST_BASE_NODE_T(mem_block_t) base; /* In the first block in the
  340. the list this is the base node of the list of blocks;
  341. in subsequent blocks this is undefined */
  342. UT_LIST_NODE_T(mem_block_t) list; /* This contains pointers to next
  343. and prev in the list. The first block allocated
  344. to the heap is also the first block in this list,
  345. though it also contains the base node of the list. */
  346. ulint len; /* physical length of this block in bytes */
  347. ulint type; /* type of heap: MEM_HEAP_DYNAMIC, or
  348. MEM_HEAP_BUF possibly ORed to MEM_HEAP_BTR_SEARCH */
  349. ibool init_block; /* TRUE if this is the first block used in fast
  350. creation of a heap: the memory will be freed
  351. by the creator, not by mem_heap_free */
  352. ulint free; /* offset in bytes of the first free position for
  353. user data in the block */
  354. ulint start; /* the value of the struct field 'free' at the
  355. creation of the block */
  356. void* free_block;
  357. /* if the MEM_HEAP_BTR_SEARCH bit is set in type,
  358. and this is the heap root, this can contain an
  359. allocated buffer frame, which can be appended as a
  360. free block to the heap, if we need more space;
  361. otherwise, this is NULL */
  362. void* buf_block;
  363. /* if this block has been allocated from the buffer
  364. pool, this contains the buf_block_t handle;
  365. otherwise, this is NULL */
  366. #ifdef MEM_PERIODIC_CHECK
  367. UT_LIST_NODE_T(mem_block_t) mem_block_list;
  368. /* List of all mem blocks allocated; protected
  369. by the mem_comm_pool mutex */
  370. #endif
  371. };
  372. #define MEM_BLOCK_MAGIC_N 764741555
  373. #define MEM_FREED_BLOCK_MAGIC_N 547711122
  374. /* Header size for a memory heap block */
  375. #define MEM_BLOCK_HEADER_SIZE ut_calc_align(sizeof(mem_block_info_t),\
  376. UNIV_MEM_ALIGNMENT)
  377. #include "mem0dbg.h"
  378. #ifndef UNIV_NONINL
  379. #include "mem0mem.ic"
  380. #endif
  381. #endif