~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Jay Pipes
  • Date: 2009-02-28 17:49:22 UTC
  • mto: (910.2.6 mordred-noatomics)
  • mto: This revision was merged to the branch mainline in revision 908.
  • Revision ID: jpipes@serialcoder-20090228174922-jczgt4d0662fqmnf
Merging in old r902 temporal changes

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
 
1
/******************************************************
21
2
The memory management
22
3
 
 
4
(c) 1994, 1995 Innobase Oy
 
5
 
23
6
Created 6/9/1994 Heikki Tuuri
24
7
*******************************************************/
25
8
 
29
12
#include "univ.i"
30
13
#include "ut0mem.h"
31
14
#include "ut0byte.h"
 
15
#include "ut0ut.h"
32
16
#include "ut0rnd.h"
33
 
#ifndef UNIV_HOTBACKUP
34
 
# include "sync0sync.h"
35
 
#endif /* UNIV_HOTBACKUP */
 
17
#include "sync0sync.h"
36
18
#include "ut0lst.h"
37
19
#include "mach0data.h"
38
20
 
58
40
                                        ORed to MEM_HEAP_BUFFER, in which
59
41
                                        case heap->free_block is used in
60
42
                                        some cases for memory allocations,
61
 
                                        and if NULL, the memory
 
43
                                        and if it's NULL, the memory
62
44
                                        allocation functions can return
63
45
                                        NULL. */
64
46
 
75
57
is the maximum size for a single allocated buffer: */
76
58
#define MEM_MAX_ALLOC_IN_BUF            (UNIV_PAGE_SIZE - 200)
77
59
 
78
 
/******************************************************************//**
 
60
/**********************************************************************
79
61
Initializes the memory system. */
80
62
UNIV_INTERN
81
63
void
82
64
mem_init(
83
65
/*=====*/
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
 
/**************************************************************//**
 
66
        ulint   size);  /* in: common pool size in bytes */
 
67
/******************************************************************
93
68
Use this macro instead of the corresponding function! Macro for memory
94
69
heap creation. */
95
70
 
96
71
#define mem_heap_create(N)      mem_heap_create_func(\
97
72
                (N), MEM_HEAP_DYNAMIC, __FILE__, __LINE__)
98
 
/**************************************************************//**
 
73
/******************************************************************
99
74
Use this macro instead of the corresponding function! Macro for memory
100
75
heap creation. */
101
76
 
102
77
#define mem_heap_create_in_buffer(N)    mem_heap_create_func(\
103
78
                (N), MEM_HEAP_BUFFER, __FILE__, __LINE__)
104
 
/**************************************************************//**
 
79
/******************************************************************
105
80
Use this macro instead of the corresponding function! Macro for memory
106
81
heap creation. */
107
82
 
109
84
                (N), MEM_HEAP_BTR_SEARCH | MEM_HEAP_BUFFER,\
110
85
                __FILE__, __LINE__)
111
86
 
112
 
/**************************************************************//**
 
87
/******************************************************************
113
88
Use this macro instead of the corresponding function! Macro for memory
114
89
heap freeing. */
115
90
 
116
91
#define mem_heap_free(heap) mem_heap_free_func(\
117
92
                                          (heap), __FILE__, __LINE__)
118
 
/*****************************************************************//**
 
93
/*********************************************************************
119
94
NOTE: Use the corresponding macros instead of this function. Creates a
120
95
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) */
 
96
arguments. */
124
97
UNIV_INLINE
125
98
mem_heap_t*
126
99
mem_heap_create_func(
127
100
/*=================*/
128
 
        ulint           n,              /*!< in: desired start block size,
 
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,
129
105
                                        this means that a single user buffer
130
106
                                        of size n will fit in the block,
131
107
                                        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
 
/*****************************************************************//**
 
108
        ulint           type,           /* in: heap type */
 
109
        const char*     file_name,      /* in: file name where created */
 
110
        ulint           line);          /* in: line where created */
 
111
/*********************************************************************
136
112
NOTE: Use the corresponding macro instead of this function. Frees the space
137
113
occupied by a memory heap. In the debug version erases the heap memory
138
114
blocks. */
140
116
void
141
117
mem_heap_free_func(
142
118
/*===============*/
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 */
 
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. */
149
124
UNIV_INLINE
150
125
void*
151
126
mem_heap_zalloc(
152
127
/*============*/
153
 
        mem_heap_t*     heap,   /*!< in: memory heap */
154
 
        ulint           n);     /*!< in: number of bytes; if the heap is allowed
 
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
155
131
                                to grow into the buffer pool, this must be
156
132
                                <= 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) */
 
133
/*******************************************************************
 
134
Allocates n bytes of memory from a memory heap. */
161
135
UNIV_INLINE
162
136
void*
163
137
mem_heap_alloc(
164
138
/*===========*/
165
 
        mem_heap_t*     heap,   /*!< in: memory heap */
166
 
        ulint           n);     /*!< in: number of bytes; if the heap is allowed
 
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
167
144
                                to grow into the buffer pool, this must be
168
145
                                <= MEM_MAX_ALLOC_IN_BUF */
169
 
/*****************************************************************//**
170
 
Returns a pointer to the heap top.
171
 
@return pointer to the heap top */
 
146
/*********************************************************************
 
147
Returns a pointer to the heap top. */
172
148
UNIV_INLINE
173
149
byte*
174
150
mem_heap_get_heap_top(
175
151
/*==================*/
176
 
        mem_heap_t*     heap);  /*!< in: memory heap */
177
 
/*****************************************************************//**
 
152
                                /* out: pointer to the heap top */
 
153
        mem_heap_t*     heap);  /* in: memory heap */
 
154
/*********************************************************************
178
155
Frees the space in a memory heap exceeding the pointer given. The
179
156
pointer must have been acquired from mem_heap_get_heap_top. The first
180
157
memory block of the heap is not freed. */
182
159
void
183
160
mem_heap_free_heap_top(
184
161
/*===================*/
185
 
        mem_heap_t*     heap,   /*!< in: heap from which to free */
186
 
        byte*           old_top);/*!< in: pointer to old top of heap */
187
 
/*****************************************************************//**
 
162
        mem_heap_t*     heap,   /* in: heap from which to free */
 
163
        byte*           old_top);/* in: pointer to old top of heap */
 
164
/*********************************************************************
188
165
Empties a memory heap. The first memory block of the heap is not freed. */
189
166
UNIV_INLINE
190
167
void
191
168
mem_heap_empty(
192
169
/*===========*/
193
 
        mem_heap_t*     heap);  /*!< in: heap to empty */
194
 
/*****************************************************************//**
 
170
        mem_heap_t*     heap);  /* in: heap to empty */
 
171
/*********************************************************************
195
172
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 */
 
173
The size of the element must be given. */
198
174
UNIV_INLINE
199
175
void*
200
176
mem_heap_get_top(
201
177
/*=============*/
202
 
        mem_heap_t*     heap,   /*!< in: memory heap */
203
 
        ulint           n);     /*!< in: size of the topmost element */
204
 
/*****************************************************************//**
 
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
/*********************************************************************
205
182
Frees the topmost element in a memory heap.
206
183
The size of the element must be given. */
207
184
UNIV_INLINE
208
185
void
209
186
mem_heap_free_top(
210
187
/*==============*/
211
 
        mem_heap_t*     heap,   /*!< in: memory heap */
212
 
        ulint           n);     /*!< in: size of the topmost element */
213
 
/*****************************************************************//**
 
188
        mem_heap_t*     heap,   /* in: memory heap */
 
189
        ulint           n);     /* in: size of the topmost element */
 
190
/*********************************************************************
214
191
Returns the space in bytes occupied by a memory heap. */
215
192
UNIV_INLINE
216
193
ulint
217
194
mem_heap_get_size(
218
195
/*==============*/
219
 
        mem_heap_t*     heap);          /*!< in: heap */
220
 
/**************************************************************//**
 
196
        mem_heap_t*     heap);          /* in: heap */
 
197
/******************************************************************
221
198
Use this macro instead of the corresponding function!
222
199
Macro for memory buffer allocation */
223
200
 
225
202
 
226
203
#define mem_alloc(N)    mem_alloc_func((N), NULL, __FILE__, __LINE__)
227
204
#define mem_alloc2(N,S) mem_alloc_func((N), (S), __FILE__, __LINE__)
228
 
/***************************************************************//**
 
205
/*******************************************************************
229
206
NOTE: Use the corresponding macro instead of this function.
230
207
Allocates a single buffer of memory from the dynamic memory of
231
208
the C compiler. Is like malloc of C. The buffer must be freed
232
 
with mem_free.
233
 
@return own: free storage */
 
209
with mem_free. */
234
210
UNIV_INLINE
235
211
void*
236
212
mem_alloc_func(
237
213
/*===========*/
238
 
        ulint           n,              /*!< in: requested size in bytes */
239
 
        ulint*          size,           /*!< out: allocated size in bytes,
 
214
                                        /* out, own: free storage */
 
215
        ulint           n,              /* in: requested size in bytes */
 
216
        ulint*          size,           /* out: allocated size in bytes,
240
217
                                        or NULL */
241
 
        const char*     file_name,      /*!< in: file name where created */
242
 
        ulint           line);          /*!< in: line where created */
 
218
        const char*     file_name,      /* in: file name where created */
 
219
        ulint           line);          /* in: line where created */
243
220
 
244
 
/**************************************************************//**
 
221
/******************************************************************
245
222
Use this macro instead of the corresponding function!
246
223
Macro for memory buffer freeing */
247
224
 
248
225
#define mem_free(PTR)   mem_free_func((PTR), __FILE__, __LINE__)
249
 
/***************************************************************//**
 
226
/*******************************************************************
250
227
NOTE: Use the corresponding macro instead of this function.
251
228
Frees a single buffer of storage from
252
229
the dynamic memory of C compiler. Similar to free of C. */
254
231
void
255
232
mem_free_func(
256
233
/*==========*/
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 */
 
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
);
260
238
 
261
 
/**********************************************************************//**
262
 
Duplicates a NUL-terminated string.
263
 
@return own: a copy of the string, must be deallocated with mem_free */
 
239
/**************************************************************************
 
240
Duplicates a NUL-terminated string. */
264
241
UNIV_INLINE
265
242
char*
266
243
mem_strdup(
267
244
/*=======*/
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 */
 
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. */
272
250
UNIV_INLINE
273
251
char*
274
252
mem_strdupl(
275
253
/*========*/
276
 
        const char*     str,    /*!< in: string to be copied */
277
 
        ulint           len);   /*!< in: length of str, in bytes */
 
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 */
278
258
 
279
 
/**********************************************************************//**
280
 
Duplicates a NUL-terminated string, allocated from a memory heap.
281
 
@return own: a copy of the string */
 
259
/**************************************************************************
 
260
Duplicates a NUL-terminated string, allocated from a memory heap. */
282
261
UNIV_INTERN
283
262
char*
284
263
mem_heap_strdup(
285
264
/*============*/
286
 
        mem_heap_t*     heap,   /*!< in: memory heap where string is allocated */
287
 
        const char*     str);   /*!< in: string to be copied */
288
 
/**********************************************************************//**
 
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
/**************************************************************************
289
269
Makes a NUL-terminated copy of a nonterminated string,
290
 
allocated from a memory heap.
291
 
@return own: a copy of the string */
 
270
allocated from a memory heap. */
292
271
UNIV_INLINE
293
272
char*
294
273
mem_heap_strdupl(
295
274
/*=============*/
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 */
 
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 */
299
279
 
300
 
/**********************************************************************//**
301
 
Concatenate two strings and return the result, using a memory heap.
302
 
@return own: the result */
 
280
/**************************************************************************
 
281
Concatenate two strings and return the result, using a memory heap. */
303
282
UNIV_INTERN
304
283
char*
305
284
mem_heap_strcat(
306
285
/*============*/
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 */
 
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 */
310
290
 
311
 
/**********************************************************************//**
312
 
Duplicate a block of data, allocated from a memory heap.
313
 
@return own: a copy of the data */
 
291
/**************************************************************************
 
292
Duplicate a block of data, allocated from a memory heap. */
314
293
UNIV_INTERN
315
294
void*
316
295
mem_heap_dup(
317
296
/*=========*/
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
 
/****************************************************************//**
 
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
/********************************************************************
323
316
A simple (s)printf replacement that dynamically allocates the space for the
324
317
formatted string from the given heap. This supports a very limited set of
325
318
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 */
 
319
required for the 'u' type). */
328
320
UNIV_INTERN
329
321
char*
330
322
mem_heap_printf(
331
323
/*============*/
332
 
        mem_heap_t*     heap,   /*!< in: memory heap */
333
 
        const char*     format, /*!< in: format string */
 
324
                                /* out: heap-allocated formatted string */
 
325
        mem_heap_t*     heap,   /* in: memory heap */
 
326
        const char*     format, /* in: format string */
334
327
        ...) __attribute__ ((format (printf, 2, 3)));
335
328
 
336
329
#ifdef MEM_PERIODIC_CHECK
337
 
/******************************************************************//**
 
330
/**********************************************************************
338
331
Goes through the list of all allocated mem blocks, checks their magic
339
332
numbers, and reports possible corruption. */
340
333
UNIV_INTERN
350
343
struct mem_block_info_struct {
351
344
        ulint   magic_n;/* magic number for debugging */
352
345
        char    file_name[8];/* file name where the mem heap was created */
353
 
        ulint   line;   /*!< line number where the mem heap was created */
 
346
        ulint   line;   /* line number where the mem heap was created */
354
347
        UT_LIST_BASE_NODE_T(mem_block_t) base; /* In the first block in the
355
348
                        the list this is the base node of the list of blocks;
356
349
                        in subsequent blocks this is undefined */
358
351
                        and prev in the list. The first block allocated
359
352
                        to the heap is also the first block in this list,
360
353
                        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
 
354
        ulint   len;    /* physical length of this block in bytes */
 
355
        ulint   type;   /* type of heap: MEM_HEAP_DYNAMIC, or
366
356
                        MEM_HEAP_BUF possibly ORed to MEM_HEAP_BTR_SEARCH */
367
 
        ulint   free;   /*!< offset in bytes of the first free position for
 
357
        ulint   free;   /* offset in bytes of the first free position for
368
358
                        user data in the block */
369
 
        ulint   start;  /*!< the value of the struct field 'free' at the
 
359
        ulint   start;  /* the value of the struct field 'free' at the
370
360
                        creation of the block */
371
 
#ifndef UNIV_HOTBACKUP
372
361
        void*   free_block;
373
362
                        /* if the MEM_HEAP_BTR_SEARCH bit is set in type,
374
363
                        and this is the heap root, this can contain an
379
368
                        /* if this block has been allocated from the buffer
380
369
                        pool, this contains the buf_block_t handle;
381
370
                        otherwise, this is NULL */
382
 
#endif /* !UNIV_HOTBACKUP */
383
371
#ifdef MEM_PERIODIC_CHECK
384
372
        UT_LIST_NODE_T(mem_block_t) mem_block_list;
385
373
                        /* List of all mem blocks allocated; protected