~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/innobase/mem/mem0mem.c

  • Committer: Monty Taylor
  • Date: 2009-08-12 06:25:19 UTC
  • mto: (1114.1.1 innodb-plugin-merge)
  • mto: This revision was merged to the branch mainline in revision 1183.
  • Revision ID: mordred@inaugust.com-20090812062519-cij02mrrunvnxblt
Tags: innodb-plugin-1.0.4
InnoDB Plugin 1.0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
*****************************************************************************/
18
18
 
19
 
/************************************************************************
 
19
/********************************************************************//**
 
20
@file mem/mem0mem.c
20
21
The memory management
21
22
 
22
23
Created 6/9/1994 Heikki Tuuri
97
98
 
98
99
#endif
99
100
 
100
 
/**************************************************************************
101
 
Duplicates a NUL-terminated string, allocated from a memory heap. */
 
101
/**********************************************************************//**
 
102
Duplicates a NUL-terminated string, allocated from a memory heap.
 
103
@return own: a copy of the string */
102
104
UNIV_INTERN
103
105
char*
104
106
mem_heap_strdup(
105
107
/*============*/
106
 
                                /* out, own: a copy of the string */
107
 
        mem_heap_t*     heap,   /* in: memory heap where string is allocated */
108
 
        const char*     str)    /* in: string to be copied */
 
108
        mem_heap_t*     heap,   /*!< in: memory heap where string is allocated */
 
109
        const char*     str)    /*!< in: string to be copied */
109
110
{
110
111
        return(mem_heap_dup(heap, str, strlen(str) + 1));
111
112
}
112
113
 
113
 
/**************************************************************************
114
 
Duplicate a block of data, allocated from a memory heap. */
 
114
/**********************************************************************//**
 
115
Duplicate a block of data, allocated from a memory heap.
 
116
@return own: a copy of the data */
115
117
UNIV_INTERN
116
118
void*
117
119
mem_heap_dup(
118
120
/*=========*/
119
 
                                /* out, own: a copy of the data */
120
 
        mem_heap_t*     heap,   /* in: memory heap where copy is allocated */
121
 
        const void*     data,   /* in: data to be copied */
122
 
        ulint           len)    /* in: length of data, in bytes */
 
121
        mem_heap_t*     heap,   /*!< in: memory heap where copy is allocated */
 
122
        const void*     data,   /*!< in: data to be copied */
 
123
        ulint           len)    /*!< in: length of data, in bytes */
123
124
{
124
125
        return(memcpy(mem_heap_alloc(heap, len), data, len));
125
126
}
126
127
 
127
 
/**************************************************************************
128
 
Concatenate two memory blocks and return the result, using a memory heap. */
129
 
UNIV_INTERN
130
 
void*
131
 
mem_heap_cat(
132
 
/*=========*/
133
 
                                /* out, own: the result */
134
 
        mem_heap_t*     heap,   /* in: memory heap where result is allocated */
135
 
        const void*     b1,     /* in: block 1 */
136
 
        ulint           len1,   /* in: length of b1, in bytes */
137
 
        const void*     b2,     /* in: block 2 */
138
 
        ulint           len2)   /* in: length of b2, in bytes */
139
 
{
140
 
        void*   res = mem_heap_alloc(heap, len1 + len2);
141
 
 
142
 
        memcpy(res, b1, len1);
143
 
        memcpy((char*)res + len1, b2, len2);
144
 
 
145
 
        return(res);
146
 
}
147
 
 
148
 
/**************************************************************************
149
 
Concatenate two strings and return the result, using a memory heap. */
 
128
/**********************************************************************//**
 
129
Concatenate two strings and return the result, using a memory heap.
 
130
@return own: the result */
150
131
UNIV_INTERN
151
132
char*
152
133
mem_heap_strcat(
153
134
/*============*/
154
 
                                /* out, own: the result */
155
 
        mem_heap_t*     heap,   /* in: memory heap where string is allocated */
156
 
        const char*     s1,     /* in: string 1 */
157
 
        const char*     s2)     /* in: string 2 */
 
135
        mem_heap_t*     heap,   /*!< in: memory heap where string is allocated */
 
136
        const char*     s1,     /*!< in: string 1 */
 
137
        const char*     s2)     /*!< in: string 2 */
158
138
{
159
139
        char*   s;
160
140
        ulint   s1_len = strlen(s1);
171
151
}
172
152
 
173
153
 
174
 
/********************************************************************
175
 
Helper function for mem_heap_printf. */
 
154
/****************************************************************//**
 
155
Helper function for mem_heap_printf.
 
156
@return length of formatted string, including terminating NUL */
176
157
static
177
158
ulint
178
159
mem_heap_printf_low(
179
160
/*================*/
180
 
                                /* out: length of formatted string,
181
 
                                including terminating NUL */
182
 
        char*           buf,    /* in/out: buffer to store formatted string
 
161
        char*           buf,    /*!< in/out: buffer to store formatted string
183
162
                                in, or NULL to just calculate length */
184
 
        const char*     format, /* in: format string */
185
 
        va_list         ap)     /* in: arguments */
 
163
        const char*     format, /*!< in: format string */
 
164
        va_list         ap)     /*!< in: arguments */
186
165
{
187
166
        ulint           len = 0;
188
167
 
281
260
        return(len);
282
261
}
283
262
 
284
 
/********************************************************************
 
263
/****************************************************************//**
285
264
A simple (s)printf replacement that dynamically allocates the space for the
286
265
formatted string from the given heap. This supports a very limited set of
287
266
the printf syntax: types 's' and 'u' and length modifier 'l' (which is
288
 
required for the 'u' type). */
 
267
required for the 'u' type).
 
268
@return heap-allocated formatted string */
289
269
UNIV_INTERN
290
270
char*
291
271
mem_heap_printf(
292
272
/*============*/
293
 
                                /* out: heap-allocated formatted string */
294
 
        mem_heap_t*     heap,   /* in: memory heap */
295
 
        const char*     format, /* in: format string */
 
273
        mem_heap_t*     heap,   /*!< in: memory heap */
 
274
        const char*     format, /*!< in: format string */
296
275
        ...)
297
276
{
298
277
        va_list         ap;
314
293
        return(str);
315
294
}
316
295
 
317
 
/*******************************************************************
318
 
Creates a memory heap block where data can be allocated. */
 
296
/***************************************************************//**
 
297
Creates a memory heap block where data can be allocated.
 
298
@return own: memory heap block, NULL if did not succeed (only possible
 
299
for MEM_HEAP_BTR_SEARCH type heaps) */
319
300
UNIV_INTERN
320
301
mem_block_t*
321
302
mem_heap_create_block(
322
303
/*==================*/
323
 
                                /* out, own: memory heap block, NULL if
324
 
                                did not succeed (only possible for
325
 
                                MEM_HEAP_BTR_SEARCH type heaps) */
326
 
        mem_heap_t*     heap,   /* in: memory heap or NULL if first block
 
304
        mem_heap_t*     heap,   /*!< in: memory heap or NULL if first block
327
305
                                should be created */
328
 
        ulint           n,      /* in: number of bytes needed for user data */
329
 
        ulint           type,   /* in: type of heap: MEM_HEAP_DYNAMIC or
 
306
        ulint           n,      /*!< in: number of bytes needed for user data */
 
307
        ulint           type,   /*!< in: type of heap: MEM_HEAP_DYNAMIC or
330
308
                                MEM_HEAP_BUFFER */
331
 
        const char*     file_name,/* in: file name where created */
332
 
        ulint           line)   /* in: line where created */
 
309
        const char*     file_name,/*!< in: file name where created */
 
310
        ulint           line)   /*!< in: line where created */
333
311
{
 
312
#ifndef UNIV_HOTBACKUP
334
313
        buf_block_t*    buf_block = NULL;
 
314
#endif /* !UNIV_HOTBACKUP */
335
315
        mem_block_t*    block;
336
316
        ulint           len;
337
317
 
345
325
        /* In dynamic allocation, calculate the size: block header + data. */
346
326
        len = MEM_BLOCK_HEADER_SIZE + MEM_SPACE_NEEDED(n);
347
327
 
 
328
#ifndef UNIV_HOTBACKUP
348
329
        if (type == MEM_HEAP_DYNAMIC || len < UNIV_PAGE_SIZE / 2) {
349
330
 
350
331
                ut_ad(type == MEM_HEAP_DYNAMIC || n <= MEM_MAX_ALLOC_IN_BUF);
374
355
 
375
356
        ut_ad(block);
376
357
        block->buf_block = buf_block;
 
358
        block->free_block = NULL;
 
359
#else /* !UNIV_HOTBACKUP */
 
360
        len = MEM_BLOCK_HEADER_SIZE + MEM_SPACE_NEEDED(n);
 
361
        block = ut_malloc(len);
 
362
        ut_ad(block);
 
363
#endif /* !UNIV_HOTBACKUP */
 
364
 
377
365
        block->magic_n = MEM_BLOCK_MAGIC_N;
378
366
        ut_strlcpy_rev(block->file_name, file_name, sizeof(block->file_name));
379
367
        block->line = line;
395
383
        mem_block_set_free(block, MEM_BLOCK_HEADER_SIZE);
396
384
        mem_block_set_start(block, MEM_BLOCK_HEADER_SIZE);
397
385
 
398
 
        block->free_block = NULL;
399
 
 
400
386
        ut_ad((ulint)MEM_BLOCK_HEADER_SIZE < len);
401
387
 
402
388
        return(block);
403
389
}
404
390
 
405
 
/*******************************************************************
406
 
Adds a new block to a memory heap. */
 
391
/***************************************************************//**
 
392
Adds a new block to a memory heap.
 
393
@return created block, NULL if did not succeed (only possible for
 
394
MEM_HEAP_BTR_SEARCH type heaps) */
407
395
UNIV_INTERN
408
396
mem_block_t*
409
397
mem_heap_add_block(
410
398
/*===============*/
411
 
                                /* out: created block, NULL if did not
412
 
                                succeed (only possible for
413
 
                                MEM_HEAP_BTR_SEARCH type heaps)*/
414
 
        mem_heap_t*     heap,   /* in: memory heap */
415
 
        ulint           n)      /* in: number of bytes user needs */
 
399
        mem_heap_t*     heap,   /*!< in: memory heap */
 
400
        ulint           n)      /*!< in: number of bytes user needs */
416
401
{
417
402
        mem_block_t*    block;
418
403
        mem_block_t*    new_block;
458
443
        return(new_block);
459
444
}
460
445
 
461
 
/**********************************************************************
 
446
/******************************************************************//**
462
447
Frees a block from a memory heap. */
463
448
UNIV_INTERN
464
449
void
465
450
mem_heap_block_free(
466
451
/*================*/
467
 
        mem_heap_t*     heap,   /* in: heap */
468
 
        mem_block_t*    block)  /* in: block to free */
 
452
        mem_heap_t*     heap,   /*!< in: heap */
 
453
        mem_block_t*    block)  /*!< in: block to free */
469
454
{
470
455
        ulint           type;
471
456
        ulint           len;
472
 
        buf_block_t*    buf_block;
 
457
#ifndef UNIV_HOTBACKUP
 
458
        buf_block_t*    buf_block       = block->buf_block;
 
459
#endif /* !UNIV_HOTBACKUP */
473
460
 
474
461
        if (block->magic_n != MEM_BLOCK_MAGIC_N) {
475
462
                mem_analyze_corruption(block);
486
473
#endif
487
474
        type = heap->type;
488
475
        len = block->len;
489
 
        buf_block = block->buf_block;
490
476
        block->magic_n = MEM_FREED_BLOCK_MAGIC_N;
491
477
 
492
478
#ifdef UNIV_MEM_DEBUG
498
484
        UNIV_MEM_ASSERT_AND_FREE(block, len);
499
485
#endif /* UNIV_MEM_DEBUG */
500
486
 
 
487
#ifndef UNIV_HOTBACKUP
501
488
        if (type == MEM_HEAP_DYNAMIC || len < UNIV_PAGE_SIZE / 2) {
502
489
 
503
490
                ut_ad(!buf_block);
507
494
 
508
495
                buf_block_free(buf_block);
509
496
        }
 
497
#else /* !UNIV_HOTBACKUP */
 
498
        ut_free(block);
 
499
#endif /* !UNIV_HOTBACKUP */
510
500
}
511
501
 
512
 
/**********************************************************************
 
502
#ifndef UNIV_HOTBACKUP
 
503
/******************************************************************//**
513
504
Frees the free_block field from a memory heap. */
514
505
UNIV_INTERN
515
506
void
516
507
mem_heap_free_block_free(
517
508
/*=====================*/
518
 
        mem_heap_t*     heap)   /* in: heap */
 
509
        mem_heap_t*     heap)   /*!< in: heap */
519
510
{
520
511
        if (UNIV_LIKELY_NULL(heap->free_block)) {
521
512
 
524
515
                heap->free_block = NULL;
525
516
        }
526
517
}
 
518
#endif /* !UNIV_HOTBACKUP */
527
519
 
528
520
#ifdef MEM_PERIODIC_CHECK
529
 
/**********************************************************************
 
521
/******************************************************************//**
530
522
Goes through the list of all allocated mem blocks, checks their magic
531
523
numbers, and reports possible corruption. */
532
524
UNIV_INTERN