MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mem0mem.h
Go to the documentation of this file.
1 /*****************************************************************************
2 
3 Copyright (c) 1994, 2010, Oracle and/or its affiliates. All Rights Reserved.
4 
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free Software
7 Foundation; version 2 of the License.
8 
9 This program is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12 
13 You should have received a copy of the GNU General Public License along with
14 this program; if not, write to the Free Software Foundation, Inc.,
15 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
16 
17 *****************************************************************************/
18 
19 /**************************************************/
26 #ifndef mem0mem_h
27 #define mem0mem_h
28 
29 #include "univ.i"
30 #include "ut0mem.h"
31 #include "ut0byte.h"
32 #include "ut0rnd.h"
33 #ifndef UNIV_HOTBACKUP
34 # include "sync0sync.h"
35 #endif /* UNIV_HOTBACKUP */
36 #include "ut0lst.h"
37 #include "mach0data.h"
38 
39 /* -------------------- MEMORY HEAPS ----------------------------- */
40 
41 /* A block of a memory heap consists of the info structure
42 followed by an area of memory */
43 typedef struct mem_block_info_t mem_block_t;
44 
45 /* A memory heap is a nonempty linear list of memory blocks */
46 typedef mem_block_t mem_heap_t;
47 
48 /* Types of allocation for memory heaps: DYNAMIC means allocation from the
49 dynamic memory pool of the C compiler, BUFFER means allocation from the
50 buffer pool; the latter method is used for very big heaps */
51 
52 #define MEM_HEAP_DYNAMIC 0 /* the most common type */
53 #define MEM_HEAP_BUFFER 1
54 #define MEM_HEAP_BTR_SEARCH 2 /* this flag can optionally be
55  ORed to MEM_HEAP_BUFFER, in which
56  case heap->free_block is used in
57  some cases for memory allocations,
58  and if it's NULL, the memory
59  allocation functions can return
60  NULL. */
61 
62 /* Different type of heaps in terms of which datastructure is using them */
63 #define MEM_HEAP_FOR_BTR_SEARCH (MEM_HEAP_BTR_SEARCH | MEM_HEAP_BUFFER)
64 #define MEM_HEAP_FOR_PAGE_HASH (MEM_HEAP_DYNAMIC)
65 #define MEM_HEAP_FOR_RECV_SYS (MEM_HEAP_BUFFER)
66 #define MEM_HEAP_FOR_LOCK_HEAP (MEM_HEAP_BUFFER)
67 
68 /* The following start size is used for the first block in the memory heap if
69 the size is not specified, i.e., 0 is given as the parameter in the call of
70 create. The standard size is the maximum (payload) size of the blocks used for
71 allocations of small buffers. */
72 
73 #define MEM_BLOCK_START_SIZE 64
74 #define MEM_BLOCK_STANDARD_SIZE \
75  (UNIV_PAGE_SIZE >= 16384 ? 8000 : MEM_MAX_ALLOC_IN_BUF)
76 
77 /* If a memory heap is allowed to grow into the buffer pool, the following
78 is the maximum size for a single allocated buffer: */
79 #define MEM_MAX_ALLOC_IN_BUF (UNIV_PAGE_SIZE - 200)
80 
81 /******************************************************************/
83 UNIV_INTERN
84 void
85 mem_init(
86 /*=====*/
87  ulint size);
88 /******************************************************************/
90 UNIV_INTERN
91 void
92 mem_close(void);
93 /*===========*/
94 
95 /**************************************************************/
99 #define mem_heap_create(N) mem_heap_create_func(\
100  (N), MEM_HEAP_DYNAMIC, __FILE__, __LINE__)
101 /**************************************************************/
105 #define mem_heap_create_typed(N, T) mem_heap_create_func(\
106  (N), (T), __FILE__, __LINE__)
107 /**************************************************************/
111 #define mem_heap_free(heap) mem_heap_free_func(\
112  (heap), __FILE__, __LINE__)
113 /*****************************************************************/
119 UNIV_INLINE
120 mem_heap_t*
122 /*=================*/
123  ulint n,
127  ulint type,
128  const char* file_name,
129  ulint line);
130 /*****************************************************************/
134 UNIV_INLINE
135 void
137 /*===============*/
138  mem_heap_t* heap,
139  const char* file_name,
140  ulint line);
141 /***************************************************************/
144 UNIV_INLINE
145 void*
147 /*============*/
148  mem_heap_t* heap,
149  ulint n);
152 /***************************************************************/
156 UNIV_INLINE
157 void*
159 /*===========*/
160  mem_heap_t* heap,
161  ulint n);
164 /*****************************************************************/
167 UNIV_INLINE
168 byte*
170 /*==================*/
171  mem_heap_t* heap);
172 /*****************************************************************/
176 UNIV_INLINE
177 void
179 /*===================*/
180  mem_heap_t* heap,
181  byte* old_top);
182 /*****************************************************************/
184 UNIV_INLINE
185 void
187 /*===========*/
188  mem_heap_t* heap);
189 /*****************************************************************/
193 UNIV_INLINE
194 void*
196 /*=============*/
197  mem_heap_t* heap,
198  ulint n);
199 /*****************************************************************/
202 UNIV_INLINE
203 void
205 /*==============*/
206  mem_heap_t* heap,
207  ulint n);
208 /*****************************************************************/
210 UNIV_INLINE
211 ulint
213 /*==============*/
214  mem_heap_t* heap);
215 /**************************************************************/
219 #define mem_zalloc(N) memset(mem_alloc(N), 0, (N))
220 
221 #define mem_alloc(N) mem_alloc_func((N), NULL, __FILE__, __LINE__)
222 #define mem_alloc2(N,S) mem_alloc_func((N), (S), __FILE__, __LINE__)
223 /***************************************************************/
229 UNIV_INLINE
230 void*
232 /*===========*/
233  ulint n,
234  ulint* size,
236  const char* file_name,
237  ulint line);
239 /**************************************************************/
243 #define mem_free(PTR) mem_free_func((PTR), __FILE__, __LINE__)
244 /***************************************************************/
248 UNIV_INLINE
249 void
251 /*==========*/
252  void* ptr,
253  const char* file_name,
254  ulint line);
256 /**********************************************************************/
259 UNIV_INLINE
260 char*
261 mem_strdup(
262 /*=======*/
263  const char* str);
264 /**********************************************************************/
267 UNIV_INLINE
268 char*
270 /*========*/
271  const char* str,
272  ulint len);
274 /**********************************************************************/
277 UNIV_INTERN
278 char*
280 /*============*/
281  mem_heap_t* heap,
282  const char* str);
283 /**********************************************************************/
287 UNIV_INLINE
288 char*
290 /*=============*/
291  mem_heap_t* heap,
292  const char* str,
293  ulint len);
295 /**********************************************************************/
298 UNIV_INTERN
299 char*
301 /*============*/
302  mem_heap_t* heap,
303  const char* s1,
304  const char* s2);
306 /**********************************************************************/
309 UNIV_INTERN
310 void*
312 /*=========*/
313  mem_heap_t* heap,
314  const void* data,
315  ulint len);
317 /****************************************************************/
323 UNIV_INTERN
324 char*
326 /*============*/
327  mem_heap_t* heap,
328  const char* format,
329  ...) __attribute__ ((format (printf, 2, 3)));
330 
331 #ifdef MEM_PERIODIC_CHECK
332 /******************************************************************/
335 UNIV_INTERN
336 void
337 mem_validate_all_blocks(void);
338 /*=========================*/
339 #endif
340 
341 /*#######################################################################*/
342 
344 struct mem_block_info_t {
345  ulint magic_n;/* magic number for debugging */
346  char file_name[8];/* file name where the mem heap was created */
347  ulint line;
348  UT_LIST_BASE_NODE_T(mem_block_t) base; /* In the first block in the
349  the list this is the base node of the list of blocks;
350  in subsequent blocks this is undefined */
351  UT_LIST_NODE_T(mem_block_t) list; /* This contains pointers to next
352  and prev in the list. The first block allocated
353  to the heap is also the first block in this list,
354  though it also contains the base node of the list. */
355  ulint len;
356  ulint total_size;
359  ulint type;
361  ulint free;
363  ulint start;
365 #ifndef UNIV_HOTBACKUP
366  void* free_block;
367  /* if the MEM_HEAP_BTR_SEARCH bit is set in type,
368  and this is the heap root, this can contain an
369  allocated buffer frame, which can be appended as a
370  free block to the heap, if we need more space;
371  otherwise, this is NULL */
372  void* buf_block;
373  /* if this block has been allocated from the buffer
374  pool, this contains the buf_block_t handle;
375  otherwise, this is NULL */
376 #endif /* !UNIV_HOTBACKUP */
377 #ifdef MEM_PERIODIC_CHECK
378  UT_LIST_NODE_T(mem_block_t) mem_block_list;
379  /* List of all mem blocks allocated; protected
380  by the mem_comm_pool mutex */
381 #endif
382 };
383 
384 #define MEM_BLOCK_MAGIC_N 764741555
385 #define MEM_FREED_BLOCK_MAGIC_N 547711122
386 
387 /* Header size for a memory heap block */
388 #define MEM_BLOCK_HEADER_SIZE ut_calc_align(sizeof(mem_block_info_t),\
389  UNIV_MEM_ALIGNMENT)
390 #include "mem0dbg.h"
391 
392 #ifndef UNIV_NONINL
393 #include "mem0mem.ic"
394 #endif
395 
396 #endif