~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Brian Aker
  • Date: 2009-11-18 06:24:48 UTC
  • mfrom: (1220.1.15 staging)
  • Revision ID: brian@gaz-20091118062448-o36lo3yv81sc6u9z
Merge Brian + Stewart

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*****************************************************************************
2
 
 
3
 
Copyright (C) 1994, 2010, Innobase Oy. 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., 51 Franklin
15
 
St, Fifth Floor, Boston, MA 02110-1301 USA
16
 
 
17
 
*****************************************************************************/
18
 
 
19
 
/**************************************************//**
20
 
@file include/mem0mem.h
21
 
The memory management
22
 
 
23
 
Created 6/9/1994 Heikki Tuuri
24
 
*******************************************************/
25
 
 
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
 
/* The info structure stored at the beginning of a heap block */
42
 
typedef struct mem_block_info_struct mem_block_info_t;
43
 
 
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;
47
 
 
48
 
/* A memory heap is a nonempty linear list of memory blocks */
49
 
typedef mem_block_t     mem_heap_t;
50
 
 
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 */
54
 
 
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
63
 
                                        NULL. */
64
 
 
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. */
69
 
 
70
 
#define MEM_BLOCK_START_SIZE            64
71
 
#define MEM_BLOCK_STANDARD_SIZE         \
72
 
        (UNIV_PAGE_SIZE >= 16384 ? 8000 : MEM_MAX_ALLOC_IN_BUF)
73
 
 
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)
77
 
 
78
 
/******************************************************************//**
79
 
Initializes the memory system. */
80
 
UNIV_INTERN
81
 
void
82
 
mem_init(
83
 
/*=====*/
84
 
        ulint   size);  /*!< in: common pool size in bytes */
85
 
/******************************************************************//**
86
 
Closes the memory system. */
87
 
UNIV_INTERN
88
 
void
89
 
mem_close(void);
90
 
/*===========*/
91
 
 
92
 
/**************************************************************//**
93
 
Use this macro instead of the corresponding function! Macro for memory
94
 
heap creation. */
95
 
 
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
100
 
heap creation. */
101
 
 
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
106
 
heap creation. */
107
 
 
108
 
#define mem_heap_create_in_btr_search(N)        mem_heap_create_func(\
109
 
                (N), MEM_HEAP_BTR_SEARCH | MEM_HEAP_BUFFER,\
110
 
                __FILE__, __LINE__)
111
 
 
112
 
/**************************************************************//**
113
 
Use this macro instead of the corresponding function! Macro for memory
114
 
heap freeing. */
115
 
 
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
121
 
arguments.
122
 
@return own: memory heap, NULL if did not succeed (only possible for
123
 
MEM_HEAP_BTR_SEARCH type heaps) */
124
 
UNIV_INLINE
125
 
mem_heap_t*
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
138
 
blocks. */
139
 
UNIV_INLINE
140
 
void
141
 
mem_heap_free_func(
142
 
/*===============*/
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 */
149
 
UNIV_INLINE
150
 
void*
151
 
mem_heap_zalloc(
152
 
/*============*/
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) */
161
 
UNIV_INLINE
162
 
void*
163
 
mem_heap_alloc(
164
 
/*===========*/
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 */
172
 
UNIV_INLINE
173
 
byte*
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. */
181
 
UNIV_INLINE
182
 
void
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. */
189
 
UNIV_INLINE
190
 
void
191
 
mem_heap_empty(
192
 
/*===========*/
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 */
198
 
UNIV_INLINE
199
 
void*
200
 
mem_heap_get_top(
201
 
/*=============*/
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. */
207
 
UNIV_INLINE
208
 
void
209
 
mem_heap_free_top(
210
 
/*==============*/
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. */
215
 
UNIV_INLINE
216
 
ulint
217
 
mem_heap_get_size(
218
 
/*==============*/
219
 
        mem_heap_t*     heap);          /*!< in: heap */
220
 
/**************************************************************//**
221
 
Use this macro instead of the corresponding function!
222
 
Macro for memory buffer allocation */
223
 
 
224
 
#define mem_zalloc(N)   memset(mem_alloc(N), 0, (N));
225
 
 
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
232
 
with mem_free.
233
 
@return own: free storage */
234
 
UNIV_INLINE
235
 
void *
236
 
mem_alloc_func(
237
 
/*===========*/
238
 
        ulint           n,              /*!< in: requested size in bytes */
239
 
        ulint*          size,           /*!< out: allocated size in bytes,
240
 
                                        or NULL */
241
 
        const char*     file_name,      /*!< in: file name where created */
242
 
        ulint           line);          /*!< in: line where created */
243
 
 
244
 
/**************************************************************//**
245
 
Use this macro instead of the corresponding function!
246
 
Macro for memory buffer freeing */
247
 
 
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. */
253
 
UNIV_INLINE
254
 
void
255
 
mem_free_func(
256
 
/*==========*/
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 */
260
 
 
261
 
/**********************************************************************//**
262
 
Duplicates a NUL-terminated string.
263
 
@return own: a copy of the string, must be deallocated with mem_free */
264
 
UNIV_INLINE
265
 
char*
266
 
mem_strdup(
267
 
/*=======*/
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 */
272
 
UNIV_INLINE
273
 
char*
274
 
mem_strdupl(
275
 
/*========*/
276
 
        const char*     str,    /*!< in: string to be copied */
277
 
        ulint           len);   /*!< in: length of str, in bytes */
278
 
 
279
 
/**********************************************************************//**
280
 
Duplicates a NUL-terminated string, allocated from a memory heap.
281
 
@return own: a copy of the string */
282
 
UNIV_INTERN
283
 
char*
284
 
mem_heap_strdup(
285
 
/*============*/
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 */
292
 
UNIV_INLINE
293
 
char*
294
 
mem_heap_strdupl(
295
 
/*=============*/
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 */
299
 
 
300
 
/**********************************************************************//**
301
 
Concatenate two strings and return the result, using a memory heap.
302
 
@return own: the result */
303
 
UNIV_INTERN
304
 
char*
305
 
mem_heap_strcat(
306
 
/*============*/
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 */
310
 
 
311
 
/**********************************************************************//**
312
 
Duplicate a block of data, allocated from a memory heap.
313
 
@return own: a copy of the data */
314
 
UNIV_INTERN
315
 
void*
316
 
mem_heap_dup(
317
 
/*=========*/
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 */
321
 
 
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 */
328
 
UNIV_INTERN
329
 
char*
330
 
mem_heap_printf(
331
 
/*============*/
332
 
        mem_heap_t*     heap,   /*!< in: memory heap */
333
 
        const char*     format, /*!< in: format string */
334
 
        ...) __attribute__ ((format (printf, 2, 3)));
335
 
 
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. */
340
 
UNIV_INTERN
341
 
void
342
 
mem_validate_all_blocks(void);
343
 
/*=========================*/
344
 
#endif
345
 
 
346
 
/*#######################################################################*/
347
 
 
348
 
/* The info header of a block in a memory heap */
349
 
 
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
372
 
        void*   free_block;
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 */
378
 
        void*   buf_block;
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 */
387
 
#endif
388
 
};
389
 
 
390
 
#define MEM_BLOCK_MAGIC_N       764741555
391
 
#define MEM_FREED_BLOCK_MAGIC_N 547711122
392
 
 
393
 
/* Header size for a memory heap block */
394
 
#define MEM_BLOCK_HEADER_SIZE   ut_calc_align(sizeof(mem_block_info_t),\
395
 
                                                        UNIV_MEM_ALIGNMENT)
396
 
#include "mem0dbg.h"
397
 
 
398
 
#ifndef UNIV_NONINL
399
 
#include "mem0mem.ic"
400
 
#endif
401
 
 
402
 
#endif