1
/*****************************************************************************
3
Copyright (C) 1994, 2010, Innobase Oy. All Rights Reserved.
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.
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.
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., 51 Franklin
15
St, Fifth Floor, Boston, MA 02110-1301 USA
17
*****************************************************************************/
19
/**************************************************//**
20
@file include/mem0mem.h
23
Created 6/9/1994 Heikki Tuuri
24
*******************************************************/
33
#ifndef UNIV_HOTBACKUP
34
# include "sync0sync.h"
35
#endif /* UNIV_HOTBACKUP */
37
#include "mach0data.h"
39
/* -------------------- MEMORY HEAPS ----------------------------- */
41
/* The info structure stored at the beginning of a heap block */
42
typedef struct mem_block_info_struct mem_block_info_t;
44
/* A block of a memory heap consists of the info structure
45
followed by an area of memory */
46
typedef mem_block_info_t mem_block_t;
48
/* A memory heap is a nonempty linear list of memory blocks */
49
typedef mem_block_t mem_heap_t;
51
/* Types of allocation for memory heaps: DYNAMIC means allocation from the
52
dynamic memory pool of the C compiler, BUFFER means allocation from the
53
buffer pool; the latter method is used for very big heaps */
55
#define MEM_HEAP_DYNAMIC 0 /* the most common type */
56
#define MEM_HEAP_BUFFER 1
57
#define MEM_HEAP_BTR_SEARCH 2 /* this flag can optionally be
58
ORed to MEM_HEAP_BUFFER, in which
59
case heap->free_block is used in
60
some cases for memory allocations,
61
and if NULL, the memory
62
allocation functions can return
65
/* The following start size is used for the first block in the memory heap if
66
the size is not specified, i.e., 0 is given as the parameter in the call of
67
create. The standard size is the maximum (payload) size of the blocks used for
68
allocations of small buffers. */
70
#define MEM_BLOCK_START_SIZE 64
71
#define MEM_BLOCK_STANDARD_SIZE \
72
(UNIV_PAGE_SIZE >= 16384 ? 8000 : MEM_MAX_ALLOC_IN_BUF)
74
/* If a memory heap is allowed to grow into the buffer pool, the following
75
is the maximum size for a single allocated buffer: */
76
#define MEM_MAX_ALLOC_IN_BUF (UNIV_PAGE_SIZE - 200)
78
/******************************************************************//**
79
Initializes the memory system. */
84
ulint size); /*!< in: common pool size in bytes */
85
/******************************************************************//**
86
Closes the memory system. */
92
/**************************************************************//**
93
Use this macro instead of the corresponding function! Macro for memory
96
#define mem_heap_create(N) mem_heap_create_func(\
97
(N), MEM_HEAP_DYNAMIC, __FILE__, __LINE__)
98
/**************************************************************//**
99
Use this macro instead of the corresponding function! Macro for memory
102
#define mem_heap_create_in_buffer(N) mem_heap_create_func(\
103
(N), MEM_HEAP_BUFFER, __FILE__, __LINE__)
104
/**************************************************************//**
105
Use this macro instead of the corresponding function! Macro for memory
108
#define mem_heap_create_in_btr_search(N) mem_heap_create_func(\
109
(N), MEM_HEAP_BTR_SEARCH | MEM_HEAP_BUFFER,\
112
/**************************************************************//**
113
Use this macro instead of the corresponding function! Macro for memory
116
#define mem_heap_free(heap) mem_heap_free_func(\
117
(heap), __FILE__, __LINE__)
118
/*****************************************************************//**
119
NOTE: Use the corresponding macros instead of this function. Creates a
120
memory heap. For debugging purposes, takes also the file name and line as
122
@return own: memory heap, NULL if did not succeed (only possible for
123
MEM_HEAP_BTR_SEARCH type heaps) */
126
mem_heap_create_func(
127
/*=================*/
128
ulint n, /*!< in: desired start block size,
129
this means that a single user buffer
130
of size n will fit in the block,
131
0 creates a default size block */
132
ulint type, /*!< in: heap type */
133
const char* file_name, /*!< in: file name where created */
134
ulint line); /*!< in: line where created */
135
/*****************************************************************//**
136
NOTE: Use the corresponding macro instead of this function. Frees the space
137
occupied by a memory heap. In the debug version erases the heap memory
143
mem_heap_t* heap, /*!< in, own: heap to be freed */
144
const char* file_name, /*!< in: file name where freed */
145
ulint line); /*!< in: line where freed */
146
/***************************************************************//**
147
Allocates and zero-fills n bytes of memory from a memory heap.
148
@return allocated, zero-filled storage */
153
mem_heap_t* heap, /*!< in: memory heap */
154
ulint n); /*!< in: number of bytes; if the heap is allowed
155
to grow into the buffer pool, this must be
156
<= MEM_MAX_ALLOC_IN_BUF */
157
/***************************************************************//**
158
Allocates n bytes of memory from a memory heap.
159
@return allocated storage, NULL if did not succeed (only possible for
160
MEM_HEAP_BTR_SEARCH type heaps) */
165
mem_heap_t* heap, /*!< in: memory heap */
166
ulint n); /*!< in: number of bytes; if the heap is allowed
167
to grow into the buffer pool, this must be
168
<= MEM_MAX_ALLOC_IN_BUF */
169
/*****************************************************************//**
170
Returns a pointer to the heap top.
171
@return pointer to the heap top */
174
mem_heap_get_heap_top(
175
/*==================*/
176
mem_heap_t* heap); /*!< in: memory heap */
177
/*****************************************************************//**
178
Frees the space in a memory heap exceeding the pointer given. The
179
pointer must have been acquired from mem_heap_get_heap_top. The first
180
memory block of the heap is not freed. */
183
mem_heap_free_heap_top(
184
/*===================*/
185
mem_heap_t* heap, /*!< in: heap from which to free */
186
byte* old_top);/*!< in: pointer to old top of heap */
187
/*****************************************************************//**
188
Empties a memory heap. The first memory block of the heap is not freed. */
193
mem_heap_t* heap); /*!< in: heap to empty */
194
/*****************************************************************//**
195
Returns a pointer to the topmost element in a memory heap.
196
The size of the element must be given.
197
@return pointer to the topmost element */
202
mem_heap_t* heap, /*!< in: memory heap */
203
ulint n); /*!< in: size of the topmost element */
204
/*****************************************************************//**
205
Frees the topmost element in a memory heap.
206
The size of the element must be given. */
211
mem_heap_t* heap, /*!< in: memory heap */
212
ulint n); /*!< in: size of the topmost element */
213
/*****************************************************************//**
214
Returns the space in bytes occupied by a memory heap. */
219
mem_heap_t* heap); /*!< in: heap */
220
/**************************************************************//**
221
Use this macro instead of the corresponding function!
222
Macro for memory buffer allocation */
224
#define mem_zalloc(N) memset(mem_alloc(N), 0, (N));
226
#define mem_alloc(N) mem_alloc_func((N), NULL, __FILE__, __LINE__)
227
#define mem_alloc2(N,S) mem_alloc_func((N), (S), __FILE__, __LINE__)
228
/***************************************************************//**
229
NOTE: Use the corresponding macro instead of this function.
230
Allocates a single buffer of memory from the dynamic memory of
231
the C compiler. Is like malloc of C. The buffer must be freed
233
@return own: free storage */
238
ulint n, /*!< in: requested size in bytes */
239
ulint* size, /*!< out: allocated size in bytes,
241
const char* file_name, /*!< in: file name where created */
242
ulint line); /*!< in: line where created */
244
/**************************************************************//**
245
Use this macro instead of the corresponding function!
246
Macro for memory buffer freeing */
248
#define mem_free(PTR) mem_free_func((PTR), __FILE__, __LINE__)
249
/***************************************************************//**
250
NOTE: Use the corresponding macro instead of this function.
251
Frees a single buffer of storage from
252
the dynamic memory of C compiler. Similar to free of C. */
257
void* ptr, /*!< in, own: buffer to be freed */
258
const char* file_name, /*!< in: file name where created */
259
ulint line); /*!< in: line where created */
261
/**********************************************************************//**
262
Duplicates a NUL-terminated string.
263
@return own: a copy of the string, must be deallocated with mem_free */
268
const char* str); /*!< in: string to be copied */
269
/**********************************************************************//**
270
Makes a NUL-terminated copy of a nonterminated string.
271
@return own: a copy of the string, must be deallocated with mem_free */
276
const char* str, /*!< in: string to be copied */
277
ulint len); /*!< in: length of str, in bytes */
279
/**********************************************************************//**
280
Duplicates a NUL-terminated string, allocated from a memory heap.
281
@return own: a copy of the string */
286
mem_heap_t* heap, /*!< in: memory heap where string is allocated */
287
const char* str); /*!< in: string to be copied */
288
/**********************************************************************//**
289
Makes a NUL-terminated copy of a nonterminated string,
290
allocated from a memory heap.
291
@return own: a copy of the string */
296
mem_heap_t* heap, /*!< in: memory heap where string is allocated */
297
const char* str, /*!< in: string to be copied */
298
ulint len); /*!< in: length of str, in bytes */
300
/**********************************************************************//**
301
Concatenate two strings and return the result, using a memory heap.
302
@return own: the result */
307
mem_heap_t* heap, /*!< in: memory heap where string is allocated */
308
const char* s1, /*!< in: string 1 */
309
const char* s2); /*!< in: string 2 */
311
/**********************************************************************//**
312
Duplicate a block of data, allocated from a memory heap.
313
@return own: a copy of the data */
318
mem_heap_t* heap, /*!< in: memory heap where copy is allocated */
319
const void* data, /*!< in: data to be copied */
320
ulint len); /*!< in: length of data, in bytes */
322
/****************************************************************//**
323
A simple (s)printf replacement that dynamically allocates the space for the
324
formatted string from the given heap. This supports a very limited set of
325
the printf syntax: types 's' and 'u' and length modifier 'l' (which is
326
required for the 'u' type).
327
@return heap-allocated formatted string */
332
mem_heap_t* heap, /*!< in: memory heap */
333
const char* format, /*!< in: format string */
334
...) __attribute__ ((format (printf, 2, 3)));
336
#ifdef MEM_PERIODIC_CHECK
337
/******************************************************************//**
338
Goes through the list of all allocated mem blocks, checks their magic
339
numbers, and reports possible corruption. */
342
mem_validate_all_blocks(void);
343
/*=========================*/
346
/*#######################################################################*/
348
/* The info header of a block in a memory heap */
350
struct mem_block_info_struct {
351
ulint magic_n;/* magic number for debugging */
352
char file_name[8];/* file name where the mem heap was created */
353
ulint line; /*!< line number where the mem heap was created */
354
UT_LIST_BASE_NODE_T(mem_block_t) base; /* In the first block in the
355
the list this is the base node of the list of blocks;
356
in subsequent blocks this is undefined */
357
UT_LIST_NODE_T(mem_block_t) list; /* This contains pointers to next
358
and prev in the list. The first block allocated
359
to the heap is also the first block in this list,
360
though it also contains the base node of the list. */
361
ulint len; /*!< physical length of this block in bytes */
362
ulint total_size; /*!< physical length in bytes of all blocks
363
in the heap. This is defined only in the base
364
node and is set to ULINT_UNDEFINED in others. */
365
ulint type; /*!< type of heap: MEM_HEAP_DYNAMIC, or
366
MEM_HEAP_BUF possibly ORed to MEM_HEAP_BTR_SEARCH */
367
ulint free; /*!< offset in bytes of the first free position for
368
user data in the block */
369
ulint start; /*!< the value of the struct field 'free' at the
370
creation of the block */
371
#ifndef UNIV_HOTBACKUP
373
/* if the MEM_HEAP_BTR_SEARCH bit is set in type,
374
and this is the heap root, this can contain an
375
allocated buffer frame, which can be appended as a
376
free block to the heap, if we need more space;
377
otherwise, this is NULL */
379
/* if this block has been allocated from the buffer
380
pool, this contains the buf_block_t handle;
381
otherwise, this is NULL */
382
#endif /* !UNIV_HOTBACKUP */
383
#ifdef MEM_PERIODIC_CHECK
384
UT_LIST_NODE_T(mem_block_t) mem_block_list;
385
/* List of all mem blocks allocated; protected
386
by the mem_comm_pool mutex */
390
#define MEM_BLOCK_MAGIC_N 764741555
391
#define MEM_FREED_BLOCK_MAGIC_N 547711122
393
/* Header size for a memory heap block */
394
#define MEM_BLOCK_HEADER_SIZE ut_calc_align(sizeof(mem_block_info_t),\
399
#include "mem0mem.ic"