~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Brian Aker
  • Date: 2008-09-04 19:31:00 UTC
  • Revision ID: brian@tangent.org-20080904193100-l849hgghfy4urj43
Changing default character set from this point on.

Show diffs side-by-side

added added

removed removed

Lines of Context:
84
84
 
85
85
#endif
86
86
 
 
87
/*******************************************************************
 
88
NOTE: Use the corresponding macro instead of this function.
 
89
Allocates a single buffer of memory from the dynamic memory of
 
90
the C compiler. Is like malloc of C. The buffer must be freed
 
91
with mem_free. */
 
92
 
 
93
void*
 
94
mem_alloc_func_noninline(
 
95
/*=====================*/
 
96
                                        /* out, own: free storage */
 
97
        ulint           n,              /* in: desired number of bytes */
 
98
        const char*     file_name,      /* in: file name where created */
 
99
        ulint           line)           /* in: line where created */
 
100
{
 
101
        return(mem_alloc_func(n, file_name, line));
 
102
}
 
103
 
87
104
/**************************************************************************
88
105
Duplicates a NUL-terminated string, allocated from a memory heap. */
89
 
UNIV_INTERN
 
106
 
90
107
char*
91
108
mem_heap_strdup(
92
109
/*============*/
99
116
 
100
117
/**************************************************************************
101
118
Duplicate a block of data, allocated from a memory heap. */
102
 
UNIV_INTERN
 
119
 
103
120
void*
104
121
mem_heap_dup(
105
122
/*=========*/
113
130
 
114
131
/**************************************************************************
115
132
Concatenate two memory blocks and return the result, using a memory heap. */
116
 
UNIV_INTERN
 
133
 
117
134
void*
118
135
mem_heap_cat(
119
136
/*=========*/
134
151
 
135
152
/**************************************************************************
136
153
Concatenate two strings and return the result, using a memory heap. */
137
 
UNIV_INTERN
 
154
 
138
155
char*
139
156
mem_heap_strcat(
140
157
/*============*/
273
290
formatted string from the given heap. This supports a very limited set of
274
291
the printf syntax: types 's' and 'u' and length modifier 'l' (which is
275
292
required for the 'u' type). */
276
 
UNIV_INTERN
 
293
 
277
294
char*
278
295
mem_heap_printf(
279
296
/*============*/
303
320
 
304
321
/*******************************************************************
305
322
Creates a memory heap block where data can be allocated. */
306
 
UNIV_INTERN
 
323
 
307
324
mem_block_t*
308
325
mem_heap_create_block(
309
326
/*==================*/
312
329
                                MEM_HEAP_BTR_SEARCH type heaps) */
313
330
        mem_heap_t*     heap,   /* in: memory heap or NULL if first block
314
331
                                should be created */
315
 
        ulint           n,      /* in: number of bytes needed for user data */
 
332
        ulint           n,      /* in: number of bytes needed for user data, or
 
333
                                if init_block is not NULL, its size in bytes */
 
334
        void*           init_block, /* in: init block in fast create,
 
335
                                type must be MEM_HEAP_DYNAMIC */
316
336
        ulint           type,   /* in: type of heap: MEM_HEAP_DYNAMIC or
317
337
                                MEM_HEAP_BUFFER */
318
338
        const char*     file_name,/* in: file name where created */
319
339
        ulint           line)   /* in: line where created */
320
340
{
321
 
        buf_block_t*    buf_block = NULL;
322
341
        mem_block_t*    block;
323
342
        ulint           len;
324
343
 
331
350
 
332
351
        /* In dynamic allocation, calculate the size: block header + data. */
333
352
 
334
 
        if (type == MEM_HEAP_DYNAMIC) {
 
353
        if (init_block != NULL) {
 
354
                ut_ad(type == MEM_HEAP_DYNAMIC);
 
355
                ut_ad(n > MEM_BLOCK_START_SIZE + MEM_BLOCK_HEADER_SIZE);
 
356
                len = n;
 
357
                block = init_block;
 
358
 
 
359
        } else if (type == MEM_HEAP_DYNAMIC) {
335
360
 
336
361
                len = MEM_BLOCK_HEADER_SIZE + MEM_SPACE_NEEDED(n);
337
 
                block = mem_area_alloc(&len, mem_comm_pool);
 
362
                block = mem_area_alloc(len, mem_comm_pool);
338
363
        } else {
339
364
                ut_ad(n <= MEM_MAX_ALLOC_IN_BUF);
340
365
 
342
367
 
343
368
                if (len < UNIV_PAGE_SIZE / 2) {
344
369
 
345
 
                        block = mem_area_alloc(&len, mem_comm_pool);
 
370
                        block = mem_area_alloc(len, mem_comm_pool);
346
371
                } else {
347
372
                        len = UNIV_PAGE_SIZE;
348
373
 
351
376
                                buffer pool, but must get the free block from
352
377
                                the heap header free block field */
353
378
 
354
 
                                buf_block = heap->free_block;
 
379
                                block = (mem_block_t*)heap->free_block;
355
380
                                heap->free_block = NULL;
356
 
 
357
 
                                if (UNIV_UNLIKELY(!buf_block)) {
358
 
 
359
 
                                        return(NULL);
360
 
                                }
361
381
                        } else {
362
 
                                buf_block = buf_block_alloc(0);
 
382
                                block = (mem_block_t*)buf_frame_alloc();
363
383
                        }
364
 
 
365
 
                        block = (mem_block_t*) buf_block->frame;
366
384
                }
367
385
        }
368
386
 
369
 
        ut_ad(block);
370
 
        block->buf_block = buf_block;
 
387
        if (block == NULL) {
 
388
                /* Only MEM_HEAP_BTR_SEARCH allocation should ever fail. */
 
389
                ut_a(type & MEM_HEAP_BTR_SEARCH);
 
390
 
 
391
                return(NULL);
 
392
        }
 
393
 
371
394
        block->magic_n = MEM_BLOCK_MAGIC_N;
372
395
        ut_strlcpy_rev(block->file_name, file_name, sizeof(block->file_name));
373
396
        block->line = line;
390
413
        mem_block_set_start(block, MEM_BLOCK_HEADER_SIZE);
391
414
 
392
415
        block->free_block = NULL;
 
416
        block->init_block = (init_block != NULL);
393
417
 
394
418
        ut_ad((ulint)MEM_BLOCK_HEADER_SIZE < len);
395
419
 
398
422
 
399
423
/*******************************************************************
400
424
Adds a new block to a memory heap. */
401
 
UNIV_INTERN
 
425
 
402
426
mem_block_t*
403
427
mem_heap_add_block(
404
428
/*===============*/
438
462
                new_size = n;
439
463
        }
440
464
 
441
 
        new_block = mem_heap_create_block(heap, new_size, heap->type,
 
465
        new_block = mem_heap_create_block(heap, new_size, NULL, heap->type,
442
466
                                          heap->file_name, heap->line);
443
467
        if (new_block == NULL) {
444
468
 
454
478
 
455
479
/**********************************************************************
456
480
Frees a block from a memory heap. */
457
 
UNIV_INTERN
 
481
 
458
482
void
459
483
mem_heap_block_free(
460
484
/*================*/
461
485
        mem_heap_t*     heap,   /* in: heap */
462
486
        mem_block_t*    block)  /* in: block to free */
463
487
{
464
 
        ulint           type;
465
 
        ulint           len;
466
 
        buf_block_t*    buf_block;
 
488
        ulint   type;
 
489
        ulint   len;
 
490
        ibool   init_block;
467
491
 
468
492
        if (block->magic_n != MEM_BLOCK_MAGIC_N) {
469
493
                mem_analyze_corruption(block);
480
504
#endif
481
505
        type = heap->type;
482
506
        len = block->len;
483
 
        buf_block = block->buf_block;
 
507
        init_block = block->init_block;
484
508
        block->magic_n = MEM_FREED_BLOCK_MAGIC_N;
485
509
 
486
510
#ifdef UNIV_MEM_DEBUG
492
516
        UNIV_MEM_ASSERT_AND_FREE(block, len);
493
517
#endif /* UNIV_MEM_DEBUG */
494
518
 
495
 
        if (type == MEM_HEAP_DYNAMIC) {
496
 
 
497
 
                ut_ad(!buf_block);
 
519
        if (init_block) {
 
520
                /* Do not have to free: do nothing */
 
521
 
 
522
        } else if (type == MEM_HEAP_DYNAMIC) {
 
523
 
498
524
                mem_area_free(block, mem_comm_pool);
499
525
        } else {
500
526
                ut_ad(type & MEM_HEAP_BUFFER);
501
527
 
502
528
                if (len >= UNIV_PAGE_SIZE / 2) {
503
 
                        buf_block_free(buf_block);
 
529
                        buf_frame_free((byte*)block);
504
530
                } else {
505
 
                        ut_ad(!buf_block);
506
531
                        mem_area_free(block, mem_comm_pool);
507
532
                }
508
533
        }
510
535
 
511
536
/**********************************************************************
512
537
Frees the free_block field from a memory heap. */
513
 
UNIV_INTERN
 
538
 
514
539
void
515
540
mem_heap_free_block_free(
516
541
/*=====================*/
517
542
        mem_heap_t*     heap)   /* in: heap */
518
543
{
519
 
        if (UNIV_LIKELY_NULL(heap->free_block)) {
 
544
        if (heap->free_block) {
520
545
 
521
 
                buf_block_free(heap->free_block);
 
546
                buf_frame_free(heap->free_block);
522
547
 
523
548
                heap->free_block = NULL;
524
549
        }
528
553
/**********************************************************************
529
554
Goes through the list of all allocated mem blocks, checks their magic
530
555
numbers, and reports possible corruption. */
531
 
UNIV_INTERN
 
556
 
532
557
void
533
558
mem_validate_all_blocks(void)
534
559
/*=========================*/