1 /****************************************************************************
5 * Memory debugger (body).
7 * Copyright (C) 2001-2020 by
8 * David Turner, Robert Wilhelm, and Werner Lemberg.
10 * This file is part of the FreeType project, and may only be used,
11 * modified, and distributed under the terms of the FreeType project
12 * license, LICENSE.TXT. By continuing to use, modify, or distribute
13 * this file you indicate that you have read the license and
14 * understand and accept it fully.
20 #include FT_CONFIG_CONFIG_H
21 #include <freetype/internal/ftdebug.h>
22 #include <freetype/internal/ftmemory.h>
23 #include <freetype/ftsystem.h>
24 #include <freetype/fterrors.h>
25 #include <freetype/fttypes.h>
28 #ifdef FT_DEBUG_MEMORY
30 #define KEEPALIVE /* `Keep alive' means that freed blocks aren't released
31 * to the heap. This is useful to detect double-frees
32 * or weird heap corruption, but it uses large amounts of
36 #include FT_CONFIG_STANDARD_LIBRARY_H
38 FT_BASE_DEF( const char* ) _ft_debug_file = NULL;
39 FT_BASE_DEF( long ) _ft_debug_lineno = 0;
42 FT_DumpMemory( FT_Memory memory );
45 typedef struct FT_MemSourceRec_* FT_MemSource;
46 typedef struct FT_MemNodeRec_* FT_MemNode;
47 typedef struct FT_MemTableRec_* FT_MemTable;
50 #define FT_MEM_VAL( addr ) ( (FT_PtrDist)(FT_Pointer)( addr ) )
53 * This structure holds statistics for a single allocation/release
54 * site. This is useful to know where memory operations happen the
57 typedef struct FT_MemSourceRec_
59 const char* file_name;
62 FT_Long cur_blocks; /* current number of allocated blocks */
63 FT_Long max_blocks; /* max. number of allocated blocks */
64 FT_Long all_blocks; /* total number of blocks allocated */
66 FT_Long cur_size; /* current cumulative allocated size */
67 FT_Long max_size; /* maximum cumulative allocated size */
68 FT_Long all_size; /* total cumulative allocated size */
70 FT_Long cur_max; /* current maximum allocated size */
79 * We don't need a resizable array for the memory sources because
80 * their number is pretty limited within FreeType.
82 #define FT_MEM_SOURCE_BUCKETS 128
85 * This structure holds information related to a single allocated
86 * memory block. If KEEPALIVE is defined, blocks that are freed by
87 * FreeType are never released to the system. Instead, their `size'
88 * field is set to `-size'. This is mainly useful to detect double
89 * frees, at the price of a large memory footprint during execution.
91 typedef struct FT_MemNodeRec_
94 FT_Long size; /* < 0 if the block was freed */
99 const char* free_file_name;
100 FT_Long free_line_no;
109 * The global structure, containing compound statistics and all hash
112 typedef struct FT_MemTableRec_
119 FT_Long alloc_current;
124 FT_Long alloc_total_max;
127 FT_Long alloc_count_max;
129 FT_MemSource sources[FT_MEM_SOURCE_BUCKETS];
134 FT_Pointer memory_user;
137 FT_Realloc_Func realloc;
142 #define FT_MEM_SIZE_MIN 7
143 #define FT_MEM_SIZE_MAX 13845163
145 #define FT_FILENAME( x ) ( (x) ? (x) : "unknown file" )
149 * Prime numbers are ugly to handle. It would be better to implement
150 * L-Hashing, which is 10% faster and doesn't require divisions.
152 static const FT_Int ft_mem_primes[] =
193 ft_mem_closest_prime( FT_Long num )
199 i < sizeof ( ft_mem_primes ) / sizeof ( ft_mem_primes[0] ); i++ )
200 if ( ft_mem_primes[i] > num )
201 return ft_mem_primes[i];
203 return FT_MEM_SIZE_MAX;
208 ft_mem_debug_panic( const char* fmt,
214 printf( "FreeType.Debug: " );
221 exit( EXIT_FAILURE );
226 ft_mem_table_alloc( FT_MemTable table,
229 FT_Memory memory = table->memory;
233 memory->user = table->memory_user;
234 block = table->alloc( memory, size );
235 memory->user = table;
242 ft_mem_table_free( FT_MemTable table,
245 FT_Memory memory = table->memory;
248 memory->user = table->memory_user;
249 table->free( memory, block );
250 memory->user = table;
255 ft_mem_table_resize( FT_MemTable table )
260 new_size = ft_mem_closest_prime( table->nodes );
261 if ( new_size != table->size )
263 FT_MemNode* new_buckets;
267 new_buckets = (FT_MemNode *)
270 new_size * (FT_Long)sizeof ( FT_MemNode ) );
274 FT_ARRAY_ZERO( new_buckets, new_size );
276 for ( i = 0; i < table->size; i++ )
278 FT_MemNode node, next, *pnode;
282 node = table->buckets[i];
286 hash = FT_MEM_VAL( node->address ) % (FT_PtrDist)new_size;
287 pnode = new_buckets + hash;
289 node->link = pnode[0];
296 if ( table->buckets )
297 ft_mem_table_free( table, table->buckets );
299 table->buckets = new_buckets;
300 table->size = new_size;
306 ft_mem_table_new( FT_Memory memory )
311 table = (FT_MemTable)memory->alloc( memory, sizeof ( *table ) );
317 table->size = FT_MEM_SIZE_MIN;
320 table->memory = memory;
322 table->memory_user = memory->user;
324 table->alloc = memory->alloc;
325 table->realloc = memory->realloc;
326 table->free = memory->free;
328 table->buckets = (FT_MemNode *)
331 table->size * (FT_Long)sizeof ( FT_MemNode ) );
332 if ( table->buckets )
333 FT_ARRAY_ZERO( table->buckets, table->size );
336 memory->free( memory, table );
346 ft_mem_table_destroy( FT_MemTable table )
349 FT_Long leak_count = 0;
353 FT_DumpMemory( table->memory );
355 /* remove all blocks from the table, revealing leaked ones */
356 for ( i = 0; i < table->size; i++ )
358 FT_MemNode *pnode = table->buckets + i, next, node = *pnode;
366 if ( node->size > 0 )
369 "leaked memory block at address %p, size %8ld in (%s:%ld)\n",
370 (void*)node->address,
372 FT_FILENAME( node->source->file_name ),
373 node->source->line_no );
378 ft_mem_table_free( table, node->address );
381 node->address = NULL;
384 ft_mem_table_free( table, node );
387 table->buckets[i] = NULL;
390 ft_mem_table_free( table, table->buckets );
391 table->buckets = NULL;
396 /* remove all sources */
397 for ( i = 0; i < FT_MEM_SOURCE_BUCKETS; i++ )
399 FT_MemSource source, next;
402 for ( source = table->sources[i]; source != NULL; source = next )
405 ft_mem_table_free( table, source );
408 table->sources[i] = NULL;
411 printf( "FreeType: total memory allocations = %ld\n",
412 table->alloc_total );
413 printf( "FreeType: maximum memory footprint = %ld\n",
416 ft_mem_table_free( table, table );
418 if ( leak_count > 0 )
420 "FreeType: %ld bytes of memory leaked in %ld blocks\n",
423 printf( "FreeType: no memory leaks detected\n" );
428 ft_mem_table_get_nodep( FT_MemTable table,
432 FT_MemNode *pnode, node;
435 hash = FT_MEM_VAL( address );
436 pnode = table->buckets + ( hash % (FT_PtrDist)table->size );
444 if ( node->address == address )
454 ft_mem_table_get_source( FT_MemTable table )
457 FT_MemSource node, *pnode;
460 /* cast to FT_PtrDist first since void* can be larger */
461 /* than FT_UInt32 and GCC 4.1.1 emits a warning */
462 hash = (FT_UInt32)(FT_PtrDist)(void*)_ft_debug_file +
463 (FT_UInt32)( 5 * _ft_debug_lineno );
464 pnode = &table->sources[hash % FT_MEM_SOURCE_BUCKETS];
472 if ( node->file_name == _ft_debug_file &&
473 node->line_no == _ft_debug_lineno )
479 node = (FT_MemSource)ft_mem_table_alloc( table, sizeof ( *node ) );
482 "not enough memory to perform memory debugging\n" );
484 node->file_name = _ft_debug_file;
485 node->line_no = _ft_debug_lineno;
487 node->cur_blocks = 0;
488 node->max_blocks = 0;
489 node->all_blocks = 0;
507 ft_mem_table_set( FT_MemTable table,
512 FT_MemNode *pnode, node;
520 pnode = ft_mem_table_get_nodep( table, address );
524 if ( node->size < 0 )
526 /* This block was already freed. Our memory is now completely */
528 /* This can only happen in keep-alive mode. */
530 "memory heap corrupted (allocating freed block)" );
534 /* This block was already allocated. This means that our memory */
535 /* is also corrupted! */
537 "memory heap corrupted (re-allocating allocated block at"
538 " %p, of size %ld)\n"
539 "org=%s:%d new=%s:%d\n",
540 node->address, node->size,
541 FT_FILENAME( node->source->file_name ), node->source->line_no,
542 FT_FILENAME( _ft_debug_file ), _ft_debug_lineno );
546 /* we need to create a new node in this table */
547 node = (FT_MemNode)ft_mem_table_alloc( table, sizeof ( *node ) );
549 ft_mem_debug_panic( "not enough memory to run memory tests" );
551 node->address = address;
553 node->source = source = ft_mem_table_get_source( table );
557 /* this is an allocation */
558 source->all_blocks++;
559 source->cur_blocks++;
560 if ( source->cur_blocks > source->max_blocks )
561 source->max_blocks = source->cur_blocks;
564 if ( size > source->cur_max )
565 source->cur_max = size;
569 /* we are growing or shrinking a reallocated block */
570 source->cur_size += delta;
571 table->alloc_current += delta;
575 /* we are allocating a new block */
576 source->cur_size += size;
577 table->alloc_current += size;
580 source->all_size += size;
582 if ( source->cur_size > source->max_size )
583 source->max_size = source->cur_size;
585 node->free_file_name = NULL;
586 node->free_line_no = 0;
588 node->link = pnode[0];
593 table->alloc_total += size;
595 if ( table->alloc_current > table->alloc_max )
596 table->alloc_max = table->alloc_current;
598 if ( table->nodes * 3 < table->size ||
599 table->size * 3 < table->nodes )
600 ft_mem_table_resize( table );
606 ft_mem_table_remove( FT_MemTable table,
612 FT_MemNode *pnode, node;
615 pnode = ft_mem_table_get_nodep( table, address );
622 if ( node->size < 0 )
624 "freeing memory block at %p more than once\n"
626 " Block was allocated at (%s:%ld)\n"
627 " and released at (%s:%ld).",
629 FT_FILENAME( _ft_debug_file ), _ft_debug_lineno,
630 FT_FILENAME( node->source->file_name ), node->source->line_no,
631 FT_FILENAME( node->free_file_name ), node->free_line_no );
633 /* scramble the node's content for additional safety */
634 FT_MEM_SET( address, 0xF3, node->size );
638 source = node->source;
640 source->cur_blocks--;
641 source->cur_size -= node->size;
643 table->alloc_current -= node->size;
646 if ( table->keep_alive )
648 /* we simply invert the node's size to indicate that the node */
650 node->size = -node->size;
651 node->free_file_name = _ft_debug_file;
652 node->free_line_no = _ft_debug_lineno;
663 ft_mem_table_free( table, node );
665 if ( table->nodes * 3 < table->size ||
666 table->size * 3 < table->nodes )
667 ft_mem_table_resize( table );
672 "trying to free unknown block at %p in (%s:%ld)\n",
674 FT_FILENAME( _ft_debug_file ), _ft_debug_lineno );
680 ft_mem_debug_alloc( FT_Memory memory,
683 FT_MemTable table = (FT_MemTable)memory->user;
688 ft_mem_debug_panic( "negative block size allocation (%ld)", size );
690 /* return NULL if the maximum number of allocations was reached */
691 if ( table->bound_count &&
692 table->alloc_count >= table->alloc_count_max )
695 /* return NULL if this allocation would overflow the maximum heap size */
696 if ( table->bound_total &&
697 table->alloc_total_max - table->alloc_current > size )
700 block = (FT_Byte *)ft_mem_table_alloc( table, size );
703 ft_mem_table_set( table, block, size, 0 );
705 table->alloc_count++;
708 _ft_debug_file = "<unknown>";
709 _ft_debug_lineno = 0;
711 return (FT_Pointer)block;
716 ft_mem_debug_free( FT_Memory memory,
719 FT_MemTable table = (FT_MemTable)memory->user;
723 ft_mem_debug_panic( "trying to free NULL in (%s:%ld)",
724 FT_FILENAME( _ft_debug_file ),
727 ft_mem_table_remove( table, (FT_Byte*)block, 0 );
729 if ( !table->keep_alive )
730 ft_mem_table_free( table, block );
732 table->alloc_count--;
734 _ft_debug_file = "<unknown>";
735 _ft_debug_lineno = 0;
740 ft_mem_debug_realloc( FT_Memory memory,
745 FT_MemTable table = (FT_MemTable)memory->user;
746 FT_MemNode node, *pnode;
747 FT_Pointer new_block;
750 const char* file_name = FT_FILENAME( _ft_debug_file );
751 FT_Long line_no = _ft_debug_lineno;
754 /* unlikely, but possible */
755 if ( new_size == cur_size )
758 /* the following is valid according to ANSI C */
760 if ( !block || !cur_size )
761 ft_mem_debug_panic( "trying to reallocate NULL in (%s:%ld)",
762 file_name, line_no );
765 /* while the following is allowed in ANSI C also, we abort since */
766 /* such case should be handled by FreeType. */
769 "trying to reallocate %p to size 0 (current is %ld) in (%s:%ld)",
770 block, cur_size, file_name, line_no );
772 /* check `cur_size' value */
773 pnode = ft_mem_table_get_nodep( table, (FT_Byte*)block );
777 "trying to reallocate unknown block at %p in (%s:%ld)",
778 block, file_name, line_no );
780 if ( node->size <= 0 )
782 "trying to reallocate freed block at %p in (%s:%ld)",
783 block, file_name, line_no );
785 if ( node->size != cur_size )
786 ft_mem_debug_panic( "invalid ft_realloc request for %p. cur_size is "
787 "%ld instead of %ld in (%s:%ld)",
788 block, cur_size, node->size, file_name, line_no );
790 /* return NULL if the maximum number of allocations was reached */
791 if ( table->bound_count &&
792 table->alloc_count >= table->alloc_count_max )
795 delta = new_size - cur_size;
797 /* return NULL if this allocation would overflow the maximum heap size */
799 table->bound_total &&
800 table->alloc_current + delta > table->alloc_total_max )
803 new_block = (FT_Pointer)ft_mem_table_alloc( table, new_size );
807 ft_mem_table_set( table, (FT_Byte*)new_block, new_size, delta );
809 ft_memcpy( new_block, block, cur_size < new_size ? (size_t)cur_size
810 : (size_t)new_size );
812 ft_mem_table_remove( table, (FT_Byte*)block, delta );
814 _ft_debug_file = "<unknown>";
815 _ft_debug_lineno = 0;
817 if ( !table->keep_alive )
818 ft_mem_table_free( table, block );
825 ft_mem_debug_init( FT_Memory memory )
831 if ( ft_getenv( "FT2_DEBUG_MEMORY" ) )
833 table = ft_mem_table_new( memory );
839 memory->user = table;
840 memory->alloc = ft_mem_debug_alloc;
841 memory->realloc = ft_mem_debug_realloc;
842 memory->free = ft_mem_debug_free;
844 p = ft_getenv( "FT2_ALLOC_TOTAL_MAX" );
847 FT_Long total_max = ft_strtol( p, NULL, 10 );
852 table->bound_total = 1;
853 table->alloc_total_max = total_max;
857 p = ft_getenv( "FT2_ALLOC_COUNT_MAX" );
860 FT_Long total_count = ft_strtol( p, NULL, 10 );
863 if ( total_count > 0 )
865 table->bound_count = 1;
866 table->alloc_count_max = total_count;
870 p = ft_getenv( "FT2_KEEP_ALIVE" );
873 FT_Long keep_alive = ft_strtol( p, NULL, 10 );
876 if ( keep_alive > 0 )
877 table->keep_alive = 1;
888 ft_mem_debug_done( FT_Memory memory )
890 FT_MemTable table = (FT_MemTable)memory->user;
895 memory->free = table->free;
896 memory->realloc = table->realloc;
897 memory->alloc = table->alloc;
899 ft_mem_table_destroy( table );
906 ft_mem_source_compare( const void* p1,
909 FT_MemSource s1 = *(FT_MemSource*)p1;
910 FT_MemSource s2 = *(FT_MemSource*)p2;
913 if ( s2->max_size > s1->max_size )
915 else if ( s2->max_size < s1->max_size )
923 FT_DumpMemory( FT_Memory memory )
925 FT_MemTable table = (FT_MemTable)memory->user;
930 FT_MemSource* bucket = table->sources;
931 FT_MemSource* limit = bucket + FT_MEM_SOURCE_BUCKETS;
932 FT_MemSource* sources;
938 for ( ; bucket < limit; bucket++ )
940 FT_MemSource source = *bucket;
943 for ( ; source; source = source->link )
947 sources = (FT_MemSource*)
949 table, count * (FT_Long)sizeof ( *sources ) );
952 for ( bucket = table->sources; bucket < limit; bucket++ )
954 FT_MemSource source = *bucket;
957 for ( ; source; source = source->link )
958 sources[count++] = source;
964 ft_mem_source_compare );
966 printf( "FreeType Memory Dump: "
967 "current=%ld max=%ld total=%ld count=%ld\n",
968 table->alloc_current, table->alloc_max,
969 table->alloc_total, table->alloc_count );
970 printf( " block block sizes sizes sizes source\n" );
971 printf( " count high sum highsum max location\n" );
972 printf( "-------------------------------------------------\n" );
974 fmt = "%6ld %6ld %8ld %8ld %8ld %s:%d\n";
976 for ( nn = 0; nn < count; nn++ )
978 FT_MemSource source = sources[nn];
982 source->cur_blocks, source->max_blocks,
983 source->cur_size, source->max_size, source->cur_max,
984 FT_FILENAME( source->file_name ),
987 printf( "------------------------------------------------\n" );
989 ft_mem_table_free( table, sources );
993 #else /* !FT_DEBUG_MEMORY */
995 /* ANSI C doesn't like empty source files */
996 typedef int _debug_mem_dummy;
998 #endif /* !FT_DEBUG_MEMORY */