[base] Clean up signedness issues in `ftdbgmem.c'.

Also fix other minor issues.

* src/base/ftdbgmem.c (FT_MemTableRec): Replace all FT_ULong types
with FT_Long for consistency.
(ft_mem_primes): Change type to `FT_Int'.
(ft_mem_closest_prime, ft_mem_table_set): Updated.

(ft_mem_debug_panic, ft_mem_debug_alloc, ft_mem_debug_free,
ft_mem_debug_realloc): Use `static' keyword and fix signedness
warnings where necessary.

(ft_mem_table_resize, ft_mem_table_new, ft_mem_table_destroy,
ft_mem_table_get_nodep, ft_mem_debug_init, FT_DumpMemory): Fix types
and add or remove casts to avoid signedness warnings.
This commit is contained in:
Werner Lemberg 2015-02-16 06:37:36 +01:00
parent 6d7d636b46
commit 48186b8168
2 changed files with 81 additions and 56 deletions

View File

@ -1,3 +1,22 @@
2015-02-15 Werner Lemberg <wl@gnu.org>
[base] Clean up signedness issues in `ftdbgmem.c'.
Also fix other minor issues.
* src/base/ftdbgmem.c (FT_MemTableRec): Replace all FT_ULong types
with FT_Long for consistency.
(ft_mem_primes): Change type to `FT_Int'.
(ft_mem_closest_prime, ft_mem_table_set): Updated.
(ft_mem_debug_panic, ft_mem_debug_alloc, ft_mem_debug_free,
ft_mem_debug_realloc): Use `static' keyword and fix signedness
warnings where necessary.
(ft_mem_table_resize, ft_mem_table_new, ft_mem_table_destroy,
ft_mem_table_get_nodep, ft_mem_debug_init, FT_DumpMemory): Fix types
and add or remove casts to avoid signedness warnings.
2015-02-15 Werner Lemberg <wl@gnu.org> 2015-02-15 Werner Lemberg <wl@gnu.org>
[base] Clean up signedness in arithmetic functions. [base] Clean up signedness in arithmetic functions.

View File

@ -47,7 +47,7 @@
typedef struct FT_MemTableRec_* FT_MemTable; typedef struct FT_MemTableRec_* FT_MemTable;
#define FT_MEM_VAL( addr ) ((FT_PtrDist)(FT_Pointer)( addr )) #define FT_MEM_VAL( addr ) ( (FT_PtrDist)(FT_Pointer)( addr ) )
/* /*
* This structure holds statistics for a single allocation/release * This structure holds statistics for a single allocation/release
@ -76,7 +76,7 @@
/* /*
* We don't need a resizable array for the memory sources, because * We don't need a resizable array for the memory sources because
* their number is pretty limited within FreeType. * their number is pretty limited within FreeType.
*/ */
#define FT_MEM_SOURCE_BUCKETS 128 #define FT_MEM_SOURCE_BUCKETS 128
@ -85,8 +85,8 @@
* This structure holds information related to a single allocated * This structure holds information related to a single allocated
* memory block. If KEEPALIVE is defined, blocks that are freed by * memory block. If KEEPALIVE is defined, blocks that are freed by
* FreeType are never released to the system. Instead, their `size' * FreeType are never released to the system. Instead, their `size'
* field is set to -size. This is mainly useful to detect double frees, * field is set to `-size'. This is mainly useful to detect double
* at the price of large memory footprint during execution. * frees, at the price of a large memory footprint during execution.
*/ */
typedef struct FT_MemNodeRec_ typedef struct FT_MemNodeRec_
{ {
@ -111,20 +111,20 @@
*/ */
typedef struct FT_MemTableRec_ typedef struct FT_MemTableRec_
{ {
FT_ULong size; FT_Long size;
FT_ULong nodes; FT_Long nodes;
FT_MemNode* buckets; FT_MemNode* buckets;
FT_ULong alloc_total; FT_Long alloc_total;
FT_ULong alloc_current; FT_Long alloc_current;
FT_ULong alloc_max; FT_Long alloc_max;
FT_ULong alloc_count; FT_Long alloc_count;
FT_Bool bound_total; FT_Bool bound_total;
FT_ULong alloc_total_max; FT_Long alloc_total_max;
FT_Bool bound_count; FT_Bool bound_count;
FT_ULong alloc_count_max; FT_Long alloc_count_max;
FT_MemSource sources[FT_MEM_SOURCE_BUCKETS]; FT_MemSource sources[FT_MEM_SOURCE_BUCKETS];
@ -142,14 +142,14 @@
#define FT_MEM_SIZE_MIN 7 #define FT_MEM_SIZE_MIN 7
#define FT_MEM_SIZE_MAX 13845163 #define FT_MEM_SIZE_MAX 13845163
#define FT_FILENAME( x ) ((x) ? (x) : "unknown file") #define FT_FILENAME( x ) ( (x) ? (x) : "unknown file" )
/* /*
* Prime numbers are ugly to handle. It would be better to implement * Prime numbers are ugly to handle. It would be better to implement
* L-Hashing, which is 10% faster and doesn't require divisions. * L-Hashing, which is 10% faster and doesn't require divisions.
*/ */
static const FT_UInt ft_mem_primes[] = static const FT_Int ft_mem_primes[] =
{ {
7, 7,
11, 11,
@ -189,10 +189,10 @@
}; };
static FT_ULong static FT_Long
ft_mem_closest_prime( FT_ULong num ) ft_mem_closest_prime( FT_Long num )
{ {
FT_UInt i; size_t i;
for ( i = 0; for ( i = 0;
@ -204,7 +204,7 @@
} }
extern void static void
ft_mem_debug_panic( const char* fmt, ft_mem_debug_panic( const char* fmt,
... ) ... )
{ {
@ -254,19 +254,20 @@
static void static void
ft_mem_table_resize( FT_MemTable table ) ft_mem_table_resize( FT_MemTable table )
{ {
FT_ULong new_size; FT_Long new_size;
new_size = ft_mem_closest_prime( table->nodes ); new_size = ft_mem_closest_prime( table->nodes );
if ( new_size != table->size ) if ( new_size != table->size )
{ {
FT_MemNode* new_buckets; FT_MemNode* new_buckets;
FT_ULong i; FT_Long i;
new_buckets = (FT_MemNode *) new_buckets = (FT_MemNode *)
ft_mem_table_alloc( table, ft_mem_table_alloc(
new_size * sizeof ( FT_MemNode ) ); table,
new_size * (FT_Long)sizeof ( FT_MemNode ) );
if ( new_buckets == NULL ) if ( new_buckets == NULL )
return; return;
@ -282,7 +283,7 @@
while ( node ) while ( node )
{ {
next = node->link; next = node->link;
hash = FT_MEM_VAL( node->address ) % new_size; hash = FT_MEM_VAL( node->address ) % (FT_PtrDist)new_size;
pnode = new_buckets + hash; pnode = new_buckets + hash;
node->link = pnode[0]; node->link = pnode[0];
@ -325,8 +326,9 @@
table->free = memory->free; table->free = memory->free;
table->buckets = (FT_MemNode *) table->buckets = (FT_MemNode *)
memory->alloc( memory, memory->alloc(
table->size * sizeof ( FT_MemNode ) ); memory,
table->size * (FT_Long)sizeof ( FT_MemNode ) );
if ( table->buckets ) if ( table->buckets )
FT_ARRAY_ZERO( table->buckets, table->size ); FT_ARRAY_ZERO( table->buckets, table->size );
else else
@ -343,9 +345,9 @@
static void static void
ft_mem_table_destroy( FT_MemTable table ) ft_mem_table_destroy( FT_MemTable table )
{ {
FT_ULong i; FT_Long i;
FT_Long leak_count = 0; FT_Long leak_count = 0;
FT_ULong leaks = 0; FT_Long leaks = 0;
FT_DumpMemory( table->memory ); FT_DumpMemory( table->memory );
@ -430,7 +432,7 @@
hash = FT_MEM_VAL( address ); hash = FT_MEM_VAL( address );
pnode = table->buckets + ( hash % table->size ); pnode = table->buckets + ( hash % (FT_PtrDist)table->size );
for (;;) for (;;)
{ {
@ -466,8 +468,8 @@
if ( node == NULL ) if ( node == NULL )
break; break;
if ( node->file_name == _ft_debug_file && if ( node->file_name == _ft_debug_file &&
node->line_no == _ft_debug_lineno ) node->line_no == _ft_debug_lineno )
goto Exit; goto Exit;
pnode = &node->link; pnode = &node->link;
@ -485,11 +487,11 @@
node->max_blocks = 0; node->max_blocks = 0;
node->all_blocks = 0; node->all_blocks = 0;
node->cur_size = 0; node->cur_size = 0;
node->max_size = 0; node->max_size = 0;
node->all_size = 0; node->all_size = 0;
node->cur_max = 0; node->cur_max = 0;
node->link = NULL; node->link = NULL;
node->hash = hash; node->hash = hash;
@ -503,7 +505,7 @@
static void static void
ft_mem_table_set( FT_MemTable table, ft_mem_table_set( FT_MemTable table,
FT_Byte* address, FT_Byte* address,
FT_ULong size, FT_Long size,
FT_Long delta ) FT_Long delta )
{ {
FT_MemNode *pnode, node; FT_MemNode *pnode, node;
@ -558,7 +560,7 @@
source->max_blocks = source->cur_blocks; source->max_blocks = source->cur_blocks;
} }
if ( size > (FT_ULong)source->cur_max ) if ( size > source->cur_max )
source->cur_max = size; source->cur_max = size;
if ( delta != 0 ) if ( delta != 0 )
@ -671,7 +673,7 @@
} }
extern FT_Pointer static FT_Pointer
ft_mem_debug_alloc( FT_Memory memory, ft_mem_debug_alloc( FT_Memory memory,
FT_Long size ) FT_Long size )
{ {
@ -688,14 +690,14 @@
return NULL; return NULL;
/* return NULL if this allocation would overflow the maximum heap size */ /* return NULL if this allocation would overflow the maximum heap size */
if ( table->bound_total && if ( table->bound_total &&
table->alloc_total_max - table->alloc_current > (FT_ULong)size ) table->alloc_total_max - table->alloc_current > size )
return NULL; return NULL;
block = (FT_Byte *)ft_mem_table_alloc( table, size ); block = (FT_Byte *)ft_mem_table_alloc( table, size );
if ( block ) if ( block )
{ {
ft_mem_table_set( table, block, (FT_ULong)size, 0 ); ft_mem_table_set( table, block, size, 0 );
table->alloc_count++; table->alloc_count++;
} }
@ -707,7 +709,7 @@
} }
extern void static void
ft_mem_debug_free( FT_Memory memory, ft_mem_debug_free( FT_Memory memory,
FT_Pointer block ) FT_Pointer block )
{ {
@ -731,7 +733,7 @@
} }
extern FT_Pointer static FT_Pointer
ft_mem_debug_realloc( FT_Memory memory, ft_mem_debug_realloc( FT_Memory memory,
FT_Long cur_size, FT_Long cur_size,
FT_Long new_size, FT_Long new_size,
@ -787,21 +789,22 @@
table->alloc_count >= table->alloc_count_max ) table->alloc_count >= table->alloc_count_max )
return NULL; return NULL;
delta = (FT_Long)( new_size - cur_size ); delta = new_size - cur_size;
/* return NULL if this allocation would overflow the maximum heap size */ /* return NULL if this allocation would overflow the maximum heap size */
if ( delta > 0 && if ( delta > 0 &&
table->bound_total && table->bound_total &&
table->alloc_current + (FT_ULong)delta > table->alloc_total_max ) table->alloc_current + delta > table->alloc_total_max )
return NULL; return NULL;
new_block = (FT_Byte *)ft_mem_table_alloc( table, new_size ); new_block = (FT_Pointer)ft_mem_table_alloc( table, new_size );
if ( new_block == NULL ) if ( new_block == NULL )
return NULL; return NULL;
ft_mem_table_set( table, (FT_Byte*)new_block, new_size, delta ); ft_mem_table_set( table, (FT_Byte*)new_block, new_size, delta );
ft_memcpy( new_block, block, cur_size < new_size ? cur_size : new_size ); ft_memcpy( new_block, block, cur_size < new_size ? (size_t)cur_size
: (size_t)new_size );
ft_mem_table_remove( table, (FT_Byte*)block, delta ); ft_mem_table_remove( table, (FT_Byte*)block, delta );
@ -844,7 +847,7 @@
if ( total_max > 0 ) if ( total_max > 0 )
{ {
table->bound_total = 1; table->bound_total = 1;
table->alloc_total_max = (FT_ULong)total_max; table->alloc_total_max = total_max;
} }
} }
@ -857,7 +860,7 @@
if ( total_count > 0 ) if ( total_count > 0 )
{ {
table->bound_count = 1; table->bound_count = 1;
table->alloc_count_max = (FT_ULong)total_count; table->alloc_count_max = total_count;
} }
} }
@ -896,7 +899,6 @@
} }
static int static int
ft_mem_source_compare( const void* p1, ft_mem_source_compare( const void* p1,
const void* p2 ) const void* p2 )
@ -925,7 +927,7 @@
FT_MemSource* bucket = table->sources; FT_MemSource* bucket = table->sources;
FT_MemSource* limit = bucket + FT_MEM_SOURCE_BUCKETS; FT_MemSource* limit = bucket + FT_MEM_SOURCE_BUCKETS;
FT_MemSource* sources; FT_MemSource* sources;
FT_UInt nn, count; FT_Int nn, count;
const char* fmt; const char* fmt;
@ -939,8 +941,9 @@
count++; count++;
} }
sources = (FT_MemSource*)ft_mem_table_alloc( sources = (FT_MemSource*)
table, sizeof ( *sources ) * count ); ft_mem_table_alloc(
table, count * (FT_Long)sizeof ( *sources ) );
count = 0; count = 0;
for ( bucket = table->sources; bucket < limit; bucket++ ) for ( bucket = table->sources; bucket < limit; bucket++ )
@ -952,7 +955,10 @@
sources[count++] = source; sources[count++] = source;
} }
ft_qsort( sources, count, sizeof ( *sources ), ft_mem_source_compare ); ft_qsort( sources,
(size_t)count,
sizeof ( *sources ),
ft_mem_source_compare );
printf( "FreeType Memory Dump: " printf( "FreeType Memory Dump: "
"current=%ld max=%ld total=%ld count=%ld\n", "current=%ld max=%ld total=%ld count=%ld\n",