* include/freetype/internal/ftmemory.h, src/base/ftdbgmem.c: improvements to the memory debugger to report more information in case of errors. Also, some allocations that occured through REALLOC couldn't be previously catched correctly..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
diff --git a/ChangeLog b/ChangeLog
index b182663..801fa1c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
2001-10-23 David Turner <david@freetype.org>
+ * include/freetype/internal/ftmemory.h, src/base/ftdbgmem.c:
+ improvements to the memory debugger to report more information in
+ case of errors. Also, some allocations that occured through
+ REALLOC couldn't be previously catched correctly..
+
+
* src/autohint/ahglyph.c, src/raster/ftraster.c,
src/smooth/ftgrays.c: replaced liberal uses of "memset" by the
MEM_Set macro instead..
diff --git a/include/freetype/internal/ftmemory.h b/include/freetype/internal/ftmemory.h
index afdd98c..ecd2306 100644
--- a/include/freetype/internal/ftmemory.h
+++ b/include/freetype/internal/ftmemory.h
@@ -63,6 +63,21 @@ FT_BEGIN_HEADER
void* *P,
const char* file_name,
FT_Long line_no );
+
+ FT_BASE( FT_Error )
+ FT_Realloc_Debug( FT_Memory memory,
+ FT_Long current,
+ FT_Long size,
+ void* *P,
+ const char* file_name,
+ FT_Long line_no );
+
+ FT_BASE( void )
+ FT_Free_Debug( FT_Memory memory,
+ FT_Pointer block,
+ const char* file_name,
+ FT_Long line_no );
+
#endif
@@ -191,6 +206,16 @@ FT_BEGIN_HEADER
# define MEM_Alloc_Array( _pointer_, _count_, _type_ ) \
FT_Alloc_Debug( memory, (_count_)*sizeof ( _type_ ), \
(void**)&(_pointer_), __FILE__, __LINE__ )
+
+# define MEM_Realloc( _pointer_, _current_, _size_ ) \
+ FT_Realloc_Debug( memory, _current_, _size_, (void**)&(_pointer_), __FILE__, __LINE__ )
+
+# define MEM_Realloc_Array( _pointer_, _current_, _new_, _type_ ) \
+ FT_Realloc_Debug( memory, (_current_)*sizeof ( _type_ ), \
+ (_new_)*sizeof ( _type_ ), (void**)&(_pointer_), __FILE__, __LINE__ )
+
+# define MEM_Free( _pointer_ ) \
+ FT_Free_Debug( memory, (void**)&(_pointer_), __FILE__, __LINE__ )
#else /* !FT_DEBUG_MEMORY */
@@ -201,15 +226,19 @@ FT_BEGIN_HEADER
FT_Alloc( memory, (_count_)*sizeof ( _type_ ), \
(void**)&(_pointer_) )
-#endif /* !FT_DEBUG_MEMORY */
-
-#define MEM_Realloc( _pointer_, _current_, _size_ ) \
- FT_Realloc( memory, _current_, _size_, (void**)&(_pointer_) )
+# define MEM_Realloc( _pointer_, _current_, _size_ ) \
+ FT_Realloc( memory, _current_, _size_, (void**)&(_pointer_) )
-#define MEM_Realloc_Array( _pointer_, _current_, _new_, _type_ ) \
- FT_Realloc( memory, (_current_)*sizeof ( _type_ ), \
+# define MEM_Realloc_Array( _pointer_, _current_, _new_, _type_ ) \
+ FT_Realloc( memory, (_current_)*sizeof ( _type_ ), \
(_new_)*sizeof ( _type_ ), (void**)&(_pointer_) )
+# define MEM_Free( _pointer_ ) \
+ FT_Free( memory, (void**)&(_pointer_) )
+
+#endif /* !FT_DEBUG_MEMORY */
+
+
#define ALLOC( _pointer_, _size_ ) \
FT_SET_ERROR( MEM_Alloc( _pointer_, _size_ ) )
@@ -225,7 +254,8 @@ FT_BEGIN_HEADER
(_current_)*sizeof ( _type_ ), \
(_count_)*sizeof ( _type_ ) ) )
-#define FREE( _pointer_ ) FT_Free( memory, (void**)&(_pointer_) )
+#define FREE( _pointer_ ) \
+ MEM_Free( _pointer_ )
FT_END_HEADER
diff --git a/src/base/ftdbgmem.c b/src/base/ftdbgmem.c
index 93198a8..048df3e 100644
--- a/src/base/ftdbgmem.c
+++ b/src/base/ftdbgmem.c
@@ -23,8 +23,11 @@
FT_Byte* address;
FT_Long size; /* < 0 if the block was freed */
- const char* file_name;
- FT_Long line_no;
+ const char* alloc_file_name;
+ FT_Long alloc_line_no;
+
+ const char* free_file_name;
+ FT_Long free_line_no;
FT_MemNode link;
@@ -54,6 +57,8 @@
#define FT_MEM_SIZE_MIN 7
#define FT_MEM_SIZE_MAX 13845163
+#define FT_FILENAME(x) ((x) ? (x) : "unknown file")
+
static const FT_UInt ft_mem_primes[] =
{
7,
@@ -103,7 +108,7 @@
va_list ap;
- printf( "FreeType.DebugMemory: " );
+ printf( "FreeType.Debug: " );
va_start( ap, fmt );
vprintf( fmt, ap );
@@ -256,10 +261,10 @@
if ( node->size > 0 )
{
- printf( "leaked memory block at address %p, size %8ld (%s:%d)\n",
+ printf( "leaked memory block at address %p, size %8ld in (%s:%d)\n",
node->address, node->size,
- node->file_name ? node->file_name : "unknown_file",
- node->line_no );
+ FT_FILENAME( node->alloc_file_name ),
+ node->alloc_line_no );
leak_count++;
leaks += node->size;
@@ -337,15 +342,13 @@
{
/* this block was already freed. this means that our memory is */
/* now completely corrupted !! */
- ft_mem_debug_panic( "memory heap corrupted" );
+ ft_mem_debug_panic( "memory heap corrupted (allocating freed block)" );
}
else
{
/* this block was already allocated. this means that our memory */
/* is also corrupted !! */
- ft_mem_debug_panic( "duplicate block allocation at address "
- "%p, size %ld\n",
- address, size );
+ ft_mem_debug_panic( "memory heap corrupted (re-allocating allocated block)" );
}
}
@@ -357,8 +360,11 @@
node->address = address;
node->size = size;
- node->file_name = table->file_name;
- node->line_no = table->line_no;
+ node->alloc_file_name = table->file_name;
+ node->alloc_line_no = table->line_no;
+
+ node->free_file_name = NULL;
+ node->free_line_no = 0;
node->link = pnode[0];
@@ -390,19 +396,30 @@
if (node)
{
if ( node->size < 0 )
- ft_mem_debug_panic( "freeing memory block at %p more than once !!",
- address );
+ ft_mem_debug_panic( "freeing memory block at %p more than once at (%s:%ld)\n"
+ "block allocated at (%s:%ld) and released at (%s:%ld)",
+ address,
+ FT_FILENAME(table->file_name),
+ table->line_no,
+ FT_FILENAME(node->alloc_file_name),
+ node->alloc_line_no,
+ FT_FILENAME(node->free_file_name),
+ node->free_line_no );
/* we simply invert the node's size to indicate that the node */
/* was freed. We also change its content.. */
memset( address, 0xF3, node->size );
-
+
table->alloc_current -= node->size;
node->size = -node->size;
+ node->free_file_name = table->file_name;
+ node->free_line_no = table->line_no;
}
else
- ft_mem_debug_panic( "trying to free unknown block at %p\n",
- address );
+ ft_mem_debug_panic( "trying to free unknown block at %p in (%s:%ld)\n",
+ address,
+ FT_FILENAME( table->file_name ),
+ table->line_no );
}
}
@@ -420,6 +437,9 @@
block = ft_mem_table_alloc( table, size );
if ( block )
ft_mem_table_set( table, block, (FT_ULong)size );
+
+ table->file_name = NULL;
+ table->line_no = 0;
return (FT_Pointer) block;
}
@@ -432,11 +452,15 @@
FT_MemTable table = memory->user;
if ( block == NULL )
- ft_mem_debug_panic( "trying to free NULL !!" );
+ ft_mem_debug_panic( "trying to free NULL in (%s:%ld)",
+ FT_FILENAME( table->file_name ),
+ table->line_no );
ft_mem_table_remove( table, (FT_Byte*)block );
/* we never really free the block */
+ table->file_name = NULL;
+ table->line_no = 0;
}
@@ -451,27 +475,33 @@
FT_MemNode node, *pnode;
FT_Pointer new_block;
+ const char* file_name = FT_FILENAME(table->file_name);
+ FT_Long line_no = table->line_no;
+
if ( block == NULL || cur_size == 0 )
- ft_mem_debug_panic( "trying to reallocate NULL" );
+ ft_mem_debug_panic( "trying to reallocate NULL in (%s:%ld)",
+ file_name, line_no );
if ( new_size <= 0 )
- ft_mem_debug_panic( "trying to reallocate %p to size 0 (current is %ld)",
- block, cur_size );
+ ft_mem_debug_panic( "trying to reallocate %p to size 0 (current is %ld)"
+ " in (%s:%ld)",
+ block, cur_size, file_name, line_no );
/* check 'cur_size' value */
pnode = ft_mem_table_get_nodep( table, (FT_Byte*)block );
node = *pnode;
if (!node)
- ft_mem_debug_panic( "trying to reallocate unknown block at %p",
- block );
+ ft_mem_debug_panic( "trying to reallocate unknown block at %p in (%s:%ld)",
+ block, file_name, line_no );
if ( node->size <= 0 )
- ft_mem_debug_panic( "trying to reallocate freed block at %p",
- block );
+ ft_mem_debug_panic( "trying to reallocate freed block at %p in (%s:%ld)",
+ block, file_name, line_no );
if ( node->size != cur_size )
ft_mem_debug_panic( "invalid realloc request for %p. cur_size is "
- "%ld instead of %ld", block, cur_size, node->size );
+ "%ld instead of %ld in (%s:%ld)",
+ block, cur_size, node->size, file_name, line_no );
new_block = ft_mem_debug_alloc( memory, new_size );
if ( new_block == NULL )
@@ -479,6 +509,9 @@
memcpy( new_block, block, cur_size < new_size ? cur_size : new_size );
+ table->file_name = file_name;
+ table->line_no = line_no;
+
ft_mem_debug_free( memory, (FT_Byte*)block );
return new_block;
@@ -542,6 +575,41 @@
}
+ FT_BASE_DEF( FT_Error )
+ FT_Realloc_Debug( FT_Memory memory,
+ FT_Long current,
+ FT_Long size,
+ void* *P,
+ const char* file_name,
+ FT_Long line_no )
+ {
+ FT_MemTable table = memory->user;
+
+ if ( table )
+ {
+ table->file_name = file_name;
+ table->line_no = line_no;
+ }
+ return FT_Realloc( memory, current, size, P );
+ }
+
+
+ FT_BASE_DEF( void )
+ FT_Free_Debug( FT_Memory memory,
+ FT_Pointer block,
+ const char* file_name,
+ FT_Long line_no )
+ {
+ FT_MemTable table = memory->user;
+
+ if ( table )
+ {
+ table->file_name = file_name;
+ table->line_no = line_no;
+ }
+ FT_Free( memory, block );
+ }
+
#else /* !FT_DEBUG_MEMORY */