]> git.wh0rd.org - fontconfig.git/blobdiff - src/fcpat.c
Copy the full pathname whenever duplicating an FcPattern; otherwise,
[fontconfig.git] / src / fcpat.c
index 572e897359057ab7aa21e945045e30e6e3e09957..b7f52793c6e63a0515038a53660e4a0776c037b8 100644 (file)
@@ -1,7 +1,7 @@
 /*
- * $XFree86: xc/lib/fontconfig/src/fcpat.c,v 1.18 2002/09/18 17:11:46 tsi Exp $
+ * $RCSId: xc/lib/fontconfig/src/fcpat.c,v 1.18 2002/09/18 17:11:46 tsi Exp $
  *
- * Copyright © 2000 Keith Packard, member of The XFree86 Project, Inc.
+ * Copyright © 2000 Keith Packard
  *
  * Permission to use, copy, modify, distribute, and sell this software and its
  * documentation for any purpose is hereby granted without fee, provided that
 #include <assert.h>
 #include "fcint.h"
 
+static FcPattern ** fcpatterns = 0;
+static int fcpattern_bank_count = 0, fcpattern_ptr, fcpattern_count;
+static FcPatternElt ** fcpatternelts = 0;
+static int fcpatternelt_ptr, fcpatternelt_count;
+static FcValueList ** fcvaluelists = 0;
+static int fcvaluelist_bank_count = 0, fcvaluelist_ptr, fcvaluelist_count;
+
+static const char *
+FcPatternFindFullFname (const FcPattern *p);
+static FcPatternEltPtr
+FcPatternEltPtrCreateDynamic (FcPatternElt * e);
+
 FcPattern *
 FcPatternCreate (void)
 {
@@ -38,7 +50,8 @@ FcPatternCreate (void)
     FcMemAlloc (FC_MEM_PATTERN, sizeof (FcPattern));
     p->num = 0;
     p->size = 0;
-    p->elts = 0;
+    p->elts = FcPatternEltPtrCreateDynamic(0);
+    p->bank = FC_BANK_DYNAMIC;
     p->ref = 1;
     return p;
 }
@@ -64,6 +77,33 @@ FcValueDestroy (FcValue v)
     }
 }
 
+FcValue
+FcValueCanonicalize (const FcValue *v)
+{
+    if (v->type & FC_STORAGE_STATIC)
+    {
+       FcValue new = *v;
+
+       switch (v->type & ~FC_STORAGE_STATIC)
+       {
+       case FcTypeString:
+           new.u.s = fc_value_string(v);
+           new.type = FcTypeString;
+           break;
+       case FcTypeCharSet:
+           new.u.c = fc_value_charset(v);
+           new.type = FcTypeCharSet;
+           break;
+       case FcTypeLangSet:
+           new.u.l = fc_value_langset(v);
+           new.type = FcTypeLangSet;
+           break;
+       }
+       return new;
+    }
+    return *v;
+}
+
 FcValue
 FcValueSave (FcValue v)
 {
@@ -95,30 +135,33 @@ FcValueSave (FcValue v)
 }
 
 void
-FcValueListDestroy (FcValueList *l)
+FcValueListDestroy (FcValueListPtr l)
 {
-    FcValueList    *next;
-    for (; l; l = next)
+    FcValueListPtr next;
+    for (; FcValueListPtrU(l); l = next)
     {
-       switch (l->value.type) {
+       switch (FcValueListPtrU(l)->value.type) {
        case FcTypeString:
-           FcStrFree ((FcChar8 *) l->value.u.s);
+           FcStrFree ((FcChar8 *)FcValueListPtrU(l)->value.u.s);
            break;
        case FcTypeMatrix:
-           FcMatrixFree ((FcMatrix *) l->value.u.m);
+           FcMatrixFree ((FcMatrix *)FcValueListPtrU(l)->value.u.m);
            break;
        case FcTypeCharSet:
-           FcCharSetDestroy ((FcCharSet *) l->value.u.c);
+           FcCharSetDestroy 
+               ((FcCharSet *) (FcValueListPtrU(l)->value.u.c));
            break;
        case FcTypeLangSet:
-           FcLangSetDestroy ((FcLangSet *) l->value.u.l);
+           FcLangSetDestroy 
+               ((FcLangSet *) (FcValueListPtrU(l)->value.u.l));
            break;
        default:
            break;
        }
-       next = l->next;
+       next = FcValueListPtrU(l)->next;
        FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
-       free (l);
+       if (l.bank == FC_BANK_DYNAMIC)
+           free(l.u.dyn);
     }
 }
 
@@ -173,7 +216,7 @@ FcDoubleHash (double d)
     return (FcChar32) d;
 }
 
-static FcChar32
+FcChar32
 FcStringHash (const FcChar8 *s)
 {
     FcChar8    c;
@@ -186,8 +229,9 @@ FcStringHash (const FcChar8 *s)
 }
 
 static FcChar32
-FcValueHash (FcValue v)
+FcValueHash (const FcValue *v0)
 {
+    FcValue v = FcValueCanonicalize(v0);
     switch (v.type) {
     case FcTypeVoid:
        return 0;
@@ -216,32 +260,34 @@ FcValueHash (FcValue v)
 }
 
 static FcBool
-FcValueListEqual (FcValueList *la, FcValueList *lb)
+FcValueListEqual (FcValueListPtr la, FcValueListPtr lb)
 {
-    if (la == lb)
+    if (FcValueListPtrU(la) == FcValueListPtrU(lb))
        return FcTrue;
 
-    while (la && lb)
+    while (FcValueListPtrU(la) && FcValueListPtrU(lb))
     {
-       if (!FcValueEqual (la->value, lb->value))
+       if (!FcValueEqual (FcValueListPtrU(la)->value, 
+                          FcValueListPtrU(lb)->value))
            return FcFalse;
-       la = la->next;
-       lb = lb->next;
+       la = FcValueListPtrU(la)->next;
+       lb = FcValueListPtrU(lb)->next;
     }
-    if (la || lb)
+    if (FcValueListPtrU(la) || FcValueListPtrU(lb))
        return FcFalse;
     return FcTrue;
 }
 
 static FcChar32
-FcValueListHash (FcValueList *l)
+FcValueListHash (FcValueListPtr l)
 {
     FcChar32   hash = 0;
     
-    while (l)
+    while (FcValueListPtrU(l))
     {
-       hash = ((hash << 1) | (hash >> 31)) ^ FcValueHash (l->value);
-       l = l->next;
+       hash = ((hash << 1) | (hash >> 31)) ^ 
+           FcValueHash (&FcValueListPtrU(l)->value);
+       l = FcValueListPtrU(l)->next;
     }
     return hash;
 }
@@ -251,18 +297,21 @@ FcPatternDestroy (FcPattern *p)
 {
     int                    i;
     
+    if (FcPatternFindFullFname (p))
+       FcPatternAddFullFname (p, 0);
+
     if (p->ref == FC_REF_CONSTANT || --p->ref > 0)
        return;
 
     for (i = 0; i < p->num; i++)
-       FcValueListDestroy (p->elts[i].values);
+       FcValueListDestroy ((FcPatternEltU(p->elts)+i)->values);
 
     p->num = 0;
-    if (p->elts)
+    if (FcPatternEltU(p->elts) && p->elts.bank == FC_BANK_DYNAMIC)
     {
        FcMemFree (FC_MEM_PATELT, p->size * sizeof (FcPatternElt));
-       free (p->elts);
-       p->elts = 0;
+       free (FcPatternEltU(p->elts));
+       p->elts = FcPatternEltPtrCreateDynamic(0);
     }
     p->size = 0;
     FcMemFree (FC_MEM_PATTERN, sizeof (FcPattern));
@@ -276,7 +325,7 @@ typedef struct _FcValueListEnt FcValueListEnt;
 
 struct _FcValueListEnt {
     FcValueListEnt  *next;
-    FcValueList            *list;
+    FcValueListPtr  list;
     FcChar32       hash, pad;
 };
 
@@ -287,7 +336,7 @@ typedef union _FcValueListAlign {
 
 static int         FcValueListFrozenCount[FcTypeLangSet + 1];
 static int         FcValueListFrozenBytes[FcTypeLangSet + 1];
-static char        *FcValueListFrozenName[] = {
+static char        FcValueListFrozenName[][8] = {
     "Void", 
     "Integer", 
     "Double", 
@@ -315,66 +364,97 @@ FcValueListReport (void)
 }
 
 static FcValueListEnt *
-FcValueListEntCreate (FcValueList *h)
+FcValueListEntCreate (FcValueListPtr h)
 {
     FcValueListAlign   *ea;
     FcValueListEnt  *e;
-    FcValueList            *l, *new;
+    FcValueListPtr  l;
+    FcValueList     *new;
     int                    n;
-    int                    string_size = 0;
-    FcChar8        *strs;
     int                    size;
 
     n = 0;
-    for (l = h; l; l = l->next)
-    {
-       if (l->value.type == FcTypeString)
-           string_size += strlen ((char *) l->value.u.s) + 1;
+    for (l = h; FcValueListPtrU(l); l = FcValueListPtrU(l)->next)
        n++;
-    }
-    size = sizeof (FcValueListAlign) + n * sizeof (FcValueList) + string_size;
-    FcValueListFrozenCount[h->value.type]++;
-    FcValueListFrozenBytes[h->value.type] += size;
-    ea = malloc (size);
+    size = sizeof (FcValueListAlign) + n * sizeof (FcValueList);
+    FcValueListFrozenCount[FcValueListPtrU(h)->value.type]++;
+    FcValueListFrozenBytes[FcValueListPtrU(h)->value.type] += size;
+    // this leaks for some reason
+    ea = malloc (sizeof (FcValueListAlign));
     if (!ea)
        return 0;
+    new = malloc (n * sizeof (FcValueList));
+    if (!new)
+        return 0;
+    memset(new, 0, n * sizeof (FcValueList));
     FcMemAlloc (FC_MEM_VALLIST, size);
     e = &ea->ent;
-    e->list = (FcValueList *) (ea + 1);
-    strs = (FcChar8 *) (e->list + n);
-    new = e->list;
-    for (l = h; l; l = l->next, new++)
+    e->list = (FcValueListPtr) FcValueListPtrCreateDynamic(new);
+    for (l = h; FcValueListPtrU(l); 
+        l = FcValueListPtrU(l)->next, new++)
     {
-       if (l->value.type == FcTypeString)
+       if ((FcValueListPtrU(l)->value.type & ~FC_STORAGE_STATIC) == FcTypeString)
        {
            new->value.type = FcTypeString;
-           new->value.u.s = strs;
-           strcpy ((char *) strs, (char *) l->value.u.s);
-           strs += strlen ((char *) strs) + 1;
+           new->value.u.s = FcStrStaticName
+               (fc_value_string(&FcValueListPtrU(l)->value));
        }
        else
        {
-           new->value = l->value;
-           new->value = FcValueSave (new->value);
+           new->value = FcValueSave (FcValueCanonicalize
+                                     (&FcValueListPtrU(l)->value));
+       }
+       new->binding = FcValueListPtrU(l)->binding;
+       if (FcValueListPtrU(FcValueListPtrU(l)->next))
+       {
+           new->next = FcValueListPtrCreateDynamic(new + 1);
        }
-       new->binding = l->binding;
-       if (l->next)
-           new->next = new + 1;
        else
-           new->next = 0;
+       {
+           new->next = FcValueListPtrCreateDynamic(0);
+       }
     }
     return e;
 }
 
+static void
+FcValueListEntDestroy (FcValueListEnt *e)
+{
+    FcValueListPtr     l;
+
+    FcValueListFrozenCount[FcValueListPtrU(e->list)->value.type]--;
+
+    /* XXX: We should perform these two operations with "size" as
+       computed in FcValueListEntCreate, but we don't have access to
+       that value here. Without this, the FcValueListFrozenBytes
+       values will be wrong as will the FcMemFree counts.
+
+       FcValueListFrozenBytes[e->list->value.type] -= size;
+       FcMemFree (FC_MEM_VALLIST, size);
+    */
+
+    for (l = e->list; FcValueListPtrU(l); 
+        l = FcValueListPtrU(l)->next)
+    {
+       if (FcValueListPtrU(l)->value.type != FcTypeString)
+           FcValueDestroy (FcValueListPtrU(l)->value);
+    }
+    /* XXX: Are we being too chummy with the implementation here to
+       free(e) when it was actually the enclosing FcValueListAlign
+       that was allocated? */
+    free (e);
+}
+
 static int     FcValueListTotal;
 static int     FcValueListUsed;
 
-static FcValueList *
-FcValueListFreeze (FcValueList *l)
+static FcValueListEnt   *FcValueListHashTable[FC_VALUE_LIST_HASH_SIZE];
+
+static FcValueListPtr
+FcValueListFreeze (FcValueListPtr l)
 {
-    static FcValueListEnt   *hashTable[FC_VALUE_LIST_HASH_SIZE];
     FcChar32               hash = FcValueListHash (l);
-    FcValueListEnt         **bucket = &hashTable[hash % FC_VALUE_LIST_HASH_SIZE];
+    FcValueListEnt         **bucket = &FcValueListHashTable[hash % FC_VALUE_LIST_HASH_SIZE];
     FcValueListEnt         *ent;
 
     FcValueListTotal++;
@@ -386,7 +466,7 @@ FcValueListFreeze (FcValueList *l)
 
     ent = FcValueListEntCreate (l);
     if (!ent)
-       return 0;
+       return FcValueListPtrCreateDynamic(0);
 
     FcValueListUsed++;
     ent->hash = hash;
@@ -395,6 +475,26 @@ FcValueListFreeze (FcValueList *l)
     return ent->list;
 }
 
+static void
+FcValueListThawAll (void)
+{
+    int i;
+    FcValueListEnt     *ent, *next;
+
+    for (i = 0; i < FC_VALUE_LIST_HASH_SIZE; i++)
+    {
+       for (ent = FcValueListHashTable[i]; ent; ent = next)
+       {
+           next = ent->next;
+           FcValueListEntDestroy (ent);
+       }
+       FcValueListHashTable[i] = 0;
+    }
+
+    FcValueListTotal = 0;
+    FcValueListUsed = 0;
+}
+
 static FcChar32
 FcPatternBaseHash (FcPattern *b)
 {
@@ -402,7 +502,8 @@ FcPatternBaseHash (FcPattern *b)
     int                i;
 
     for (i = 0; i < b->num; i++)
-       hash = ((hash << 1) | (hash >> 31)) ^ ((long) b->elts[i].values);
+       hash = ((hash << 1) | (hash >> 31)) ^ 
+           (long) (FcValueListPtrU((FcPatternEltU(b->elts)+i)->values));
     return hash;
 }
 
@@ -411,102 +512,150 @@ typedef struct _FcPatternEnt FcPatternEnt;
 struct _FcPatternEnt {
     FcPatternEnt    *next;
     FcChar32       hash;
-    FcPattern      pattern;
+    FcPattern              *pattern;
 };
 
 static int     FcPatternTotal;
 static int     FcPatternUsed;
 
+static FcPatternEnt    *FcPatternHashTable[FC_VALUE_LIST_HASH_SIZE];
+
 static FcPattern *
 FcPatternBaseFreeze (FcPattern *b)
 {
-    static FcPatternEnt        *hashTable[FC_VALUE_LIST_HASH_SIZE];
+    FcPattern           *ep;
+    FcPatternElt       *epp;
     FcChar32           hash = FcPatternBaseHash (b);
-    FcPatternEnt       **bucket = &hashTable[hash % FC_VALUE_LIST_HASH_SIZE];
+    FcPatternEnt       **bucket = &FcPatternHashTable[hash % FC_VALUE_LIST_HASH_SIZE];
     FcPatternEnt       *ent;
     int                        i;
-    char               *objects;
-    int                        size_objects;
-    int                        size;
 
     FcPatternTotal++;
     for (ent = *bucket; ent; ent = ent->next)
     {
-       if (ent->hash == hash && b->num == ent->pattern.num)
-       {
+        if (ent->hash == hash && b->num == ent->pattern->num)
+        {
            for (i = 0; i < b->num; i++)
            {
-               if (strcmp (b->elts[i].object, ent->pattern.elts[i].object))
+               if (FcObjectPtrCompare((FcPatternEltU(b->elts)+i)->object,
+                                      (FcPatternEltU(ent->pattern->elts)+i)->object) != 0)
                    break;
-               if (b->elts[i].values != ent->pattern.elts[i].values)
+               if (FcValueListPtrU((FcPatternEltU(b->elts)+i)->values) != 
+                    FcValueListPtrU((FcPatternEltU(ent->pattern->elts)+i)->values))
                    break;
            }
            if (i == b->num)
-               return &ent->pattern;
+               return ent->pattern;
        }
     }
 
     /*
-     * Compute size of pattern + elts + object names
+     * Compute size of pattern + elts
      */
-    size_objects = 0;
-    for (i = 0; i < b->num; i++)
-       size_objects += strlen (b->elts[i].object) + 1;
-
-    size = sizeof (FcPatternEnt) + b->num*sizeof (FcPatternElt) + size_objects;
-    ent = malloc (size);
+    ent = malloc (sizeof (FcPatternEnt));
     if (!ent)
        return 0;
 
-    FcMemAlloc (FC_MEM_PATTERN, size);
+    FcMemAlloc (FC_MEM_PATTERN, sizeof (FcPatternEnt));
     FcPatternUsed++;
 
-    ent->pattern.elts = (FcPatternElt *) (ent + 1);
-    ent->pattern.num = b->num;
-    ent->pattern.size = b->num;
-    ent->pattern.ref = FC_REF_CONSTANT;
+    ep = FcPatternCreate();
+    if (!ep)
+        return 0;
+    ent->pattern = ep;
+    epp = malloc(b->num * sizeof (FcPatternElt));
+    if (!epp)
+        goto bail;
+    ep->elts = FcPatternEltPtrCreateDynamic(epp);
+
+    FcMemAlloc (FC_MEM_PATELT, sizeof (FcPatternElt)*(b->num));
+
+    ep->num = b->num;
+    ep->size = b->num;
+    ep->ref = FC_REF_CONSTANT;
 
-    objects = (char *) (ent->pattern.elts + b->num);
     for (i = 0; i < b->num; i++)
     {
-       ent->pattern.elts[i].values = b->elts[i].values;
-       strcpy (objects, b->elts[i].object);
-       ent->pattern.elts[i].object = objects;
-       objects += strlen (objects) + 1;
+       (FcPatternEltU(ep->elts)+i)->values = 
+           (FcPatternEltU(b->elts)+i)->values;
+       (FcPatternEltU(ep->elts)+i)->object = 
+           (FcPatternEltU(b->elts)+i)->object;
     }
 
+    if (FcPatternFindElt (b, FC_FILE))
+       FcPatternTransferFullFname (ep, b);
+
     ent->hash = hash;
     ent->next = *bucket;
     *bucket = ent;
-    return &ent->pattern;
+    return ent->pattern;
+ bail:
+    free(ent);
+    FcMemFree (FC_MEM_PATTERN, sizeof (FcPatternEnt));
+    FcPatternUsed--;
+    return 0;
+}
+
+static void
+FcPatternBaseThawAll (void)
+{
+    int i;
+    FcPatternEnt       *ent, *next;
+
+    for (i = 0; i < FC_VALUE_LIST_HASH_SIZE; i++)
+    {
+       for (ent = FcPatternHashTable[i]; ent; ent = next)
+       {
+           next = ent->next;
+           free (ent);
+       }
+       FcPatternHashTable[i] = 0;
+    }
+
+    FcPatternTotal = 0;
+    FcPatternUsed = 0;
 }
 
 FcPattern *
 FcPatternFreeze (FcPattern *p)
 {
     FcPattern  *b, *n = 0;
-    int                size;
+    FcPatternElt *e;
     int                i;
     
-    size = sizeof (FcPattern) + p->num * sizeof (FcPatternElt);
-    b = (FcPattern *) malloc (size);
+    if (p->ref == FC_REF_CONSTANT)
+       return p;
+
+    b = FcPatternCreate();
     if (!b)
-       return 0;
-    FcMemAlloc (FC_MEM_PATTERN, size);
+        return 0;
+
     b->num = p->num;
     b->size = b->num;
     b->ref = 1;
-    b->elts = (FcPatternElt *) (b + 1);
+
+    e = malloc(b->num * sizeof (FcPatternElt));
+    if (!e)
+        return 0;
+    b->elts = FcPatternEltPtrCreateDynamic(e);
+    FcMemAlloc (FC_MEM_PATELT, sizeof (FcPatternElt)*(b->num));
+
     /*
      * Freeze object lists
      */
     for (i = 0; i < p->num; i++)
     {
-       b->elts[i].object = p->elts[i].object;
-       b->elts[i].values = FcValueListFreeze (p->elts[i].values);
-       if (!b->elts[i].values)
+       (FcPatternEltU(b->elts)+i)->object = 
+           (FcPatternEltU(p->elts)+i)->object;
+       (FcPatternEltU(b->elts)+i)->values = 
+           FcValueListFreeze((FcPatternEltU(p->elts)+i)->values);
+       if (!FcValueListPtrU((FcPatternEltU(p->elts)+i)->values))
            goto bail;
     }
+
+    if (FcPatternFindElt (p, FC_FILE))
+       FcPatternTransferFullFname (b, p);
+
     /*
      * Freeze base
      */
@@ -518,8 +667,11 @@ FcPatternFreeze (FcPattern *p)
        printf ("Patterns:   total %9d used %9d\n", FcPatternTotal, FcPatternUsed);
     }
 #endif
-bail:
-    free (b);
+ bail:
+    free(FcPatternEltU(b->elts));
+    b->elts = FcPatternEltPtrCreateDynamic(0);
+    FcMemFree (FC_MEM_PATELT, sizeof (FcPatternElt)*(b->num));
+    b->num = -1;
 #ifdef DEBUG
     assert (FcPatternEqual (n, p));
 #endif
@@ -530,7 +682,9 @@ static int
 FcPatternPosition (const FcPattern *p, const char *object)
 {
     int            low, high, mid, c;
+    FcObjectPtr obj;
 
+    obj = FcObjectToPtr(object);
     low = 0;
     high = p->num - 1;
     c = 1;
@@ -538,7 +692,7 @@ FcPatternPosition (const FcPattern *p, const char *object)
     while (low <= high)
     {
        mid = (low + high) >> 1;
-       c = strcmp (p->elts[mid].object, object);
+       c = FcObjectPtrCompare((FcPatternEltU(p->elts)+mid)->object, obj);
        if (c == 0)
            return mid;
        if (c < 0)
@@ -557,7 +711,7 @@ FcPatternFindElt (const FcPattern *p, const char *object)
     int            i = FcPatternPosition (p, object);
     if (i < 0)
        return 0;
-    return &p->elts[i];
+    return FcPatternEltU(p->elts)+i;
 }
 
 FcPatternElt *
@@ -571,42 +725,52 @@ FcPatternInsertElt (FcPattern *p, const char *object)
     {
        i = -i - 1;
     
-       /* grow array */
+       /* reallocate array */
        if (p->num + 1 >= p->size)
        {
            int s = p->size + 16;
-           if (p->elts)
-               e = (FcPatternElt *) realloc (p->elts, s * sizeof (FcPatternElt));
+           if (FcPatternEltU(p->elts))
+           {
+               FcPatternElt *e0 = FcPatternEltU(p->elts);
+               e = (FcPatternElt *) realloc (e0, s * sizeof (FcPatternElt));
+               if (!e) /* maybe it was mmapped */
+               {
+                   e = malloc(s * sizeof (FcPatternElt));
+                   if (e)
+                       memcpy(e, e0, p->num * sizeof (FcPatternElt));
+               }
+           }
            else
                e = (FcPatternElt *) malloc (s * sizeof (FcPatternElt));
            if (!e)
                return FcFalse;
-           p->elts = e;
+           p->elts = FcPatternEltPtrCreateDynamic(e);
            if (p->size)
                FcMemFree (FC_MEM_PATELT, p->size * sizeof (FcPatternElt));
            FcMemAlloc (FC_MEM_PATELT, s * sizeof (FcPatternElt));
            while (p->size < s)
            {
-               p->elts[p->size].object = 0;
-               p->elts[p->size].values = 0;
+               (FcPatternEltU(p->elts)+p->size)->object = 0;
+               (FcPatternEltU(p->elts)+p->size)->values = 
+                   FcValueListPtrCreateDynamic(0);
                p->size++;
            }
        }
        
        /* move elts up */
-       memmove (p->elts + i + 1,
-                p->elts + i,
+       memmove (FcPatternEltU(p->elts) + i + 1,
+                FcPatternEltU(p->elts) + i,
                 sizeof (FcPatternElt) *
                 (p->num - i));
                 
        /* bump count */
        p->num++;
        
-       p->elts[i].object = object;
-       p->elts[i].values = 0;
+       (FcPatternEltU(p->elts)+i)->object = FcObjectToPtr (object);
+       (FcPatternEltU(p->elts)+i)->values = FcValueListPtrCreateDynamic(0);
     }
     
-    return &p->elts[i];
+    return FcPatternEltU(p->elts)+i;
 }
 
 FcBool
@@ -621,9 +785,11 @@ FcPatternEqual (const FcPattern *pa, const FcPattern *pb)
        return FcFalse;
     for (i = 0; i < pa->num; i++)
     {
-       if (strcmp (pa->elts[i].object, pb->elts[i].object) != 0)
+       if (FcObjectPtrCompare((FcPatternEltU(pa->elts)+i)->object,
+                              (FcPatternEltU(pb->elts)+i)->object) != 0)
            return FcFalse;
-       if (!FcValueListEqual (pa->elts[i].values, pb->elts[i].values))
+       if (!FcValueListEqual ((FcPatternEltU(pa->elts)+i)->values, 
+                              (FcPatternEltU(pb->elts)+i)->values))
            return FcFalse;
     }
     return FcTrue;
@@ -638,22 +804,22 @@ FcPatternHash (const FcPattern *p)
     for (i = 0; i < p->num; i++)
     {
        h = (((h << 1) | (h >> 31)) ^ 
-            FcStringHash ((const FcChar8 *) p->elts[i].object) ^
-            FcValueListHash (p->elts[i].values));
+            FcStringHash ((FcChar8 *)FcObjectPtrU ((FcPatternEltU(p->elts)+i)->object)) ^
+            FcValueListHash ((FcPatternEltU(p->elts)+i)->values));
     }
     return h;
 }
 
 FcBool
-FcPatternEqualSubset (const FcPattern *pa, const FcPattern *pb, const FcObjectSet *os)
+FcPatternEqualSubset (const FcPattern *pai, const FcPattern *pbi, const FcObjectSet *os)
 {
     FcPatternElt    *ea, *eb;
     int                    i;
     
     for (i = 0; i < os->nobject; i++)
     {
-       ea = FcPatternFindElt (pa, os->objects[i]);
-       eb = FcPatternFindElt (pb, os->objects[i]);
+       ea = FcPatternFindElt (pai, os->objects[i]);
+       eb = FcPatternFindElt (pbi, os->objects[i]);
        if (ea)
        {
            if (!eb)
@@ -678,24 +844,27 @@ FcPatternAddWithBinding  (FcPattern           *p,
                          FcBool            append)
 {
     FcPatternElt   *e;
-    FcValueList    *new, **prev;
+    FcValueListPtr new, *prev;
+    FcValueList *  newp;
 
     if (p->ref == FC_REF_CONSTANT)
        goto bail0;
 
-    new = (FcValueList *) malloc (sizeof (FcValueList));
-    if (!new)
+    newp = malloc (sizeof (FcValueList));
+    if (!newp)
        goto bail0;
 
+    memset(newp, 0, sizeof (FcValueList));
+    new = FcValueListPtrCreateDynamic(newp);
     FcMemAlloc (FC_MEM_VALLIST, sizeof (FcValueList));
     /* dup string */
     value = FcValueSave (value);
     if (value.type == FcTypeVoid)
        goto bail1;
 
-    new->value = value;
-    new->binding = binding;
-    new->next = 0;
+    FcValueListPtrU(new)->value = value;
+    FcValueListPtrU(new)->binding = binding;
+    FcValueListPtrU(new)->next = FcValueListPtrCreateDynamic(0);
     
     e = FcPatternInsertElt (p, object);
     if (!e)
@@ -703,12 +872,13 @@ FcPatternAddWithBinding  (FcPattern           *p,
     
     if (append)
     {
-       for (prev = &e->values; *prev; prev = &(*prev)->next);
+       for (prev = &e->values; FcValueListPtrU(*prev); prev = &FcValueListPtrU(*prev)->next)
+           ;
        *prev = new;
     }
     else
     {
-       new->next = e->values;
+       FcValueListPtrU(new)->next = e->values;
        e->values = new;
     }
     
@@ -733,7 +903,7 @@ bail2:
     }
 bail1:
     FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
-    free (new);
+    free (FcValueListPtrU(new));
 bail0:
     return FcFalse;
 }
@@ -754,25 +924,51 @@ FcBool
 FcPatternDel (FcPattern *p, const char *object)
 {
     FcPatternElt   *e;
-    int                    i;
 
     e = FcPatternFindElt (p, object);
     if (!e)
        return FcFalse;
 
-    i = e - p->elts;
-    
     /* destroy value */
     FcValueListDestroy (e->values);
     
     /* shuffle existing ones down */
-    memmove (e, e+1, (p->elts + p->num - (e + 1)) * sizeof (FcPatternElt));
+    memmove (e, e+1, 
+            (FcPatternEltU(p->elts) + p->num - (e + 1)) * 
+            sizeof (FcPatternElt));
     p->num--;
-    p->elts[p->num].object = 0;
-    p->elts[p->num].values = 0;
+    (FcPatternEltU(p->elts)+p->num)->object = 0;
+    (FcPatternEltU(p->elts)+p->num)->values = FcValueListPtrCreateDynamic(0);
     return FcTrue;
 }
 
+FcBool
+FcPatternRemove (FcPattern *p, const char *object, int id)
+{
+    FcPatternElt    *e;
+    FcValueListPtr  *prev, l;
+
+    e = FcPatternFindElt (p, object);
+    if (!e)
+       return FcFalse;
+    for (prev = &e->values; 
+        FcValueListPtrU(l = *prev); 
+        prev = &FcValueListPtrU(l)->next)
+    {
+       if (!id)
+       {
+           *prev = FcValueListPtrU(l)->next;
+           FcValueListPtrU(l)->next = FcValueListPtrCreateDynamic(0);
+           FcValueListDestroy (l);
+           if (!FcValueListPtrU(e->values))
+               FcPatternDel (p, object);
+           return FcTrue;
+       }
+       id--;
+    }
+    return FcFalse;
+}
+
 FcBool
 FcPatternAddInteger (FcPattern *p, const char *object, int i)
 {
@@ -800,7 +996,7 @@ FcPatternAddString (FcPattern *p, const char *object, const FcChar8 *s)
     FcValue    v;
 
     v.type = FcTypeString;
-    v.u.s = s;
+    v.u.s = FcStrStaticName(s);
     return FcPatternAdd (p, object, v, FcTrue);
 }
 
@@ -810,7 +1006,7 @@ FcPatternAddMatrix (FcPattern *p, const char *object, const FcMatrix *s)
     FcValue    v;
 
     v.type = FcTypeMatrix;
-    v.u.m = (FcMatrix *) s;
+    v.u.m = s;
     return FcPatternAdd (p, object, v, FcTrue);
 }
 
@@ -831,7 +1027,7 @@ FcPatternAddCharSet (FcPattern *p, const char *object, const FcCharSet *c)
     FcValue    v;
 
     v.type = FcTypeCharSet;
-    v.u.c = (FcCharSet *) c;
+    v.u.c = (FcCharSet *)c;
     return FcPatternAdd (p, object, v, FcTrue);
 }
 
@@ -851,7 +1047,7 @@ FcPatternAddLangSet (FcPattern *p, const char *object, const FcLangSet *ls)
     FcValue    v;
 
     v.type = FcTypeLangSet;
-    v.u.l = (FcLangSet *) ls;
+    v.u.l = (FcLangSet *)ls;
     return FcPatternAdd (p, object, v, FcTrue);
 }
 
@@ -859,16 +1055,16 @@ FcResult
 FcPatternGet (const FcPattern *p, const char *object, int id, FcValue *v)
 {
     FcPatternElt   *e;
-    FcValueList    *l;
+    FcValueListPtr l;
 
     e = FcPatternFindElt (p, object);
     if (!e)
        return FcResultNoMatch;
-    for (l = e->values; l; l = l->next)
+    for (l = e->values; FcValueListPtrU(l); l = FcValueListPtrU(l)->next)
     {
        if (!id)
        {
-           *v = l->value;
+           *v = FcValueCanonicalize(&FcValueListPtrU(l)->value);
            return FcResultMatch;
        }
        id--;
@@ -931,6 +1127,42 @@ FcPatternGetString (const FcPattern *p, const char *object, int id, FcChar8 ** s
        return r;
     if (v.type != FcTypeString)
         return FcResultTypeMismatch;
+
+    if (FcObjectToPtr(object) == FcObjectToPtr(FC_FILE))
+    {
+       const char *fn, *fpath;
+       FcChar8 *fname;
+       int size;
+
+       fn = FcPatternFindFullFname(p);
+       if (fn)
+       {
+           *s = (FcChar8 *) fn;
+           return FcResultMatch;
+       }
+
+       if (!p->bank)
+       {
+           *s = (FcChar8 *) v.u.s;
+           return FcResultMatch;
+       }
+
+       fpath = FcCacheFindBankDir (p->bank);
+       size = strlen((char*)fpath) + 1 + strlen ((char *)v.u.s) + 1;
+       fname = malloc (size);
+       if (!fname)
+           return FcResultOutOfMemory;
+
+       FcMemAlloc (FC_MEM_STRING, size);
+       strcpy ((char *)fname, (char *)fpath);
+       strcat ((char *)fname, "/");
+       strcat ((char *)fname, (char *)v.u.s);
+       
+       FcPatternAddFullFname (p, (const char *)fname);
+       *s = (FcChar8 *)fname;
+       return FcResultMatch;
+    }
+
     *s = (FcChar8 *) v.u.s;
     return FcResultMatch;
 }
@@ -946,7 +1178,7 @@ FcPatternGetMatrix(const FcPattern *p, const char *object, int id, FcMatrix **m)
        return r;
     if (v.type != FcTypeMatrix)
         return FcResultTypeMismatch;
-    *m = (FcMatrix *) v.u.m;
+    *m = (FcMatrix *)v.u.m;
     return FcResultMatch;
 }
 
@@ -977,7 +1209,7 @@ FcPatternGetCharSet(const FcPattern *p, const char *object, int id, FcCharSet **
        return r;
     if (v.type != FcTypeCharSet)
         return FcResultTypeMismatch;
-    *c = (FcCharSet *) v.u.c;
+    *c = (FcCharSet *)v.u.c;
     return FcResultMatch;
 }
 
@@ -1007,7 +1239,7 @@ FcPatternGetLangSet(const FcPattern *p, const char *object, int id, FcLangSet **
        return r;
     if (v.type != FcTypeLangSet)
         return FcResultTypeMismatch;
-    *ls = (FcLangSet *) v.u.l;
+    *ls = (FcLangSet *)v.u.l;
     return FcResultMatch;
 }
 
@@ -1015,18 +1247,32 @@ FcPattern *
 FcPatternDuplicate (const FcPattern *orig)
 {
     FcPattern      *new;
+    FcPatternElt    *e;
     int                    i;
-    FcValueList    *l;
+    FcValueListPtr  l;
 
     new = FcPatternCreate ();
     if (!new)
        goto bail0;
 
+    e = FcPatternEltU(orig->elts);
+
     for (i = 0; i < orig->num; i++)
     {
-       for (l = orig->elts[i].values; l; l = l->next)
-           if (!FcPatternAdd (new, orig->elts[i].object, l->value, FcTrue))
+       for (l = (e + i)->values; 
+            FcValueListPtrU(l); 
+            l = FcValueListPtrU(l)->next)
+           if (!FcPatternAdd (new, FcObjectPtrU((e + i)->object), 
+                               FcValueCanonicalize(&FcValueListPtrU(l)->value),
+                              FcTrue))
                goto bail1;
+
+       if (!strcmp ((char *)FcObjectPtrU((e + i)->object), FC_FILE))
+       {
+           FcChar8 * s;
+           FcPatternGetString (orig, FC_FILE, 0, &s);
+           FcPatternAddFullFname (new, FcPatternFindFullFname(orig));
+       }
     }
 
     return new;
@@ -1063,3 +1309,688 @@ FcPatternBuild (FcPattern *orig, ...)
     va_end (va);
     return orig;
 }
+
+/*
+ * Add all of the elements in 's' to 'p'
+ */
+FcBool
+FcPatternAppend (FcPattern *p, FcPattern *s)
+{
+    int                    i;
+    FcPatternElt    *e;
+    FcValueListPtr  v;
+    
+    for (i = 0; i < s->num; i++)
+    {
+       e = FcPatternEltU(s->elts)+i;
+       for (v = e->values; FcValueListPtrU(v); 
+            v = FcValueListPtrU(v)->next)
+       {
+           if (!FcPatternAddWithBinding (p, FcObjectPtrU(e->object),
+                                         FcValueCanonicalize(&FcValueListPtrU(v)->value), 
+                                         FcValueListPtrU(v)->binding, FcTrue))
+               return FcFalse;
+       }
+    }
+    return FcTrue;
+}
+
+#define OBJECT_HASH_SIZE    31
+static struct objectBucket {
+    struct objectBucket        *next;
+    FcChar32           hash;
+} *FcObjectBuckets[OBJECT_HASH_SIZE];
+
+const FcChar8 *
+FcStrStaticName (const FcChar8 *name)
+{
+    FcChar32           hash = FcStringHash (name);
+    struct objectBucket        **p;
+    struct objectBucket        *b;
+    int                        size;
+
+    for (p = &FcObjectBuckets[hash % OBJECT_HASH_SIZE]; (b = *p); p = &(b->next))
+       if (b->hash == hash && !strcmp ((char *)name, (char *) (b + 1)))
+           return (FcChar8 *) (b + 1);
+    size = sizeof (struct objectBucket) + strlen ((char *)name) + 1;
+    b = malloc (size + sizeof (int));
+    /* workaround glibc bug which reads strlen in groups of 4 */
+    FcMemAlloc (FC_MEM_STATICSTR, size + sizeof (int));
+    if (!b)
+        return NULL;
+    b->next = 0;
+    b->hash = hash;
+    strcpy ((char *) (b + 1), (char *)name);
+    *p = b;
+    return (FcChar8 *) (b + 1);
+}
+
+static void
+FcStrStaticNameFini (void)
+{
+    int i, size;
+    struct objectBucket *b, *next;
+    char *name;
+
+    for (i = 0; i < OBJECT_HASH_SIZE; i++)
+    {
+       for (b = FcObjectBuckets[i]; b; b = next)
+       {
+           next = b->next;
+           name = (char *) (b + 1);
+           size = sizeof (struct objectBucket) + strlen (name) + 1;
+           FcMemFree (FC_MEM_STATICSTR, size);
+           free (b);
+       }
+       FcObjectBuckets[i] = 0;
+    }
+}
+
+void
+FcPatternFini (void)
+{
+    FcPatternBaseThawAll ();
+    FcValueListThawAll ();
+    FcStrStaticNameFini ();
+    FcObjectStaticNameFini ();
+}
+
+FcPatternElt *
+FcPatternEltU (FcPatternEltPtr pei)
+{
+    if (pei.bank == FC_BANK_DYNAMIC)
+       return pei.u.dyn;
+
+    return &fcpatternelts[FcCacheBankToIndex(pei.bank)][pei.u.stat];
+}
+
+static FcPatternEltPtr
+FcPatternEltPtrCreateDynamic (FcPatternElt * e)
+{
+    FcPatternEltPtr new;
+    new.bank = FC_BANK_DYNAMIC;
+    new.u.dyn = e;
+    return new;
+}
+
+static FcPatternEltPtr
+FcPatternEltPtrCreateStatic (int bank, int i)
+{
+    FcPatternEltPtr new;
+    new.bank = bank;
+    new.u.stat = i;
+    return new;
+}
+
+static void
+FcStrNewBank (void);
+static int
+FcStrNeededBytes (const FcChar8 * s);
+static void *
+FcStrDistributeBytes (FcCache * metadata, void * block_ptr);
+static const FcChar8 *
+FcStrSerialize (int bank, const FcChar8 * s);
+static void *
+FcStrUnserialize (FcCache metadata, void *block_ptr);
+
+static void
+FcValueListNewBank (void);
+static int
+FcValueListNeededBytes (FcValueList * vl);
+static void *
+FcValueListDistributeBytes (FcCache * metadata, void *block_ptr);
+static FcValueListPtr
+FcValueListSerialize(int bank, FcValueList *pi);
+static void *
+FcValueListUnserialize (FcCache metadata, void *block_ptr);
+
+
+void
+FcPatternNewBank (void)
+{
+    fcpattern_count = 0;
+    fcpatternelt_count = 0;
+
+    FcStrNewBank();
+    FcValueListNewBank();
+}
+
+int
+FcPatternNeededBytes (FcPattern * p)
+{
+    int i, cum = 0, c;
+
+    fcpattern_count++;
+    fcpatternelt_count += p->num;
+
+    for (i = 0; i < p->num; i++)
+    {
+       c = FcValueListNeededBytes (FcValueListPtrU
+                                   (((FcPatternEltU(p->elts)+i)->values)));
+       if (c < 0)
+           return c;
+       cum += c;
+    }
+
+    return cum + sizeof (FcPattern) + sizeof(FcPatternElt)*p->num;
+}
+
+static FcBool
+FcPatternEnsureBank (int bi)
+{
+    FcPattern **pp;
+    FcPatternElt **ep;
+    int i;
+
+    if (!fcpatterns || fcpattern_bank_count <= bi)
+    {
+       int new_count = bi + 4;
+       pp = realloc (fcpatterns, sizeof (FcPattern *) * new_count);
+       if (!pp)
+           return 0;
+
+       FcMemAlloc (FC_MEM_PATTERN, sizeof (FcPattern *) * new_count);
+       fcpatterns = pp;
+
+       ep = realloc (fcpatternelts, sizeof (FcPatternElt *) * new_count);
+       if (!ep)
+           return 0;
+
+       FcMemAlloc (FC_MEM_PATELT, sizeof (FcPatternElt *) * new_count);
+       fcpatternelts = ep;
+
+       for (i = fcpattern_bank_count; i < new_count; i++)
+       {
+           fcpatterns[i] = 0;
+           fcpatternelts[i] = 0;
+       }
+
+       fcpattern_bank_count = new_count;
+    }
+
+    FcMemAlloc (FC_MEM_PATTERN, sizeof (FcPattern) * fcpattern_count);
+    return FcTrue;
+}
+
+void *
+FcPatternDistributeBytes (FcCache * metadata, void * block_ptr)
+{
+    int bi = FcCacheBankToIndex(metadata->bank);
+
+    if (!FcPatternEnsureBank(bi))
+       return 0;
+
+    fcpattern_ptr = 0;
+    fcpatterns[bi] = (FcPattern *)block_ptr;
+    block_ptr = (void *)((char *)block_ptr + 
+                        (sizeof (FcPattern) * fcpattern_count));
+    
+    FcMemAlloc (FC_MEM_PATELT, sizeof (FcPatternElt) * fcpatternelt_count);
+    fcpatternelt_ptr = 0;
+    fcpatternelts[bi] = (FcPatternElt *)block_ptr;
+    block_ptr = (void *)((char *)block_ptr + 
+                        (sizeof (FcPatternElt) * fcpatternelt_count));
+
+    metadata->pattern_count = fcpattern_count;
+    metadata->patternelt_count = fcpatternelt_count;
+
+    block_ptr = FcStrDistributeBytes (metadata, block_ptr);
+    block_ptr = FcValueListDistributeBytes (metadata, block_ptr);
+    return block_ptr;
+}
+
+FcPattern *
+FcPatternSerialize (int bank, FcPattern *old)
+{
+    FcPattern *p;
+    FcPatternElt *e, *nep;
+    FcValueList * nv;
+    FcValueListPtr v, nv_head, nvp;
+    int i, elts, bi = FcCacheBankToIndex(bank);
+
+    p = &fcpatterns[bi][fcpattern_ptr++];
+    p->bank = bank;
+    elts = fcpatternelt_ptr;
+    nep = &fcpatternelts[bi][elts];
+    if (!nep)
+       return FcFalse;
+
+    fcpatternelt_ptr += old->num;
+
+    for (e = FcPatternEltU(old->elts), i=0; i < old->num; i++, e++) 
+    {
+        v = e->values;
+        nvp = nv_head = FcValueListSerialize(bank, FcValueListPtrU(v));
+        if (!FcValueListPtrU(nv_head))
+            return 0;
+       nv = FcValueListPtrU(nvp);
+       
+        for (;
+             FcValueListPtrU(v);
+             v = FcValueListPtrU(v)->next, 
+                nv = FcValueListPtrU(nv->next))
+        {
+           
+           if (FcValueListPtrU(FcValueListPtrU(v)->next))
+           {
+                nvp = FcValueListSerialize
+                   (bank, FcValueListPtrU(FcValueListPtrU(v)->next));
+                nv->next = nvp;
+           }
+        }
+       
+       nep[i].values = nv_head;
+       nep[i].object = e->object;
+    }
+
+    p->elts = old->elts;
+    p->elts = FcPatternEltPtrCreateStatic(bank, elts);
+    p->size = old->num;
+    p->num = old->num;
+    p->ref = FC_REF_CONSTANT;
+    return p;
+}
+
+void *
+FcPatternUnserialize (FcCache metadata, void *block_ptr)
+{
+    int bi = FcCacheBankToIndex(metadata.bank);
+    if (!FcPatternEnsureBank(bi))
+       return FcFalse;
+
+    FcMemAlloc (FC_MEM_PATTERN, sizeof (FcPattern) * metadata.pattern_count);
+    fcpatterns[bi] = (FcPattern *)block_ptr;
+    block_ptr = (void *)((char *)block_ptr + 
+                        (sizeof (FcPattern) * metadata.pattern_count));
+    
+    FcMemAlloc (FC_MEM_PATELT, 
+               sizeof (FcPatternElt) * metadata.patternelt_count);
+    fcpatternelts[bi] = (FcPatternElt *)block_ptr;
+    block_ptr = (void *)((char *)block_ptr + 
+                        (sizeof (FcPatternElt) * metadata.patternelt_count));
+       
+    block_ptr = FcStrUnserialize (metadata, block_ptr);
+    block_ptr = FcValueListUnserialize (metadata, block_ptr);
+
+    return block_ptr;
+}
+
+static void
+FcValueListNewBank (void)
+{
+    fcvaluelist_count = 0;
+
+    FcCharSetNewBank();
+    FcLangSetNewBank();
+}
+
+static int
+FcValueListNeededBytes (FcValueList *p)
+{
+    FcValueList *vl;
+    int cum = 0;
+
+    for (vl = p;
+        vl; 
+        vl = FcValueListPtrU(vl->next))
+    {
+       FcValue v = FcValueCanonicalize(&vl->value); // unserialize just in case
+
+       switch (v.type)
+       {
+       case FcTypeCharSet:
+           cum += FcCharSetNeededBytes(v.u.c);
+           break;
+       case FcTypeLangSet:
+           cum += FcLangSetNeededBytes(v.u.l);
+           break;
+       case FcTypeString:
+           cum += FcStrNeededBytes(v.u.s);
+       default:
+           break;
+       }
+       fcvaluelist_count++;
+       cum += sizeof (FcValueList);
+    }
+    
+    return cum;
+}
+
+static FcBool
+FcValueListEnsureBank (int bi)
+{
+    FcValueList **pvl;
+
+    if (!fcvaluelists || fcvaluelist_bank_count <= bi)
+    {
+       int new_count = bi + 2, i;
+
+       pvl = realloc (fcvaluelists, sizeof (FcValueList *) * new_count);
+       if (!pvl)
+           return FcFalse;
+
+       FcMemAlloc (FC_MEM_VALLIST, sizeof (FcValueList *) * new_count);
+
+       fcvaluelists = pvl;
+       for (i = fcvaluelist_bank_count; i < new_count; i++)
+           fcvaluelists[i] = 0;
+
+       fcvaluelist_bank_count = new_count;
+    }
+    return FcTrue;
+}
+
+static void *
+FcValueListDistributeBytes (FcCache * metadata, void *block_ptr)
+{
+    int bi = FcCacheBankToIndex(metadata->bank);
+
+    if (!FcValueListEnsureBank(bi))
+       return 0;
+
+    FcMemAlloc (FC_MEM_VALLIST, sizeof (FcValueList) * fcvaluelist_count);
+    fcvaluelist_ptr = 0;
+    fcvaluelists[bi] = (FcValueList *)block_ptr;
+    block_ptr = (void *)((char *)block_ptr + 
+                        (sizeof (FcValueList) * fcvaluelist_count));
+    metadata->valuelist_count = fcvaluelist_count;
+
+    block_ptr = FcCharSetDistributeBytes(metadata, block_ptr);
+    block_ptr = FcLangSetDistributeBytes(metadata, block_ptr);
+
+    return block_ptr;
+}
+
+static FcValueListPtr
+FcValueListSerialize(int bank, FcValueList *pi)
+{
+    FcValueListPtr new; 
+    FcValue * v;
+    int bi = FcCacheBankToIndex(bank);
+
+    if (!pi)
+    {
+       new.bank = FC_BANK_DYNAMIC;
+       new.u.dyn = 0;
+       return new;
+    }
+
+    fcvaluelists[bi][fcvaluelist_ptr] = *pi;
+    new.bank = bank;
+    new.u.stat = fcvaluelist_ptr++;
+    v = &fcvaluelists[bi][new.u.stat].value;
+    switch (v->type)
+    {
+    case FcTypeString:
+       if (v->u.s)
+       {
+           const FcChar8 * s = FcStrSerialize(bank, v->u.s);
+           if (!s)
+               return FcValueListPtrCreateDynamic(pi);
+           v->u.s_off = s - (const FcChar8 *)v;
+           v->type |= FC_STORAGE_STATIC;
+       }
+       break;
+    case FcTypeMatrix:
+       break;
+    case FcTypeCharSet:
+       if (v->u.c)
+       {
+           FcCharSet * c = FcCharSetSerialize(bank, (FcCharSet *)v->u.c);
+           if (!c)
+               return FcValueListPtrCreateDynamic(pi);
+           v->u.c_off = (char *)c - (char *)v;
+           v->type |= FC_STORAGE_STATIC;
+       }
+       break;
+    case FcTypeLangSet:
+       if (v->u.l)
+       {
+           FcLangSet * l = FcLangSetSerialize(bank, (FcLangSet *)v->u.l);
+           if (!l)
+               return FcValueListPtrCreateDynamic(pi);
+           v->u.l_off = (char *)l - (char *)v;
+           v->type |= FC_STORAGE_STATIC;
+       }
+       break;
+    default:
+       break;
+    }
+    return new;
+}
+
+static void *
+FcValueListUnserialize (FcCache metadata, void *block_ptr)
+{
+    int bi = FcCacheBankToIndex(metadata.bank);
+
+    if (!FcValueListEnsureBank(bi))
+       return 0;
+
+    FcMemAlloc (FC_MEM_VALLIST, 
+               sizeof (FcValueList) * metadata.valuelist_count);
+    fcvaluelists[bi] = (FcValueList *)block_ptr;
+    block_ptr = (void *)((char *)block_ptr + 
+                        (sizeof (FcValueList) * metadata.valuelist_count));
+
+    block_ptr = FcCharSetUnserialize(metadata, block_ptr);
+    block_ptr = FcLangSetUnserialize(metadata, block_ptr);
+
+    return block_ptr;
+}
+
+FcValueList * 
+FcValueListPtrU (FcValueListPtr pi)
+{
+    if (pi.bank == FC_BANK_DYNAMIC)
+        return pi.u.dyn;
+
+    return &fcvaluelists[FcCacheBankToIndex(pi.bank)][pi.u.stat];
+}
+
+FcValueListPtr
+FcValueListPtrCreateDynamic(FcValueList * p)
+{
+    FcValueListPtr r; 
+
+    r.bank = FC_BANK_DYNAMIC; 
+    r.u.dyn = p;
+    return r;
+}
+
+static FcChar8 ** static_strs;
+static int static_str_bank_count = 0, fcstr_ptr, fcstr_count;
+
+static struct objectBucket *FcStrBuckets[OBJECT_HASH_SIZE];
+
+static void
+FcStrNewBank (void)
+{
+    int i, size;
+    struct objectBucket *b, *next;
+    char *name;
+
+    for (i = 0; i < OBJECT_HASH_SIZE; i++)
+    {
+       for (b = FcStrBuckets[i]; b; b = next)
+       {
+           next = b->next;
+           name = (char *) (b + 1);
+           size = sizeof (struct objectBucket) + strlen (name) + 1;
+           FcMemFree (FC_MEM_STATICSTR, size);
+           free (b);
+       }
+       FcStrBuckets[i] = 0;
+    }
+
+    fcstr_count = 0;
+}
+
+static int
+FcStrNeededBytes (const FcChar8 * s)
+{
+    FcChar32            hash = FcStringHash ((const FcChar8 *) s);
+    struct objectBucket **p;
+    struct objectBucket *b;
+    int                 size;
+
+    for (p = &FcStrBuckets[hash % OBJECT_HASH_SIZE]; (b = *p); p = &(b->next))
+        if (b->hash == hash && !strcmp ((char *)s, (char *) (b + 1)))
+            return 0;
+    size = sizeof (struct objectBucket) + strlen ((char *)s) + 1 + sizeof(char *);
+    b = malloc (size);
+    FcMemAlloc (FC_MEM_STATICSTR, size);
+    if (!b)
+        return -1;
+    b->next = 0;
+    b->hash = hash;
+    strcpy ((char *) (b + 1), (char *)s);
+    *(char **)((char *) (b + 1) + strlen((char *)s) + 1) = 0;
+    *p = b;
+
+    fcstr_count += strlen((char *)s) + 1;
+    return strlen((char *)s) + 1;
+}
+
+static FcBool
+FcStrEnsureBank (int bi)
+{
+    FcChar8 ** ss;
+
+    if (!static_strs || static_str_bank_count <= bi)
+    {
+       int new_count = bi + 4, i;
+       ss = realloc (static_strs, sizeof (const char *) * new_count);
+       if (!ss)
+           return FcFalse;
+
+       FcMemAlloc (FC_MEM_STRING, sizeof (const char *) * (new_count-static_str_bank_count));
+       static_strs = ss;
+
+       for (i = static_str_bank_count; i < new_count; i++)
+           static_strs[i] = 0;
+       static_str_bank_count = new_count;
+    }
+    return FcTrue;
+}
+
+static void *
+FcStrDistributeBytes (FcCache * metadata, void * block_ptr)
+{
+    int bi = FcCacheBankToIndex(metadata->bank);
+    if (!FcStrEnsureBank(bi)) 
+       return 0;
+
+    FcMemAlloc (FC_MEM_STRING, sizeof (char) * fcstr_count);
+    static_strs[bi] = (FcChar8 *)block_ptr;
+    block_ptr = (void *)((char *)block_ptr + (sizeof (char) * fcstr_count));
+    metadata->str_count = fcstr_count;
+    fcstr_ptr = 0;
+
+    return block_ptr;
+}
+
+static const FcChar8 *
+FcStrSerialize (int bank, const FcChar8 * s)
+{
+    FcChar32            hash = FcStringHash ((const FcChar8 *) s);
+    struct objectBucket **p;
+    struct objectBucket *b;
+    int bi = FcCacheBankToIndex(bank);
+
+    for (p = &FcStrBuckets[hash % OBJECT_HASH_SIZE]; (b = *p); p = &(b->next))
+        if (b->hash == hash && !strcmp ((char *)s, (char *) (b + 1)))
+       {
+           FcChar8 * t = *(FcChar8 **)(((FcChar8 *)(b + 1)) + strlen ((char *)s) + 1);
+           if (!t)
+           {
+               strcpy((char *)(static_strs[bi] + fcstr_ptr), (char *)s);
+               *(FcChar8 **)((FcChar8 *) (b + 1) + strlen((char *)s) + 1) = (static_strs[bi] + fcstr_ptr);
+               fcstr_ptr += strlen((char *)s) + 1;
+               t = *(FcChar8 **)(((FcChar8 *)(b + 1)) + strlen ((char *)s) + 1);
+           }
+           return t;
+       }
+    return 0;
+}
+
+static void *
+FcStrUnserialize (FcCache metadata, void *block_ptr)
+{
+    int bi = FcCacheBankToIndex(metadata.bank);
+    if (!FcStrEnsureBank(bi))
+       return 0;
+
+    FcMemAlloc (FC_MEM_STRING, sizeof (char) * metadata.str_count);
+    static_strs[bi] = (FcChar8 *)block_ptr;
+    block_ptr = (void *)((char *)block_ptr + 
+                        (sizeof (char) * metadata.str_count));
+
+    return block_ptr;
+}
+
+/* we don't store these in the FcPattern itself because
+ * we don't want to serialize the directory names */
+
+/* I suppose this should be cleaned, too... */
+typedef struct _FcPatternDirMapping {
+    const FcPattern    *p;
+    const char *fname;
+} FcPatternDirMapping;
+
+#define PATTERNDIR_HASH_SIZE    31
+static struct patternDirBucket {
+    struct patternDirBucket    *next;
+    FcPatternDirMapping                m;
+} FcPatternDirBuckets[PATTERNDIR_HASH_SIZE];
+
+void
+FcPatternAddFullFname (const FcPattern *p, const char *fname)
+{
+    struct patternDirBucket    *pb;
+
+    /* N.B. FcPatternHash fails, since it's contents-based, not
+     * address-based, and we're in the process of mutating the FcPattern. */
+    for (pb = &FcPatternDirBuckets
+             [((int)p / sizeof (FcPattern *)) % PATTERNDIR_HASH_SIZE];
+         pb->m.p != p && pb->next; 
+         pb = pb->next)
+        ;
+
+    if (pb->m.p == p)
+    {
+        pb->m.fname = fname;
+        return;
+    }
+
+    pb->next = malloc (sizeof (struct patternDirBucket));
+    if (!pb->next)
+        return;
+    FcMemAlloc (FC_MEM_CACHE, sizeof (struct patternDirBucket));
+
+    pb->next->next = 0;
+    pb->next->m.p = p;
+    pb->next->m.fname = fname;
+}
+
+static const char *
+FcPatternFindFullFname (const FcPattern *p)
+{
+    struct patternDirBucket    *pb;
+
+    for (pb = &FcPatternDirBuckets
+             [((int)p / sizeof (FcPattern *)) % PATTERNDIR_HASH_SIZE]; 
+         pb; pb = pb->next)
+       if (pb->m.p == p)
+           return pb->m.fname;
+
+    return 0;
+}
+
+void
+FcPatternTransferFullFname (const FcPattern *new, const FcPattern *orig)
+{
+    FcChar8 * s;
+    FcPatternGetString (orig, FC_FILE, 0, &s);
+    FcPatternAddFullFname (new, FcPatternFindFullFname(orig));
+}