| OLD | NEW |
| 1 /***************************************************************************/ | 1 /***************************************************************************/ |
| 2 /* */ | 2 /* */ |
| 3 /* ftdbgmem.c */ | 3 /* ftdbgmem.c */ |
| 4 /* */ | 4 /* */ |
| 5 /* Memory debugger (body). */ | 5 /* Memory debugger (body). */ |
| 6 /* */ | 6 /* */ |
| 7 /* Copyright 2001-2006, 2009, 2013 by */ | 7 /* Copyright 2001-2015 by */ |
| 8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */ | 8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */ |
| 9 /* */ | 9 /* */ |
| 10 /* This file is part of the FreeType project, and may only be used, */ | 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 */ | 11 /* modified, and distributed under the terms of the FreeType project */ |
| 12 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */ | 12 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */ |
| 13 /* this file you indicate that you have read the license and */ | 13 /* this file you indicate that you have read the license and */ |
| 14 /* understand and accept it fully. */ | 14 /* understand and accept it fully. */ |
| 15 /* */ | 15 /* */ |
| 16 /***************************************************************************/ | 16 /***************************************************************************/ |
| 17 | 17 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 28 #ifdef FT_DEBUG_MEMORY | 28 #ifdef FT_DEBUG_MEMORY |
| 29 | 29 |
| 30 #define KEEPALIVE /* `Keep alive' means that freed blocks aren't released | 30 #define KEEPALIVE /* `Keep alive' means that freed blocks aren't released |
| 31 * to the heap. This is useful to detect double-frees | 31 * to the heap. This is useful to detect double-frees |
| 32 * or weird heap corruption, but it uses large amounts of | 32 * or weird heap corruption, but it uses large amounts of |
| 33 * memory, however. | 33 * memory, however. |
| 34 */ | 34 */ |
| 35 | 35 |
| 36 #include FT_CONFIG_STANDARD_LIBRARY_H | 36 #include FT_CONFIG_STANDARD_LIBRARY_H |
| 37 | 37 |
| 38 FT_BASE_DEF( const char* ) _ft_debug_file = 0; | 38 FT_BASE_DEF( const char* ) _ft_debug_file = NULL; |
| 39 FT_BASE_DEF( long ) _ft_debug_lineno = 0; | 39 FT_BASE_DEF( long ) _ft_debug_lineno = 0; |
| 40 | 40 |
| 41 extern void | 41 extern void |
| 42 FT_DumpMemory( FT_Memory memory ); | 42 FT_DumpMemory( FT_Memory memory ); |
| 43 | 43 |
| 44 | 44 |
| 45 typedef struct FT_MemSourceRec_* FT_MemSource; | 45 typedef struct FT_MemSourceRec_* FT_MemSource; |
| 46 typedef struct FT_MemNodeRec_* FT_MemNode; | 46 typedef struct FT_MemNodeRec_* FT_MemNode; |
| 47 typedef struct FT_MemTableRec_* FT_MemTable; | 47 typedef struct FT_MemTableRec_* FT_MemTable; |
| 48 | 48 |
| 49 | 49 |
| 50 #define FT_MEM_VAL( addr ) ((FT_PtrDist)(FT_Pointer)( addr )) | 50 #define FT_MEM_VAL( addr ) ( (FT_PtrDist)(FT_Pointer)( addr ) ) |
| 51 | 51 |
| 52 /* | 52 /* |
| 53 * This structure holds statistics for a single allocation/release | 53 * This structure holds statistics for a single allocation/release |
| 54 * site. This is useful to know where memory operations happen the | 54 * site. This is useful to know where memory operations happen the |
| 55 * most. | 55 * most. |
| 56 */ | 56 */ |
| 57 typedef struct FT_MemSourceRec_ | 57 typedef struct FT_MemSourceRec_ |
| 58 { | 58 { |
| 59 const char* file_name; | 59 const char* file_name; |
| 60 long line_no; | 60 long line_no; |
| 61 | 61 |
| 62 FT_Long cur_blocks; /* current number of allocated blocks */ | 62 FT_Long cur_blocks; /* current number of allocated blocks */ |
| 63 FT_Long max_blocks; /* max. number of allocated blocks */ | 63 FT_Long max_blocks; /* max. number of allocated blocks */ |
| 64 FT_Long all_blocks; /* total number of blocks allocated */ | 64 FT_Long all_blocks; /* total number of blocks allocated */ |
| 65 | 65 |
| 66 FT_Long cur_size; /* current cumulative allocated size */ | 66 FT_Long cur_size; /* current cumulative allocated size */ |
| 67 FT_Long max_size; /* maximum cumulative allocated size */ | 67 FT_Long max_size; /* maximum cumulative allocated size */ |
| 68 FT_Long all_size; /* total cumulative allocated size */ | 68 FT_Long all_size; /* total cumulative allocated size */ |
| 69 | 69 |
| 70 FT_Long cur_max; /* current maximum allocated size */ | 70 FT_Long cur_max; /* current maximum allocated size */ |
| 71 | 71 |
| 72 FT_UInt32 hash; | 72 FT_UInt32 hash; |
| 73 FT_MemSource link; | 73 FT_MemSource link; |
| 74 | 74 |
| 75 } FT_MemSourceRec; | 75 } FT_MemSourceRec; |
| 76 | 76 |
| 77 | 77 |
| 78 /* | 78 /* |
| 79 * We don't need a resizable array for the memory sources, because | 79 * We don't need a resizable array for the memory sources because |
| 80 * their number is pretty limited within FreeType. | 80 * their number is pretty limited within FreeType. |
| 81 */ | 81 */ |
| 82 #define FT_MEM_SOURCE_BUCKETS 128 | 82 #define FT_MEM_SOURCE_BUCKETS 128 |
| 83 | 83 |
| 84 /* | 84 /* |
| 85 * This structure holds information related to a single allocated | 85 * This structure holds information related to a single allocated |
| 86 * memory block. If KEEPALIVE is defined, blocks that are freed by | 86 * memory block. If KEEPALIVE is defined, blocks that are freed by |
| 87 * FreeType are never released to the system. Instead, their `size' | 87 * FreeType are never released to the system. Instead, their `size' |
| 88 * field is set to -size. This is mainly useful to detect double frees, | 88 * field is set to `-size'. This is mainly useful to detect double |
| 89 * at the price of large memory footprint during execution. | 89 * frees, at the price of a large memory footprint during execution. |
| 90 */ | 90 */ |
| 91 typedef struct FT_MemNodeRec_ | 91 typedef struct FT_MemNodeRec_ |
| 92 { | 92 { |
| 93 FT_Byte* address; | 93 FT_Byte* address; |
| 94 FT_Long size; /* < 0 if the block was freed */ | 94 FT_Long size; /* < 0 if the block was freed */ |
| 95 | 95 |
| 96 FT_MemSource source; | 96 FT_MemSource source; |
| 97 | 97 |
| 98 #ifdef KEEPALIVE | 98 #ifdef KEEPALIVE |
| 99 const char* free_file_name; | 99 const char* free_file_name; |
| 100 FT_Long free_line_no; | 100 FT_Long free_line_no; |
| 101 #endif | 101 #endif |
| 102 | 102 |
| 103 FT_MemNode link; | 103 FT_MemNode link; |
| 104 | 104 |
| 105 } FT_MemNodeRec; | 105 } FT_MemNodeRec; |
| 106 | 106 |
| 107 | 107 |
| 108 /* | 108 /* |
| 109 * The global structure, containing compound statistics and all hash | 109 * The global structure, containing compound statistics and all hash |
| 110 * tables. | 110 * tables. |
| 111 */ | 111 */ |
| 112 typedef struct FT_MemTableRec_ | 112 typedef struct FT_MemTableRec_ |
| 113 { | 113 { |
| 114 FT_ULong size; | 114 FT_Long size; |
| 115 FT_ULong nodes; | 115 FT_Long nodes; |
| 116 FT_MemNode* buckets; | 116 FT_MemNode* buckets; |
| 117 | 117 |
| 118 FT_ULong alloc_total; | 118 FT_Long alloc_total; |
| 119 FT_ULong alloc_current; | 119 FT_Long alloc_current; |
| 120 FT_ULong alloc_max; | 120 FT_Long alloc_max; |
| 121 FT_ULong alloc_count; | 121 FT_Long alloc_count; |
| 122 | 122 |
| 123 FT_Bool bound_total; | 123 FT_Bool bound_total; |
| 124 FT_ULong alloc_total_max; | 124 FT_Long alloc_total_max; |
| 125 | 125 |
| 126 FT_Bool bound_count; | 126 FT_Bool bound_count; |
| 127 FT_ULong alloc_count_max; | 127 FT_Long alloc_count_max; |
| 128 | 128 |
| 129 FT_MemSource sources[FT_MEM_SOURCE_BUCKETS]; | 129 FT_MemSource sources[FT_MEM_SOURCE_BUCKETS]; |
| 130 | 130 |
| 131 FT_Bool keep_alive; | 131 FT_Bool keep_alive; |
| 132 | 132 |
| 133 FT_Memory memory; | 133 FT_Memory memory; |
| 134 FT_Pointer memory_user; | 134 FT_Pointer memory_user; |
| 135 FT_Alloc_Func alloc; | 135 FT_Alloc_Func alloc; |
| 136 FT_Free_Func free; | 136 FT_Free_Func free; |
| 137 FT_Realloc_Func realloc; | 137 FT_Realloc_Func realloc; |
| 138 | 138 |
| 139 } FT_MemTableRec; | 139 } FT_MemTableRec; |
| 140 | 140 |
| 141 | 141 |
| 142 #define FT_MEM_SIZE_MIN 7 | 142 #define FT_MEM_SIZE_MIN 7 |
| 143 #define FT_MEM_SIZE_MAX 13845163 | 143 #define FT_MEM_SIZE_MAX 13845163 |
| 144 | 144 |
| 145 #define FT_FILENAME( x ) ((x) ? (x) : "unknown file") | 145 #define FT_FILENAME( x ) ( (x) ? (x) : "unknown file" ) |
| 146 | 146 |
| 147 | 147 |
| 148 /* | 148 /* |
| 149 * Prime numbers are ugly to handle. It would be better to implement | 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. | 150 * L-Hashing, which is 10% faster and doesn't require divisions. |
| 151 */ | 151 */ |
| 152 static const FT_UInt ft_mem_primes[] = | 152 static const FT_Int ft_mem_primes[] = |
| 153 { | 153 { |
| 154 7, | 154 7, |
| 155 11, | 155 11, |
| 156 19, | 156 19, |
| 157 37, | 157 37, |
| 158 73, | 158 73, |
| 159 109, | 159 109, |
| 160 163, | 160 163, |
| 161 251, | 161 251, |
| 162 367, | 162 367, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 182 1215497, | 182 1215497, |
| 183 1823231, | 183 1823231, |
| 184 2734867, | 184 2734867, |
| 185 4102283, | 185 4102283, |
| 186 6153409, | 186 6153409, |
| 187 9230113, | 187 9230113, |
| 188 13845163, | 188 13845163, |
| 189 }; | 189 }; |
| 190 | 190 |
| 191 | 191 |
| 192 static FT_ULong | 192 static FT_Long |
| 193 ft_mem_closest_prime( FT_ULong num ) | 193 ft_mem_closest_prime( FT_Long num ) |
| 194 { | 194 { |
| 195 FT_UInt i; | 195 size_t i; |
| 196 | 196 |
| 197 | 197 |
| 198 for ( i = 0; | 198 for ( i = 0; |
| 199 i < sizeof ( ft_mem_primes ) / sizeof ( ft_mem_primes[0] ); i++ ) | 199 i < sizeof ( ft_mem_primes ) / sizeof ( ft_mem_primes[0] ); i++ ) |
| 200 if ( ft_mem_primes[i] > num ) | 200 if ( ft_mem_primes[i] > num ) |
| 201 return ft_mem_primes[i]; | 201 return ft_mem_primes[i]; |
| 202 | 202 |
| 203 return FT_MEM_SIZE_MAX; | 203 return FT_MEM_SIZE_MAX; |
| 204 } | 204 } |
| 205 | 205 |
| 206 | 206 |
| 207 extern void | 207 static void |
| 208 ft_mem_debug_panic( const char* fmt, | 208 ft_mem_debug_panic( const char* fmt, |
| 209 ... ) | 209 ... ) |
| 210 { | 210 { |
| 211 va_list ap; | 211 va_list ap; |
| 212 | 212 |
| 213 | 213 |
| 214 printf( "FreeType.Debug: " ); | 214 printf( "FreeType.Debug: " ); |
| 215 | 215 |
| 216 va_start( ap, fmt ); | 216 va_start( ap, fmt ); |
| 217 vprintf( fmt, ap ); | 217 vprintf( fmt, ap ); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 247 | 247 |
| 248 memory->user = table->memory_user; | 248 memory->user = table->memory_user; |
| 249 table->free( memory, block ); | 249 table->free( memory, block ); |
| 250 memory->user = table; | 250 memory->user = table; |
| 251 } | 251 } |
| 252 | 252 |
| 253 | 253 |
| 254 static void | 254 static void |
| 255 ft_mem_table_resize( FT_MemTable table ) | 255 ft_mem_table_resize( FT_MemTable table ) |
| 256 { | 256 { |
| 257 FT_ULong new_size; | 257 FT_Long new_size; |
| 258 | 258 |
| 259 | 259 |
| 260 new_size = ft_mem_closest_prime( table->nodes ); | 260 new_size = ft_mem_closest_prime( table->nodes ); |
| 261 if ( new_size != table->size ) | 261 if ( new_size != table->size ) |
| 262 { | 262 { |
| 263 FT_MemNode* new_buckets; | 263 FT_MemNode* new_buckets; |
| 264 FT_ULong i; | 264 FT_Long i; |
| 265 | 265 |
| 266 | 266 |
| 267 new_buckets = (FT_MemNode *) | 267 new_buckets = (FT_MemNode *) |
| 268 ft_mem_table_alloc( table, | 268 ft_mem_table_alloc( |
| 269 new_size * sizeof ( FT_MemNode ) ); | 269 table, |
| 270 new_size * (FT_Long)sizeof ( FT_MemNode ) ); |
| 270 if ( new_buckets == NULL ) | 271 if ( new_buckets == NULL ) |
| 271 return; | 272 return; |
| 272 | 273 |
| 273 FT_ARRAY_ZERO( new_buckets, new_size ); | 274 FT_ARRAY_ZERO( new_buckets, new_size ); |
| 274 | 275 |
| 275 for ( i = 0; i < table->size; i++ ) | 276 for ( i = 0; i < table->size; i++ ) |
| 276 { | 277 { |
| 277 FT_MemNode node, next, *pnode; | 278 FT_MemNode node, next, *pnode; |
| 278 FT_PtrDist hash; | 279 FT_PtrDist hash; |
| 279 | 280 |
| 280 | 281 |
| 281 node = table->buckets[i]; | 282 node = table->buckets[i]; |
| 282 while ( node ) | 283 while ( node ) |
| 283 { | 284 { |
| 284 next = node->link; | 285 next = node->link; |
| 285 hash = FT_MEM_VAL( node->address ) % new_size; | 286 hash = FT_MEM_VAL( node->address ) % (FT_PtrDist)new_size; |
| 286 pnode = new_buckets + hash; | 287 pnode = new_buckets + hash; |
| 287 | 288 |
| 288 node->link = pnode[0]; | 289 node->link = pnode[0]; |
| 289 pnode[0] = node; | 290 pnode[0] = node; |
| 290 | 291 |
| 291 node = next; | 292 node = next; |
| 292 } | 293 } |
| 293 } | 294 } |
| 294 | 295 |
| 295 if ( table->buckets ) | 296 if ( table->buckets ) |
| (...skipping 22 matching lines...) Expand all Loading... |
| 318 | 319 |
| 319 table->memory = memory; | 320 table->memory = memory; |
| 320 | 321 |
| 321 table->memory_user = memory->user; | 322 table->memory_user = memory->user; |
| 322 | 323 |
| 323 table->alloc = memory->alloc; | 324 table->alloc = memory->alloc; |
| 324 table->realloc = memory->realloc; | 325 table->realloc = memory->realloc; |
| 325 table->free = memory->free; | 326 table->free = memory->free; |
| 326 | 327 |
| 327 table->buckets = (FT_MemNode *) | 328 table->buckets = (FT_MemNode *) |
| 328 memory->alloc( memory, | 329 memory->alloc( |
| 329 table->size * sizeof ( FT_MemNode ) ); | 330 memory, |
| 331 table->size * (FT_Long)sizeof ( FT_MemNode ) ); |
| 330 if ( table->buckets ) | 332 if ( table->buckets ) |
| 331 FT_ARRAY_ZERO( table->buckets, table->size ); | 333 FT_ARRAY_ZERO( table->buckets, table->size ); |
| 332 else | 334 else |
| 333 { | 335 { |
| 334 memory->free( memory, table ); | 336 memory->free( memory, table ); |
| 335 table = NULL; | 337 table = NULL; |
| 336 } | 338 } |
| 337 | 339 |
| 338 Exit: | 340 Exit: |
| 339 return table; | 341 return table; |
| 340 } | 342 } |
| 341 | 343 |
| 342 | 344 |
| 343 static void | 345 static void |
| 344 ft_mem_table_destroy( FT_MemTable table ) | 346 ft_mem_table_destroy( FT_MemTable table ) |
| 345 { | 347 { |
| 346 FT_ULong i; | 348 FT_Long i; |
| 347 FT_Long leak_count = 0; | 349 FT_Long leak_count = 0; |
| 348 FT_ULong leaks = 0; | 350 FT_Long leaks = 0; |
| 349 | 351 |
| 350 | 352 |
| 351 FT_DumpMemory( table->memory ); | 353 FT_DumpMemory( table->memory ); |
| 352 | 354 |
| 353 /* remove all blocks from the table, revealing leaked ones */ | 355 /* remove all blocks from the table, revealing leaked ones */ |
| 354 for ( i = 0; i < table->size; i++ ) | 356 for ( i = 0; i < table->size; i++ ) |
| 355 { | 357 { |
| 356 FT_MemNode *pnode = table->buckets + i, next, node = *pnode; | 358 FT_MemNode *pnode = table->buckets + i, next, node = *pnode; |
| 357 | 359 |
| 358 | 360 |
| 359 while ( node ) | 361 while ( node ) |
| 360 { | 362 { |
| 361 next = node->link; | 363 next = node->link; |
| 362 node->link = 0; | 364 node->link = NULL; |
| 363 | 365 |
| 364 if ( node->size > 0 ) | 366 if ( node->size > 0 ) |
| 365 { | 367 { |
| 366 printf( | 368 printf( |
| 367 "leaked memory block at address %p, size %8ld in (%s:%ld)\n", | 369 "leaked memory block at address %p, size %8ld in (%s:%ld)\n", |
| 368 node->address, node->size, | 370 node->address, node->size, |
| 369 FT_FILENAME( node->source->file_name ), | 371 FT_FILENAME( node->source->file_name ), |
| 370 node->source->line_no ); | 372 node->source->line_no ); |
| 371 | 373 |
| 372 leak_count++; | 374 leak_count++; |
| 373 leaks += node->size; | 375 leaks += node->size; |
| 374 | 376 |
| 375 ft_mem_table_free( table, node->address ); | 377 ft_mem_table_free( table, node->address ); |
| 376 } | 378 } |
| 377 | 379 |
| 378 node->address = NULL; | 380 node->address = NULL; |
| 379 node->size = 0; | 381 node->size = 0; |
| 380 | 382 |
| 381 ft_mem_table_free( table, node ); | 383 ft_mem_table_free( table, node ); |
| 382 node = next; | 384 node = next; |
| 383 } | 385 } |
| 384 table->buckets[i] = 0; | 386 table->buckets[i] = NULL; |
| 385 } | 387 } |
| 386 | 388 |
| 387 ft_mem_table_free( table, table->buckets ); | 389 ft_mem_table_free( table, table->buckets ); |
| 388 table->buckets = NULL; | 390 table->buckets = NULL; |
| 389 | 391 |
| 390 table->size = 0; | 392 table->size = 0; |
| 391 table->nodes = 0; | 393 table->nodes = 0; |
| 392 | 394 |
| 393 /* remove all sources */ | 395 /* remove all sources */ |
| 394 for ( i = 0; i < FT_MEM_SOURCE_BUCKETS; i++ ) | 396 for ( i = 0; i < FT_MEM_SOURCE_BUCKETS; i++ ) |
| (...skipping 28 matching lines...) Expand all Loading... |
| 423 | 425 |
| 424 static FT_MemNode* | 426 static FT_MemNode* |
| 425 ft_mem_table_get_nodep( FT_MemTable table, | 427 ft_mem_table_get_nodep( FT_MemTable table, |
| 426 FT_Byte* address ) | 428 FT_Byte* address ) |
| 427 { | 429 { |
| 428 FT_PtrDist hash; | 430 FT_PtrDist hash; |
| 429 FT_MemNode *pnode, node; | 431 FT_MemNode *pnode, node; |
| 430 | 432 |
| 431 | 433 |
| 432 hash = FT_MEM_VAL( address ); | 434 hash = FT_MEM_VAL( address ); |
| 433 pnode = table->buckets + ( hash % table->size ); | 435 pnode = table->buckets + ( hash % (FT_PtrDist)table->size ); |
| 434 | 436 |
| 435 for (;;) | 437 for (;;) |
| 436 { | 438 { |
| 437 node = pnode[0]; | 439 node = pnode[0]; |
| 438 if ( !node ) | 440 if ( !node ) |
| 439 break; | 441 break; |
| 440 | 442 |
| 441 if ( node->address == address ) | 443 if ( node->address == address ) |
| 442 break; | 444 break; |
| 443 | 445 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 459 hash = (FT_UInt32)(FT_PtrDist)(void*)_ft_debug_file + | 461 hash = (FT_UInt32)(FT_PtrDist)(void*)_ft_debug_file + |
| 460 (FT_UInt32)( 5 * _ft_debug_lineno ); | 462 (FT_UInt32)( 5 * _ft_debug_lineno ); |
| 461 pnode = &table->sources[hash % FT_MEM_SOURCE_BUCKETS]; | 463 pnode = &table->sources[hash % FT_MEM_SOURCE_BUCKETS]; |
| 462 | 464 |
| 463 for ( ;; ) | 465 for ( ;; ) |
| 464 { | 466 { |
| 465 node = *pnode; | 467 node = *pnode; |
| 466 if ( node == NULL ) | 468 if ( node == NULL ) |
| 467 break; | 469 break; |
| 468 | 470 |
| 469 if ( node->file_name == _ft_debug_file && | 471 if ( node->file_name == _ft_debug_file && |
| 470 node->line_no == _ft_debug_lineno ) | 472 node->line_no == _ft_debug_lineno ) |
| 471 goto Exit; | 473 goto Exit; |
| 472 | 474 |
| 473 pnode = &node->link; | 475 pnode = &node->link; |
| 474 } | 476 } |
| 475 | 477 |
| 476 node = (FT_MemSource)ft_mem_table_alloc( table, sizeof ( *node ) ); | 478 node = (FT_MemSource)ft_mem_table_alloc( table, sizeof ( *node ) ); |
| 477 if ( node == NULL ) | 479 if ( node == NULL ) |
| 478 ft_mem_debug_panic( | 480 ft_mem_debug_panic( |
| 479 "not enough memory to perform memory debugging\n" ); | 481 "not enough memory to perform memory debugging\n" ); |
| 480 | 482 |
| 481 node->file_name = _ft_debug_file; | 483 node->file_name = _ft_debug_file; |
| 482 node->line_no = _ft_debug_lineno; | 484 node->line_no = _ft_debug_lineno; |
| 483 | 485 |
| 484 node->cur_blocks = 0; | 486 node->cur_blocks = 0; |
| 485 node->max_blocks = 0; | 487 node->max_blocks = 0; |
| 486 node->all_blocks = 0; | 488 node->all_blocks = 0; |
| 487 | 489 |
| 488 node->cur_size = 0; | 490 node->cur_size = 0; |
| 489 node->max_size = 0; | 491 node->max_size = 0; |
| 490 node->all_size = 0; | 492 node->all_size = 0; |
| 491 | 493 |
| 492 node->cur_max = 0; | 494 node->cur_max = 0; |
| 493 | 495 |
| 494 node->link = NULL; | 496 node->link = NULL; |
| 495 node->hash = hash; | 497 node->hash = hash; |
| 496 *pnode = node; | 498 *pnode = node; |
| 497 | 499 |
| 498 Exit: | 500 Exit: |
| 499 return node; | 501 return node; |
| 500 } | 502 } |
| 501 | 503 |
| 502 | 504 |
| 503 static void | 505 static void |
| 504 ft_mem_table_set( FT_MemTable table, | 506 ft_mem_table_set( FT_MemTable table, |
| 505 FT_Byte* address, | 507 FT_Byte* address, |
| 506 FT_ULong size, | 508 FT_Long size, |
| 507 FT_Long delta ) | 509 FT_Long delta ) |
| 508 { | 510 { |
| 509 FT_MemNode *pnode, node; | 511 FT_MemNode *pnode, node; |
| 510 | 512 |
| 511 | 513 |
| 512 if ( table ) | 514 if ( table ) |
| 513 { | 515 { |
| 514 FT_MemSource source; | 516 FT_MemSource source; |
| 515 | 517 |
| 516 | 518 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 551 | 553 |
| 552 if ( delta == 0 ) | 554 if ( delta == 0 ) |
| 553 { | 555 { |
| 554 /* this is an allocation */ | 556 /* this is an allocation */ |
| 555 source->all_blocks++; | 557 source->all_blocks++; |
| 556 source->cur_blocks++; | 558 source->cur_blocks++; |
| 557 if ( source->cur_blocks > source->max_blocks ) | 559 if ( source->cur_blocks > source->max_blocks ) |
| 558 source->max_blocks = source->cur_blocks; | 560 source->max_blocks = source->cur_blocks; |
| 559 } | 561 } |
| 560 | 562 |
| 561 if ( size > (FT_ULong)source->cur_max ) | 563 if ( size > source->cur_max ) |
| 562 source->cur_max = size; | 564 source->cur_max = size; |
| 563 | 565 |
| 564 if ( delta != 0 ) | 566 if ( delta != 0 ) |
| 565 { | 567 { |
| 566 /* we are growing or shrinking a reallocated block */ | 568 /* we are growing or shrinking a reallocated block */ |
| 567 source->cur_size += delta; | 569 source->cur_size += delta; |
| 568 table->alloc_current += delta; | 570 table->alloc_current += delta; |
| 569 } | 571 } |
| 570 else | 572 else |
| 571 { | 573 { |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 664 } | 666 } |
| 665 else | 667 else |
| 666 ft_mem_debug_panic( | 668 ft_mem_debug_panic( |
| 667 "trying to free unknown block at %p in (%s:%ld)\n", | 669 "trying to free unknown block at %p in (%s:%ld)\n", |
| 668 address, | 670 address, |
| 669 FT_FILENAME( _ft_debug_file ), _ft_debug_lineno ); | 671 FT_FILENAME( _ft_debug_file ), _ft_debug_lineno ); |
| 670 } | 672 } |
| 671 } | 673 } |
| 672 | 674 |
| 673 | 675 |
| 674 extern FT_Pointer | 676 static FT_Pointer |
| 675 ft_mem_debug_alloc( FT_Memory memory, | 677 ft_mem_debug_alloc( FT_Memory memory, |
| 676 FT_Long size ) | 678 FT_Long size ) |
| 677 { | 679 { |
| 678 FT_MemTable table = (FT_MemTable)memory->user; | 680 FT_MemTable table = (FT_MemTable)memory->user; |
| 679 FT_Byte* block; | 681 FT_Byte* block; |
| 680 | 682 |
| 681 | 683 |
| 682 if ( size <= 0 ) | 684 if ( size <= 0 ) |
| 683 ft_mem_debug_panic( "negative block size allocation (%ld)", size ); | 685 ft_mem_debug_panic( "negative block size allocation (%ld)", size ); |
| 684 | 686 |
| 685 /* return NULL if the maximum number of allocations was reached */ | 687 /* return NULL if the maximum number of allocations was reached */ |
| 686 if ( table->bound_count && | 688 if ( table->bound_count && |
| 687 table->alloc_count >= table->alloc_count_max ) | 689 table->alloc_count >= table->alloc_count_max ) |
| 688 return NULL; | 690 return NULL; |
| 689 | 691 |
| 690 /* return NULL if this allocation would overflow the maximum heap size */ | 692 /* return NULL if this allocation would overflow the maximum heap size */ |
| 691 if ( table->bound_total && | 693 if ( table->bound_total && |
| 692 table->alloc_total_max - table->alloc_current > (FT_ULong)size ) | 694 table->alloc_total_max - table->alloc_current > size ) |
| 693 return NULL; | 695 return NULL; |
| 694 | 696 |
| 695 block = (FT_Byte *)ft_mem_table_alloc( table, size ); | 697 block = (FT_Byte *)ft_mem_table_alloc( table, size ); |
| 696 if ( block ) | 698 if ( block ) |
| 697 { | 699 { |
| 698 ft_mem_table_set( table, block, (FT_ULong)size, 0 ); | 700 ft_mem_table_set( table, block, size, 0 ); |
| 699 | 701 |
| 700 table->alloc_count++; | 702 table->alloc_count++; |
| 701 } | 703 } |
| 702 | 704 |
| 703 _ft_debug_file = "<unknown>"; | 705 _ft_debug_file = "<unknown>"; |
| 704 _ft_debug_lineno = 0; | 706 _ft_debug_lineno = 0; |
| 705 | 707 |
| 706 return (FT_Pointer)block; | 708 return (FT_Pointer)block; |
| 707 } | 709 } |
| 708 | 710 |
| 709 | 711 |
| 710 extern void | 712 static void |
| 711 ft_mem_debug_free( FT_Memory memory, | 713 ft_mem_debug_free( FT_Memory memory, |
| 712 FT_Pointer block ) | 714 FT_Pointer block ) |
| 713 { | 715 { |
| 714 FT_MemTable table = (FT_MemTable)memory->user; | 716 FT_MemTable table = (FT_MemTable)memory->user; |
| 715 | 717 |
| 716 | 718 |
| 717 if ( block == NULL ) | 719 if ( block == NULL ) |
| 718 ft_mem_debug_panic( "trying to free NULL in (%s:%ld)", | 720 ft_mem_debug_panic( "trying to free NULL in (%s:%ld)", |
| 719 FT_FILENAME( _ft_debug_file ), | 721 FT_FILENAME( _ft_debug_file ), |
| 720 _ft_debug_lineno ); | 722 _ft_debug_lineno ); |
| 721 | 723 |
| 722 ft_mem_table_remove( table, (FT_Byte*)block, 0 ); | 724 ft_mem_table_remove( table, (FT_Byte*)block, 0 ); |
| 723 | 725 |
| 724 if ( !table->keep_alive ) | 726 if ( !table->keep_alive ) |
| 725 ft_mem_table_free( table, block ); | 727 ft_mem_table_free( table, block ); |
| 726 | 728 |
| 727 table->alloc_count--; | 729 table->alloc_count--; |
| 728 | 730 |
| 729 _ft_debug_file = "<unknown>"; | 731 _ft_debug_file = "<unknown>"; |
| 730 _ft_debug_lineno = 0; | 732 _ft_debug_lineno = 0; |
| 731 } | 733 } |
| 732 | 734 |
| 733 | 735 |
| 734 extern FT_Pointer | 736 static FT_Pointer |
| 735 ft_mem_debug_realloc( FT_Memory memory, | 737 ft_mem_debug_realloc( FT_Memory memory, |
| 736 FT_Long cur_size, | 738 FT_Long cur_size, |
| 737 FT_Long new_size, | 739 FT_Long new_size, |
| 738 FT_Pointer block ) | 740 FT_Pointer block ) |
| 739 { | 741 { |
| 740 FT_MemTable table = (FT_MemTable)memory->user; | 742 FT_MemTable table = (FT_MemTable)memory->user; |
| 741 FT_MemNode node, *pnode; | 743 FT_MemNode node, *pnode; |
| 742 FT_Pointer new_block; | 744 FT_Pointer new_block; |
| 743 FT_Long delta; | 745 FT_Long delta; |
| 744 | 746 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 780 if ( node->size != cur_size ) | 782 if ( node->size != cur_size ) |
| 781 ft_mem_debug_panic( "invalid ft_realloc request for %p. cur_size is " | 783 ft_mem_debug_panic( "invalid ft_realloc request for %p. cur_size is " |
| 782 "%ld instead of %ld in (%s:%ld)", | 784 "%ld instead of %ld in (%s:%ld)", |
| 783 block, cur_size, node->size, file_name, line_no ); | 785 block, cur_size, node->size, file_name, line_no ); |
| 784 | 786 |
| 785 /* return NULL if the maximum number of allocations was reached */ | 787 /* return NULL if the maximum number of allocations was reached */ |
| 786 if ( table->bound_count && | 788 if ( table->bound_count && |
| 787 table->alloc_count >= table->alloc_count_max ) | 789 table->alloc_count >= table->alloc_count_max ) |
| 788 return NULL; | 790 return NULL; |
| 789 | 791 |
| 790 delta = (FT_Long)( new_size - cur_size ); | 792 delta = new_size - cur_size; |
| 791 | 793 |
| 792 /* return NULL if this allocation would overflow the maximum heap size */ | 794 /* return NULL if this allocation would overflow the maximum heap size */ |
| 793 if ( delta > 0 && | 795 if ( delta > 0 && |
| 794 table->bound_total && | 796 table->bound_total && |
| 795 table->alloc_current + (FT_ULong)delta > table->alloc_total_max ) | 797 table->alloc_current + delta > table->alloc_total_max ) |
| 796 return NULL; | 798 return NULL; |
| 797 | 799 |
| 798 new_block = (FT_Byte *)ft_mem_table_alloc( table, new_size ); | 800 new_block = (FT_Pointer)ft_mem_table_alloc( table, new_size ); |
| 799 if ( new_block == NULL ) | 801 if ( new_block == NULL ) |
| 800 return NULL; | 802 return NULL; |
| 801 | 803 |
| 802 ft_mem_table_set( table, (FT_Byte*)new_block, new_size, delta ); | 804 ft_mem_table_set( table, (FT_Byte*)new_block, new_size, delta ); |
| 803 | 805 |
| 804 ft_memcpy( new_block, block, cur_size < new_size ? cur_size : new_size ); | 806 ft_memcpy( new_block, block, cur_size < new_size ? (size_t)cur_size |
| 807 : (size_t)new_size ); |
| 805 | 808 |
| 806 ft_mem_table_remove( table, (FT_Byte*)block, delta ); | 809 ft_mem_table_remove( table, (FT_Byte*)block, delta ); |
| 807 | 810 |
| 808 _ft_debug_file = "<unknown>"; | 811 _ft_debug_file = "<unknown>"; |
| 809 _ft_debug_lineno = 0; | 812 _ft_debug_lineno = 0; |
| 810 | 813 |
| 811 if ( !table->keep_alive ) | 814 if ( !table->keep_alive ) |
| 812 ft_mem_table_free( table, block ); | 815 ft_mem_table_free( table, block ); |
| 813 | 816 |
| 814 return new_block; | 817 return new_block; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 837 | 840 |
| 838 p = getenv( "FT2_ALLOC_TOTAL_MAX" ); | 841 p = getenv( "FT2_ALLOC_TOTAL_MAX" ); |
| 839 if ( p != NULL ) | 842 if ( p != NULL ) |
| 840 { | 843 { |
| 841 FT_Long total_max = ft_atol( p ); | 844 FT_Long total_max = ft_atol( p ); |
| 842 | 845 |
| 843 | 846 |
| 844 if ( total_max > 0 ) | 847 if ( total_max > 0 ) |
| 845 { | 848 { |
| 846 table->bound_total = 1; | 849 table->bound_total = 1; |
| 847 table->alloc_total_max = (FT_ULong)total_max; | 850 table->alloc_total_max = total_max; |
| 848 } | 851 } |
| 849 } | 852 } |
| 850 | 853 |
| 851 p = getenv( "FT2_ALLOC_COUNT_MAX" ); | 854 p = getenv( "FT2_ALLOC_COUNT_MAX" ); |
| 852 if ( p != NULL ) | 855 if ( p != NULL ) |
| 853 { | 856 { |
| 854 FT_Long total_count = ft_atol( p ); | 857 FT_Long total_count = ft_atol( p ); |
| 855 | 858 |
| 856 | 859 |
| 857 if ( total_count > 0 ) | 860 if ( total_count > 0 ) |
| 858 { | 861 { |
| 859 table->bound_count = 1; | 862 table->bound_count = 1; |
| 860 table->alloc_count_max = (FT_ULong)total_count; | 863 table->alloc_count_max = total_count; |
| 861 } | 864 } |
| 862 } | 865 } |
| 863 | 866 |
| 864 p = getenv( "FT2_KEEP_ALIVE" ); | 867 p = getenv( "FT2_KEEP_ALIVE" ); |
| 865 if ( p != NULL ) | 868 if ( p != NULL ) |
| 866 { | 869 { |
| 867 FT_Long keep_alive = ft_atol( p ); | 870 FT_Long keep_alive = ft_atol( p ); |
| 868 | 871 |
| 869 | 872 |
| 870 if ( keep_alive > 0 ) | 873 if ( keep_alive > 0 ) |
| (...skipping 18 matching lines...) Expand all Loading... |
| 889 memory->free = table->free; | 892 memory->free = table->free; |
| 890 memory->realloc = table->realloc; | 893 memory->realloc = table->realloc; |
| 891 memory->alloc = table->alloc; | 894 memory->alloc = table->alloc; |
| 892 | 895 |
| 893 ft_mem_table_destroy( table ); | 896 ft_mem_table_destroy( table ); |
| 894 memory->user = NULL; | 897 memory->user = NULL; |
| 895 } | 898 } |
| 896 } | 899 } |
| 897 | 900 |
| 898 | 901 |
| 899 | |
| 900 static int | 902 static int |
| 901 ft_mem_source_compare( const void* p1, | 903 ft_mem_source_compare( const void* p1, |
| 902 const void* p2 ) | 904 const void* p2 ) |
| 903 { | 905 { |
| 904 FT_MemSource s1 = *(FT_MemSource*)p1; | 906 FT_MemSource s1 = *(FT_MemSource*)p1; |
| 905 FT_MemSource s2 = *(FT_MemSource*)p2; | 907 FT_MemSource s2 = *(FT_MemSource*)p2; |
| 906 | 908 |
| 907 | 909 |
| 908 if ( s2->max_size > s1->max_size ) | 910 if ( s2->max_size > s1->max_size ) |
| 909 return 1; | 911 return 1; |
| 910 else if ( s2->max_size < s1->max_size ) | 912 else if ( s2->max_size < s1->max_size ) |
| 911 return -1; | 913 return -1; |
| 912 else | 914 else |
| 913 return 0; | 915 return 0; |
| 914 } | 916 } |
| 915 | 917 |
| 916 | 918 |
| 917 extern void | 919 extern void |
| 918 FT_DumpMemory( FT_Memory memory ) | 920 FT_DumpMemory( FT_Memory memory ) |
| 919 { | 921 { |
| 920 FT_MemTable table = (FT_MemTable)memory->user; | 922 FT_MemTable table = (FT_MemTable)memory->user; |
| 921 | 923 |
| 922 | 924 |
| 923 if ( table ) | 925 if ( table ) |
| 924 { | 926 { |
| 925 FT_MemSource* bucket = table->sources; | 927 FT_MemSource* bucket = table->sources; |
| 926 FT_MemSource* limit = bucket + FT_MEM_SOURCE_BUCKETS; | 928 FT_MemSource* limit = bucket + FT_MEM_SOURCE_BUCKETS; |
| 927 FT_MemSource* sources; | 929 FT_MemSource* sources; |
| 928 FT_UInt nn, count; | 930 FT_Int nn, count; |
| 929 const char* fmt; | 931 const char* fmt; |
| 930 | 932 |
| 931 | 933 |
| 932 count = 0; | 934 count = 0; |
| 933 for ( ; bucket < limit; bucket++ ) | 935 for ( ; bucket < limit; bucket++ ) |
| 934 { | 936 { |
| 935 FT_MemSource source = *bucket; | 937 FT_MemSource source = *bucket; |
| 936 | 938 |
| 937 | 939 |
| 938 for ( ; source; source = source->link ) | 940 for ( ; source; source = source->link ) |
| 939 count++; | 941 count++; |
| 940 } | 942 } |
| 941 | 943 |
| 942 sources = (FT_MemSource*)ft_mem_table_alloc( | 944 sources = (FT_MemSource*) |
| 943 table, sizeof ( *sources ) * count ); | 945 ft_mem_table_alloc( |
| 946 table, count * (FT_Long)sizeof ( *sources ) ); |
| 944 | 947 |
| 945 count = 0; | 948 count = 0; |
| 946 for ( bucket = table->sources; bucket < limit; bucket++ ) | 949 for ( bucket = table->sources; bucket < limit; bucket++ ) |
| 947 { | 950 { |
| 948 FT_MemSource source = *bucket; | 951 FT_MemSource source = *bucket; |
| 949 | 952 |
| 950 | 953 |
| 951 for ( ; source; source = source->link ) | 954 for ( ; source; source = source->link ) |
| 952 sources[count++] = source; | 955 sources[count++] = source; |
| 953 } | 956 } |
| 954 | 957 |
| 955 ft_qsort( sources, count, sizeof ( *sources ), ft_mem_source_compare ); | 958 ft_qsort( sources, |
| 959 (size_t)count, |
| 960 sizeof ( *sources ), |
| 961 ft_mem_source_compare ); |
| 956 | 962 |
| 957 printf( "FreeType Memory Dump: " | 963 printf( "FreeType Memory Dump: " |
| 958 "current=%ld max=%ld total=%ld count=%ld\n", | 964 "current=%ld max=%ld total=%ld count=%ld\n", |
| 959 table->alloc_current, table->alloc_max, | 965 table->alloc_current, table->alloc_max, |
| 960 table->alloc_total, table->alloc_count ); | 966 table->alloc_total, table->alloc_count ); |
| 961 printf( " block block sizes sizes sizes source\n" ); | 967 printf( " block block sizes sizes sizes source\n" ); |
| 962 printf( " count high sum highsum max location\n" ); | 968 printf( " count high sum highsum max location\n" ); |
| 963 printf( "-------------------------------------------------\n" ); | 969 printf( "-------------------------------------------------\n" ); |
| 964 | 970 |
| 965 fmt = "%6ld %6ld %8ld %8ld %8ld %s:%d\n"; | 971 fmt = "%6ld %6ld %8ld %8ld %8ld %s:%d\n"; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 983 | 989 |
| 984 #else /* !FT_DEBUG_MEMORY */ | 990 #else /* !FT_DEBUG_MEMORY */ |
| 985 | 991 |
| 986 /* ANSI C doesn't like empty source files */ | 992 /* ANSI C doesn't like empty source files */ |
| 987 typedef int _debug_mem_dummy; | 993 typedef int _debug_mem_dummy; |
| 988 | 994 |
| 989 #endif /* !FT_DEBUG_MEMORY */ | 995 #endif /* !FT_DEBUG_MEMORY */ |
| 990 | 996 |
| 991 | 997 |
| 992 /* END */ | 998 /* END */ |
| OLD | NEW |