Commit b280537b6d330a2e24307afc8058a79f5b3b8864

David Turner 2003-03-13T21:07:51

* src/base/ftdbgmem.c, docs/DEBUG.TXT: added new environment variables to control memory debugging with FreeType. See the description of "FT2_DEBUG_MEMORY", "FT2_ALLOC_TOTAL_MAX" and "FT2_ALLOC_COUNT_MAX" in DEBUG.TXT * src/cache/ftccache.c, src/cache/ftccmap.c, src/cache/ftcsbits.c, ftlru.c: fixed the cache sub-system to correctly deal with out-of-memory conditions. * src/pfr/pfrobjs.c, src/pfr/pfrsbits.c: fixing compiler warnings and a small memory leak * src/psaux/psobjs.c (t1_reallocate_table): fixed a bug (memory leak) that only happened when trying to resize an array would end in an OOM. * src/smooth/ftgrays.c: removed compiler warnings / volatile bug * src/truetype/ttobjs.c: removed segmentation fault that happened in tight memory environments.

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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
diff --git a/ChangeLog b/ChangeLog
index 8c2c416..409e3bd 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,25 @@
+2003-03-13  David Turner  <david@freetype.org>
+
+    * src/base/ftdbgmem.c, docs/DEBUG.TXT: added new environment variables
+    to control memory debugging with FreeType. See the description of
+    "FT2_DEBUG_MEMORY", "FT2_ALLOC_TOTAL_MAX" and "FT2_ALLOC_COUNT_MAX"
+    in DEBUG.TXT
+
+    * src/cache/ftccache.c, src/cache/ftccmap.c, src/cache/ftcsbits.c,
+    ftlru.c: fixed the cache sub-system to correctly deal with out-of-memory
+    conditions.
+
+    * src/pfr/pfrobjs.c, src/pfr/pfrsbits.c: fixing compiler warnings and a
+    small memory leak
+
+    * src/psaux/psobjs.c (t1_reallocate_table): fixed a bug (memory leak) that
+    only happened when trying to resize an array would end in an OOM.
+
+    * src/smooth/ftgrays.c: removed compiler warnings / volatile bug
+
+    * src/truetype/ttobjs.c: removed segmentation fault that happened in
+    tight memory environments.
+
 2003-02-28  Pixel  <pixel@mandrakesoft.com>
 
     * src/gzip/ftgzip.c (ft_gzip_file_done): fixed memory leak, the ZLib
diff --git a/docs/DEBUG.TXT b/docs/DEBUG.TXT
index ef7faa1..8996d66 100644
--- a/docs/DEBUG.TXT
+++ b/docs/DEBUG.TXT
@@ -159,4 +159,25 @@ behaviour of FreeType at runtime:
     ignored in other builds.
 
 
+  FT2_ALLOC_TOTAL_MAX
+
+    this variable is ignored if FT2_DEBUG_MEMORY is not defined. It allows
+    you to specify a maximum heap size for all memory allocations performed
+    by FreeType. This is very useful to test the robustness of the font
+    engine and programs that use it in tight memory conditions.
+    
+    If it is undefined, or if its value is not strictly positive, then no
+    allocation bounds are checked at runtime.
+
+
+  FT2_ALLOC_COUNT_MAX
+  
+    this variable is ignored if FT2_DEBUG_MEMORY is not defined. It allows
+    you to sepcify a maximum number of memory allocations performed by
+    FreeType before returning the error FT_Err_Out_Of_Memory. This is
+    useful for debugging and testing the engine's robustness.
+    
+    If it is undefined, or if its value is not strictly positive, then no
+    allocation bounsd are checked at runtime.
+
 End of file
diff --git a/src/base/ftdbgmem.c b/src/base/ftdbgmem.c
index 125b5b7..15d186a 100644
--- a/src/base/ftdbgmem.c
+++ b/src/base/ftdbgmem.c
@@ -62,6 +62,13 @@
     FT_ULong         alloc_total;
     FT_ULong         alloc_current;
     FT_ULong         alloc_max;
+    FT_ULong         alloc_count;
+
+    FT_Bool          bound_total;    
+    FT_ULong         alloc_total_max;
+    
+    FT_Bool          bound_count;
+    FT_ULong         alloc_count_max;
 
     const char*      file_name;
     FT_Long          line_no;
@@ -476,10 +483,22 @@
     if ( size <= 0 )
       ft_mem_debug_panic( "negative block size allocation (%ld)", size );
 
+    /* return NULL if the maximum number of allocations was reached */
+    if ( table->bound_count &&
+         table->alloc_count >= table->alloc_count_max )
+      return NULL;
+
+    /* return NULL if this allocation would overflow the maximum heap size */
+    if ( table->bound_total && 
+         table->alloc_current + (FT_ULong)size > table->alloc_total_max )
+      return NULL;         
+
     block = (FT_Byte *)ft_mem_table_alloc( table, size );
     if ( block )
       ft_mem_table_set( table, block, (FT_ULong)size );
 
+    table->alloc_count++;
+
     table->file_name = NULL;
     table->line_no   = 0;
 
@@ -570,15 +589,42 @@
     FT_Int       result = 0;
 
 
-    if ( getenv( "FT_DEBUG_MEMORY" ) )
+    if ( getenv( "FT2_DEBUG_MEMORY" ) )
     {
       table = ft_mem_table_new( memory );
       if ( table )
       {
+        const char*  p;
+        
         memory->user    = table;
         memory->alloc   = ft_mem_debug_alloc;
         memory->realloc = ft_mem_debug_realloc;
         memory->free    = ft_mem_debug_free;
+        
+        p = getenv( "FT2_ALLOC_TOTAL_MAX" );
+        if ( p != NULL )
+        {
+          FT_Long   total_max = atol(p);
+          
+          if ( total_max > 0 )
+          {
+            table->bound_total     = 1;
+            table->alloc_total_max = (FT_ULong) total_max;
+          }
+        }
+        
+        p = getenv( "FT2_ALLOC_COUNT_MAX" );
+        if ( p != NULL )
+        {
+          FT_Long  total_count = atol(p);
+          
+          if ( total_count > 0 )
+          {
+            table->bound_count     = 1;
+            table->alloc_count_max = (FT_ULong) total_count;
+          }
+        }
+
         result = 1;
       }
     }
diff --git a/src/cache/ftccache.c b/src/cache/ftccache.c
index 343f08e..aba7ca4 100644
--- a/src/cache/ftccache.c
+++ b/src/cache/ftccache.c
@@ -357,10 +357,12 @@
 
     FT_FREE( node );
 
+#if 0
     /* check, just in case of general corruption :-) */
     if ( manager->num_nodes == 0 )
       FT_ERROR(( "ftc_node_destroy: invalid cache node count! = %d\n",
                   manager->num_nodes ));
+#endif                  
   }
 
 
@@ -546,8 +548,10 @@
                     FTC_Query   query,
                     FTC_Node   *anode )
   {
-    FT_Error    error = FT_Err_Ok;
-    FT_LruNode  lru;
+    FT_Error     error = FT_Err_Ok;
+    FTC_Manager  manager;
+    FT_LruNode   lru;
+    FT_UInt      free_count = 0;
 
 
     if ( !cache || !query || !anode )
@@ -558,152 +562,237 @@
     query->hash   = 0;
     query->family = NULL;
 
-    /* XXX: we break encapsulation for the sake of speed! */
-    {
-      /* first of all, find the relevant family */
-      FT_LruList              list    = cache->families;
-      FT_LruNode              fam, *pfam;
-      FT_LruNode_CompareFunc  compare = list->clazz->node_compare;
+    manager = cache->manager;
+
+   /*  here's a small note explaining what's hapenning in the code below.
+    *
+    *  we need to deal intelligently with out-of-memory (OOM) conditions
+    *  when trying to create a new family or cache node during the lookup.
+    *
+    *  when an OOM is detected, we'll try to free one or more "old" nodes
+    *  from the cache, then try again. it may be necessary to do that several
+    *  times, so a loop is needed.
+    *
+    *  the local variable "free_count" holds the number of "old" nodes to
+    *  discard on each attempt. it starts at 1 and doubles on each iteration.
+    *  the loop stops when:
+    *
+    *   - a non-OOM error is detected
+    *   - a succesful lookup is performed
+    *   - there are no more unused nodes in the cache
+    *
+    *  for the record, remember that all used nodes appear _before_
+    *  unused ones in the manager's MRU node list.
+    */
 
-      pfam = &list->nodes;
-      for (;;)
+    for (;;)
+    {
       {
-        fam = *pfam;
-        if ( fam == NULL )
+        /* first of all, find the relevant family */
+        FT_LruList              list    = cache->families;
+        FT_LruNode              fam, *pfam;
+        FT_LruNode_CompareFunc  compare = list->clazz->node_compare;
+  
+        pfam = &list->nodes;
+        for (;;)
         {
-          error = FT_LruList_Lookup( list, query, &lru );
-          if ( error )
-            goto Exit;
-
-          goto Skip;
+          fam = *pfam;
+          if ( fam == NULL )
+          {
+            error = FT_LruList_Lookup( list, query, &lru );
+            if ( error )
+              goto Fail;
+  
+            goto Skip;
+          }
+  
+          if ( compare( fam, query, list->data ) )
+            break;
+  
+          pfam = &fam->next;
         }
-
-        if ( compare( fam, query, list->data ) )
-          break;
-
-        pfam = &fam->next;
-      }
-
-      FT_ASSERT( fam != NULL );
-
-      /* move to top of list when needed */
-      if ( fam != list->nodes )
-      {
-        *pfam       = fam->next;
-        fam->next   = list->nodes;
-        list->nodes = fam;
-      }
-
-      lru = fam;
-
-    Skip:
-      ;
-    }
-
-    {
-      FTC_Family  family = (FTC_Family) lru;
-      FT_UFast    hash    = query->hash;
-      FTC_Node*   bucket;
-      FT_UInt     idx;
-
-
-      idx = hash & cache->mask;
-      if ( idx < cache->p )
-        idx = hash & ( cache->mask * 2 + 1 );
-
-      bucket  = cache->buckets + idx;
-
-
-      if ( query->family     != family                        ||
-           family->fam_index >= cache->manager->families.size )
-      {
-        FT_ERROR((
-          "ftc_cache_lookup: invalid query (bad 'family' field)\n" ));
-        return FTC_Err_Invalid_Argument;
+  
+        FT_ASSERT( fam != NULL );
+  
+        /* move to top of list when needed */
+        if ( fam != list->nodes )
+        {
+          *pfam       = fam->next;
+          fam->next   = list->nodes;
+          list->nodes = fam;
+        }
+  
+        lru = fam;
+  
+      Skip:
+        ;
       }
 
-      if ( *bucket )
       {
-        FTC_Node*             pnode   = bucket;
-        FTC_Node_CompareFunc  compare = cache->clazz->node_compare;
-
-
-        for ( ;; )
+        FTC_Manager  manager = cache->manager;
+        FTC_Family   family  = (FTC_Family) lru;
+        FT_UFast     hash    = query->hash;
+        FTC_Node*    bucket;
+        FT_UInt      idx;
+  
+  
+        idx = hash & cache->mask;
+        if ( idx < cache->p )
+          idx = hash & ( cache->mask * 2 + 1 );
+  
+        bucket  = cache->buckets + idx;
+  
+  
+        if ( query->family     != family                 ||
+             family->fam_index >= manager->families.size )
         {
-          FTC_Node  node;
-
-
-          node = *pnode;
-          if ( node == NULL )
-            break;
+          FT_ERROR((
+            "ftc_cache_lookup: invalid query (bad 'family' field)\n" ));
+          error = FTC_Err_Invalid_Argument;
+          goto Exit;
+        }
+  
+        if ( *bucket )
+        {
+          FTC_Node*             pnode   = bucket;
+          FTC_Node_CompareFunc  compare = cache->clazz->node_compare;
+  
 
-          if ( node->hash == hash                            &&
-               (FT_UInt)node->fam_index == family->fam_index &&
-               compare( node, query, cache ) )
+          for ( ;; )
           {
-            /* move to head of bucket list */
-            if ( pnode != bucket )
+            FTC_Node  node;
+  
+  
+            node = *pnode;
+            if ( node == NULL )
+              break;
+  
+            if ( node->hash == hash                            &&
+                 (FT_UInt)node->fam_index == family->fam_index &&
+                 compare( node, query, cache ) )
             {
-              *pnode     = node->link;
-              node->link = *bucket;
-              *bucket    = node;
+              /* move to head of bucket list */
+              if ( pnode != bucket )
+              {
+                *pnode     = node->link;
+                node->link = *bucket;
+                *bucket    = node;
+              }
+  
+              /* move to head of MRU list */
+              if ( node != manager->nodes_list )
+                ftc_node_mru_up( node, manager );
+  
+              *anode = node;
+              goto Exit;
             }
-
-            /* move to head of MRU list */
-            if ( node != cache->manager->nodes_list )
-              ftc_node_mru_up( node, cache->manager );
-
-            *anode = node;
-            goto Exit;
+  
+            pnode = &node->link;
           }
-
-          pnode = &node->link;
         }
+  
+        /* didn't find a node, create a new one */
+        {
+          FTC_Cache_Class  clazz   = cache->clazz;
+          FT_Memory        memory  = cache->memory;
+          FTC_Node         node;
+  
+  
+          if ( FT_ALLOC( node, clazz->node_size ) )
+            goto Fail;
+  
+          node->fam_index = (FT_UShort) family->fam_index;
+          node->hash      = query->hash;
+          node->ref_count = 0;
+  
+          error = clazz->node_init( node, query, cache );
+          if ( error )
+          {
+            FT_FREE( node );
+            goto Fail;
+          }
+  
+          error = ftc_node_hash_link( node, cache );
+          if ( error )
+          {
+            clazz->node_done( node, cache );
+            FT_FREE( node );
+            goto Fail;
+          }
+  
+          ftc_node_mru_link( node, cache->manager );
+  
+          cache->manager->cur_weight += clazz->node_weight( node, cache );
+  
+          /* now try to compress the node pool when necessary */
+          if ( manager->cur_weight >= manager->max_weight )
+          {
+            node->ref_count++;
+            FTC_Manager_Compress( manager );
+            node->ref_count--;
+          }
+  
+          *anode = node;
+        }
+        
+       /* all is well, exit now
+        */
+        goto Exit;
       }
-
-      /* didn't find a node, create a new one */
+      
+    Fail:
+      if ( error != FT_Err_Out_Of_Memory )
+        goto Exit;
+     
+     /* there is not enough memory, try to release some unused nodes
+      * from the cache to make room for a new one.
+      */
       {
-        FTC_Cache_Class  clazz   = cache->clazz;
-        FTC_Manager      manager = cache->manager;
-        FT_Memory        memory  = cache->memory;
-        FTC_Node         node;
+        FT_UInt   new_count;
 
+        new_count = 1 + free_count*2;
 
-        if ( FT_ALLOC( node, clazz->node_size ) )
+        /* check overflow and bounds */
+        if ( new_count < free_count || free_count > manager->num_nodes )
           goto Exit;
 
-        node->fam_index = (FT_UShort) family->fam_index;
-        node->hash      = query->hash;
-        node->ref_count = 0;
-
-        error = clazz->node_init( node, query, cache );
-        if ( error )
+        free_count = new_count;
+        
+        /* try to remove "new_count" nodes from the list */
         {
-          FT_FREE( node );
-          goto Exit;
-        }
+          FTC_Node   first = manager->nodes_list;
+          FTC_Node   node;
 
-        error = ftc_node_hash_link( node, cache );
-        if ( error )
-        {
-          clazz->node_done( node, cache );
-          FT_FREE( node );
-          goto Exit;
-        }
+          if ( first == NULL )  /* empty list ! */
+            goto Exit;
 
-        ftc_node_mru_link( node, cache->manager );
+         /* go to last node - it's a circular list */
+          node = first->mru_prev;
+          for ( ; node && new_count > 0; new_count-- )
+          {
+            FTC_Node  prev = node->mru_prev;
 
-        cache->manager->cur_weight += clazz->node_weight( node, cache );
+           /* used nodes always appear before unused one in the MRU
+            * list. if we find one here, we'd better stop right now
+            * our iteration
+            */
+            if ( node->ref_count > 0 )
+            {
+              /* if there are no unused nodes in the list, we'd better exit */
+              if ( new_count == free_count )
+                goto Exit;
+                
+              break;
+            }
 
-        /* now try to compress the node pool when necessary */
-        if ( manager->cur_weight >= manager->max_weight )
-        {
-          node->ref_count++;
-          FTC_Manager_Compress( manager );
-          node->ref_count--;
-        }
+            ftc_node_destroy( node, manager );
+
+            if ( node == first )
+              break;
 
-        *anode = node;
+            node = prev;
+          }
+        }
       }
     }
 
diff --git a/src/cache/ftccmap.c b/src/cache/ftccmap.c
index 77e6ecb..8366ce5 100644
--- a/src/cache/ftccmap.c
+++ b/src/cache/ftccmap.c
@@ -27,6 +27,9 @@
 
 #include "ftcerror.h"
 
+#undef  FT_COMPONENT
+#define FT_COMPONENT  trace_cache
+
   /*************************************************************************/
   /*                                                                       */
   /* Each FTC_CMapNode contains a simple array to map a range of character */
diff --git a/src/cache/ftcsbits.c b/src/cache/ftcsbits.c
index 6634676..5840382 100644
--- a/src/cache/ftcsbits.c
+++ b/src/cache/ftcsbits.c
@@ -216,7 +216,7 @@
       /* we mark unloaded glyphs with `sbit.buffer == 0' */
       /* and 'width == 255', 'height == 0'               */
       /*                                                 */
-      if ( error )
+      if ( error && error != FT_Err_Out_Of_Memory )
       {
         sbit->width = 255;
         error       = 0;
diff --git a/src/cache/ftlru.c b/src/cache/ftlru.c
index 4a6f603..47d6562 100644
--- a/src/cache/ftlru.c
+++ b/src/cache/ftlru.c
@@ -21,6 +21,7 @@
 #include FT_CACHE_INTERNAL_LRU_H
 #include FT_LIST_H
 #include FT_INTERNAL_OBJECTS_H
+#include FT_INTERNAL_DEBUG_H
 
 #include "ftcerror.h"
 
@@ -187,80 +188,135 @@
       goto Exit;
     }
 
-    /* we haven't found the relevant element.  We will now try */
-    /* to create a new one.                                    */
-    /*                                                         */
-
-    /* first, check if our list if full, when appropriate */
-    if ( list->max_nodes > 0 && list->num_nodes >= list->max_nodes )
+   /* since we haven't found the relevant element in our LRU list,
+    * we're going to "create" a new one.
+    *
+    * the following code is a bit special, because it tries to handle
+    * out-of-memory conditions (OOM) in an intelligent way.
+    *
+    * more precisely, if not enough memory is available to create a
+    * new node or "flush" an old one, we need to remove the oldest
+    * elements from our list, and try again. since several tries may
+    * be necessary, a loop is needed
+    *
+    * this loop will only exit when:
+    *
+    *   - a new node was succesfully created, or an old node flushed
+    *   - an error other than FT_Err_Out_Of_Memory is detected
+    *   - the list of nodes is empty, and it isn't possible to create
+    *     new nodes
+    *
+    * on each unsucesful attempt, one node will be removed from the list
+    *
+    */
+    
     {
-      /* this list list is full; we will now flush */
-      /* the oldest node, if there's one!          */
-      FT_LruNode  last = *plast;
-
+      FT_Int   drop_last = ( list->max_nodes > 0 && 
+                             list->num_nodes >= list->max_nodes );
 
-      if ( last )
+      for (;;)
       {
-        if ( clazz->node_flush )
+        node = NULL;
+
+       /* when "drop_last" is true, we should free the last node in
+        * the list to make room for a new one. note that we re-use
+        * its memory block to save allocation calls.
+        */
+        if ( drop_last )
         {
-          error = clazz->node_flush( last, key, list->data );
+         /* find the last node in the list
+          */
+          pnode = &list->nodes;
+          node  = *pnode;
+  
+          if ( node == NULL )
+          {
+            FT_ASSERT( list->nodes == 0 );
+            error = FT_Err_Out_Of_Memory;
+            goto Exit;
+          }
+
+          FT_ASSERT( list->nodes > 0 );
+
+          while ( node->next )
+          {
+            pnode = &node->next;
+            node  = *pnode;
+          }
+  
+         /* remove it from the list, and try to "flush" it. doing this will
+          * save a significant number of dynamic allocations compared to
+          * a classic destroy/create cycle
+          */
+          *pnode = NULL;
+          list->num_nodes -= 1;
+  
+          if ( clazz->node_flush )
+          {
+            error = clazz->node_flush( node, key, list->data );
+            if ( !error )
+              goto Success;
+
+           /* note that if an error occured during the flush, we need to
+            * finalize it since it is potentially in incomplete state.
+            */
+          }
+
+         /* we finalize, but do not destroy the last node, we
+          * simply re-use its memory block !
+          */
+          if ( clazz->node_done )
+            clazz->node_done( node, list->data );
+            
+          FT_MEM_ZERO( node, clazz->node_size );
         }
         else
         {
-          if ( clazz->node_done )
-            clazz->node_done( last, list->data );
-
-          last->key  = key;
-          error = clazz->node_init( last, key, list->data );
+         /* try to allocate a new node when "drop_last" is not TRUE
+          * this usually happens on the first pass, when the LRU list
+          * is not already full.
+          */
+          if ( FT_ALLOC( node, clazz->node_size ) )
+            goto Fail;
         }
+  
+        FT_ASSERT( node != NULL );
 
-        if ( !error )
+        node->key = key;
+        error = clazz->node_init( node, key, list->data );
+        if ( error )
         {
-          /* move it to the top of the list */
-          *plast      = NULL;
-          last->next  = list->nodes;
-          list->nodes = last;
+          if ( clazz->node_done )
+            clazz->node_done( node, list->data );
 
-          result = last;
-          goto Exit;
+          FT_FREE( node );
+          goto Fail;
         }
 
-        /* in case of error during the flush or done/init cycle, */
-        /* we need to discard the node                           */
-        if ( clazz->node_done )
-          clazz->node_done( last, list->data );
-
-        *plast = NULL;
-        list->num_nodes--;
+      Success:
+        result = node;
 
-        FT_FREE( last );
+        node->next  = list->nodes;
+        list->nodes = node;
+        list->num_nodes++;
         goto Exit;
+  
+      Fail:
+        if ( error != FT_Err_Out_Of_Memory )
+          goto Exit;
+        
+        drop_last = 1;
+        continue;
       }
     }
 
-    /* otherwise, simply allocate a new node */
-    if ( FT_ALLOC( node, clazz->node_size ) )
-      goto Exit;
-
-    node->key = key;
-    error = clazz->node_init( node, key, list->data );
-    if ( error )
-    {
-      FT_FREE( node );
-      goto Exit;
-    }
-
-    result      = node;
-    node->next  = list->nodes;
-    list->nodes = node;
-    list->num_nodes++;
-
   Exit:
     *anode = result;
     return error;
   }
 
 
+
   FT_EXPORT_DEF( void )
   FT_LruList_Remove( FT_LruList  list,
                      FT_LruNode  node )
diff --git a/src/pfr/pfrobjs.c b/src/pfr/pfrobjs.c
index 2021848..867644a 100644
--- a/src/pfr/pfrobjs.c
+++ b/src/pfr/pfrobjs.c
@@ -41,6 +41,8 @@
   FT_LOCAL_DEF( void )
   pfr_face_done( PFR_Face  face )
   {
+    FT_Memory   memory = face->root.driver->root.memory;
+
     /* we don't want dangling pointers */
     face->root.family_name = NULL;
     face->root.style_name  = NULL;
@@ -49,6 +51,7 @@
     pfr_phy_font_done( &face->phy_font, FT_FACE_MEMORY( face ) );
 
     /* no need to finalize the logical font or the header */
+    FT_FREE( face->root.available_sizes );
   }
 
 
@@ -179,8 +182,8 @@
          strike = phy_font->strikes;
          for ( n = 0; n < count; n++, size++, strike++ )
          {
-           size->height = strike->y_ppm;
-           size->width  = strike->x_ppm;
+           size->height = (FT_UShort) strike->y_ppm;
+           size->width  = (FT_UShort) strike->x_ppm;
          }
          root->num_fixed_sizes = count;
        }
diff --git a/src/psaux/psobjs.c b/src/psaux/psobjs.c
index 6035950..e930b71 100644
--- a/src/psaux/psobjs.c
+++ b/src/psaux/psobjs.c
@@ -111,7 +111,10 @@
 
     /* allocate new base block */
     if ( FT_ALLOC( table->block, new_size ) )
+    {
+      table->block = old_base;
       return error;
+    }
 
     /* copy elements and shift offsets */
     if (old_base )
diff --git a/src/smooth/ftgrays.c b/src/smooth/ftgrays.c
index 5ca7c70..6c09481 100644
--- a/src/smooth/ftgrays.c
+++ b/src/smooth/ftgrays.c
@@ -1827,9 +1827,9 @@
   gray_convert_glyph( RAS_ARG )
   {
     TBand            bands[40];
-    volatile TBand*  band;
-    volatile int     n, num_bands;
-    volatile TPos    min, max, max_y;
+    TBand* volatile  band;
+    int volatile     n, num_bands;
+    TPos volatile    min, max, max_y;
     FT_BBox*         clip;
 
 
diff --git a/src/truetype/ttobjs.c b/src/truetype/ttobjs.c
index 6835648..9d78bae 100644
--- a/src/truetype/ttobjs.c
+++ b/src/truetype/ttobjs.c
@@ -70,14 +70,17 @@
   {
     FT_Memory  memory = zone->memory;
 
-
-    FT_FREE( zone->contours );
-    FT_FREE( zone->tags );
-    FT_FREE( zone->cur );
-    FT_FREE( zone->org );
-
-    zone->max_points   = zone->n_points   = 0;
-    zone->max_contours = zone->n_contours = 0;
+    if ( memory )
+    {
+      FT_FREE( zone->contours );
+      FT_FREE( zone->tags );
+      FT_FREE( zone->cur );
+      FT_FREE( zone->org );
+
+      zone->max_points   = zone->n_points   = 0;
+      zone->max_contours = zone->n_contours = 0;
+      zone->memory       = NULL;
+    }
   }