Commit 5a1de37e7e1a2fdc1a5ef1679850b8964975d09b

David Turner 2001-10-24T07:32:55

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 !!

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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
diff --git a/builds/amiga/src/base/ftsystem.c b/builds/amiga/src/base/ftsystem.c
index 702b253..c9433fc 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 a112877..5b7d483 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 a112877..d58c07c 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 1a27e0e..afdd98c 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 <string.h>
 
-#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 390be7e..97ac862 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 011d699..93198a8 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 034cb7d..fca525c 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 33d42b6..2a63552 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 5f2aef9..54a814f 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;
   }