From 5a1de37e7e1a2fdc1a5ef1679850b8964975d09b Mon Sep 17 00:00:00 2001 From: David Turner Date: Wed, 24 Oct 2001 07:32:55 +0000 Subject: [PATCH] replaced liberal uses of "memset" by the "MEM_Set" macro call (some platforms don't provide this ANSI function !!) some changes to "ftsystem.c" implementations in order to use the new memory debugger on Unix, VMS and Amiga too !! --- builds/amiga/src/base/ftsystem.c | 28 +++++++++-- builds/unix/ftsystem.c | 18 ++++++- builds/vms/ftsystem.c | 17 +++++++ include/freetype/internal/ftmemory.h | 2 +- src/autohint/ahglyph.c | 8 ++-- src/base/ftdbgmem.c | 72 ++++++++++++++++++++++------ src/base/ftsystem.c | 6 +-- src/raster/ftraster.c | 6 ++- src/smooth/ftgrays.c | 8 +++- 9 files changed, 134 insertions(+), 31 deletions(-) diff --git a/builds/amiga/src/base/ftsystem.c b/builds/amiga/src/base/ftsystem.c index 702b25304..c9433fc8a 100644 --- a/builds/amiga/src/base/ftsystem.c +++ b/builds/amiga/src/base/ftsystem.c @@ -346,6 +346,18 @@ void FreeVecPooled(APTR poolHeader, APTR memory) } + +#ifdef FT_DEBUG_MEMORY + + extern FT_Int + ft_mem_debug_init( FT_Memory memory ); + + extern void + ft_mem_debug_done( FT_Memory memory ); + +#endif + + /* documentation is in ftobjs.h */ FT_EXPORT_DEF( FT_Memory ) @@ -360,14 +372,20 @@ void FreeVecPooled(APTR poolHeader, APTR memory) { // memory->user = 0; memory->user = AsmCreatePool ( MEMF_PUBLIC, 2048, 2048, SysBase ); - memory->alloc = ft_alloc; - memory->realloc = ft_realloc; - memory->free = ft_free; if ( memory->user == NULL ) { FreeVec ( memory ); memory = NULL; } + else + { + memory->alloc = ft_alloc; + memory->realloc = ft_realloc; + memory->free = ft_free; +#ifdef FT_DEBUG_MEMORY + ft_mem_debug_init( memory ); +#endif + } } return memory; @@ -379,7 +397,9 @@ void FreeVecPooled(APTR poolHeader, APTR memory) FT_EXPORT_DEF( void ) FT_Done_Memory( FT_Memory memory ) { -// memory->free( memory, memory ); +#ifdef FT_DEBUG_MEMORY + ft_mem_debug_done( memory ); +#endif AsmDeletePool( memory->user, SysBase ); FreeVec( memory ); diff --git a/builds/unix/ftsystem.c b/builds/unix/ftsystem.c index a11287712..5b7d483cc 100644 --- a/builds/unix/ftsystem.c +++ b/builds/unix/ftsystem.c @@ -271,6 +271,17 @@ } +#ifdef FT_DEBUG_MEMORY + + extern FT_Int + ft_mem_debug_init( FT_Memory memory ); + + extern void + ft_mem_debug_done( FT_Memory memory ); + +#endif + + /* documentation is in ftobjs.h */ FT_EXPORT_DEF( FT_Memory ) @@ -281,11 +292,13 @@ memory = (FT_Memory)malloc( sizeof ( *memory ) ); if ( memory ) - { memory->user = 0; memory->alloc = ft_alloc; memory->realloc = ft_realloc; memory->free = ft_free; +#ifdef FT_DEBUG_MEMORY + ft_mem_debug_init( memory ); +#endif } return memory; @@ -297,6 +310,9 @@ FT_EXPORT_DEF( void ) FT_Done_Memory( FT_Memory memory ) { +#ifdef FT_DEBUG_MEMORY + ft_mem_debug_done( memory ); +#endif memory->free( memory, memory ); } diff --git a/builds/vms/ftsystem.c b/builds/vms/ftsystem.c index a11287712..d58c07c21 100644 --- a/builds/vms/ftsystem.c +++ b/builds/vms/ftsystem.c @@ -271,6 +271,17 @@ } +#ifdef FT_DEBUG_MEMORY + + extern FT_Int + ft_mem_debug_init( FT_Memory memory ); + + extern void + ft_mem_debug_done( FT_Memory memory ); + +#endif + + /* documentation is in ftobjs.h */ FT_EXPORT_DEF( FT_Memory ) @@ -286,6 +297,9 @@ memory->alloc = ft_alloc; memory->realloc = ft_realloc; memory->free = ft_free; +#ifdef FT_DEBUG_MEMORY + ft_mem_debug_init( memory ); +#endif } return memory; @@ -297,6 +311,9 @@ FT_EXPORT_DEF( void ) FT_Done_Memory( FT_Memory memory ) { +#ifdef FT_DEBUG_MEMORY + ft_mem_debug_done( memory ); +#endif memory->free( memory, memory ); } diff --git a/include/freetype/internal/ftmemory.h b/include/freetype/internal/ftmemory.h index 1a27e0ef1..afdd98c0c 100644 --- a/include/freetype/internal/ftmemory.h +++ b/include/freetype/internal/ftmemory.h @@ -161,7 +161,7 @@ FT_BEGIN_HEADER /* available on all platforms we know of. */ #include -#define MEM_Set( dest, byte, count ) memset( dest, byte, count ) +#define MEM_Set( dest, byte, count ) memset( dest, byte, count ) #define MEM_Copy( dest, source, count ) memcpy( dest, source, count ) diff --git a/src/autohint/ahglyph.c b/src/autohint/ahglyph.c index 390be7ef8..97ac86215 100644 --- a/src/autohint/ahglyph.c +++ b/src/autohint/ahglyph.c @@ -816,7 +816,7 @@ segment_dir = point->out_dir; /* clear all segment fields */ - memset( segment, 0, sizeof ( *segment ) ); + MEM_Set( segment, 0, sizeof ( *segment ) ); segment->dir = segment_dir; segment->flags = ah_edge_normal; @@ -878,7 +878,7 @@ if ( min_point ) { /* clear all segment fields */ - memset( segment, 0, sizeof ( *segment ) ); + MEM_Set( segment, 0, sizeof ( *segment ) ); segment->dir = segment_dir; segment->flags = ah_edge_normal; @@ -894,7 +894,7 @@ if ( max_point ) { /* clear all segment fields */ - memset( segment, 0, sizeof ( *segment ) ); + MEM_Set( segment, 0, sizeof ( *segment ) ); segment->dir = segment_dir; segment->flags = ah_edge_normal; @@ -1122,7 +1122,7 @@ edge_limit++; /* clear all edge fields */ - memset( edge, 0, sizeof ( *edge ) ); + MEM_Set( edge, 0, sizeof ( *edge ) ); /* add the segment to the new edge's list */ edge->first = seg; diff --git a/src/base/ftdbgmem.c b/src/base/ftdbgmem.c index 011d69982..93198a8f3 100644 --- a/src/base/ftdbgmem.c +++ b/src/base/ftdbgmem.c @@ -32,7 +32,6 @@ typedef struct FT_MemTableRec_ { - FT_Memory memory; FT_ULong size; FT_ULong nodes; FT_MemNode* buckets; @@ -43,6 +42,12 @@ const char* file_name; FT_Long line_no; + + FT_Memory memory; + FT_Pointer memory_user; + FT_Alloc_Func alloc; + FT_Free_Func free; + FT_Realloc_Func realloc; } FT_MemTableRec; @@ -122,6 +127,31 @@ } + static FT_Pointer + ft_mem_table_alloc( FT_MemTable table, + FT_Long size ) + { + FT_Memory memory = table->memory; + FT_Pointer block; + + memory->user = table->memory_user; + block = table->alloc( memory, size ); + memory->user = table; + + return block; + } + + static void + ft_mem_table_free( FT_MemTable table, + FT_Pointer block ) + { + FT_Memory memory = table->memory; + + memory->user = table->memory_user; + table->free( memory, block ); + memory->user = table; + } + static void ft_mem_table_resize( FT_MemTable table ) @@ -134,7 +164,7 @@ FT_MemNode* new_buckets ; FT_ULong i; - new_buckets = malloc( new_size * sizeof(FT_MemNode) ); + new_buckets = ft_mem_table_alloc( table, new_size * sizeof(FT_MemNode) ); if ( new_buckets == NULL ) return; @@ -160,7 +190,7 @@ } if ( table->buckets ) - free( table->buckets ); + ft_mem_table_free( table, table->buckets ); table->buckets = new_buckets; table->size = new_size; @@ -169,24 +199,32 @@ static FT_MemTable - ft_mem_table_new( void ) + ft_mem_table_new( FT_Memory memory ) { FT_MemTable table; - table = malloc( sizeof(*table) ); + table = memory->alloc( memory, sizeof(*table) ); if ( table == NULL ) goto Exit; memset( table, 0, sizeof(*table) ); table->size = FT_MEM_SIZE_MIN; table->nodes = 0; + + table->memory = memory; - table->buckets = malloc( table->size * sizeof(FT_MemNode) ); + table->memory_user = memory->user; + + table->alloc = memory->alloc; + table->realloc = memory->realloc; + table->free = memory->free; + + table->buckets = memory->alloc( memory, table->size * sizeof(FT_MemNode) ); if ( table->buckets ) memset( table->buckets, 0, sizeof(FT_MemNode)*table->size ); else { - free( table ); + memory->free( memory, table ); table = NULL; } @@ -203,8 +241,9 @@ if ( table ) { - FT_Long leak_count = 0; - FT_ULong leaks = 0; + FT_Memory memory = table->memory; + FT_Long leak_count = 0; + FT_ULong leaks = 0; for ( i = 0; i < table->size; i++ ) { @@ -225,7 +264,7 @@ leak_count++; leaks += node->size; - free( node->address ); + ft_mem_table_free( table, node->address ); } node->address = NULL; @@ -236,7 +275,7 @@ } table->buckets[i] = 0; } - free( table->buckets ); + ft_mem_table_free( table, table->buckets ); table->buckets = NULL; table->size = 0; @@ -311,7 +350,7 @@ } /* we need to create a new node in this table */ - node = malloc( sizeof(*node) ); + node = ft_mem_table_alloc( table, sizeof(*node) ); if ( node == NULL ) ft_mem_debug_panic( "not enough memory to run memory tests" ); @@ -378,7 +417,7 @@ if ( size <= 0 ) ft_mem_debug_panic( "negative block size allocation (%ld)", size ); - block = malloc( size ); + block = ft_mem_table_alloc( table, size ); if ( block ) ft_mem_table_set( table, block, (FT_ULong)size ); @@ -454,7 +493,7 @@ if ( getenv( "FT_DEBUG_MEMORY") ) { - table = ft_mem_table_new(); + table = ft_mem_table_new( memory ); if ( table ) { memory->user = table; @@ -475,9 +514,12 @@ if ( table ) { + memory->free = table->free; + memory->realloc = table->realloc; + memory->alloc = table->alloc; + ft_mem_table_destroy( table ); memory->user = NULL; - memory->free = (FT_Free_Func) free; } } diff --git a/src/base/ftsystem.c b/src/base/ftsystem.c index 034cb7d82..fca525c73 100644 --- a/src/base/ftsystem.c +++ b/src/base/ftsystem.c @@ -276,14 +276,14 @@ memory = (FT_Memory)malloc( sizeof ( *memory ) ); if ( memory ) -#ifdef FT_DEBUG_MEMORY - if ( !ft_mem_debug_init( memory ) ) -#endif { memory->user = 0; memory->alloc = ft_alloc; memory->realloc = ft_realloc; memory->free = ft_free; +#ifdef FT_DEBUG_MEMORY + ft_mem_debug_init( memory ); +#endif } return memory; diff --git a/src/raster/ftraster.c b/src/raster/ftraster.c index 33d42b6bc..2a635525b 100644 --- a/src/raster/ftraster.c +++ b/src/raster/ftraster.c @@ -105,6 +105,10 @@ /*************************************************************************/ /*************************************************************************/ +#ifdef MEM_Set +# define MEM_Set(d,s,c) memset(d,s,c) +#endif + /* define DEBUG_RASTER if you want to compile a debugging version */ #define xxxDEBUG_RASTER @@ -3141,7 +3145,7 @@ *araster = &the_raster; - memset( &the_raster, sizeof ( the_raster ), 0 ); + MEM_Set( &the_raster, sizeof ( the_raster ), 0 ); ft_black_init( &the_raster ); return 0; diff --git a/src/smooth/ftgrays.c b/src/smooth/ftgrays.c index 5f2aef9fb..54a814f2a 100644 --- a/src/smooth/ftgrays.c +++ b/src/smooth/ftgrays.c @@ -140,6 +140,10 @@ #endif /* _STANDALONE_ */ +#ifndef MEM_Set +# define MEM_Set(d,s,c) memset(d,s,c) +#endif + /* define this to dump debugging information */ #define xxxDEBUG_GRAYS @@ -1228,7 +1232,7 @@ { if ( spans->coverage ) #if 1 - memset( p + spans->x, (unsigned char)spans->coverage, spans->len ); + MEM_Set( p + spans->x, (unsigned char)spans->coverage, spans->len ); #else /* 1 */ { q = p + spans->x; @@ -1968,7 +1972,7 @@ *araster = (FT_Raster)&the_raster; - memset( &the_raster, 0, sizeof ( the_raster ) ); + MEM_Set( &the_raster, 0, sizeof ( the_raster ) ); return 0; }