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 \
54
(UNIV_PAGE_SIZE >= 16384 ? 8000 : MEM_MAX_ALLOC_IN_BUF)
56
/* If a memory heap is allowed to grow into the buffer pool, the following
57
is the maximum size for a single allocated buffer: */
58
#define MEM_MAX_ALLOC_IN_BUF (UNIV_PAGE_SIZE - 200)
60
/**********************************************************************
61
Initializes the memory system. */
66
ulint size); /* in: common pool size in bytes */
67
/******************************************************************
68
Use this macro instead of the corresponding function! Macro for memory
71
#define mem_heap_create(N) mem_heap_create_func(\
72
(N), MEM_HEAP_DYNAMIC, __FILE__, __LINE__)
73
/******************************************************************
74
Use this macro instead of the corresponding function! Macro for memory
77
#define mem_heap_create_in_buffer(N) mem_heap_create_func(\
78
(N), MEM_HEAP_BUFFER, __FILE__, __LINE__)
79
/******************************************************************
80
Use this macro instead of the corresponding function! Macro for memory
83
#define mem_heap_create_in_btr_search(N) mem_heap_create_func(\
84
(N), MEM_HEAP_BTR_SEARCH | MEM_HEAP_BUFFER,\
87
/******************************************************************
88
Use this macro instead of the corresponding function! Macro for memory
91
#define mem_heap_free(heap) mem_heap_free_func(\
92
(heap), __FILE__, __LINE__)
93
/*********************************************************************
94
NOTE: Use the corresponding macros instead of this function. Creates a
95
memory heap. For debugging purposes, takes also the file name and line as
100
/*=================*/
101
/* out, own: memory heap, NULL if
102
did not succeed (only possible for
103
MEM_HEAP_BTR_SEARCH type heaps)*/
104
ulint n, /* in: desired start block size,
105
this means that a single user buffer
106
of size n will fit in the block,
107
0 creates a default size block */
108
ulint type, /* in: heap type */
109
const char* file_name, /* in: file name where created */
110
ulint line); /* in: line where created */
111
/*********************************************************************
112
NOTE: Use the corresponding macro instead of this function. Frees the space
113
occupied by a memory heap. In the debug version erases the heap memory
119
mem_heap_t* heap, /* in, own: heap to be freed */
120
const char* file_name, /* in: file name where freed */
121
ulint line); /* in: line where freed */
122
/*******************************************************************
123
Allocates and zero-fills n bytes of memory from a memory heap. */
128
/* out: allocated, zero-filled storage */
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
Allocates n bytes of memory from a memory heap. */
139
/* out: allocated storage, NULL if did not
140
succeed (only possible for
141
MEM_HEAP_BTR_SEARCH type heaps) */
142
mem_heap_t* heap, /* in: memory heap */
143
ulint n); /* in: number of bytes; if the heap is allowed
144
to grow into the buffer pool, this must be
145
<= MEM_MAX_ALLOC_IN_BUF */
146
/*********************************************************************
147
Returns a pointer to the heap top. */
150
mem_heap_get_heap_top(
151
/*==================*/
152
/* out: pointer to the heap top */
153
mem_heap_t* heap); /* in: memory heap */
154
/*********************************************************************
155
Frees the space in a memory heap exceeding the pointer given. The
156
pointer must have been acquired from mem_heap_get_heap_top. The first
157
memory block of the heap is not freed. */
160
mem_heap_free_heap_top(
161
/*===================*/
162
mem_heap_t* heap, /* in: heap from which to free */
163
byte* old_top);/* in: pointer to old top of heap */
164
/*********************************************************************
165
Empties a memory heap. The first memory block of the heap is not freed. */
170
mem_heap_t* heap); /* in: heap to empty */
171
/*********************************************************************
172
Returns a pointer to the topmost element in a memory heap.
173
The size of the element must be given. */
178
/* out: pointer to the topmost element */
179
mem_heap_t* heap, /* in: memory heap */
180
ulint n); /* in: size of the topmost element */
181
/*********************************************************************
182
Frees the topmost element in a memory heap.
183
The size of the element must be given. */
188
mem_heap_t* heap, /* in: memory heap */
189
ulint n); /* in: size of the topmost element */
190
/*********************************************************************
191
Returns the space in bytes occupied by a memory heap. */
196
mem_heap_t* heap); /* in: heap */
197
/******************************************************************
198
Use this macro instead of the corresponding function!
199
Macro for memory buffer allocation */
201
#define mem_zalloc(N) memset(mem_alloc(N), 0, (N));
203
#define mem_alloc(N) mem_alloc_func((N), NULL, __FILE__, __LINE__)
204
#define mem_alloc2(N,S) mem_alloc_func((N), (S), __FILE__, __LINE__)
205
/*******************************************************************
206
NOTE: Use the corresponding macro instead of this function.
207
Allocates a single buffer of memory from the dynamic memory of
208
the C compiler. Is like malloc of C. The buffer must be freed
214
/* out, own: free storage */
215
ulint n, /* in: requested size in bytes */
216
ulint* size, /* out: allocated size in bytes,
218
const char* file_name, /* in: file name where created */
219
ulint line); /* in: line where created */
221
/******************************************************************
222
Use this macro instead of the corresponding function!
223
Macro for memory buffer freeing */
225
#define mem_free(PTR) mem_free_func((PTR), __FILE__, __LINE__)
226
/*******************************************************************
227
NOTE: Use the corresponding macro instead of this function.
228
Frees a single buffer of storage from
229
the dynamic memory of C compiler. Similar to free of C. */
234
void* ptr, /* in, own: buffer to be freed */
235
const char* file_name, /* in: file name where created */
236
ulint line /* in: line where created */
239
/**************************************************************************
240
Duplicates a NUL-terminated string. */
245
/* out, own: a copy of the string,
246
must be deallocated with mem_free */
247
const char* str); /* in: string to be copied */
248
/**************************************************************************
249
Makes a NUL-terminated copy of a nonterminated string. */
254
/* out, own: a copy of the string,
255
must be deallocated with mem_free */
256
const char* str, /* in: string to be copied */
257
ulint len); /* in: length of str, in bytes */
259
/**************************************************************************
260
Duplicates a NUL-terminated string, allocated from a memory heap. */
265
/* out, own: a copy of the string */
266
mem_heap_t* heap, /* in: memory heap where string is allocated */
267
const char* str); /* in: string to be copied */
268
/**************************************************************************
269
Makes a NUL-terminated copy of a nonterminated string,
270
allocated from a memory heap. */
275
/* out, own: a copy of the string */
276
mem_heap_t* heap, /* in: memory heap where string is allocated */
277
const char* str, /* in: string to be copied */
278
ulint len); /* in: length of str, in bytes */
280
/**************************************************************************
281
Concatenate two strings and return the result, using a memory heap. */
286
/* out, own: the result */
287
mem_heap_t* heap, /* in: memory heap where string is allocated */
288
const char* s1, /* in: string 1 */
289
const char* s2); /* in: string 2 */
291
/**************************************************************************
292
Duplicate a block of data, allocated from a memory heap. */
297
/* out, own: a copy of the data */
298
mem_heap_t* heap, /* in: memory heap where copy is allocated */
299
const void* data, /* in: data to be copied */
300
ulint len); /* in: length of data, in bytes */
302
/**************************************************************************
303
Concatenate two memory blocks and return the result, using a memory heap. */
308
/* out, own: the result */
309
mem_heap_t* heap, /* in: memory heap where result is allocated */
310
const void* b1, /* in: block 1 */
311
ulint len1, /* in: length of b1, in bytes */
312
const void* b2, /* in: block 2 */
313
ulint len2); /* in: length of b2, in bytes */
315
/********************************************************************
316
A simple (s)printf replacement that dynamically allocates the space for the
317
formatted string from the given heap. This supports a very limited set of
318
the printf syntax: types 's' and 'u' and length modifier 'l' (which is
319
required for the 'u' type). */
324
/* out: heap-allocated formatted string */
325
mem_heap_t* heap, /* in: memory heap */
326
const char* format, /* in: format string */
327
...) __attribute__ ((format (printf, 2, 3)));
329
#ifdef MEM_PERIODIC_CHECK
330
/**********************************************************************
331
Goes through the list of all allocated mem blocks, checks their magic
332
numbers, and reports possible corruption. */
335
mem_validate_all_blocks(void);
336
/*=========================*/
339
/*#######################################################################*/
341
/* The info header of a block in a memory heap */
343
struct mem_block_info_struct {
344
ulint magic_n;/* magic number for debugging */
345
char file_name[8];/* file name where the mem heap was created */
346
ulint line; /* line number where the mem heap was created */
347
UT_LIST_BASE_NODE_T(mem_block_t) base; /* In the first block in the
348
the list this is the base node of the list of blocks;
349
in subsequent blocks this is undefined */
350
UT_LIST_NODE_T(mem_block_t) list; /* This contains pointers to next
351
and prev in the list. The first block allocated
352
to the heap is also the first block in this list,
353
though it also contains the base node of the list. */
354
ulint len; /* physical length of this block in bytes */
355
ulint type; /* type of heap: MEM_HEAP_DYNAMIC, or
356
MEM_HEAP_BUF possibly ORed to MEM_HEAP_BTR_SEARCH */
357
ulint free; /* offset in bytes of the first free position for
358
user data in the block */
359
ulint start; /* the value of the struct field 'free' at the
360
creation of the block */
362
/* if the MEM_HEAP_BTR_SEARCH bit is set in type,
363
and this is the heap root, this can contain an
364
allocated buffer frame, which can be appended as a
365
free block to the heap, if we need more space;
366
otherwise, this is NULL */
368
/* if this block has been allocated from the buffer
369
pool, this contains the buf_block_t handle;
370
otherwise, this is NULL */
371
#ifdef MEM_PERIODIC_CHECK
372
UT_LIST_NODE_T(mem_block_t) mem_block_list;
373
/* List of all mem blocks allocated; protected
374
by the mem_comm_pool mutex */
378
#define MEM_BLOCK_MAGIC_N 764741555
379
#define MEM_FREED_BLOCK_MAGIC_N 547711122
381
/* Header size for a memory heap block */
382
#define MEM_BLOCK_HEADER_SIZE ut_calc_align(sizeof(mem_block_info_t),\
387
#include "mem0mem.ic"