X-Git-Url: https://git.wh0rd.org/?a=blobdiff_plain;f=src%2Ffccharset.c;h=96dcbe7169e4c545e2f24ecf06631703f3e50c61;hb=c02886485b293179e8492cad9a34eb431dd4bfc9;hp=00b2010e5aac399c7f452b1203204931d0d658e9;hpb=212c9f437e959fbdc5fe344c67b8c1cf8ca63edb;p=fontconfig.git diff --git a/src/fccharset.c b/src/fccharset.c index 00b2010..96dcbe7 100644 --- a/src/fccharset.c +++ b/src/fccharset.c @@ -22,35 +22,13 @@ * PERFORMANCE OF THIS SOFTWARE. */ -#include -#include #include "fcint.h" +#include /* #define CHECK */ /* #define CHATTY */ -static FcCharSet * charsets = 0; -static FcChar16 * numbers = 0; -static int charset_ptr, charset_count; -static int charset_numbers_ptr, charset_numbers_count; -static FcCharLeaf * leaves = 0; -static int charset_leaf_ptr, charset_leaf_count; -static int * leaf_idx = 0; -static int charset_leaf_idx_ptr, charset_leaf_idx_count; - -void -FcCharSetClearStatic() -{ - charsets = 0; - numbers = 0; - charset_ptr = 0; charset_count = 0; - leaves = 0; - charset_leaf_ptr = 0; charset_leaf_count = 0; - leaf_idx = 0; - charset_leaf_idx_ptr = 0; charset_leaf_idx_count = 0; -} - FcCharSet * FcCharSetCreate (void) { @@ -62,9 +40,8 @@ FcCharSetCreate (void) FcMemAlloc (FC_MEM_CHARSET, sizeof (FcCharSet)); fcs->ref = 1; fcs->num = 0; - fcs->storage = FcStorageDynamic; - fcs->u.dyn.leaves = 0; - fcs->u.dyn.numbers = 0; + fcs->leaves_offset = FcPtrToOffset (fcs, NULL); + fcs->numbers_offset = FcPtrToOffset (fcs, NULL); return fcs; } @@ -77,43 +54,29 @@ FcCharSetNew (void) return FcCharSetCreate (); } -void -FcCharSetPtrDestroy (FcCharSetPtr fcs) -{ - FcCharSetDestroy (FcCharSetPtrU(fcs)); - if (fcs.storage == FcStorageDynamic && - FcCharSetPtrU(fcs)->ref != FC_REF_CONSTANT) - { - free (fcs.u.dyn); - FcMemFree (FC_MEM_CHARSET, sizeof(FcCharSet)); - } -} - void FcCharSetDestroy (FcCharSet *fcs) { int i; + if (fcs->ref == FC_REF_CONSTANT) return; if (--fcs->ref > 0) return; - if (fcs->storage == FcStorageDynamic) + for (i = 0; i < fcs->num; i++) { - for (i = 0; i < fcs->num; i++) - { - FcMemFree (FC_MEM_CHARLEAF, sizeof (FcCharLeaf)); - free (fcs->u.dyn.leaves[i]); - } - if (fcs->u.dyn.leaves) - { - FcMemFree (FC_MEM_CHARSET, fcs->num * sizeof (FcCharLeaf *)); - free (fcs->u.dyn.leaves); - } - if (fcs->u.dyn.numbers) - { - FcMemFree (FC_MEM_CHARSET, fcs->num * sizeof (FcChar16)); - free (fcs->u.dyn.numbers); - } + FcMemFree (FC_MEM_CHARLEAF, sizeof (FcCharLeaf)); + free (FcCharSetLeaf (fcs, i)); + } + if (FcCharSetLeaves (fcs)) + { + FcMemFree (FC_MEM_CHARSET, fcs->num * sizeof (intptr_t)); + free (FcCharSetLeaves (fcs)); + } + if (FcCharSetNumbers (fcs)) + { + FcMemFree (FC_MEM_CHARSET, fcs->num * sizeof (FcChar16)); + free (FcCharSetNumbers (fcs)); } FcMemFree (FC_MEM_CHARSET, sizeof (FcCharSet)); free (fcs); @@ -128,7 +91,7 @@ FcCharSetDestroy (FcCharSet *fcs) static int FcCharSetFindLeafPos (const FcCharSet *fcs, FcChar32 ucs4) { - FcChar16 *numbers = FcCharSetGetNumbers(fcs); + FcChar16 *numbers = FcCharSetNumbers(fcs); FcChar16 page; int low = 0; int high = fcs->num - 1; @@ -157,7 +120,7 @@ FcCharSetFindLeaf (const FcCharSet *fcs, FcChar32 ucs4) { int pos = FcCharSetFindLeafPos (fcs, ucs4); if (pos >= 0) - return FcCharSetGetLeaf(fcs, pos); + return FcCharSetLeaf(fcs, pos); return 0; } @@ -167,61 +130,55 @@ FcCharSetPutLeaf (FcCharSet *fcs, FcCharLeaf *leaf, int pos) { - FcCharLeaf **leaves; - FcChar16 *numbers; + intptr_t *leaves = FcCharSetLeaves (fcs); + FcChar16 *numbers = FcCharSetNumbers (fcs); ucs4 >>= 8; if (ucs4 >= 0x10000) return FcFalse; - if (fcs->storage == FcStorageStatic) + if (!leaves) + leaves = malloc (sizeof (*leaves)); + else { - int i; - - leaves = malloc ((fcs->num + 1) * sizeof (FcCharLeaf *)); - if (!leaves) - return FcFalse; - FcMemAlloc (FC_MEM_CHARSET, (fcs->num + 1) * sizeof (FcCharLeaf *)); - numbers = malloc ((fcs->num + 1) * sizeof (FcChar16)); - if (!numbers) - return FcFalse; - FcMemAlloc (FC_MEM_CHARSET, (fcs->num + 1) * sizeof (FcChar16)); + intptr_t *new_leaves = realloc (leaves, (fcs->num + 1) * + sizeof (*leaves)); + intptr_t distance = (intptr_t) new_leaves - (intptr_t) leaves; + + if (new_leaves && distance) + { + int i; - for (i = 0; i < fcs->num; i++) - leaves[i] = FcCharSetGetLeaf(fcs, i); - memcpy (numbers, FcCharSetGetNumbers(fcs), - fcs->num * sizeof (FcChar16)); - fcs->storage = FcStorageDynamic; + for (i = 0; i < fcs->num; i++) + new_leaves[i] -= distance; + } + leaves = new_leaves; } + if (!leaves) + return FcFalse; + + if (fcs->num) + FcMemFree (FC_MEM_CHARSET, fcs->num * sizeof (intptr_t)); + FcMemAlloc (FC_MEM_CHARSET, (fcs->num + 1) * sizeof (intptr_t)); + fcs->leaves_offset = FcPtrToOffset (fcs, leaves); + + if (!numbers) + numbers = malloc (sizeof (FcChar16)); else - { - if (!fcs->u.dyn.leaves) - leaves = malloc (sizeof (FcCharLeaf *)); - else - leaves = realloc (fcs->u.dyn.leaves, (fcs->num + 1) * sizeof (FcCharLeaf *)); - if (!leaves) - return FcFalse; - if (fcs->num) - FcMemFree (FC_MEM_CHARSET, fcs->num * sizeof (FcCharLeaf *)); - FcMemAlloc (FC_MEM_CHARSET, (fcs->num + 1) * sizeof (FcCharLeaf *)); - fcs->u.dyn.leaves = leaves; - if (!fcs->u.dyn.numbers) - numbers = malloc (sizeof (FcChar16)); - else - numbers = realloc (fcs->u.dyn.numbers, (fcs->num + 1) * sizeof (FcChar16)); - if (!numbers) - return FcFalse; - if (fcs->num) - FcMemFree (FC_MEM_CHARSET, fcs->num * sizeof (FcChar16)); - FcMemAlloc (FC_MEM_CHARSET, (fcs->num + 1) * sizeof (FcChar16)); - fcs->u.dyn.numbers = numbers; - } + numbers = realloc (numbers, (fcs->num + 1) * sizeof (FcChar16)); + if (!numbers) + return FcFalse; + + if (fcs->num) + FcMemFree (FC_MEM_CHARSET, fcs->num * sizeof (FcChar16)); + FcMemAlloc (FC_MEM_CHARSET, (fcs->num + 1) * sizeof (FcChar16)); + fcs->numbers_offset = FcPtrToOffset (fcs, numbers); - memmove (fcs->u.dyn.leaves + pos + 1, fcs->u.dyn.leaves + pos, - (fcs->num - pos) * sizeof (FcCharLeaf *)); - memmove (fcs->u.dyn.numbers + pos + 1, fcs->u.dyn.numbers + pos, - (fcs->num - pos) * sizeof (FcChar16)); - fcs->u.dyn.numbers[pos] = (FcChar16) ucs4; - fcs->u.dyn.leaves[pos] = leaf; + memmove (leaves + pos + 1, leaves + pos, + (fcs->num - pos) * sizeof (*leaves)); + memmove (numbers + pos + 1, numbers + pos, + (fcs->num - pos) * sizeof (*numbers)); + numbers[pos] = (FcChar16) ucs4; + leaves[pos] = FcPtrToOffset (leaves, leaf); fcs->num++; return FcTrue; } @@ -239,7 +196,7 @@ FcCharSetFindLeafCreate (FcCharSet *fcs, FcChar32 ucs4) pos = FcCharSetFindLeafPos (fcs, ucs4); if (pos >= 0) - return FcCharSetGetLeaf(fcs, pos); + return FcCharSetLeaf(fcs, pos); leaf = calloc (1, sizeof (FcCharLeaf)); if (!leaf) @@ -264,15 +221,9 @@ FcCharSetInsertLeaf (FcCharSet *fcs, FcChar32 ucs4, FcCharLeaf *leaf) if (pos >= 0) { FcMemFree (FC_MEM_CHARLEAF, sizeof (FcCharLeaf)); - if (fcs->storage == FcStorageDynamic) - { - free (fcs->u.dyn.leaves[pos]); - fcs->u.dyn.leaves[pos] = leaf; - } - else - { - leaves[leaf_idx[fcs->u.stat.leafidx_offset]+pos] = *leaf; - } + free (FcCharSetLeaf (fcs, pos)); + FcCharSetLeaves(fcs)[pos] = FcPtrToOffset (FcCharSetLeaves(fcs), + leaf); return FcTrue; } pos = -pos - 1; @@ -323,9 +274,9 @@ FcCharSetIterSet (const FcCharSet *fcs, FcCharSetIter *iter) iter->leaf = 0; return; } - iter->ucs4 = (FcChar32) FcCharSetGetNumbers(fcs)[pos] << 8; + iter->ucs4 = (FcChar32) FcCharSetNumbers(fcs)[pos] << 8; } - iter->leaf = FcCharSetGetLeaf(fcs, pos); + iter->leaf = FcCharSetLeaf(fcs, pos); iter->pos = pos; #ifdef CHATTY printf ("set %08x: %08x\n", iter->ucs4, (FcChar32) iter->leaf); @@ -343,8 +294,8 @@ FcCharSetIterNext (const FcCharSet *fcs, FcCharSetIter *iter) } else { - iter->ucs4 = (FcChar32) FcCharSetGetNumbers(fcs)[pos] << 8; - iter->leaf = FcCharSetGetLeaf(fcs, pos); + iter->ucs4 = (FcChar32) FcCharSetNumbers(fcs)[pos] << 8; + iter->leaf = FcCharSetLeaf(fcs, pos); iter->pos = pos; } } @@ -373,6 +324,7 @@ FcCharSetIterStart (const FcCharSet *fcs, FcCharSetIter *iter) FcCharSetDump (fcs); #endif iter->ucs4 = 0; + iter->pos = 0; FcCharSetIterSet (fcs, iter); } @@ -384,14 +336,6 @@ FcCharSetCopy (FcCharSet *src) return src; } -FcCharSetPtr -FcCharSetCopyPtr (FcCharSetPtr src) -{ - if (FcCharSetPtrU(src)->ref != FC_REF_CONSTANT) - FcCharSetPtrU(src)->ref++; - return src; -} - FcBool FcCharSetEqual (const FcCharSet *a, const FcCharSet *b) { @@ -634,7 +578,7 @@ FcCharSetSubtractCount (const FcCharSet *a, const FcCharSet *b) int i = 256/32; if (ai.ucs4 == bi.ucs4) { - FcChar32 *bm = bi.leaf->map;; + FcChar32 *bm = bi.leaf->map; while (i--) count += FcCharSetPopCount (*am++ & ~*bm++); } @@ -668,15 +612,15 @@ FcCharSetIsSubset (const FcCharSet *a, const FcCharSet *b) ai = 0; while (ai < a->num && bi < b->num) { - an = FcCharSetGetNumbers(a)[ai]; - bn = FcCharSetGetNumbers(b)[bi]; + an = FcCharSetNumbers(a)[ai]; + bn = FcCharSetNumbers(b)[bi]; /* * Check matching pages */ if (an == bn) { - FcChar32 *am = FcCharSetGetLeaf(a, ai)->map; - FcChar32 *bm = FcCharSetGetLeaf(b, bi)->map; + FcChar32 *am = FcCharSetLeaf(a, ai)->map; + FcChar32 *bm = FcCharSetLeaf(b, bi)->map; if (am != bm) { @@ -707,7 +651,7 @@ FcCharSetIsSubset (const FcCharSet *a, const FcCharSet *b) while (low <= high) { int mid = (low + high) >> 1; - bn = FcCharSetGetNumbers(b)[mid]; + bn = FcCharSetNumbers(b)[mid]; if (bn == an) { high = mid; @@ -719,7 +663,7 @@ FcCharSetIsSubset (const FcCharSet *a, const FcCharSet *b) high = mid - 1; } bi = high; - while (bi < b->num && FcCharSetGetNumbers(b)[bi] < an) + while (bi < b->num && FcCharSetNumbers(b)[bi] < an) bi++; } } @@ -1026,11 +970,11 @@ FcCharSetHash (FcCharSet *fcs) int i; /* hash in leaves */ - for (i = 0; i < fcs->num * sizeof (FcCharLeaf *) / sizeof (FcChar32); i++) - hash = ((hash << 1) | (hash >> 31)) ^ (FcChar32)(FcCharSetGetLeaf(fcs, i)->map); + for (i = 0; i < fcs->num * (int) (sizeof (FcCharLeaf *) / sizeof (FcChar32)); i++) + hash = ((hash << 1) | (hash >> 31)) ^ (FcChar32)(FcCharSetLeaf(fcs, i)->map); /* hash in numbers */ for (i = 0; i < fcs->num; i++) - hash = ((hash << 1) | (hash >> 31)) ^ *FcCharSetGetNumbers(fcs); + hash = ((hash << 1) | (hash >> 31)) ^ *FcCharSetNumbers(fcs); return hash; } @@ -1047,6 +991,7 @@ FcCharSetFreezeBase (FcCharSet *fcs) FcCharSetEnt **bucket = &FcCharSetHashTable[hash % FC_CHAR_SET_HASH_SIZE]; FcCharSetEnt *ent; int size; + int i; FcCharSetTotal++; FcCharSetTotalEnts += fcs->num; @@ -1054,15 +999,15 @@ FcCharSetFreezeBase (FcCharSet *fcs) { if (ent->hash == hash && ent->set.num == fcs->num && - !memcmp (FcCharSetGetNumbers(&ent->set), - FcCharSetGetNumbers(fcs), + !memcmp (FcCharSetNumbers(&ent->set), + FcCharSetNumbers(fcs), fcs->num * sizeof (FcChar16))) { FcBool ok = FcTrue; int i; for (i = 0; i < fcs->num; i++) - if (FcCharSetGetLeaf(&ent->set, i) != FcCharSetGetLeaf(fcs, i)) + if (FcCharSetLeaf(&ent->set, i) != FcCharSetLeaf(fcs, i)) ok = FcFalse; if (ok) return &ent->set; @@ -1081,26 +1026,26 @@ FcCharSetFreezeBase (FcCharSet *fcs) ent->set.ref = FC_REF_CONSTANT; ent->set.num = fcs->num; - ent->set.storage = fcs->storage; - if (fcs->storage == FcStorageDynamic) + if (fcs->num) { - if (fcs->num) - { - ent->set.u.dyn.leaves = (FcCharLeaf **) (ent + 1); - ent->set.u.dyn.numbers = (FcChar16 *) (ent->set.u.dyn.leaves + fcs->num); - memcpy (ent->set.u.dyn.leaves, fcs->u.dyn.leaves, fcs->num * sizeof (FcCharLeaf *)); - memcpy (ent->set.u.dyn.numbers, fcs->u.dyn.numbers, fcs->num * sizeof (FcChar16)); - } - else - { - ent->set.u.dyn.leaves = 0; - ent->set.u.dyn.numbers = 0; - } + intptr_t *ent_leaves; + + ent->set.leaves_offset = sizeof (ent->set); + ent->set.numbers_offset = (ent->set.leaves_offset + + fcs->num * sizeof (intptr_t)); + + ent_leaves = FcCharSetLeaves (&ent->set); + for (i = 0; i < fcs->num; i++) + ent_leaves[i] = FcPtrToOffset (ent_leaves, + FcCharSetLeaf (fcs, i)); + memcpy (FcCharSetNumbers (&ent->set), + FcCharSetNumbers (fcs), + fcs->num * sizeof (FcChar16)); } else { - ent->set.u.stat.leafidx_offset = fcs->u.stat.leafidx_offset; - ent->set.u.stat.numbers_offset = fcs->u.stat.numbers_offset; + ent->set.leaves_offset = 0; + ent->set.numbers_offset = 0; } ent->hash = hash; @@ -1146,26 +1091,23 @@ FcCharSetFreeze (FcCharSet *fcs) goto bail0; for (i = 0; i < fcs->num; i++) { - l = FcCharSetFreezeLeaf (FcCharSetGetLeaf(fcs, i)); + l = FcCharSetFreezeLeaf (FcCharSetLeaf(fcs, i)); if (!l) goto bail1; - if (!FcCharSetInsertLeaf (b, FcCharSetGetNumbers(fcs)[i] << 8, l)) + if (!FcCharSetInsertLeaf (b, FcCharSetNumbers(fcs)[i] << 8, l)) goto bail1; } n = FcCharSetFreezeBase (b); bail1: - if (b->storage == FcStorageDynamic) + if (FcCharSetLeaves (b)) { - if (b->u.dyn.leaves) - { - FcMemFree (FC_MEM_CHARSET, b->num * sizeof (FcCharLeaf *)); - free (b->u.dyn.leaves); - } - if (b->u.dyn.numbers) - { - FcMemFree (FC_MEM_CHARSET, b->num * sizeof (FcChar16)); - free (b->u.dyn.numbers); - } + FcMemFree (FC_MEM_CHARSET, b->num * sizeof (FcCharLeaf *)); + free (FcCharSetLeaves (b)); + } + if (FcCharSetNumbers (b)) + { + FcMemFree (FC_MEM_CHARSET, b->num * sizeof (FcChar16)); + free (FcCharSetNumbers (b)); } FcMemFree (FC_MEM_CHARSET, sizeof (FcCharSet)); free (b); @@ -1231,20 +1173,17 @@ FcNameParseCharSet (FcChar8 *string) #endif n = FcCharSetFreezeBase (c); bail1: - if (c->storage == FcStorageDynamic) + if (FcCharSetLeaves (c)) { - if (c->u.dyn.leaves) - { - FcMemFree (FC_MEM_CHARSET, c->num * sizeof (FcCharLeaf *)); - free (c->u.dyn.leaves); - } - if (c->u.dyn.numbers) - { - FcMemFree (FC_MEM_CHARSET, c->num * sizeof (FcChar16)); - free (c->u.dyn.numbers); - } - FcMemFree (FC_MEM_CHARSET, sizeof (FcCharSet)); + 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; @@ -1323,205 +1262,68 @@ FcNameUnparseCharSet (FcStrBuf *buf, const FcCharSet *c) return FcTrue; } - -FcCharSet * -FcCharSetPtrU (FcCharSetPtr ci) -{ - switch (ci.storage) - { - case FcStorageDynamic: - return ci.u.dyn; - case FcStorageStatic: - return &charsets[ci.u.stat]; - default: - return 0; - } -} - -FcCharSetPtr -FcCharSetPtrCreateDynamic(FcCharSet *c) -{ - FcCharSetPtr new; - - new.storage = FcStorageDynamic; - new.u.dyn = c; - return new; -} - FcBool -FcCharSetPrepareSerialize(FcCharSet *c) -{ - /* note the redundancy */ - charset_count++; - charset_leaf_idx_count++; - charset_leaf_count += c->num; - charset_numbers_count += c->num; - return FcTrue; -} - -FcCharSetPtr -FcCharSetSerialize(FcCharSet *c) +FcCharSetSerializeAlloc (FcSerialize *serialize, const FcCharSet *cs) { - int i; - FcCharSetPtr newp; - FcCharSet new; - - if (!charsets) - { - charsets = malloc(sizeof(FcCharSet) * charset_count); - if (!charsets) goto bail; - numbers = malloc(sizeof(FcChar16) * charset_numbers_count); - if (!numbers) goto bail1; - leaves = malloc(sizeof(FcCharLeaf) * charset_leaf_count); - if (!leaves) goto bail2; - leaf_idx = malloc(sizeof(int)*charset_leaf_idx_count); - if (!leaf_idx) goto bail3; - } - - new.ref = FC_REF_CONSTANT; - new.storage = FcStorageStatic; - new.u.stat.leafidx_offset = charset_leaf_idx_ptr; - new.u.stat.numbers_offset = charset_numbers_ptr; - new.num = c->num; - - newp.storage = FcStorageStatic; - newp.u.stat = charset_ptr; - charsets[charset_ptr++] = new; - - leaf_idx[charset_leaf_idx_ptr++] = charset_leaf_ptr; - for (i = 0; i < c->num; i++) - { - memcpy (&leaves[charset_leaf_ptr++], - c->u.dyn.leaves[i], sizeof(FcCharLeaf)); - numbers[charset_numbers_ptr++] = c->u.dyn.numbers[i]; - } - - return newp; - - bail3: - free (leaves); - bail2: - free (numbers); - bail1: - free (charsets); - bail: - return FcCharSetPtrCreateDynamic(0); -} - -FcBool -FcCharSetRead (int fd, FcCache metadata) -{ - charsets = mmap(NULL, - metadata.charsets_length * sizeof (FcCharSet), - PROT_READ, - MAP_SHARED, fd, metadata.charsets_offset); - if (charsets == MAP_FAILED) - goto bail; - charset_count = charset_ptr = metadata.charsets_length; - - leaves = mmap(NULL, - metadata.charset_num_sum * sizeof (FcCharLeaf), - PROT_READ, - MAP_SHARED, fd, metadata.charset_leaf_offset); - if (leaves == MAP_FAILED) - goto bail1; - charset_leaf_count = charset_leaf_ptr = metadata.charset_num_sum; - - leaf_idx = mmap(NULL, - metadata.charsets_length * sizeof (FcCharLeaf*), - PROT_READ, - MAP_SHARED, fd, metadata.charset_leafidx_offset); - if (leaf_idx == MAP_FAILED) - goto bail2; - charset_leaf_idx_count = charset_leaf_idx_ptr = metadata.charsets_length; - - numbers = mmap(NULL, - metadata.charset_num_sum * sizeof (FcChar16), - PROT_READ, - MAP_SHARED, fd, metadata.charset_numbers_offset); - if (numbers == MAP_FAILED) - goto bail3; - charset_numbers_count = charset_numbers_ptr = metadata.charset_num_sum; - + intptr_t *leaves = FcCharSetLeaves (cs); + FcChar16 *numbers = FcCharSetNumbers (cs); + int i; + + if (!FcSerializeAlloc (serialize, cs, sizeof (FcCharSet))) + return FcFalse; + if (!FcSerializeAlloc (serialize, leaves, cs->num * sizeof (intptr_t))) + return FcFalse; + if (!FcSerializeAlloc (serialize, numbers, cs->num * sizeof (FcChar16))) + return FcFalse; + for (i = 0; i < cs->num; i++) + if (!FcSerializeAlloc (serialize, FcCharSetLeaf(cs, i), + sizeof (FcCharLeaf))) + return FcFalse; return FcTrue; - - bail3: - munmap (leaf_idx, metadata.charsets_length * sizeof (FcCharLeaf*)); - bail2: - munmap (leaves, metadata.charset_num_sum * sizeof (FcCharLeaf)); - bail1: - munmap (charsets, metadata.charsets_length * sizeof (FcCharSet)); - bail: - return FcFalse; } - -FcBool -FcCharSetWrite (int fd, FcCache *metadata) + +FcCharSet * +FcCharSetSerialize(FcSerialize *serialize, const FcCharSet *cs) { - metadata->charsets_length = charset_ptr; - metadata->charsets_offset = FcCacheNextOffset(fd); - - if (charset_ptr > 0) - { - lseek (fd, metadata->charsets_offset, SEEK_SET); - if (write (fd, charsets, charset_ptr * sizeof(FcCharSet)) == -1) - return FcFalse; - } - - metadata->charset_leaf_offset = FcCacheNextOffset(fd); - metadata->charset_num_sum = charset_leaf_ptr; - if (charset_leaf_ptr > 0) - { - lseek (fd, metadata->charset_leaf_offset, SEEK_SET); - if (write (fd, leaves, charset_leaf_ptr * sizeof(FcCharLeaf)) == -1) - return FcFalse; - } - - metadata->charset_leafidx_offset = FcCacheNextOffset(fd); - if (charset_leaf_idx_ptr > 0) - { - lseek (fd, metadata->charset_leafidx_offset, SEEK_SET); - if (write (fd, leaf_idx, charset_leaf_idx_ptr * sizeof(FcCharLeaf*)) == -1) - return FcFalse; - } + FcCharSet *cs_serialized = FcSerializePtr (serialize, cs); + intptr_t *leaves, *leaves_serialized; + FcChar16 *numbers, *numbers_serialized; + FcCharLeaf *leaf, *leaf_serialized; + int i; + if (!cs_serialized) + return NULL; + + cs_serialized->ref = FC_REF_CONSTANT; + cs_serialized->num = cs->num; - metadata->charset_numbers_offset = FcCacheNextOffset(fd); - if (charset_leaf_ptr > 0) - { - lseek (fd, metadata->charset_numbers_offset, SEEK_SET); - if (write (fd, numbers, charset_leaf_ptr * sizeof(FcChar16)) == -1) - return FcFalse; - } + leaves = FcCharSetLeaves (cs); + leaves_serialized = FcSerializePtr (serialize, leaves); + if (!leaves_serialized) + return NULL; - return FcTrue; -} + cs_serialized->leaves_offset = FcPtrToOffset (cs_serialized, + leaves_serialized); + + numbers = FcCharSetNumbers (cs); + numbers_serialized = FcSerializePtr (serialize, numbers); + if (!numbers) + return NULL; -FcCharLeaf * -FcCharSetGetLeaf(const FcCharSet *c, int i) -{ - switch (c->storage) - { - case FcStorageDynamic: - return c->u.dyn.leaves[i]; - case FcStorageStatic: - return &leaves[leaf_idx[c->u.stat.leafidx_offset]+i]; - default: - return 0; - } -} + cs_serialized->numbers_offset = FcPtrToOffset (cs_serialized, + numbers_serialized); -FcChar16 * -FcCharSetGetNumbers(const FcCharSet *c) -{ - switch (c->storage) + for (i = 0; i < cs->num; i++) { - case FcStorageDynamic: - return c->u.dyn.numbers; - case FcStorageStatic: - return &numbers[c->u.stat.numbers_offset]; - default: - return 0; + leaf = FcCharSetLeaf (cs, i); + leaf_serialized = FcSerializePtr (serialize, leaf); + if (!leaf_serialized) + return NULL; + *leaf_serialized = *leaf; + leaves_serialized[i] = FcPtrToOffset (leaves_serialized, + leaf_serialized); + numbers_serialized[i] = numbers[i]; } + + return cs_serialized; } -