]> git.wh0rd.org - fontconfig.git/blobdiff - src/fccharset.c
Pass directory information around in FcCache structure. Freeze charsets.
[fontconfig.git] / src / fccharset.c
index 3a93c4a96cec00723e6a696a817f3530a2803507..43aa22c46d873ccc55288047a222db1dba07bd3d 100644 (file)
@@ -27,8 +27,6 @@
 
 /* #define CHECK */
 
-/* #define CHATTY */
-
 FcCharSet *
 FcCharSetCreate (void)
 {
@@ -40,8 +38,8 @@ FcCharSetCreate (void)
     FcMemAlloc (FC_MEM_CHARSET, sizeof (FcCharSet));
     fcs->ref = 1;
     fcs->num = 0;
-    fcs->leaves_offset = FcPtrToOffset (fcs, NULL);
-    fcs->numbers_offset = FcPtrToOffset (fcs, NULL);
+    fcs->leaves_offset = 0;
+    fcs->numbers_offset = 0;
     return fcs;
 }
 
@@ -68,12 +66,12 @@ FcCharSetDestroy (FcCharSet *fcs)
        FcMemFree (FC_MEM_CHARLEAF, sizeof (FcCharLeaf));
        free (FcCharSetLeaf (fcs, i));
     }
-    if (FcCharSetLeaves (fcs))
+    if (fcs->num)
     {
        FcMemFree (FC_MEM_CHARSET, fcs->num * sizeof (intptr_t));
        free (FcCharSetLeaves (fcs));
     }
-    if (FcCharSetNumbers (fcs))
+    if (fcs->num)
     {
        FcMemFree (FC_MEM_CHARSET, fcs->num * sizeof (FcChar16));
        free (FcCharSetNumbers (fcs));
@@ -136,7 +134,7 @@ FcCharSetPutLeaf (FcCharSet *fcs,
     ucs4 >>= 8;
     if (ucs4 >= 0x10000)
        return FcFalse;
-    if (!leaves)
+    if (!fcs->num)
        leaves = malloc (sizeof (*leaves));
     else
     {
@@ -161,7 +159,7 @@ FcCharSetPutLeaf (FcCharSet *fcs,
     FcMemAlloc (FC_MEM_CHARSET, (fcs->num + 1) * sizeof (intptr_t));
     fcs->leaves_offset = FcPtrToOffset (fcs, leaves);
     
-    if (!numbers)
+    if (!fcs->num)
        numbers = malloc (sizeof (FcChar16));
     else
        numbers = realloc (numbers, (fcs->num + 1) * sizeof (FcChar16));
@@ -278,9 +276,6 @@ FcCharSetIterSet (const FcCharSet *fcs, FcCharSetIter *iter)
     }
     iter->leaf = FcCharSetLeaf(fcs, pos);
     iter->pos = pos;
-#ifdef CHATTY
-    printf ("set %08x: %08x\n", iter->ucs4, (FcChar32) iter->leaf);
-#endif
 }
 
 static void
@@ -300,29 +295,10 @@ FcCharSetIterNext (const FcCharSet *fcs, FcCharSetIter *iter)
     }
 }
 
-#ifdef CHATTY
-static void
-FcCharSetDump (const FcCharSet *fcs)
-{
-    int                pos;
-
-    printf ("fcs %08x:\n", (FcChar32) fcs);
-    for (pos = 0; pos < fcs->num; pos++)
-    {
-       FcCharLeaf      *leaf = fcs->leaves[pos];
-       FcChar32        ucs4 = (FcChar32) fcs->numbers[pos] << 8;
-       
-       printf ("    %08x: %08x\n", ucs4, (FcChar32) leaf);
-    }
-}
-#endif
 
 static void
 FcCharSetIterStart (const FcCharSet *fcs, FcCharSetIter *iter)
 {
-#ifdef CHATTY
-    FcCharSetDump (fcs);
-#endif
     iter->ucs4 = 0;
     iter->pos = 0;
     FcCharSetIterSet (fcs, iter);
@@ -854,6 +830,133 @@ FcCharSetUnparseValue (FcStrBuf *buf, FcChar32 value)
     return FcTrue;
 }
 
+FcCharSet *
+FcNameParseCharSet (FcChar8 *string)
+{
+    FcCharSet  *c;
+    FcChar32   ucs4;
+    FcCharLeaf *leaf;
+    FcCharLeaf temp;
+    FcChar32   bits;
+    int                i;
+
+    c = FcCharSetCreate ();
+    if (!c)
+       goto bail0;
+    while (*string)
+    {
+       string = FcCharSetParseValue (string, &ucs4);
+       if (!string)
+           goto bail1;
+       bits = 0;
+       for (i = 0; i < 256/32; i++)
+       {
+           string = FcCharSetParseValue (string, &temp.map[i]);
+           if (!string)
+               goto bail1;
+           bits |= temp.map[i];
+       }
+       if (bits)
+       {
+           leaf = malloc (sizeof (FcCharLeaf));
+           if (!leaf)
+               goto bail1;
+           *leaf = temp;
+           if (!FcCharSetInsertLeaf (c, ucs4, leaf))
+               goto bail1;
+       }
+    }
+    return c;
+bail1:
+    if (c->num)
+    {
+       FcMemFree (FC_MEM_CHARSET, c->num * sizeof (FcCharLeaf *));
+       free (FcCharSetLeaves (c));
+    }
+    if (c->num)
+    {
+       FcMemFree (FC_MEM_CHARSET, c->num * sizeof (FcChar16));
+       free (FcCharSetNumbers (c));
+    }
+    FcMemFree (FC_MEM_CHARSET, sizeof (FcCharSet));
+    free (c);
+bail0:
+    return NULL;
+}
+
+FcBool
+FcNameUnparseCharSet (FcStrBuf *buf, const FcCharSet *c)
+{
+    FcCharSetIter   ci;
+    int                    i;
+#ifdef CHECK
+    int                    len = buf->len;
+#endif
+
+    for (FcCharSetIterStart (c, &ci);
+        ci.leaf;
+        FcCharSetIterNext (c, &ci))
+    {
+       if (!FcCharSetUnparseValue (buf, ci.ucs4))
+           return FcFalse;
+       for (i = 0; i < 256/32; i++)
+           if (!FcCharSetUnparseValue (buf, ci.leaf->map[i]))
+               return FcFalse;
+    }
+#ifdef CHECK
+    {
+       FcCharSet       *check;
+       FcChar32        missing;
+       FcCharSetIter   ci, checki;
+       
+       /* null terminate for parser */
+       FcStrBufChar (buf, '\0');
+       /* step back over null for life after test */
+       buf->len--;
+       check = FcNameParseCharSet (buf->buf + len);
+       FcCharSetIterStart (c, &ci);
+       FcCharSetIterStart (check, &checki);
+       while (ci.leaf || checki.leaf)
+       {
+           if (ci.ucs4 < checki.ucs4)
+           {
+               printf ("Missing leaf node at 0x%x\n", ci.ucs4);
+               FcCharSetIterNext (c, &ci);
+           }
+           else if (checki.ucs4 < ci.ucs4)
+           {
+               printf ("Extra leaf node at 0x%x\n", checki.ucs4);
+               FcCharSetIterNext (check, &checki);
+           }
+           else
+           {
+               int         i = 256/32;
+               FcChar32    *cm = ci.leaf->map;
+               FcChar32    *checkm = checki.leaf->map;
+
+               for (i = 0; i < 256; i += 32)
+               {
+                   if (*cm != *checkm)
+                       printf ("Mismatching sets at 0x%08x: 0x%08x != 0x%08x\n",
+                               ci.ucs4 + i, *cm, *checkm);
+                   cm++;
+                   checkm++;
+               }
+               FcCharSetIterNext (c, &ci);
+               FcCharSetIterNext (check, &checki);
+           }
+       }
+       if ((missing = FcCharSetSubtractCount (c, check)))
+           printf ("%d missing in reparsed result\n", missing);
+       if ((missing = FcCharSetSubtractCount (check, c)))
+           printf ("%d extra in reparsed result\n", missing);
+       FcCharSetDestroy (check);
+    }
+#endif
+    
+    return FcTrue;
+}
+
 typedef struct _FcCharLeafEnt FcCharLeafEnt;
 
 struct _FcCharLeafEnt {
@@ -863,36 +966,63 @@ struct _FcCharLeafEnt {
 };
 
 #define FC_CHAR_LEAF_BLOCK     (4096 / sizeof (FcCharLeafEnt))
-static FcCharLeafEnt **FcCharLeafBlocks;
-static int FcCharLeafBlockCount;
+#define FC_CHAR_LEAF_HASH_SIZE 257
+
+typedef struct _FcCharSetEnt FcCharSetEnt;
+
+struct _FcCharSetEnt {
+    FcCharSetEnt       *next;
+    FcChar32           hash;
+    FcCharSet          set;
+};
+
+typedef struct _FcCharSetOrigEnt FcCharSetOrigEnt;
+
+struct _FcCharSetOrigEnt {
+    FcCharSetOrigEnt   *next;
+    const FcCharSet            *orig;
+    const FcCharSet            *frozen;
+};
+
+#define FC_CHAR_SET_HASH_SIZE    67
+
+struct _FcCharSetFreezer {
+    FcCharLeafEnt   *leaf_hash_table[FC_CHAR_LEAF_HASH_SIZE];
+    FcCharLeafEnt   **leaf_blocks;
+    int                    leaf_block_count;
+    FcCharSetEnt    *set_hash_table[FC_CHAR_SET_HASH_SIZE];
+    FcCharSetOrigEnt   *orig_hash_table[FC_CHAR_SET_HASH_SIZE];
+    FcCharLeafEnt   *current_block;
+    int                    leaf_remain;
+    int                    leaves_seen;
+    int                    charsets_seen;
+    int                    leaves_allocated;
+    int                    charsets_allocated;
+};
 
 static FcCharLeafEnt *
-FcCharLeafEntCreate (void)
+FcCharLeafEntCreate (FcCharSetFreezer *freezer)
 {
-    static FcCharLeafEnt    *block;
-    static int             remain;
-
-    if (!remain)
+    if (!freezer->leaf_remain)
     {
        FcCharLeafEnt **newBlocks;
 
-       FcCharLeafBlockCount++;
-       newBlocks = realloc (FcCharLeafBlocks, FcCharLeafBlockCount * sizeof (FcCharLeafEnt *));
+       freezer->leaf_block_count++;
+       newBlocks = realloc (freezer->leaf_blocks, freezer->leaf_block_count * sizeof (FcCharLeafEnt *));
        if (!newBlocks)
            return 0;
-       FcCharLeafBlocks = newBlocks;
-       block = FcCharLeafBlocks[FcCharLeafBlockCount-1] = malloc (FC_CHAR_LEAF_BLOCK * sizeof (FcCharLeafEnt));
-       if (!block)
+       freezer->leaf_blocks = newBlocks;
+       freezer->current_block = freezer->leaf_blocks[freezer->leaf_block_count-1] = malloc (FC_CHAR_LEAF_BLOCK * sizeof (FcCharLeafEnt));
+       if (!freezer->current_block)
            return 0;
        FcMemAlloc (FC_MEM_CHARLEAF, FC_CHAR_LEAF_BLOCK * sizeof (FcCharLeafEnt));
-       remain = FC_CHAR_LEAF_BLOCK;
+       freezer->leaf_remain = FC_CHAR_LEAF_BLOCK;
     }
-    remain--;
-    return block++;
+    freezer->leaf_remain--;
+    freezer->leaves_allocated++;
+    return freezer->current_block++;
 }
 
-#define FC_CHAR_LEAF_HASH_SIZE 257
-
 static FcChar32
 FcCharLeafHash (FcCharLeaf *leaf)
 {
@@ -904,29 +1034,22 @@ FcCharLeafHash (FcCharLeaf *leaf)
     return hash;
 }
 
-static int     FcCharLeafTotal;
-static int     FcCharLeafUsed;
-
-static FcCharLeafEnt   *FcCharLeafHashTable[FC_CHAR_LEAF_HASH_SIZE];
-
 static FcCharLeaf *
-FcCharSetFreezeLeaf (FcCharLeaf *leaf)
+FcCharSetFreezeLeaf (FcCharSetFreezer *freezer, FcCharLeaf *leaf)
 {
     FcChar32                   hash = FcCharLeafHash (leaf);
-    FcCharLeafEnt              **bucket = &FcCharLeafHashTable[hash % FC_CHAR_LEAF_HASH_SIZE];
+    FcCharLeafEnt              **bucket = &freezer->leaf_hash_table[hash % FC_CHAR_LEAF_HASH_SIZE];
     FcCharLeafEnt              *ent;
     
-    FcCharLeafTotal++;
     for (ent = *bucket; ent; ent = ent->next)
     {
        if (ent->hash == hash && !memcmp (&ent->leaf, leaf, sizeof (FcCharLeaf)))
            return &ent->leaf;
     }
 
-    ent = FcCharLeafEntCreate();
+    ent = FcCharLeafEntCreate(freezer);
     if (!ent)
        return 0;
-    FcCharLeafUsed++;
     ent->leaf = *leaf;
     ent->hash = hash;
     ent->next = *bucket;
@@ -934,35 +1057,6 @@ FcCharSetFreezeLeaf (FcCharLeaf *leaf)
     return &ent->leaf;
 }
 
-static void
-FcCharSetThawAllLeaf (void)
-{
-    int i;
-
-    for (i = 0; i < FC_CHAR_LEAF_HASH_SIZE; i++)
-       FcCharLeafHashTable[i] = 0;
-
-    FcCharLeafTotal = 0;
-    FcCharLeafUsed = 0;
-
-    for (i = 0; i < FcCharLeafBlockCount; i++)
-       free (FcCharLeafBlocks[i]);
-
-    free (FcCharLeafBlocks);
-    FcCharLeafBlocks = 0;
-    FcCharLeafBlockCount = 0;
-}
-
-typedef struct _FcCharSetEnt FcCharSetEnt;
-
-struct _FcCharSetEnt {
-    FcCharSetEnt       *next;
-    FcChar32           hash;
-    FcCharSet          set;
-};
-
-#define FC_CHAR_SET_HASH_SIZE    67
-
 static FcChar32
 FcCharSetHash (FcCharSet *fcs)
 {
@@ -978,23 +1072,31 @@ FcCharSetHash (FcCharSet *fcs)
     return hash;
 }
 
-static int     FcCharSetTotal;
-static int     FcCharSetUsed;
-static int     FcCharSetTotalEnts, FcCharSetUsedEnts;
-
-static FcCharSetEnt    *FcCharSetHashTable[FC_CHAR_SET_HASH_SIZE];
+static FcBool
+FcCharSetFreezeOrig (FcCharSetFreezer *freezer, const FcCharSet *orig, const FcCharSet *frozen)
+{
+    FcCharSetOrigEnt   **bucket = &freezer->orig_hash_table[((uintptr_t) orig) & FC_CHAR_SET_HASH_SIZE];
+    FcCharSetOrigEnt   *ent;
+    
+    ent = malloc (sizeof (FcCharSetOrigEnt));
+    if (!ent)
+       return FcFalse;
+    ent->orig = orig;
+    ent->frozen = frozen;
+    ent->next = *bucket;
+    *bucket = ent;
+    return FcTrue;
+}
 
 static FcCharSet *
-FcCharSetFreezeBase (FcCharSet *fcs)
+FcCharSetFreezeBase (FcCharSetFreezer *freezer, FcCharSet *fcs, const FcCharSet *orig)
 {
     FcChar32           hash = FcCharSetHash (fcs);
-    FcCharSetEnt       **bucket = &FcCharSetHashTable[hash % FC_CHAR_SET_HASH_SIZE];
+    FcCharSetEnt       **bucket = &freezer->set_hash_table[hash % FC_CHAR_SET_HASH_SIZE];
     FcCharSetEnt       *ent;
     int                        size;
     int                        i;
 
-    FcCharSetTotal++;
-    FcCharSetTotalEnts += fcs->num;
     for (ent = *bucket; ent; ent = ent->next)
     {
        if (ent->hash == hash &&
@@ -1021,8 +1123,8 @@ FcCharSetFreezeBase (FcCharSet *fcs)
     if (!ent)
        return 0;
     FcMemAlloc (FC_MEM_CHARSET, size);
-    FcCharSetUsed++;
-    FcCharSetUsedEnts += fcs->num;
+    
+    freezer->charsets_allocated++;
     
     ent->set.ref = FC_REF_CONSTANT;
     ent->set.num = fcs->num;
@@ -1051,60 +1153,60 @@ FcCharSetFreezeBase (FcCharSet *fcs)
     ent->hash = hash;
     ent->next = *bucket;
     *bucket = ent;
+
     return &ent->set;
 }
 
-void
-FcCharSetThawAll (void)
+static const FcCharSet *
+FcCharSetFindFrozen (FcCharSetFreezer *freezer, const FcCharSet *orig)
 {
-    int i;
-    FcCharSetEnt       *ent, *next;
-
-    for (i = 0; i < FC_CHAR_SET_HASH_SIZE; i++)
-    {
-       for (ent = FcCharSetHashTable[i]; ent; ent = next)
-       {
-           next = ent->next;
-           free (ent);
-       }
-       FcCharSetHashTable[i] = 0;
-    }
-
-    FcCharSetTotal = 0;
-    FcCharSetTotalEnts = 0;
-    FcCharSetUsed = 0;
-    FcCharSetUsedEnts = 0;
-
-    FcCharSetThawAllLeaf ();
+    FcCharSetOrigEnt    **bucket = &freezer->orig_hash_table[((uintptr_t) orig) & FC_CHAR_SET_HASH_SIZE];
+    FcCharSetOrigEnt   *ent;
+    
+    for (ent = *bucket; ent; ent = ent->next)
+       if (ent->orig == orig)
+           return ent->frozen;
+    return NULL;
 }
 
-FcCharSet *
-FcCharSetFreeze (FcCharSet *fcs)
+static const FcCharSet *
+FcCharSetFreeze (FcCharSetFreezer *freezer, const FcCharSet *fcs)
 {
-    FcCharSet  *b;
-    FcCharSet  *n = 0;
-    FcCharLeaf *l;
-    int                i;
+    FcCharSet      *b;
+    const FcCharSet *n = 0;
+    FcCharLeaf     *l;
+    int                    i;
 
+    n = FcCharSetFindFrozen (freezer, fcs);
+    if (n)
+       return n;
+    
     b = FcCharSetCreate ();
     if (!b)
        goto bail0;
     for (i = 0; i < fcs->num; i++)
     {
-       l = FcCharSetFreezeLeaf (FcCharSetLeaf(fcs, i));
+       l = FcCharSetFreezeLeaf (freezer, FcCharSetLeaf(fcs, i));
        if (!l)
            goto bail1;
        if (!FcCharSetInsertLeaf (b, FcCharSetNumbers(fcs)[i] << 8, l))
            goto bail1;
     }
-    n = FcCharSetFreezeBase (b);
+    n = FcCharSetFreezeBase (freezer, b, fcs);
+    if (!FcCharSetFreezeOrig (freezer, fcs, n))
+    {
+       n = NULL;
+       goto bail1;
+    }
+    freezer->charsets_seen++;
+    freezer->leaves_seen += fcs->num;
 bail1:
-    if (FcCharSetLeaves (b))
+    if (b->num)
     {
        FcMemFree (FC_MEM_CHARSET, b->num * sizeof (FcCharLeaf *));
        free (FcCharSetLeaves (b));
     }
-    if (FcCharSetNumbers (b))
+    if (b->num)
     {
        FcMemFree (FC_MEM_CHARSET, b->num * sizeof (FcChar16));
        free (FcCharSetNumbers (b));
@@ -1115,160 +1217,74 @@ bail0:
     return n;
 }
 
-FcCharSet *
-FcNameParseCharSet (FcChar8 *string)
+static FcCharSetFreezer *
+FcCharSetFreezerCreate (void)
 {
-    FcCharSet  *c, *n = 0;
-    FcChar32   ucs4;
-    FcCharLeaf *leaf;
-    FcCharLeaf temp;
-    FcChar32   bits;
-    int                i;
+    FcCharSetFreezer   *freezer;
 
-    c = FcCharSetCreate ();
-    if (!c)
-       goto bail0;
-    while (*string)
-    {
-       string = FcCharSetParseValue (string, &ucs4);
-       if (!string)
-           goto bail1;
-       bits = 0;
-       for (i = 0; i < 256/32; i++)
-       {
-           string = FcCharSetParseValue (string, &temp.map[i]);
-           if (!string)
-               goto bail1;
-           bits |= temp.map[i];
-       }
-       if (bits)
-       {
-           leaf = FcCharSetFreezeLeaf (&temp);
-           if (!leaf)
-               goto bail1;
-           if (!FcCharSetInsertLeaf (c, ucs4, leaf))
-               goto bail1;
-       }
-    }
-#ifdef CHATTY
-    printf ("          %8s %8s %8s %8s\n", "total", "totalmem", "new", "newmem");
-    printf ("Leaves:   %8d %8d %8d %8d\n",
-           FcCharLeafTotal, sizeof (FcCharLeaf) * FcCharLeafTotal,
-           FcCharLeafUsed, sizeof (FcCharLeaf) * FcCharLeafUsed);
-    printf ("Charsets: %8d %8d %8d %8d\n",
-           FcCharSetTotal, sizeof (FcCharSet) * FcCharSetTotal,
-           FcCharSetUsed, sizeof (FcCharSet) * FcCharSetUsed);
-    printf ("Tables:   %8d %8d %8d %8d\n",
-           FcCharSetTotalEnts, FcCharSetTotalEnts * (sizeof (FcCharLeaf *) + sizeof (FcChar16)),
-           FcCharSetUsedEnts, FcCharSetUsedEnts * (sizeof (FcCharLeaf *) + sizeof (FcChar16)));
-    printf ("Total:    %8s %8d %8s %8d\n", 
-           "", 
-           sizeof (FcCharLeaf) * FcCharLeafTotal +
-           sizeof (FcCharSet) * FcCharSetTotal +
-           FcCharSetTotalEnts * (sizeof (FcCharLeaf *) + sizeof (FcChar16)),
-           "",
-           sizeof (FcCharLeaf) * FcCharLeafUsed +
-           sizeof (FcCharSet) * FcCharSetUsed +
-           FcCharSetUsedEnts * (sizeof (FcCharLeaf *) + sizeof (FcChar16)));
-#endif
-    n = FcCharSetFreezeBase (c);
-bail1:
-    if (FcCharSetLeaves (c))
-    {
-       FcMemFree (FC_MEM_CHARSET, c->num * sizeof (FcCharLeaf *));
-       free (FcCharSetLeaves (c));
-    }
-    if (FcCharSetNumbers (c))
-    {
-       FcMemFree (FC_MEM_CHARSET, c->num * sizeof (FcChar16));
-       free (FcCharSetNumbers (c));
-    }
-    FcMemFree (FC_MEM_CHARSET, sizeof (FcCharSet));
-    free (c);
-bail0:
-    return n;
+    freezer = calloc (1, sizeof (FcCharSetFreezer));
+    return freezer;
 }
 
-FcBool
-FcNameUnparseCharSet (FcStrBuf *buf, const FcCharSet *c)
+void
+FcCharSetFreezerDestroy (FcCharSetFreezer *freezer)
 {
-    FcCharSetIter   ci;
-    int                    i;
-#ifdef CHECK
-    int                    len = buf->len;
-#endif
+    int i;
 
-    for (FcCharSetIterStart (c, &ci);
-        ci.leaf;
-        FcCharSetIterNext (c, &ci))
+    if (FcDebug() & FC_DBG_CACHE)
     {
-       if (!FcCharSetUnparseValue (buf, ci.ucs4))
-           return FcFalse;
-       for (i = 0; i < 256/32; i++)
-           if (!FcCharSetUnparseValue (buf, ci.leaf->map[i]))
-               return FcFalse;
+       printf ("\ncharsets %d -> %d leaves %d -> %d\n",
+               freezer->charsets_seen, freezer->charsets_allocated,
+               freezer->leaves_seen, freezer->leaves_allocated);
     }
-#ifdef CHECK
+    for (i = 0; i < FC_CHAR_SET_HASH_SIZE; i++)
     {
-       FcCharSet       *check;
-       FcChar32        missing;
-       FcCharSetIter   ci, checki;
-       
-       /* null terminate for parser */
-       FcStrBufChar (buf, '\0');
-       /* step back over null for life after test */
-       buf->len--;
-       check = FcNameParseCharSet (buf->buf + len);
-       FcCharSetIterStart (c, &ci);
-       FcCharSetIterStart (check, &checki);
-       while (ci.leaf || checki.leaf)
+       FcCharSetEnt    *ent, *next;
+       for (ent = freezer->set_hash_table[i]; ent; ent = next)
        {
-           if (ci.ucs4 < checki.ucs4)
-           {
-               printf ("Missing leaf node at 0x%x\n", ci.ucs4);
-               FcCharSetIterNext (c, &ci);
-           }
-           else if (checki.ucs4 < ci.ucs4)
-           {
-               printf ("Extra leaf node at 0x%x\n", checki.ucs4);
-               FcCharSetIterNext (check, &checki);
-           }
-           else
-           {
-               int         i = 256/32;
-               FcChar32    *cm = ci.leaf->map;
-               FcChar32    *checkm = checki.leaf->map;
+           next = ent->next;
+           free (ent);
+       }
+    }
 
-               for (i = 0; i < 256; i += 32)
-               {
-                   if (*cm != *checkm)
-                       printf ("Mismatching sets at 0x%08x: 0x%08x != 0x%08x\n",
-                               ci.ucs4 + i, *cm, *checkm);
-                   cm++;
-                   checkm++;
-               }
-               FcCharSetIterNext (c, &ci);
-               FcCharSetIterNext (check, &checki);
-           }
+    for (i = 0; i < FC_CHAR_SET_HASH_SIZE; i++)
+    {
+       FcCharSetOrigEnt        *ent, *next;
+       for (ent = freezer->orig_hash_table[i]; ent; ent = next)
+       {
+           next = ent->next;
+           free (ent);
        }
-       if ((missing = FcCharSetSubtractCount (c, check)))
-           printf ("%d missing in reparsed result\n", missing);
-       if ((missing = FcCharSetSubtractCount (check, c)))
-           printf ("%d extra in reparsed result\n", missing);
-       FcCharSetDestroy (check);
     }
-#endif
-    
-    return FcTrue;
+
+    for (i = 0; i < freezer->leaf_block_count; i++)
+       free (freezer->leaf_blocks[i]);
+
+    free (freezer->leaf_blocks);
+    free (freezer);
 }
 
 FcBool
 FcCharSetSerializeAlloc (FcSerialize *serialize, const FcCharSet *cs)
 {
-    intptr_t       *leaves = FcCharSetLeaves (cs);
-    FcChar16       *numbers = FcCharSetNumbers (cs);
+    intptr_t       *leaves;
+    FcChar16       *numbers;
     int                    i;
     
+    if (cs->ref != FC_REF_CONSTANT)
+    {
+       if (!serialize->cs_freezer)
+       {
+           serialize->cs_freezer = FcCharSetFreezerCreate ();
+           if (!serialize->cs_freezer)
+               return FcFalse;
+       }
+        cs = FcCharSetFreeze (serialize->cs_freezer, cs);
+    }
+    
+    leaves = FcCharSetLeaves (cs);
+    numbers = FcCharSetNumbers (cs);
+    
     if (!FcSerializeAlloc (serialize, cs, sizeof (FcCharSet)))
        return FcFalse;
     if (!FcSerializeAlloc (serialize, leaves, cs->num * sizeof (intptr_t)))
@@ -1285,12 +1301,20 @@ FcCharSetSerializeAlloc (FcSerialize *serialize, const FcCharSet *cs)
 FcCharSet *
 FcCharSetSerialize(FcSerialize *serialize, const FcCharSet *cs)
 {
-    FcCharSet  *cs_serialized = FcSerializePtr (serialize, cs);
+    FcCharSet  *cs_serialized;
     intptr_t   *leaves, *leaves_serialized;
     FcChar16   *numbers, *numbers_serialized;
     FcCharLeaf *leaf, *leaf_serialized;
     int                i;
 
+    if (cs->ref != FC_REF_CONSTANT && serialize->cs_freezer)
+    {
+       cs = FcCharSetFindFrozen (serialize->cs_freezer, cs);
+       if (!cs)
+           return NULL;
+    }
+                   
+    cs_serialized = FcSerializePtr (serialize, cs);
     if (!cs_serialized)
        return NULL;