~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Monty Taylor
  • Date: 2009-04-14 19:16:51 UTC
  • mto: (997.2.5 mordred)
  • mto: This revision was merged to the branch mainline in revision 994.
  • Revision ID: mordred@inaugust.com-20090414191651-ltbww6hpqks8k7qk
Clarified instructions in README.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
*****************************************************************************/
18
18
 
19
 
/********************************************************************//**
20
 
@file mem/mem0dbg.c
 
19
/************************************************************************
21
20
The memory management: the debug code. This is not a compilation module,
22
21
but is included in mem0mem.* !
23
22
 
25
24
*************************************************************************/
26
25
 
27
26
#ifdef UNIV_MEM_DEBUG
28
 
# ifndef UNIV_HOTBACKUP
29
27
/* The mutex which protects in the debug version the hash table
30
28
containing the list of live memory heaps, and also the global
31
29
variables below. */
32
30
UNIV_INTERN mutex_t     mem_hash_mutex;
33
 
# endif /* !UNIV_HOTBACKUP */
34
31
 
35
32
/* The following variables contain information about the
36
33
extent of memory allocations. Only used in the debug version.
41
38
static ulint            mem_total_allocated_memory      = 0;
42
39
UNIV_INTERN ulint       mem_current_allocated_memory    = 0;
43
40
static ulint            mem_max_allocated_memory        = 0;
44
 
# ifndef UNIV_HOTBACKUP
45
41
static ulint            mem_last_print_info             = 0;
46
 
static ibool            mem_hash_initialized            = FALSE;
47
 
# endif /* !UNIV_HOTBACKUP */
48
42
 
49
43
/* Size of the hash table for memory management tracking */
50
44
#define MEM_HASH_SIZE   997
54
48
typedef struct mem_hash_node_struct mem_hash_node_t;
55
49
struct mem_hash_node_struct {
56
50
        UT_LIST_NODE_T(mem_hash_node_t)
57
 
                                list;   /*!< hash list node */
58
 
        mem_heap_t*             heap;   /*!< memory heap */
 
51
                                list;   /* hash list node */
 
52
        mem_heap_t*             heap;   /* memory heap */
59
53
        const char*             file_name;/* file where heap was created*/
60
 
        ulint                   line;   /*!< file line of creation */
 
54
        ulint                   line;   /* file line of creation */
61
55
        ulint                   nth_heap;/* this is the nth heap created */
62
56
        UT_LIST_NODE_T(mem_hash_node_t)
63
57
                                all_list;/* list of all created heaps */
71
65
/* The base node of the list of all allocated heaps */
72
66
static mem_hash_cell_t          mem_all_list_base;
73
67
 
 
68
static ibool    mem_hash_initialized    = FALSE;
74
69
 
75
70
 
76
71
UNIV_INLINE
133
128
}
134
129
#endif /* UNIV_MEM_DEBUG */
135
130
 
136
 
#ifndef UNIV_HOTBACKUP
137
 
/******************************************************************//**
 
131
/**********************************************************************
138
132
Initializes the memory system. */
139
133
UNIV_INTERN
140
134
void
141
135
mem_init(
142
136
/*=====*/
143
 
        ulint   size)   /*!< in: common pool size in bytes */
 
137
        ulint   size)   /* in: common pool size in bytes */
144
138
{
145
139
#ifdef UNIV_MEM_DEBUG
146
140
 
170
164
 
171
165
        mem_comm_pool = mem_pool_create(size);
172
166
}
173
 
#endif /* !UNIV_HOTBACKUP */
174
167
 
175
168
#ifdef UNIV_MEM_DEBUG
176
 
/******************************************************************//**
 
169
/**********************************************************************
177
170
Initializes an allocated memory field in the debug version. */
178
171
UNIV_INTERN
179
172
void
180
173
mem_field_init(
181
174
/*===========*/
182
 
        byte*   buf,    /*!< in: memory field */
183
 
        ulint   n)      /*!< in: how many bytes the user requested */
 
175
        byte*   buf,    /* in: memory field */
 
176
        ulint   n)      /* in: how many bytes the user requested */
184
177
{
185
178
        ulint   rnd;
186
179
        byte*   usr_buf;
220
213
        mem_init_buf(usr_buf, n);
221
214
}
222
215
 
223
 
/******************************************************************//**
 
216
/**********************************************************************
224
217
Erases an allocated memory field in the debug version. */
225
218
UNIV_INTERN
226
219
void
227
220
mem_field_erase(
228
221
/*============*/
229
 
        byte*   buf,    /*!< in: memory field */
 
222
        byte*   buf,    /* in: memory field */
230
223
        ulint   n __attribute__((unused)))
231
 
                        /*!< in: how many bytes the user requested */
 
224
                        /* in: how many bytes the user requested */
232
225
{
233
226
        byte*   usr_buf;
234
227
 
247
240
        mem_erase_buf(buf, MEM_SPACE_NEEDED(n));
248
241
}
249
242
 
250
 
/***************************************************************//**
 
243
/*******************************************************************
251
244
Initializes a buffer to a random combination of hex BA and BE.
252
245
Used to initialize allocated memory. */
253
246
UNIV_INTERN
254
247
void
255
248
mem_init_buf(
256
249
/*=========*/
257
 
        byte*   buf,    /*!< in: pointer to buffer */
258
 
        ulint    n)     /*!< in: length of buffer */
 
250
        byte*   buf,    /* in: pointer to buffer */
 
251
        ulint    n)     /* in: length of buffer */
259
252
{
260
253
        byte*   ptr;
261
254
 
273
266
        UNIV_MEM_INVALID(buf, n);
274
267
}
275
268
 
276
 
/***************************************************************//**
 
269
/*******************************************************************
277
270
Initializes a buffer to a random combination of hex DE and AD.
278
 
Used to erase freed memory. */
 
271
Used to erase freed memory.*/
279
272
UNIV_INTERN
280
273
void
281
274
mem_erase_buf(
282
275
/*==========*/
283
 
        byte*   buf,    /*!< in: pointer to buffer */
284
 
        ulint   n)      /*!< in: length of buffer */
 
276
        byte*   buf,    /* in: pointer to buffer */
 
277
        ulint    n)      /* in: length of buffer */
285
278
{
286
279
        byte*   ptr;
287
280
 
298
291
        UNIV_MEM_FREE(buf, n);
299
292
}
300
293
 
301
 
/***************************************************************//**
 
294
/*******************************************************************
302
295
Inserts a created memory heap to the hash table of current allocated
303
296
memory heaps. */
304
297
UNIV_INTERN
305
298
void
306
299
mem_hash_insert(
307
300
/*============*/
308
 
        mem_heap_t*     heap,      /*!< in: the created heap */
309
 
        const char*     file_name, /*!< in: file name of creation */
310
 
        ulint           line)      /*!< in: line where created */
 
301
        mem_heap_t*     heap,      /* in: the created heap */
 
302
        const char*     file_name, /* in: file name of creation */
 
303
        ulint           line)      /* in: line where created */
311
304
{
312
305
        mem_hash_node_t*        new_node;
313
306
        ulint                   cell_no ;
336
329
        mutex_exit(&mem_hash_mutex);
337
330
}
338
331
 
339
 
/***************************************************************//**
 
332
/*******************************************************************
340
333
Removes a memory heap (which is going to be freed by the caller)
341
334
from the list of live memory heaps. Returns the size of the heap
342
335
in terms of how much memory in bytes was allocated for the user of
348
341
void
349
342
mem_hash_remove(
350
343
/*============*/
351
 
        mem_heap_t*     heap,      /*!< in: the heap to be freed */
352
 
        const char*     file_name, /*!< in: file name of freeing */
353
 
        ulint           line)      /*!< in: line where freed */
 
344
        mem_heap_t*     heap,      /* in: the heap to be freed */
 
345
        const char*     file_name, /* in: file name of freeing */
 
346
        ulint           line)      /* in: line where freed */
354
347
{
355
348
        mem_hash_node_t*        node;
356
349
        ulint                   cell_no;
417
410
#endif /* UNIV_MEM_DEBUG */
418
411
 
419
412
#if defined UNIV_MEM_DEBUG || defined UNIV_DEBUG
420
 
/***************************************************************//**
 
413
/*******************************************************************
421
414
Checks a memory heap for consistency and prints the contents if requested.
422
415
Outputs the sum of sizes of buffers given to the user (only in
423
416
the debug version), the physical size of the heap and the number of
427
420
void
428
421
mem_heap_validate_or_print(
429
422
/*=======================*/
430
 
        mem_heap_t*     heap,   /*!< in: memory heap */
 
423
        mem_heap_t*     heap,   /* in: memory heap */
431
424
        byte*           top __attribute__((unused)),
432
 
                                /*!< in: calculate and validate only until
 
425
                                /* in: calculate and validate only until
433
426
                                this top pointer in the heap is reached,
434
427
                                if this pointer is NULL, ignored */
435
 
        ibool           print,  /*!< in: if TRUE, prints the contents
 
428
        ibool           print,  /* in: if TRUE, prints the contents
436
429
                                of the heap; works only in
437
430
                                the debug version */
438
 
        ibool*          error,  /*!< out: TRUE if error */
439
 
        ulint*          us_size,/*!< out: allocated memory
 
431
        ibool*          error,  /* out: TRUE if error */
 
432
        ulint*          us_size,/* out: allocated memory
440
433
                                (for the user) in the heap,
441
434
                                if a NULL pointer is passed as this
442
435
                                argument, it is ignored; in the
443
436
                                non-debug version this is always -1 */
444
 
        ulint*          ph_size,/*!< out: physical size of the heap,
 
437
        ulint*          ph_size,/* out: physical size of the heap,
445
438
                                if a NULL pointer is passed as this
446
439
                                argument, it is ignored */
447
 
        ulint*          n_blocks) /*!< out: number of blocks in the heap,
 
440
        ulint*          n_blocks) /* out: number of blocks in the heap,
448
441
                                if a NULL pointer is passed as this
449
442
                                argument, it is ignored */
450
443
{
592
585
        *error = FALSE;
593
586
}
594
587
 
595
 
/**************************************************************//**
 
588
/******************************************************************
596
589
Prints the contents of a memory heap. */
597
590
static
598
591
void
599
592
mem_heap_print(
600
593
/*===========*/
601
 
        mem_heap_t*     heap)   /*!< in: memory heap */
 
594
        mem_heap_t*     heap)   /* in: memory heap */
602
595
{
603
596
        ibool   error;
604
597
        ulint   us_size;
617
610
        ut_a(!error);
618
611
}
619
612
 
620
 
/**************************************************************//**
621
 
Validates the contents of a memory heap.
622
 
@return TRUE if ok */
 
613
/******************************************************************
 
614
Validates the contents of a memory heap. */
623
615
UNIV_INTERN
624
616
ibool
625
617
mem_heap_validate(
626
618
/*==============*/
627
 
        mem_heap_t*     heap)   /*!< in: memory heap */
 
619
                                /* out: TRUE if ok */
 
620
        mem_heap_t*     heap)   /* in: memory heap */
628
621
{
629
622
        ibool   error;
630
623
        ulint   us_size;
646
639
#endif /* UNIV_MEM_DEBUG || UNIV_DEBUG */
647
640
 
648
641
#ifdef UNIV_DEBUG
649
 
/**************************************************************//**
650
 
Checks that an object is a memory heap (or a block of it).
651
 
@return TRUE if ok */
 
642
/******************************************************************
 
643
Checks that an object is a memory heap (or a block of it). */
652
644
UNIV_INTERN
653
645
ibool
654
646
mem_heap_check(
655
647
/*===========*/
656
 
        mem_heap_t*     heap)   /*!< in: memory heap */
 
648
                                /* out: TRUE if ok */
 
649
        mem_heap_t*     heap)   /* in: memory heap */
657
650
{
658
651
        ut_a(heap->magic_n == MEM_BLOCK_MAGIC_N);
659
652
 
662
655
#endif /* UNIV_DEBUG */
663
656
 
664
657
#ifdef UNIV_MEM_DEBUG
665
 
/*****************************************************************//**
666
 
TRUE if no memory is currently allocated.
667
 
@return TRUE if no heaps exist */
 
658
/*********************************************************************
 
659
TRUE if no memory is currently allocated. */
668
660
UNIV_INTERN
669
661
ibool
670
662
mem_all_freed(void)
671
663
/*===============*/
 
664
                        /* out: TRUE if no heaps exist */
672
665
{
673
666
        mem_hash_node_t*        node;
674
667
        ulint                   heap_count      = 0;
690
683
        mutex_exit(&mem_hash_mutex);
691
684
 
692
685
        if (heap_count == 0) {
693
 
# ifndef UNIV_HOTBACKUP
 
686
 
694
687
                ut_a(mem_pool_get_reserved(mem_comm_pool) == 0);
695
 
# endif /* !UNIV_HOTBACKUP */
696
688
 
697
689
                return(TRUE);
698
690
        } else {
700
692
        }
701
693
}
702
694
 
703
 
/*****************************************************************//**
704
 
Validates the dynamic memory allocation system.
705
 
@return TRUE if error */
 
695
/*********************************************************************
 
696
Validates the dynamic memory allocation system. */
706
697
UNIV_INTERN
707
698
ibool
708
699
mem_validate_no_assert(void)
709
700
/*========================*/
 
701
                        /* out: TRUE if error */
710
702
{
711
703
        mem_hash_node_t*        node;
712
704
        ulint                   n_heaps                 = 0;
717
709
        ulint                   n_blocks;
718
710
        ulint                   i;
719
711
 
720
 
# ifndef UNIV_HOTBACKUP
721
712
        mem_pool_validate(mem_comm_pool);
722
 
# endif /* !UNIV_HOTBACKUP */
723
713
 
724
714
        mutex_enter(&mem_hash_mutex);
725
715
 
775
765
        return(error);
776
766
}
777
767
 
778
 
/************************************************************//**
779
 
Validates the dynamic memory
780
 
@return TRUE if ok */
 
768
/****************************************************************
 
769
Validates the dynamic memory */
781
770
UNIV_INTERN
782
771
ibool
783
772
mem_validate(void)
784
773
/*==============*/
 
774
                        /* out: TRUE if ok */
785
775
{
786
776
        ut_a(!mem_validate_no_assert());
787
777
 
789
779
}
790
780
#endif /* UNIV_MEM_DEBUG */
791
781
 
792
 
/************************************************************//**
 
782
/****************************************************************
793
783
Tries to find neigboring memory allocation blocks and dumps to stderr
794
784
the neighborhood of a given pointer. */
795
785
UNIV_INTERN
796
786
void
797
787
mem_analyze_corruption(
798
788
/*===================*/
799
 
        void*   ptr)    /*!< in: pointer to place of possible corruption */
 
789
        void*   ptr)    /* in: pointer to place of possible corruption */
800
790
{
801
791
        byte*   p;
802
792
        ulint   i;
897
887
        }
898
888
}
899
889
 
900
 
#ifndef UNIV_HOTBACKUP
901
 
/*****************************************************************//**
 
890
/*********************************************************************
902
891
Prints information of dynamic memory usage and currently allocated
903
892
memory heaps or buffers. Can only be used in the debug version. */
904
893
static
905
894
void
906
895
mem_print_info_low(
907
896
/*===============*/
908
 
        ibool   print_all)      /*!< in: if TRUE, all heaps are printed,
 
897
        ibool   print_all)      /* in: if TRUE, all heaps are printed,
909
898
                                else only the heaps allocated after the
910
899
                                previous call of this function */
911
900
{
1002
991
#endif
1003
992
}
1004
993
 
1005
 
/*****************************************************************//**
 
994
/*********************************************************************
1006
995
Prints information of dynamic memory usage and currently allocated memory
1007
996
heaps or buffers. Can only be used in the debug version. */
1008
997
UNIV_INTERN
1013
1002
        mem_print_info_low(TRUE);
1014
1003
}
1015
1004
 
1016
 
/*****************************************************************//**
 
1005
/*********************************************************************
1017
1006
Prints information of dynamic memory usage and currently allocated memory
1018
1007
heaps or buffers since the last ..._print_info or..._print_new_info. */
1019
1008
UNIV_INTERN
1023
1012
{
1024
1013
        mem_print_info_low(FALSE);
1025
1014
}
1026
 
#endif /* !UNIV_HOTBACKUP */