17
17
*****************************************************************************/
19
/********************************************************************//**
19
/************************************************************************
21
20
The memory management: the debug code. This is not a compilation module,
22
21
but is included in mem0mem.* !
25
24
*************************************************************************/
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 */
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 */
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 */
134
129
#endif /* UNIV_MEM_DEBUG */
136
#ifndef UNIV_HOTBACKUP
137
/******************************************************************//**
131
/**********************************************************************
138
132
Initializes the memory system. */
143
ulint size) /*!< in: common pool size in bytes */
137
ulint size) /* in: common pool size in bytes */
145
139
#ifdef UNIV_MEM_DEBUG
171
165
mem_comm_pool = mem_pool_create(size);
173
#endif /* !UNIV_HOTBACKUP */
175
168
#ifdef UNIV_MEM_DEBUG
176
/******************************************************************//**
169
/**********************************************************************
177
170
Initializes an allocated memory field in the debug version. */
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 */
220
213
mem_init_buf(usr_buf, n);
223
/******************************************************************//**
216
/**********************************************************************
224
217
Erases an allocated memory field in the debug version. */
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 */
247
240
mem_erase_buf(buf, MEM_SPACE_NEEDED(n));
250
/***************************************************************//**
243
/*******************************************************************
251
244
Initializes a buffer to a random combination of hex BA and BE.
252
245
Used to initialize allocated memory. */
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 */
273
266
UNIV_MEM_INVALID(buf, n);
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.*/
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 */
298
291
UNIV_MEM_FREE(buf, n);
301
/***************************************************************//**
294
/*******************************************************************
302
295
Inserts a created memory heap to the hash table of current allocated
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 */
312
305
mem_hash_node_t* new_node;
336
329
mutex_exit(&mem_hash_mutex);
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
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 */
355
348
mem_hash_node_t* node;
417
410
#endif /* UNIV_MEM_DEBUG */
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
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 */
595
/**************************************************************//**
588
/******************************************************************
596
589
Prints the contents of a memory heap. */
601
mem_heap_t* heap) /*!< in: memory heap */
594
mem_heap_t* heap) /* in: memory heap */
620
/**************************************************************//**
621
Validates the contents of a memory heap.
622
@return TRUE if ok */
613
/******************************************************************
614
Validates the contents of a memory heap. */
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 */
646
639
#endif /* UNIV_MEM_DEBUG || UNIV_DEBUG */
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). */
656
mem_heap_t* heap) /*!< in: memory heap */
648
/* out: TRUE if ok */
649
mem_heap_t* heap) /* in: memory heap */
658
651
ut_a(heap->magic_n == MEM_BLOCK_MAGIC_N);
662
655
#endif /* UNIV_DEBUG */
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. */
670
662
mem_all_freed(void)
671
663
/*===============*/
664
/* out: TRUE if no heaps exist */
673
666
mem_hash_node_t* node;
674
667
ulint heap_count = 0;
703
/*****************************************************************//**
704
Validates the dynamic memory allocation system.
705
@return TRUE if error */
695
/*********************************************************************
696
Validates the dynamic memory allocation system. */
708
699
mem_validate_no_assert(void)
709
700
/*========================*/
701
/* out: TRUE if error */
711
703
mem_hash_node_t* node;
712
704
ulint n_heaps = 0;
778
/************************************************************//**
779
Validates the dynamic memory
780
@return TRUE if ok */
768
/****************************************************************
769
Validates the dynamic memory */
783
772
mem_validate(void)
784
773
/*==============*/
774
/* out: TRUE if ok */
786
776
ut_a(!mem_validate_no_assert());
790
780
#endif /* UNIV_MEM_DEBUG */
792
/************************************************************//**
782
/****************************************************************
793
783
Tries to find neigboring memory allocation blocks and dumps to stderr
794
784
the neighborhood of a given pointer. */
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 */
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. */
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 */
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. */
1013
1002
mem_print_info_low(TRUE);
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. */