Commit 581ec91c2719f1a6594cfb1a3e46f94bf5feab03

David Turner 2003-12-22T21:53:37

* include/freetype/ftcache.h, include/freetype/cache/ftcmanag.h, include/freetype/cache/ftccache.h, include/freetype/cache/ftcmanag.h, include/freetype/cache/ftcmru.h (added), include/freetype/cache/ftlru.h (removed), include/freetype/cache/ftcsbits.h, include/freetype/cache/ftcimage.h, include/freetype/cache/ftcglyph.h, src/cache/ftcmru.c, src/cache/ftcmanag.c, src/cache/ftccache.c, src/cache/ftcglyph.c, src/cache/ftcimage.c, src/cache/ftcsbits.c, src/cache/ftccmap.c, src/cache/ftcbasic.c (added), src/cache/ftclru.c (removed): *Complete* rewrite of the cache sub-system to "solve" the following points: - all public APIs have been moved to FT_CACHE_H, everything under "include/freetype/cache" is only needed by client applications that want to implement their own caches - a new function named FTC_Manager_RemoveFaceID to deal with the uninstallation of FaceIDs - the image and sbit cache are now abstract classes, that can be extended much more easily by client applications - better performance in certain areas. Further optimizations to come shortly anyway... - the FTC_CMapCache_Lookup function has changed its signature, charmaps can now only be retrieved by index - FTC_Manager_Lookup_Face => FTC_Manager_LookupFace FTC_Manager_Lookup_Size => FTC_Manager_LookupSize (still in private header for the moment)

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
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
diff --git a/ChangeLog b/ChangeLog
index 3678898..084679b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,48 @@
+2003-12-22  David Turner  <david@freetype.org>
+
+        * include/freetype/ftcache.h,
+        include/freetype/cache/ftcmanag.h,
+        include/freetype/cache/ftccache.h,
+        include/freetype/cache/ftcmanag.h,
+        include/freetype/cache/ftcmru.h (added),
+        include/freetype/cache/ftlru.h (removed),
+        include/freetype/cache/ftcsbits.h,
+        include/freetype/cache/ftcimage.h,
+        include/freetype/cache/ftcglyph.h,
+        src/cache/ftcmru.c,
+        src/cache/ftcmanag.c,
+        src/cache/ftccache.c,
+        src/cache/ftcglyph.c,
+        src/cache/ftcimage.c,
+        src/cache/ftcsbits.c,
+        src/cache/ftccmap.c,
+        src/cache/ftcbasic.c (added),
+        src/cache/ftclru.c (removed):
+
+          *Complete* rewrite of the cache sub-system to "solve" the
+          following points:
+
+            - all public APIs have been moved to FT_CACHE_H, everything
+              under "include/freetype/cache" is only needed by client
+              applications that want to implement their own caches
+
+            - a new function named FTC_Manager_RemoveFaceID to deal
+              with the uninstallation of FaceIDs
+
+            - the image and sbit cache are now abstract classes, that
+              can be extended much more easily by client applications
+
+            - better performance in certain areas. Further optimizations
+              to come shortly anyway...
+
+            - the FTC_CMapCache_Lookup function has changed its signature,
+              charmaps can now only be retrieved by index
+
+            - FTC_Manager_Lookup_Face => FTC_Manager_LookupFace
+              FTC_Manager_Lookup_Size => FTC_Manager_LookupSize (still in
+              private header for the moment)
+
+
 2003-12-21  Werner Lemberg  <wl@gnu.org>
 
 	* src/type1/t1load.c (parse_dict): Stop parsing if `eexec' keyword
diff --git a/include/freetype/cache/ftccache.h b/include/freetype/cache/ftccache.h
index 0f78c34..b629459 100644
--- a/include/freetype/cache/ftccache.h
+++ b/include/freetype/cache/ftccache.h
@@ -19,6 +19,7 @@
 #ifndef __FTCCACHE_H__
 #define __FTCCACHE_H__
 
+
 #include FT_CACHE_INTERNAL_MRU_H
 
 FT_BEGIN_HEADER
@@ -64,8 +65,8 @@ FT_BEGIN_HEADER
 #define FTC_NODE( x )    ( (FTC_Node)(x) )
 #define FTC_NODE_P( x )  ( (FTC_Node*)(x) )
 
-#define FTC_NODE__MRU_NEXT(x)  FTC_NODE( (x)->mru.next )
-#define FTC_NODE__MRU_PREV(x)  FTC_NODE( (x)->mru.prev )
+#define FTC_NODE__NEXT(x)  FTC_NODE( (x)->mru.next )
+#define FTC_NODE__PREV(x)  FTC_NODE( (x)->mru.prev )
 
   /*************************************************************************/
   /*                                                                       */
diff --git a/include/freetype/cache/ftcmanag.h b/include/freetype/cache/ftcmanag.h
index 21ca51e..71ff2ef 100644
--- a/include/freetype/cache/ftcmanag.h
+++ b/include/freetype/cache/ftcmanag.h
@@ -71,7 +71,6 @@
 
 FT_BEGIN_HEADER
 
-#define FT_DEBUG_ERROR
 
   /*************************************************************************/
   /*                                                                       */
diff --git a/include/freetype/cache/ftcmru.h b/include/freetype/cache/ftcmru.h
index e3bb2a3..5b329f1 100644
--- a/include/freetype/cache/ftcmru.h
+++ b/include/freetype/cache/ftcmru.h
@@ -53,6 +53,7 @@
 #error "so that freetype.h of FreeType 2 is found first."
 #endif
 
+#define xxFT_DEBUG_ERROR
 
 FT_BEGIN_HEADER
 
@@ -65,7 +66,6 @@ FT_BEGIN_HEADER
 
   } FTC_MruNodeRec;
 
-
   FT_EXPORT( void )
   FTC_MruNode_Prepend( FTC_MruNode  *plist,
                        FTC_MruNode   node );
@@ -78,7 +78,6 @@ FT_BEGIN_HEADER
   FTC_MruNode_Remove( FTC_MruNode  *plist,
                       FTC_MruNode   node );
 
-
   typedef struct FTC_MruListRec_*              FTC_MruList;
 
   typedef struct FTC_MruListClassRec_ const *  FTC_MruListClass;
@@ -107,7 +106,6 @@ FT_BEGIN_HEADER
 
   } FTC_MruListClassRec;
 
-
   typedef struct FTC_MruListRec_
   {
     FT_UInt                  num_nodes;
@@ -134,20 +132,20 @@ FT_BEGIN_HEADER
   FT_EXPORT( void )
   FTC_MruList_Done( FTC_MruList  list );
 
-
   FT_EXPORT( FTC_MruNode )
-  FTC_MruList_Lookup( FTC_MruList   list,
-                      FT_Pointer    key );
-
-  FT_EXPORT( void )
-  FTC_MruList_Up( FTC_MruList    list,
-                  FTC_MruNode    node );
+  FTC_MruList_Find( FTC_MruList  list,
+                    FT_Pointer    key );
 
   FT_EXPORT( FT_Error )
   FTC_MruList_New( FTC_MruList    list,
                    FT_Pointer     key,
                    FTC_MruNode   *anode );
 
+  FT_EXPORT( FT_Error )
+  FTC_MruList_Lookup( FTC_MruList   list,
+                      FT_Pointer    key,
+                      FTC_MruNode  *pnode );
+
   FT_EXPORT( void )
   FTC_MruList_Remove( FTC_MruList  list,
                       FTC_MruNode  node );
diff --git a/include/freetype/ftcache.h b/include/freetype/ftcache.h
index 10178c1..bc677d9 100644
--- a/include/freetype/ftcache.h
+++ b/include/freetype/ftcache.h
@@ -108,10 +108,10 @@ FT_BEGIN_HEADER
   /*    FTC_FaceID                                                         */
   /*                                                                       */
   /* <Description>                                                         */
-  /*    A generic pointer type that is used to identity face objects.  The */
+  /*    An opaque pointer type that is used to identity face objects.  The */
   /*    contents of such objects is application-dependent.                 */
   /*                                                                       */
-  typedef FT_Pointer  FTC_FaceID;
+  typedef struct FTC_FaceIDRec_*    FTC_FaceID;
 
 
   /*************************************************************************/
@@ -401,21 +401,6 @@ FT_BEGIN_HEADER
 
   /*************************************************************************/
   /*                                                                       */
-  /* @type:                                                                */
-  /*    FTC_CMapDesc                                                       */
-  /*                                                                       */
-  /* @description:                                                         */
-  /*    A handle to an @FTC_CMapDescRec structure used to describe a given */
-  /*    charmap in a charmap cache.                                        */
-  /*                                                                       */
-  /*    Each @FTC_CMapDesc describes which charmap (of which @FTC_FaceID)  */
-  /*    we want to use in @FTC_CMapCache_Lookup.                           */
-  /*                                                                       */
-  typedef struct FTC_CMapDescRec_*  FTC_CMapDesc;
-
-
-  /*************************************************************************/
-  /*                                                                       */
   /* @function:                                                            */
   /*    FTC_CMapCache_New                                                  */
   /*                                                                       */
@@ -442,7 +427,7 @@ FT_BEGIN_HEADER
 
  /* retrieve the index of a given charmap
   */
-  FT_EXPORT( FT_UInt )
+  FT_EXPORT( FT_Int )
   FT_Get_CharMap_Index( FT_CharMap  charmap );
 
   /*************************************************************************/
@@ -457,21 +442,19 @@ FT_BEGIN_HEADER
   /* @input:                                                               */
   /*    cache     :: A charmap cache handle.                               */
   /*                                                                       */
-  /*    cmap_desc :: A charmap descriptor handle.                          */
+  /*    face_id    :: source face id                                       */
+  /*                                                                       */
+  /*    cmap_index :: index of charmap in source face                      */
   /*                                                                       */
   /*    char_code :: The character code (in the corresponding charmap).    */
   /*                                                                       */
   /* @return:                                                              */
   /*    Glyph index.  0 means "no glyph".                                  */
   /*                                                                       */
-  /* @note:                                                                */
-  /*    This function doesn't return @FTC_Node handles, since there is no  */
-  /*    real use for them with typical uses of charmaps.                   */
-  /*                                                                       */
   FT_EXPORT( FT_UInt )
   FTC_CMapCache_Lookup( FTC_CMapCache  cache,
                         FTC_FaceID     face_id,
-                        FT_UInt        cmap_index,
+                        FT_Int         cmap_index,
                         FT_UInt32      char_code );
 
 
@@ -480,7 +463,7 @@ FT_BEGIN_HEADER
   /* <Section>                                                             */
   /*    cache_subsystem                                                    */
   /*                                                                       */
-  /*************************************************************************/
+  /**********************************************************************/
 
 
   /*************************************************************************/
diff --git a/src/cache/ftcbasic.c b/src/cache/ftcbasic.c
index 78fd1c0..b1f825e 100644
--- a/src/cache/ftcbasic.c
+++ b/src/cache/ftcbasic.c
@@ -63,26 +63,6 @@
   }
 
 
-  static FT_Bool
-  ftc_basic_gnode_compare_faceid( FTC_GNode   gnode,
-                                  FTC_FaceID  face_id,
-                                  FTC_Cache   cache )
-  {
-    FTC_BasicFamily  family = (FTC_BasicFamily) gnode->family;
-    FT_Bool          result;
-
-    result = FT_BOOL( family->attrs.scaler.face_id == face_id );
-    if ( result )
-    {
-     /* we must call this function to avoid this node from appearing
-      * in later lookups with the same face_id !!
-      */
-      FTC_GNode_UnselectFamily( gnode, cache );
-    }
-    return result;
-  }
-
-
   static FT_UInt
   ftc_basic_family_get_count( FTC_BasicFamily  family,
                               FTC_Manager      manager )
@@ -91,7 +71,7 @@
     FT_Face   face;
     FT_UInt   result = 0;
 
-    error = FTC_Manager_LookupFace( manager, &family->attrs.scaler.face_id,
+    error = FTC_Manager_LookupFace( manager, family->attrs.scaler.face_id,
                                     &face );
     if ( !error )
       result = face->num_glyphs;
@@ -168,6 +148,27 @@
   }
 
 
+  static FT_Bool
+  ftc_basic_gnode_compare_faceid( FTC_GNode   gnode,
+                                  FTC_FaceID  face_id,
+                                  FTC_Cache   cache )
+  {
+    FTC_BasicFamily  family = (FTC_BasicFamily) gnode->family;
+    FT_Bool          result;
+
+    result = FT_BOOL( family->attrs.scaler.face_id == face_id );
+    if ( result )
+    {
+     /* we must call this function to avoid this node from appearing
+      * in later lookups with the same face_id !!
+      */
+      FTC_GNode_UnselectFamily( gnode, cache );
+    }
+    return result;
+  }
+
+
+
  /*
   *
   * basic image cache
@@ -246,7 +247,7 @@
     query.attrs.scaler.pixel   = 1;
     query.attrs.load_flags     = type->flags;
 
-    hash = FTC_BASIC_ATTR_HASH( &query.attrs ) ^ (gindex << 8);
+    hash = FTC_BASIC_ATTR_HASH( &query.attrs ) + gindex;
 
     error = FTC_GCache_Lookup( FTC_GCACHE( cache ),
                                hash, gindex,
@@ -347,7 +348,7 @@
 
    /* beware, the hash must be the same for all glyph ranges !!
     */
-    hash = FTC_BASIC_ATTR_HASH( &query.attrs ) ^
+    hash = FTC_BASIC_ATTR_HASH( &query.attrs ) +
            (gindex/FTC_SBIT_ITEMS_PER_NODE);
 
     error = FTC_GCache_Lookup( FTC_GCACHE( cache ),
diff --git a/src/cache/ftccache.c b/src/cache/ftccache.c
index 4db4757..e3b5bfc 100644
--- a/src/cache/ftccache.c
+++ b/src/cache/ftccache.c
@@ -29,7 +29,7 @@
 #define FTC_HASH_SUB_LOAD  ( FTC_HASH_MAX_LOAD - FTC_HASH_MIN_LOAD )
 
 /* this one _must_ be a power of 2! */
-#define FTC_HASH_INITIAL_SIZE  512
+#define FTC_HASH_INITIAL_SIZE  8
 
 
   /*************************************************************************/
@@ -76,9 +76,6 @@
   static void
   ftc_cache_resize( FTC_Cache  cache )
   {
-#if 1
-    FT_UNUSED(cache);
-#else
     for (;;)
     {
       FTC_Node   node, *pnode;
@@ -173,7 +170,6 @@
       else /* the hash table is balanced */
         break;
     }
-#endif
   }
 
 
@@ -376,6 +372,8 @@
                  FTC_Node     node )
   {
     node->hash = hash;
+    node->cache_index = (FT_UInt16) cache->index;
+    node->ref_count   = 0;
 
     ftc_node_hash_link( node, cache );
     ftc_node_mru_link( node, cache->manager );
@@ -461,9 +459,20 @@
     *
     */
     error = cache->clazz.node_new( &node, query, cache );
-    if ( !error )
-      goto AddNode;
+    if ( error )
+      goto FlushCache;
+
+  AddNode:
+   /* don't assume that the cache has the same number of buckets, since
+    * our allocation request might have triggered global cache flushing
+    */
+    ftc_cache_add( cache, hash, node );
 
+  Exit:
+    *anode = node;
+    return error;
+
+  FlushCache:
     node = NULL;
     if ( error != FT_Err_Out_Of_Memory )
       goto Exit;
@@ -495,20 +504,7 @@
         tries = count;
       }
     }
-
-  AddNode:
-    node->ref_count   = 0;
-    node->cache_index = (FT_UInt16) cache->index;
-    node->hash        = hash;
-
-   /* don't assume that the cache has the same number of buckets, since
-    * our allocation request might have triggered global cache flushing
-    */
-    ftc_cache_add( cache, hash, node );
-
-  Exit:
-    *anode = node;
-    return error;
+    goto AddNode;
   }
 
 
@@ -566,5 +562,4 @@
     ftc_cache_resize( cache );
   }
 
-
 /* END */
diff --git a/src/cache/ftccmap.c b/src/cache/ftccmap.c
index b107daa..034ab24 100644
--- a/src/cache/ftccmap.c
+++ b/src/cache/ftccmap.c
@@ -200,7 +200,7 @@
   FT_EXPORT_DEF( FT_UInt )
   FTC_CMapCache_Lookup( FTC_CMapCache  cmap_cache,
                         FTC_FaceID     face_id,
-                        FT_UInt        cmap_index,
+                        FT_Int         cmap_index,
                         FT_UInt32      char_code )
   {
     FTC_Cache         cache = FTC_CACHE( cmap_cache );
@@ -218,7 +218,7 @@
     }
 
     query.face_id    = face_id;
-    query.cmap_index = cmap_index;
+    query.cmap_index = (FT_UInt)cmap_index;
     query.char_code  = char_code;
 
     hash = FTC_CMAP_HASH( face_id, cmap_index, char_code );
diff --git a/src/cache/ftcglyph.c b/src/cache/ftcglyph.c
index bca87f3..d0a3777 100644
--- a/src/cache/ftcglyph.c
+++ b/src/cache/ftcglyph.c
@@ -26,7 +26,6 @@
 #include "ftcerror.h"
 
 
-
   /* create a new chunk node, setting its cache index and ref count */
   FT_EXPORT_DEF( void )
   FTC_GNode_Init( FTC_GNode   gnode,
@@ -35,7 +34,6 @@
   {
     gnode->family = family;
     gnode->gindex = gindex;
-
     family->num_nodes++;
   }
 
@@ -48,8 +46,10 @@
 
     gnode->family = NULL;
     if ( family && --family->num_nodes <= 0 )
+    {
       FTC_MruList_Remove( & FTC_GCACHE(cache)->families, (FTC_MruNode)family );
   }
+  }
 
 
   FT_EXPORT_DEF( void )
@@ -138,26 +138,14 @@
                      FTC_Node    *anode )
   {
     FT_Error    error;
-    FTC_Family  family;
 
     query->gindex = gindex;
 
-    family = (FTC_Family) FTC_MruList_Lookup( &cache->families, query );
-    if ( family == NULL )
-    {
-      error = FTC_MruList_New( &cache->families, query, (FTC_MruNode*) &family );
-      if ( error )
-      {
-        *anode = NULL;
-        goto Exit;
-      }
-    }
-
-    query->family = family;
-
+    error = FTC_MruList_Lookup( &cache->families, query,
+                                (FTC_MruNode*) &query->family );
+    if ( !error )
     error = FTC_Cache_Lookup( FTC_CACHE(cache), hash, query, anode );
 
-  Exit:
     return error;
   }
 
diff --git a/src/cache/ftcmanag.c b/src/cache/ftcmanag.c
index b6bb4e9..769075d 100644
--- a/src/cache/ftcmanag.c
+++ b/src/cache/ftcmanag.c
@@ -144,7 +144,7 @@
                           FTC_Scaler     scaler,
                           FT_Size       *asize )
   {
-    FT_Error      error = 0;
+    FT_Error      error;
     FTC_SizeNode  node;
 
 
@@ -156,20 +156,16 @@
     if ( !manager )
       return FTC_Err_Invalid_Cache_Handle;
 
-    error = FTC_MruList_Get( &manager->sizes, scaler, (FTC_MruNode*) &node );
+    error = FTC_MruList_Lookup( &manager->sizes,
+                                scaler,
+                                (FTC_MruNode*) &node );
     if ( !error )
-    {
       *asize = node->size;
-      FT_Activate_Size( node->size );
-    }
 
-  Exit:
     return error;
   }
 
 
-
-
   /*************************************************************************/
   /*************************************************************************/
   /*****                                                               *****/
@@ -223,11 +219,8 @@
           node->face_id );
 
     /* all right, we can discard the face now */
-    if ( node->face )
-    {
-      FT_Done_Face( node->face );
-      node->face    = NULL;
-    }
+    FT_Done_Face( node->face );
+    node->face    = NULL;
     node->face_id = NULL;
   }
 
@@ -259,7 +252,7 @@
                           FTC_FaceID   face_id,
                           FT_Face     *aface )
   {
-    FT_Error      error = 0;
+    FT_Error      error;
     FTC_FaceNode  node;
 
 
@@ -271,17 +264,12 @@
     if ( !manager )
       return FTC_Err_Invalid_Cache_Handle;
 
-    node = (FTC_FaceNode) FTC_MruList_Lookup( &manager->faces, face_id );
-    if ( node == NULL )
-    {
-      error = FTC_MruList_New( &manager->faces, face_id, (FTC_MruNode*) &node );
-      if (error)
-        goto Exit;
-    }
-
+    error = FTC_MruList_Lookup( &manager->faces,
+                                face_id,
+                                (FTC_MruNode*) &node );
+    if ( !error )
     *aface = node->face;
 
-  Exit:
     return error;
   }
 
@@ -429,7 +417,7 @@
 
       do
       {
-        FTC_Cache     cache = manager->caches[node->cache_index];
+        FTC_Cache     cache = manager->caches[ node->cache_index ];
 
         if ( (FT_UInt)node->cache_index >= manager->num_caches )
           FT_ERROR(( "FTC_Manager_Check: invalid node (cache index = %ld\n",
@@ -439,7 +427,7 @@
           weight += cache->clazz.node_weight( node, cache );
         }
 
-        node = FTC_NODE__MRU_NEXT(node);
+        node = FTC_NODE__NEXT(node);
 
       } while ( node != first );
 
@@ -458,7 +446,7 @@
       do
       {
         count++;
-        node = FTC_NODE__MRU_NEXT(node);
+        node = FTC_NODE__NEXT(node);
 
       } while ( node != first );
 
@@ -501,23 +489,20 @@
       return;
 
     /* go to last node - it's a circular list */
-    node = FTC_NODE__MRU_PREV(first);
+    node = FTC_NODE__PREV(first);
     do
     {
-      FTC_Node  prev = FTC_NODE__MRU_PREV(node);
+      FTC_Node  prev;
 
 
-      prev = FTC_NODE__MRU_PREV(node);
+      prev = ( node == first ) ? NULL : FTC_NODE__PREV(node);
 
       if ( node->ref_count <= 0 )
         ftc_node_destroy( node, manager );
 
-      if ( node == first )
-        break;
-
       node = prev;
 
-    } while ( manager->cur_weight > manager->max_weight );
+    } while ( node && manager->cur_weight > manager->max_weight );
   }
 
 
@@ -586,10 +571,11 @@
       return 0;
 
     /* go to last node - it's a circular list */
-    node = FTC_NODE__MRU_PREV( first );
+    node = FTC_NODE__PREV(first);
     for ( result = 0; result < count; )
     {
-      FTC_Node  prev = FTC_NODE__MRU_PREV(node);
+      FTC_Node  prev = FTC_NODE__PREV(node);
+
 
      /* don't touch locked nodes */
       if ( node->ref_count <= 0 )
@@ -598,7 +584,7 @@
         result++;
       }
 
-      if ( node == first )
+      if ( prev == manager->nodes_list )
         break;
 
       node = prev;
diff --git a/src/cache/ftcmru.c b/src/cache/ftcmru.c
index 003c153..2c696fc 100644
--- a/src/cache/ftcmru.c
+++ b/src/cache/ftcmru.c
@@ -6,7 +6,6 @@
 
 #include "ftcerror.h"
 
-
   FT_EXPORT_DEF( void )
   FTC_MruNode_Prepend( FTC_MruNode  *plist,
                        FTC_MruNode   node )
@@ -17,10 +16,26 @@
     {
       FTC_MruNode  last = first->prev;
 
-      last->next  = node;
+#ifdef FT_DEBUG_ERROR
+      {
+        FTC_MruNode  cnode = first;
+        do
+        {
+          if ( cnode == node )
+          {
+            fprintf( stderr, "FTC_MruNode_Prepend: invalid action !!\n" );
+            exit(2);
+          }
+          cnode = cnode->next;
+        }
+        while ( cnode != first );
+      }
+#endif
+
       first->prev = node;
-      node->prev  = last;
+      last->next  = node;
       node->next  = first;
+      node->prev  = last;
     }
     else
     {
@@ -39,29 +54,46 @@
 
     FT_ASSERT( first != NULL );
 
-    if ( node != first )
+    if ( first != node )
     {
-      FTC_MruNode  prev = node->prev;
-      FTC_MruNode  next = node->next;
-      FTC_MruNode  last;
+      FTC_MruNode  prev, next, last;
+
+#ifdef FT_DEBUG_ERROR
+      {
+        FTC_MruNode  cnode = first;
+        do
+        {
+          if ( cnode == node )
+            goto Ok;
+          cnode = cnode->next;
+        }
+        while ( cnode != first );
+
+        fprintf( stderr, "FTC_MruNode_Up: invalid action !!\n" );
+        exit(2);
+      Ok:
+      }
+#endif
+      prev = node->prev;
+      next = node->next;
 
       prev->next = next;
       next->prev = prev;
 
       last = first->prev;
 
-      first->prev = node;
       last->next  = node;
+      first->prev = node;
 
-      node->prev  = last;
       node->next  = first;
+      node->prev = last;
 
       *plist = node;
     }
   }
 
 
-  FT_EXPORT( void )
+  FT_EXPORT_DEF( void )
   FTC_MruNode_Remove( FTC_MruNode  *plist,
                       FTC_MruNode   node )
   {
@@ -70,27 +102,39 @@
 
     FT_ASSERT( first != NULL );
 
-    next = node->next;
+#ifdef FT_DEBUG_ERROR
+      {
+        FTC_MruNode  cnode = first;
+        do
+        {
+          if ( cnode == node )
+            goto Ok;
+          cnode = cnode->next;
+        }
+        while ( cnode != first );
+
+        fprintf( stderr, "FTC_MruNode_Remove: invalid action !!\n" );
+        exit(2);
+      Ok:
+      }
+#endif
+
     prev = node->prev;
+    next = node->next;
+
+    prev->next = next;
+    next->prev = prev;
 
     if ( node == next )
     {
-      FT_ASSERT( node == prev );
-      FT_ASSERT( node == first );
+      FT_ASSERT( first == node );
+      FT_ASSERT( prev  == node );
 
       *plist = NULL;
     }
-    else
-    {
-      prev->next = next;
-      next->prev = prev;
-
-      if ( node == first )
+    else if ( node == first )
         *plist = next;
     }
-    node->prev = NULL;
-    node->next = NULL;
-  }
 
 
 
@@ -110,34 +154,13 @@
   }
 
 
-
-
   FT_EXPORT( void )
   FTC_MruList_Reset( FTC_MruList  list )
   {
-    FT_Memory    memory = list->memory;
-    FTC_MruNode  first  = list->nodes;
-
-    if ( first )
-    {
-      FTC_MruNode  node = first;
-      FTC_MruNode  next;
-
-      do
-      {
-        next = node->next;
-
-        if ( list->clazz.node_done )
-          list->clazz.node_done( node, list->data );
+    while ( list->nodes )
+      FTC_MruList_Remove( list, list->nodes );
 
-        FT_FREE( node );
-
-        node = next;
-      }
-      while ( node != first );
-    }
-    list->nodes     = NULL;
-    list->num_nodes = 0;
+    FT_ASSERT( list->num_nodes == 0 );
   }
 
 
@@ -148,20 +171,13 @@
   }
 
 
-  FT_EXPORT_DEF( void )
-  FTC_MruList_Up( FTC_MruList  list,
-                  FTC_MruNode  node )
-  {
-    FTC_MruNode_Up( &list->nodes, node );
-  }
-
 
   FT_EXPORT_DEF( FTC_MruNode )
-  FTC_MruList_Lookup( FTC_MruList  list,
+  FTC_MruList_Find( FTC_MruList  list,
                       FT_Pointer   key )
   {
     FTC_MruNode_CompareFunc  compare = list->clazz.node_compare;
-    FTC_MruNode              node, first;
+    FTC_MruNode              first, node;
 
     first = list->nodes;
     node  = NULL;
@@ -172,16 +188,13 @@
       do
       {
         if ( compare( node, key ) )
-          goto Exit;
+          return node;
 
         node = node->next;
-
-      } while ( node != first );
-
-      node = NULL;
     }
-  Exit:
-    return node;
+      while ( node != first) ;
+    }
+    return NULL;
   }
 
 
@@ -191,15 +204,15 @@
                    FT_Pointer     key,
                    FTC_MruNode   *anode )
   {
-    FT_Memory    memory = list->memory;
     FT_Error     error;
     FTC_MruNode  node;
+    FT_Memory    memory = list->memory;
 
-    if ( list->max_nodes > 0 && list->num_nodes >= list->max_nodes )
+    if ( list->num_nodes >= list->max_nodes && list->max_nodes > 0 )
     {
-      FT_ASSERT( list->nodes != NULL );
+      node = list->nodes->prev;
 
-      node = list->nodes->prev;  /* last node */
+      FT_ASSERT( node );
 
       if ( list->clazz.node_reset )
       {
@@ -216,48 +229,43 @@
       if ( list->clazz.node_done )
         list->clazz.node_done( node, list->data );
     }
-    else
-    {
-      if ( FT_ALLOC( node, list->clazz.node_size ) )
+    else if ( FT_ALLOC( node, list->clazz.node_size ) )
         goto Exit;
-    }
+
     error = list->clazz.node_init( node, key, list->data );
-    if ( !error )
-    {
+    if ( error )
+      goto Fail;
+
       FTC_MruNode_Prepend( &list->nodes, node );
       list->num_nodes++;
-      goto Exit;
-    }
 
+  Exit:
+    *anode = node;
+    return error;
+
+  Fail:
     if ( list->clazz.node_done )
       list->clazz.node_done( node, list->data );
 
     FT_FREE( node );
-
-  Exit:
-    *anode = node;
-    return error;
+    goto Exit;
   }
 
 
-
-  FT_EXPORT_DEF( FT_Error )
-  FTC_MruList_Get( FTC_MruList   list,
+  FT_EXPORT( FT_Error )
+  FTC_MruList_Lookup( FTC_MruList   list,
                    FT_Pointer    key,
                    FTC_MruNode  *anode )
   {
-    FT_Error     error = 0;
     FTC_MruNode  node;
+    FT_Error     error = 0;
 
-    node = FTC_MruList_Lookup( list, key );
+    node = FTC_MruList_Find( list, key );
     if ( node == NULL )
-    {
-      error = FTC_MruList_New( list, key, &node );
-      if ( error )
-        node = NULL;
-    }
+      return FTC_MruList_New( list, key, anode );
+
     *anode = node;
-    return error;
+    return 0;
   }
 
 
@@ -265,18 +273,18 @@
   FTC_MruList_Remove( FTC_MruList   list,
                       FTC_MruNode   node )
   {
-    FT_Memory    memory = list->memory;
-
-    FT_ASSERT( list->nodes != NULL && list->num_nodes > 0 );
-
     FTC_MruNode_Remove( &list->nodes, node );
     list->num_nodes--;
 
+    {
+      FT_Memory  memory = list->memory;
+
     if ( list->clazz.node_done )
       list->clazz.node_done( node, list->data );
 
     FT_FREE( node );
   }
+  }
 
 
   FT_EXPORT_DEF( void )
@@ -284,8 +292,9 @@
                                FTC_MruNode_CompareFunc  select,
                                FT_Pointer               key )
   {
-    FTC_MruNode   first = list->nodes;
+    FTC_MruNode  first, node, next;
 
+    first = list->nodes;
     while ( first && select( first, key ) )
     {
       FTC_MruList_Remove( list, first );
@@ -294,9 +303,7 @@
 
     if ( first )
     {
-      FTC_MruNode  node = first->next;
-      FTC_MruNode  next;
-
+      node = first->next;
       while ( node != first )
       {
         next = node->next;
diff --git a/src/cache/ftcsbits.c b/src/cache/ftcsbits.c
index 3530993..78c3182 100644
--- a/src/cache/ftcsbits.c
+++ b/src/cache/ftcsbits.c
@@ -235,12 +235,11 @@
     FT_ULong   size;
 
 
+    FT_ASSERT( snode->count <= FTC_SBITS_ITEM_PER_NODE );
+
     /* the node itself */
     size = sizeof ( *snode );
 
-    /* the sbit records */
-    size += count * sizeof( FTC_SBitRec );
-
     for ( ; count > 0; count--, sbit++ )
     {
       if ( sbit->buffer )