InnoDB Plugin  1.0
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 #ifdef UNIV_DEBUG
96 /**************************************************************/
100 # define mem_heap_create(N) mem_heap_create_func( \
101  (N), __FILE__, __LINE__, MEM_HEAP_DYNAMIC)
102 /**************************************************************/
106 # define mem_heap_create_typed(N, T) mem_heap_create_func( \
107  (N), __FILE__, __LINE__, (T))
108 
109 #else /* UNIV_DEBUG */
110 /**************************************************************/
114 # define mem_heap_create(N) mem_heap_create_func( \
115  (N), MEM_HEAP_DYNAMIC)
116 /**************************************************************/
120 # define mem_heap_create_typed(N, T) mem_heap_create_func( \
121  (N), (T))
123 #endif /* UNIV_DEBUG */
124 /**************************************************************/
128 #define mem_heap_free(heap) mem_heap_free_func(\
129  (heap), __FILE__, __LINE__)
130 /*****************************************************************/
136 UNIV_INLINE
137 mem_heap_t*
139 /*=================*/
140  ulint n,
144 #ifdef UNIV_DEBUG
145  const char* file_name,
146  ulint line,
147 #endif /* UNIV_DEBUG */
148  ulint type);
149 /*****************************************************************/
153 UNIV_INLINE
154 void
156 /*===============*/
157  mem_heap_t* heap,
158  const char* file_name,
159  ulint line);
160 /***************************************************************/
163 UNIV_INLINE
164 void*
166 /*============*/
167  mem_heap_t* heap,
168  ulint n);
171 /***************************************************************/
175 UNIV_INLINE
176 void*
178 /*===========*/
179  mem_heap_t* heap,
180  ulint n);
183 /*****************************************************************/
186 UNIV_INLINE
187 byte*
189 /*==================*/
190  mem_heap_t* heap);
191 /*****************************************************************/
195 UNIV_INLINE
196 void
198 /*===================*/
199  mem_heap_t* heap,
200  byte* old_top);
201 /*****************************************************************/
203 UNIV_INLINE
204 void
206 /*===========*/
207  mem_heap_t* heap);
208 /*****************************************************************/
212 UNIV_INLINE
213 void*
215 /*=============*/
216  mem_heap_t* heap,
217  ulint n);
218 /*****************************************************************/
221 UNIV_INLINE
222 void
224 /*==============*/
225  mem_heap_t* heap,
226  ulint n);
227 /*****************************************************************/
229 UNIV_INLINE
230 ulint
232 /*==============*/
233  mem_heap_t* heap);
234 /**************************************************************/
238 #define mem_zalloc(N) memset(mem_alloc(N), 0, (N))
239 
240 #ifdef UNIV_DEBUG
241 #define mem_alloc(N) mem_alloc_func((N), __FILE__, __LINE__, NULL)
242 #define mem_alloc2(N,S) mem_alloc_func((N), __FILE__, __LINE__, (S))
243 #else /* UNIV_DEBUG */
244 #define mem_alloc(N) mem_alloc_func((N), NULL)
245 #define mem_alloc2(N,S) mem_alloc_func((N), (S))
246 #endif /* UNIV_DEBUG */
247 
248 /***************************************************************/
254 UNIV_INLINE
255 void*
257 /*===========*/
258  ulint n,
259 #ifdef UNIV_DEBUG
260  const char* file_name,
261  ulint line,
262 #endif /* UNIV_DEBUG */
263  ulint* size);
266 /**************************************************************/
270 #define mem_free(PTR) mem_free_func((PTR), __FILE__, __LINE__)
271 /***************************************************************/
275 UNIV_INLINE
276 void
278 /*==========*/
279  void* ptr,
280  const char* file_name,
281  ulint line);
283 /**********************************************************************/
286 UNIV_INLINE
287 char*
288 mem_strdup(
289 /*=======*/
290  const char* str);
291 /**********************************************************************/
294 UNIV_INLINE
295 char*
297 /*========*/
298  const char* str,
299  ulint len);
301 /**********************************************************************/
304 UNIV_INTERN
305 char*
307 /*============*/
308  mem_heap_t* heap,
309  const char* str);
310 /**********************************************************************/
314 UNIV_INLINE
315 char*
317 /*=============*/
318  mem_heap_t* heap,
319  const char* str,
320  ulint len);
322 /**********************************************************************/
325 UNIV_INTERN
326 char*
328 /*============*/
329  mem_heap_t* heap,
330  const char* s1,
331  const char* s2);
333 /**********************************************************************/
336 UNIV_INTERN
337 void*
339 /*=========*/
340  mem_heap_t* heap,
341  const void* data,
342  ulint len);
344 /****************************************************************/
350 UNIV_INTERN
351 char*
353 /*============*/
354  mem_heap_t* heap,
355  const char* format,
356  ...) __attribute__ ((format (printf, 2, 3)));
357 
358 #ifdef MEM_PERIODIC_CHECK
359 /******************************************************************/
362 UNIV_INTERN
363 void
364 mem_validate_all_blocks(void);
365 /*=========================*/
366 #endif
367 
368 /*#######################################################################*/
371 struct mem_block_info_t {
372  ulint magic_n;/* magic number for debugging */
373 #ifdef UNIV_DEBUG
374  char file_name[8];/* file name where the mem heap was created */
375  ulint line;
376 #endif /* UNIV_DEBUG */
377  UT_LIST_BASE_NODE_T(mem_block_t) base; /* In the first block in the
378  the list this is the base node of the list of blocks;
379  in subsequent blocks this is undefined */
380  UT_LIST_NODE_T(mem_block_t) list; /* This contains pointers to next
381  and prev in the list. The first block allocated
382  to the heap is also the first block in this list,
383  though it also contains the base node of the list. */
384  ulint len;
385  ulint total_size;
388  ulint type;
390  ulint free;
392  ulint start;
394 #ifndef UNIV_HOTBACKUP
395  void* free_block;
396  /* if the MEM_HEAP_BTR_SEARCH bit is set in type,
397  and this is the heap root, this can contain an
398  allocated buffer frame, which can be appended as a
399  free block to the heap, if we need more space;
400  otherwise, this is NULL */
401  void* buf_block;
402  /* if this block has been allocated from the buffer
403  pool, this contains the buf_block_t handle;
404  otherwise, this is NULL */
405 #endif /* !UNIV_HOTBACKUP */
406 #ifdef MEM_PERIODIC_CHECK
407  UT_LIST_NODE_T(mem_block_t) mem_block_list;
408  /* List of all mem blocks allocated; protected
409  by the mem_comm_pool mutex */
410 #endif
411 };
412 
413 #define MEM_BLOCK_MAGIC_N 764741555
414 #define MEM_FREED_BLOCK_MAGIC_N 547711122
415 
416 /* Header size for a memory heap block */
417 #define MEM_BLOCK_HEADER_SIZE ut_calc_align(sizeof(mem_block_info_t),\
418  UNIV_MEM_ALIGNMENT)
419 #include "mem0dbg.h"
420 
421 #ifndef UNIV_NONINL
422 #include "mem0mem.ic"
423 #endif
424 
425 #endif