1
/******************************************************
4
(c) 1994, 1995 Innobase Oy
6
Created 6/9/1994 Heikki Tuuri
7
*******************************************************/
17
#include "sync0sync.h"
19
#include "mach0data.h"
21
/* -------------------- MEMORY HEAPS ----------------------------- */
23
/* The info structure stored at the beginning of a heap block */
24
typedef struct mem_block_info_struct mem_block_info_t;
26
/* A block of a memory heap consists of the info structure
27
followed by an area of memory */
28
typedef mem_block_info_t mem_block_t;
30
/* A memory heap is a nonempty linear list of memory blocks */
31
typedef mem_block_t mem_heap_t;
33
/* Types of allocation for memory heaps: DYNAMIC means allocation from the
34
dynamic memory pool of the C compiler, BUFFER means allocation from the
35
buffer pool; the latter method is used for very big heaps */
37
#define MEM_HEAP_DYNAMIC 0 /* the most common type */
38
#define MEM_HEAP_BUFFER 1
39
#define MEM_HEAP_BTR_SEARCH 2 /* this flag can optionally be
40
ORed to MEM_HEAP_BUFFER, in which
41
case heap->free_block is used in
42
some cases for memory allocations,
43
and if it's NULL, the memory
44
allocation functions can return
47
/* The following start size is used for the first block in the memory heap if
48
the size is not specified, i.e., 0 is given as the parameter in the call of
49
create. The standard size is the maximum (payload) size of the blocks used for
50
allocations of small buffers. */
52
#define MEM_BLOCK_START_SIZE 64
53
#define MEM_BLOCK_STANDARD_SIZE 8000
55
/* If a memory heap is allowed to grow into the buffer pool, the following
56
is the maximum size for a single allocated buffer: */
57
#define MEM_MAX_ALLOC_IN_BUF (UNIV_PAGE_SIZE - 200)
59
/**********************************************************************
60
Initializes the memory system. */
65
ulint size); /* in: common pool size in bytes */
66
/******************************************************************
67
Use this macro instead of the corresponding function! Macro for memory
70
#define mem_heap_create(N) mem_heap_create_func(\
71
(N), NULL, MEM_HEAP_DYNAMIC, __FILE__, __LINE__)
72
/******************************************************************
73
Use this macro instead of the corresponding function! Macro for memory
76
#define mem_heap_create_in_buffer(N) mem_heap_create_func(\
77
(N), NULL, MEM_HEAP_BUFFER, __FILE__, __LINE__)
78
/******************************************************************
79
Use this macro instead of the corresponding function! Macro for memory
82
#define mem_heap_create_in_btr_search(N) mem_heap_create_func(\
83
(N), NULL, MEM_HEAP_BTR_SEARCH | MEM_HEAP_BUFFER,\
85
/******************************************************************
86
Use this macro instead of the corresponding function! Macro for fast
87
memory heap creation. An initial block of memory B is given by the
88
caller, N is its size, and this memory block is not freed by
89
mem_heap_free. See the parameter comment in mem_heap_create_func below. */
91
#define mem_heap_fast_create(N, B) mem_heap_create_func(\
92
(N), (B), MEM_HEAP_DYNAMIC, __FILE__, __LINE__)
94
/******************************************************************
95
Use this macro instead of the corresponding function! Macro for memory
98
#define mem_heap_free(heap) mem_heap_free_func(\
99
(heap), __FILE__, __LINE__)
100
/*********************************************************************
101
NOTE: Use the corresponding macros instead of this function. Creates a
102
memory heap. For debugging purposes, takes also the file name and line as
106
mem_heap_create_func(
107
/*=================*/
108
/* out, own: memory heap, NULL if
109
did not succeed (only possible for
110
MEM_HEAP_BTR_SEARCH type heaps)*/
111
ulint n, /* in: desired start block size,
112
this means that a single user buffer
113
of size n will fit in the block,
114
0 creates a default size block;
115
if init_block is not NULL, n tells
117
void* init_block, /* in: if very fast creation is
118
wanted, the caller can reserve some
119
memory from its stack, for example,
120
and pass it as the the initial block
121
to the heap: then no OS call of malloc
122
is needed at the creation. CAUTION:
123
the caller must make sure the initial
124
block is not unintentionally erased
125
(if allocated in the stack), before
126
the memory heap is explicitly freed. */
127
ulint type, /* in: heap type */
128
const char* file_name, /* in: file name where created */
129
ulint line); /* in: line where created */
130
/*********************************************************************
131
NOTE: Use the corresponding macro instead of this function. Frees the space
132
occupied by a memory heap. In the debug version erases the heap memory
138
mem_heap_t* heap, /* in, own: heap to be freed */
139
const char* file_name, /* in: file name where freed */
140
ulint line); /* in: line where freed */
141
/*******************************************************************
142
Allocates n bytes of memory from a memory heap. */
147
/* out: allocated storage, NULL if did not
148
succeed (only possible for
149
MEM_HEAP_BTR_SEARCH type heaps) */
150
mem_heap_t* heap, /* in: memory heap */
151
ulint n); /* in: number of bytes; if the heap is allowed
152
to grow into the buffer pool, this must be
153
<= MEM_MAX_ALLOC_IN_BUF */
154
/*********************************************************************
155
Returns a pointer to the heap top. */
158
mem_heap_get_heap_top(
159
/*==================*/
160
/* out: pointer to the heap top */
161
mem_heap_t* heap); /* in: memory heap */
162
/*********************************************************************
163
Frees the space in a memory heap exceeding the pointer given. The
164
pointer must have been acquired from mem_heap_get_heap_top. The first
165
memory block of the heap is not freed. */
168
mem_heap_free_heap_top(
169
/*===================*/
170
mem_heap_t* heap, /* in: heap from which to free */
171
byte* old_top);/* in: pointer to old top of heap */
172
/*********************************************************************
173
Empties a memory heap. The first memory block of the heap is not freed. */
178
mem_heap_t* heap); /* in: heap to empty */
179
/*********************************************************************
180
Returns a pointer to the topmost element in a memory heap.
181
The size of the element must be given. */
186
/* out: pointer to the topmost element */
187
mem_heap_t* heap, /* in: memory heap */
188
ulint n); /* in: size of the topmost element */
189
/*********************************************************************
190
Frees the topmost element in a memory heap.
191
The size of the element must be given. */
196
mem_heap_t* heap, /* in: memory heap */
197
ulint n); /* in: size of the topmost element */
198
/*********************************************************************
199
Returns the space in bytes occupied by a memory heap. */
204
mem_heap_t* heap); /* in: heap */
205
/******************************************************************
206
Use this macro instead of the corresponding function!
207
Macro for memory buffer allocation */
209
#define mem_alloc(N) mem_alloc_func((N), __FILE__, __LINE__)
210
/******************************************************************
211
Use this macro instead of the corresponding function!
212
Macro for memory buffer allocation */
214
#define mem_alloc_noninline(N) mem_alloc_func_noninline(\
215
(N), __FILE__, __LINE__)
216
/*******************************************************************
217
NOTE: Use the corresponding macro instead of this function.
218
Allocates a single buffer of memory from the dynamic memory of
219
the C compiler. Is like malloc of C. The buffer must be freed
225
/* out, own: free storage */
226
ulint n, /* in: desired number of bytes */
227
const char* file_name, /* in: file name where created */
228
ulint line /* in: line where created */
230
/*******************************************************************
231
NOTE: Use the corresponding macro instead of this function.
232
Allocates a single buffer of memory from the dynamic memory of
233
the C compiler. Is like malloc of C. The buffer must be freed
237
mem_alloc_func_noninline(
238
/*=====================*/
239
/* out, own: free storage */
240
ulint n, /* in: desired number of 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 */
262
/**************************************************************************
263
Duplicates a NUL-terminated string. */
268
/* out, own: a copy of the string,
269
must be deallocated with mem_free */
270
const char* str); /* in: string to be copied */
271
/**************************************************************************
272
Makes a NUL-terminated copy of a nonterminated string. */
277
/* out, own: a copy of the string,
278
must be deallocated with mem_free */
279
const char* str, /* in: string to be copied */
280
ulint len); /* in: length of str, in bytes */
282
/**************************************************************************
283
Duplicates a NUL-terminated string, allocated from a memory heap. */
288
/* out, own: a copy of the string */
289
mem_heap_t* heap, /* in: memory heap where string is allocated */
290
const char* str); /* in: string to be copied */
291
/**************************************************************************
292
Makes a NUL-terminated copy of a nonterminated string,
293
allocated from a memory heap. */
298
/* out, own: a copy of the string */
299
mem_heap_t* heap, /* in: memory heap where string is allocated */
300
const char* str, /* in: string to be copied */
301
ulint len); /* in: length of str, in bytes */
303
/**************************************************************************
304
Concatenate two strings and return the result, using a memory heap. */
309
/* out, own: the result */
310
mem_heap_t* heap, /* in: memory heap where string is allocated */
311
const char* s1, /* in: string 1 */
312
const char* s2); /* in: string 2 */
314
/**************************************************************************
315
Duplicate a block of data, allocated from a memory heap. */
320
/* out, own: a copy of the data */
321
mem_heap_t* heap, /* in: memory heap where copy is allocated */
322
const void* data, /* in: data to be copied */
323
ulint len); /* in: length of data, in bytes */
325
/**************************************************************************
326
Concatenate two memory blocks and return the result, using a memory heap. */
331
/* out, own: the result */
332
mem_heap_t* heap, /* in: memory heap where result is allocated */
333
const void* b1, /* in: block 1 */
334
ulint len1, /* in: length of b1, in bytes */
335
const void* b2, /* in: block 2 */
336
ulint len2); /* in: length of b2, in bytes */
338
/********************************************************************
339
A simple (s)printf replacement that dynamically allocates the space for the
340
formatted string from the given heap. This supports a very limited set of
341
the printf syntax: types 's' and 'u' and length modifier 'l' (which is
342
required for the 'u' type). */
347
/* out: heap-allocated formatted string */
348
mem_heap_t* heap, /* in: memory heap */
349
const char* format, /* in: format string */
350
...) __attribute__ ((format (printf, 2, 3)));
352
#ifdef MEM_PERIODIC_CHECK
353
/**********************************************************************
354
Goes through the list of all allocated mem blocks, checks their magic
355
numbers, and reports possible corruption. */
358
mem_validate_all_blocks(void);
359
/*=========================*/
362
/*#######################################################################*/
364
/* The info header of a block in a memory heap */
366
struct mem_block_info_struct {
367
ulint magic_n;/* magic number for debugging */
368
char file_name[8];/* file name where the mem heap was created */
369
ulint line; /* line number where the mem heap was created */
370
UT_LIST_BASE_NODE_T(mem_block_t) base; /* In the first block in the
371
the list this is the base node of the list of blocks;
372
in subsequent blocks this is undefined */
373
UT_LIST_NODE_T(mem_block_t) list; /* This contains pointers to next
374
and prev in the list. The first block allocated
375
to the heap is also the first block in this list,
376
though it also contains the base node of the list. */
377
ulint len; /* physical length of this block in bytes */
378
ulint type; /* type of heap: MEM_HEAP_DYNAMIC, or
379
MEM_HEAP_BUF possibly ORed to MEM_HEAP_BTR_SEARCH */
380
ibool init_block; /* TRUE if this is the first block used in fast
381
creation of a heap: the memory will be freed
382
by the creator, not by mem_heap_free */
383
ulint free; /* offset in bytes of the first free position for
384
user data in the block */
385
ulint start; /* the value of the struct field 'free' at the
386
creation of the block */
388
/* if the MEM_HEAP_BTR_SEARCH bit is set in type,
389
and this is the heap root, this can contain an
390
allocated buffer frame, which can be appended as a
391
free block to the heap, if we need more space;
392
otherwise, this is NULL */
393
#ifdef MEM_PERIODIC_CHECK
394
UT_LIST_NODE_T(mem_block_t) mem_block_list;
395
/* List of all mem blocks allocated; protected
396
by the mem_comm_pool mutex */
400
#define MEM_BLOCK_MAGIC_N 764741555
401
#define MEM_FREED_BLOCK_MAGIC_N 547711122
403
/* Header size for a memory heap block */
404
#define MEM_BLOCK_HEADER_SIZE ut_calc_align(sizeof(mem_block_info_t),\
409
#include "mem0mem.ic"