~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/innobase/include/mem0mem.h

Fix merge issues with 1.0 CC fix.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/******************************************************
2
 
The memory management
3
 
 
4
 
(c) 1994, 1995 Innobase Oy
5
 
 
6
 
Created 6/9/1994 Heikki Tuuri
7
 
*******************************************************/
8
 
 
9
 
#ifndef mem0mem_h
10
 
#define mem0mem_h
11
 
 
12
 
#include "univ.i"
13
 
#include "ut0mem.h"
14
 
#include "ut0byte.h"
15
 
#include "ut0ut.h"
16
 
#include "ut0rnd.h"
17
 
#include "sync0sync.h"
18
 
#include "ut0lst.h"
19
 
#include "mach0data.h"
20
 
 
21
 
/* -------------------- MEMORY HEAPS ----------------------------- */
22
 
 
23
 
/* The info structure stored at the beginning of a heap block */
24
 
typedef struct mem_block_info_struct mem_block_info_t;
25
 
 
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;
29
 
 
30
 
/* A memory heap is a nonempty linear list of memory blocks */
31
 
typedef mem_block_t     mem_heap_t;
32
 
 
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 */
36
 
 
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
45
 
                                        NULL. */
46
 
 
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. */
51
 
 
52
 
#define MEM_BLOCK_START_SIZE            64
53
 
#define MEM_BLOCK_STANDARD_SIZE         \
54
 
        (UNIV_PAGE_SIZE >= 16384 ? 8000 : MEM_MAX_ALLOC_IN_BUF)
55
 
 
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)
59
 
 
60
 
/**********************************************************************
61
 
Initializes the memory system. */
62
 
UNIV_INTERN
63
 
void
64
 
mem_init(
65
 
/*=====*/
66
 
        ulint   size);  /* in: common pool size in bytes */
67
 
/******************************************************************
68
 
Use this macro instead of the corresponding function! Macro for memory
69
 
heap creation. */
70
 
 
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
75
 
heap creation. */
76
 
 
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
81
 
heap creation. */
82
 
 
83
 
#define mem_heap_create_in_btr_search(N)        mem_heap_create_func(\
84
 
                (N), MEM_HEAP_BTR_SEARCH | MEM_HEAP_BUFFER,\
85
 
                __FILE__, __LINE__)
86
 
 
87
 
/******************************************************************
88
 
Use this macro instead of the corresponding function! Macro for memory
89
 
heap freeing. */
90
 
 
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
96
 
arguments. */
97
 
UNIV_INLINE
98
 
mem_heap_t*
99
 
mem_heap_create_func(
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
114
 
blocks. */
115
 
UNIV_INLINE
116
 
void
117
 
mem_heap_free_func(
118
 
/*===============*/
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. */
124
 
UNIV_INLINE
125
 
void*
126
 
mem_heap_zalloc(
127
 
/*============*/
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. */
135
 
UNIV_INLINE
136
 
void*
137
 
mem_heap_alloc(
138
 
/*===========*/
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. */
148
 
UNIV_INLINE
149
 
byte*
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. */
158
 
UNIV_INLINE
159
 
void
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. */
166
 
UNIV_INLINE
167
 
void
168
 
mem_heap_empty(
169
 
/*===========*/
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. */
174
 
UNIV_INLINE
175
 
void*
176
 
mem_heap_get_top(
177
 
/*=============*/
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. */
184
 
UNIV_INLINE
185
 
void
186
 
mem_heap_free_top(
187
 
/*==============*/
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. */
192
 
UNIV_INLINE
193
 
ulint
194
 
mem_heap_get_size(
195
 
/*==============*/
196
 
        mem_heap_t*     heap);          /* in: heap */
197
 
/******************************************************************
198
 
Use this macro instead of the corresponding function!
199
 
Macro for memory buffer allocation */
200
 
 
201
 
#define mem_zalloc(N)   memset(mem_alloc(N), 0, (N));
202
 
 
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
209
 
with mem_free. */
210
 
UNIV_INLINE
211
 
void*
212
 
mem_alloc_func(
213
 
/*===========*/
214
 
                                        /* out, own: free storage */
215
 
        ulint           n,              /* in: requested size in bytes */
216
 
        ulint*          size,           /* out: allocated size in bytes,
217
 
                                        or NULL */
218
 
        const char*     file_name,      /* in: file name where created */
219
 
        ulint           line);          /* in: line where created */
220
 
 
221
 
/******************************************************************
222
 
Use this macro instead of the corresponding function!
223
 
Macro for memory buffer freeing */
224
 
 
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. */
230
 
UNIV_INLINE
231
 
void
232
 
mem_free_func(
233
 
/*==========*/
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 */
237
 
);
238
 
 
239
 
/**************************************************************************
240
 
Duplicates a NUL-terminated string. */
241
 
UNIV_INLINE
242
 
char*
243
 
mem_strdup(
244
 
/*=======*/
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. */
250
 
UNIV_INLINE
251
 
char*
252
 
mem_strdupl(
253
 
/*========*/
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 */
258
 
 
259
 
/**************************************************************************
260
 
Duplicates a NUL-terminated string, allocated from a memory heap. */
261
 
UNIV_INTERN
262
 
char*
263
 
mem_heap_strdup(
264
 
/*============*/
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. */
271
 
UNIV_INLINE
272
 
char*
273
 
mem_heap_strdupl(
274
 
/*=============*/
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 */
279
 
 
280
 
/**************************************************************************
281
 
Concatenate two strings and return the result, using a memory heap. */
282
 
UNIV_INTERN
283
 
char*
284
 
mem_heap_strcat(
285
 
/*============*/
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 */
290
 
 
291
 
/**************************************************************************
292
 
Duplicate a block of data, allocated from a memory heap. */
293
 
UNIV_INTERN
294
 
void*
295
 
mem_heap_dup(
296
 
/*=========*/
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 */
301
 
 
302
 
/**************************************************************************
303
 
Concatenate two memory blocks and return the result, using a memory heap. */
304
 
UNIV_INTERN
305
 
void*
306
 
mem_heap_cat(
307
 
/*=========*/
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 */
314
 
 
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). */
320
 
UNIV_INTERN
321
 
char*
322
 
mem_heap_printf(
323
 
/*============*/
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)));
328
 
 
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. */
333
 
UNIV_INTERN
334
 
void
335
 
mem_validate_all_blocks(void);
336
 
/*=========================*/
337
 
#endif
338
 
 
339
 
/*#######################################################################*/
340
 
 
341
 
/* The info header of a block in a memory heap */
342
 
 
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 */
361
 
        void*   free_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 */
367
 
        void*   buf_block;
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 */
375
 
#endif
376
 
};
377
 
 
378
 
#define MEM_BLOCK_MAGIC_N       764741555
379
 
#define MEM_FREED_BLOCK_MAGIC_N 547711122
380
 
 
381
 
/* Header size for a memory heap block */
382
 
#define MEM_BLOCK_HEADER_SIZE   ut_calc_align(sizeof(mem_block_info_t),\
383
 
                                                        UNIV_MEM_ALIGNMENT)
384
 
#include "mem0dbg.h"
385
 
 
386
 
#ifndef UNIV_NONINL
387
 
#include "mem0mem.ic"
388
 
#endif
389
 
 
390
 
#endif