Commit 212c9f43 authored by Patrick Lam's avatar Patrick Lam

#ifdef out old cache stuff, replace with first version of new mmapping

    cache. Add *Read and *Write procedures which mmap in and write out the
    fontconfig data structures to disk. Currently, create cache in /tmp,
    with different sections for each architecture (as returned by uname's
    .machine field. Run the fc-cache binary to create a new cache file;
    fontconfig then uses this cache file on subsequent runs, saving lots of
    memory. Also fixes a few bugs and leaks.
parent e1b9d091
......@@ -93,6 +93,7 @@ usage (char *program)
exit (1);
}
#if 0
static int
nsubdirs (FcStrSet *set)
{
......@@ -222,6 +223,7 @@ scanDirs (FcStrList *list, FcConfig *config, char *program, FcBool force, FcBool
FcStrListDone (list);
return ret;
}
#endif
int
main (int argc, char **argv)
......@@ -268,12 +270,20 @@ main (int argc, char **argv)
if (systemOnly)
FcConfigEnableHome (FcFalse);
config = FcInitLoadConfig ();
FcCacheForce (FcTrue);
/* need to use FcInitLoadConfig when we use dirs */
FcInit ();
config = FcConfigGetCurrent ();
if (!config)
{
fprintf (stderr, "%s: Can't init font config library\n", argv[0]);
return 1;
}
/* We don't yet have per-directory caches. */
ret = (FcCacheWrite (config) == FcFalse);
#if 0
if (argv[i])
{
dirs = FcStrSetCreate ();
......@@ -298,6 +308,7 @@ main (int argc, char **argv)
else
list = FcConfigGetConfigDirs (config);
ret = scanDirs (list, config, argv[0], force, verbose);
#endif
/*
* Now we need to sleep a second (or two, to be extra sure), to make
* sure that timestamps for changes after this run of fc-cache are later
......
......@@ -47,6 +47,12 @@ FcMemFree (int kind, int size)
{
}
int
FcCacheNextOffset (int fd)
{
return -1;
}
FcChar8 *
FcConfigHome (void)
{
......
This diff is collapsed.
......@@ -252,16 +252,22 @@ FcConfigBuildFonts (FcConfig *config)
FcStrList *list;
FcChar8 *dir;
#if 0
if (config->cache)
FcGlobalCacheLoad (cache, config->cache);
#endif
if (config->fonts[FcSetSystem])
return FcTrue;
fonts = FcFontSetCreate ();
if (!fonts)
goto bail0;
#if 0
cache = FcGlobalCacheCreate ();
if (!cache)
goto bail1;
if (config->cache)
FcGlobalCacheLoad (cache, config->cache);
#endif
list = FcConfigGetFontDirs (config);
if (!list)
......@@ -280,9 +286,11 @@ FcConfigBuildFonts (FcConfig *config)
if (FcDebug () & FC_DBG_FONTSET)
FcFontSetPrint (fonts);
#if 0
if (config->cache)
FcGlobalCacheSave (cache, config->cache);
FcGlobalCacheDestroy (cache);
#endif
FcConfigSetFonts (config, fonts, FcSetSystem);
......
......@@ -23,6 +23,7 @@
*/
#include <stdlib.h>
#include <sys/mman.h>
#include "fcint.h"
/* #define CHECK */
......@@ -383,6 +384,14 @@ 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)
{
......@@ -1369,10 +1378,11 @@ FcCharSetSerialize(FcCharSet *c)
if (!leaf_idx) goto bail3;
}
new.ref = c->ref;
new.ref = FC_REF_CONSTANT;
new.storage = FcStorageStatic;
new.u.stat.leafidx_offset = charset_leaf_ptr;
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;
......@@ -1398,6 +1408,95 @@ FcCharSetSerialize(FcCharSet *c)
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;
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)
{
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;
}
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;
}
return FcTrue;
}
FcCharLeaf *
FcCharSetGetLeaf(const FcCharSet *c, int i)
{
......
......@@ -45,13 +45,15 @@ FcFileScanConfig (FcFontSet *set,
FcConfig *config)
{
int id;
#if 0
FcChar8 *name;
FcGlobalCacheFile *cache_file;
FcGlobalCacheDir *cache_dir;
#endif
FcPattern *font;
FcBool ret = FcTrue;
FcBool isDir;
int count = 0;
FcGlobalCacheFile *cache_file;
FcGlobalCacheDir *cache_dir;
FcBool need_scan;
if (config && !FcConfigAcceptFilename (config, file))
......@@ -64,6 +66,7 @@ FcFileScanConfig (FcFontSet *set,
{
need_scan = FcTrue;
font = 0;
#if 0
/*
* Check the cache
*/
......@@ -104,6 +107,7 @@ FcFileScanConfig (FcFontSet *set,
}
}
}
#endif
/*
* Nothing in the cache, scan the file
*/
......@@ -123,6 +127,7 @@ FcFileScanConfig (FcFontSet *set,
isDir = FcTrue;
ret = FcStrSetAdd (dirs, file);
}
#if 0
/*
* Update the cache
*/
......@@ -137,6 +142,7 @@ FcFileScanConfig (FcFontSet *set,
FcStrFree (unparse);
}
}
#endif
}
/*
* Add the font
......@@ -193,6 +199,7 @@ FcDirScanConfig (FcFontSet *set,
if (!force)
{
#if 0
/*
* Check fonts.cache-<version> file
*/
......@@ -208,6 +215,7 @@ FcDirScanConfig (FcFontSet *set,
*/
if (cache && FcGlobalCacheScanDir (set, dirs, cache, dir, config))
return FcTrue;
#endif
}
/* freed below */
......@@ -246,8 +254,10 @@ FcDirScanConfig (FcFontSet *set,
* Now that the directory has been scanned,
* add the cache entry
*/
#if 0
if (ret && cache)
FcGlobalCacheUpdate (cache, dir, 0, 0);
#endif
return ret;
}
......@@ -266,5 +276,8 @@ FcDirScan (FcFontSet *set,
FcBool
FcDirSave (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
{
#if 0
return FcDirCacheWriteDir (set, dirs, dir);
#endif
return FcTrue;
}
......@@ -116,3 +116,98 @@ FcFontSetClearStatic (void)
{
FcPatternClearStatic();
}
FcBool
FcFontSetRead(int fd, FcConfig * config, FcCache metadata)
{
int i, mz, j;
FcPattern * buf;
lseek(fd, metadata.fontsets_offset, SEEK_SET);
for (i = FcSetSystem; i <= FcSetApplication; i++)
{
if (config->fonts[i])
{
if (config->fonts[i]->nfont > 0 && config->fonts[i]->fonts)
free (config->fonts[i]->fonts);
free (config->fonts[i]);
}
}
for (i = FcSetSystem; i <= FcSetApplication; i++)
{
read(fd, &mz, sizeof(int));
if (mz != FC_CACHE_MAGIC)
continue;
config->fonts[i] = malloc(sizeof(FcFontSet));
if (!config->fonts[i])
return FcFalse;
FcMemAlloc(FC_MEM_FONTSET, sizeof(FcFontSet));
if (read(fd, config->fonts[i], sizeof(FcFontSet)) == -1)
goto bail;
if (config->fonts[i]->sfont > 0)
{
config->fonts[i]->fonts = malloc
(config->fonts[i]->sfont*sizeof(FcPattern *));
buf = malloc (config->fonts[i]->sfont * sizeof(FcPattern));
if (!config->fonts[i]->fonts || !buf)
goto bail;
for (j = 0; j < config->fonts[i]->nfont; j++)
{
config->fonts[i]->fonts[j] = buf+j;
if (read(fd, buf+j, sizeof(FcPattern)) == -1)
goto bail;
}
}
}
return FcTrue;
bail:
for (i = FcSetSystem; i <= FcSetApplication; i++)
{
if (config->fonts[i])
{
if (config->fonts[i]->fonts)
free (config->fonts[i]->fonts);
free(config->fonts[i]);
}
config->fonts[i] = 0;
}
return FcFalse;
}
FcBool
FcFontSetWrite(int fd, FcConfig * config, FcCache * metadata)
{
int c, t, i, j;
int m = FC_CACHE_MAGIC, z = 0;
metadata->fontsets_offset = FcCacheNextOffset(fd);
lseek(fd, metadata->fontsets_offset, SEEK_SET);
for (i = FcSetSystem; i <= FcSetApplication; i++)
{
if (!config->fonts[i])
{
write(fd, &z, sizeof(int));
continue;
}
else
write(fd, &m, sizeof(int));
if ((c = write(fd, config->fonts[i], sizeof(FcFontSet))) == -1)
return FcFalse;
t = c;
if (config->fonts[i]->nfont > 0)
{
for (j = 0; j < config->fonts[i]->nfont; j++)
{
if ((c = write(fd, config->fonts[i]->fonts[j],
sizeof(FcPattern))) == -1)
return FcFalse;
}
}
}
return FcTrue;
}
......@@ -61,6 +61,9 @@ FcInitLoadConfig (void)
if (!config)
return FcFalse;
if (!FcCacheRead(config))
FcCacheForce(FcTrue);
if (!FcConfigParseAndLoad (config, 0, FcTrue))
{
FcConfigDestroy (config);
......
......@@ -259,6 +259,25 @@ typedef struct _FcStrBuf {
int size;
} FcStrBuf;
typedef struct _FcCache {
int magic;
off_t fontsets_offset;
off_t pattern_offset; int pattern_length;
off_t patternelt_offset; int patternelt_length;
off_t valuelist_offset; int valuelist_length;
off_t object_offset; int object_length;
off_t objectcontent_offset; int objectcontent_length;
off_t langsets_offset; int langsets_length;
off_t charsets_offset; int charsets_length;
off_t charset_leaf_offset; int charset_num_sum;
off_t charset_leafidx_offset;
off_t charset_numbers_offset;
off_t matrices_offset; int matrices_length;
off_t strsets_offset; int strsets_length;
off_t strsets_idx_offset; int strsets_idx_length;
off_t strset_buf_offset; int strset_buf_length;
} FcCache;
/*
* To map adobe glyph names to unicode values, a precomputed hash
* table is used
......@@ -312,6 +331,7 @@ typedef struct _FcCaseFold {
* cache which is then rewritten to the users home directory
*/
#define FC_CACHE_MAGIC 0x12345678
#define FC_GLOBAL_CACHE_DIR_HASH_SIZE 37
#define FC_GLOBAL_CACHE_FILE_HASH_SIZE 67
......@@ -429,66 +449,27 @@ typedef struct _FcCharMap FcCharMap;
/* fccache.c */
FcGlobalCache *
FcGlobalCacheCreate (void);
void
FcGlobalCacheDestroy (FcGlobalCache *cache);
FcBool
FcGlobalCacheCheckTime (const FcChar8*file, FcGlobalCacheInfo *info);
int
FcCacheNextOffset(int fd);
void
FcGlobalCacheReferenced (FcGlobalCache *cache,
FcGlobalCacheInfo *info);
FcCacheForce(FcBool force);
void
FcGlobalCacheReferenceSubdir (FcGlobalCache *cache,
const FcChar8 *dir);
FcGlobalCacheDir *
FcGlobalCacheDirGet (FcGlobalCache *cache,
const FcChar8 *dir,
int len,
FcBool create_missing);
FcBool
FcGlobalCacheScanDir (FcFontSet *set,
FcStrSet *dirs,
FcGlobalCache *cache,
const FcChar8 *dir,
FcConfig *config);
FcGlobalCacheFile *
FcGlobalCacheFileGet (FcGlobalCache *cache,
const FcChar8 *file,
int id,
int *count);
void
FcGlobalCacheLoad (FcGlobalCache *cache,
const FcChar8 *cache_file);
FcCacheClearStatic(void);
FcBool
FcGlobalCacheUpdate (FcGlobalCache *cache,
const FcChar8 *file,
int id,
const FcChar8 *name);
FcCachePrepareSerialize(FcConfig * config);
FcBool
FcGlobalCacheSave (FcGlobalCache *cache,
const FcChar8 *cache_file);
FcCacheSerialize (FcConfig * config);
FcBool
FcDirCacheReadDir (FcFontSet *set,
FcStrSet *dirs,
const FcChar8 *dir,
FcConfig *config);
FcCacheRead (FcConfig *config);
FcBool
FcDirCacheWriteDir (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir);
FcCacheWrite (FcConfig * config);
/* fccfg.c */
FcBool
......@@ -553,6 +534,9 @@ FcConfigAcceptFont (FcConfig *config,
FcCharSet *
FcCharSetFreeze (FcCharSet *cs);
FcCharSetPtr
FcCharSetCopyPtr (FcCharSetPtr src);
void
FcCharSetThawAll (void);
......@@ -586,6 +570,12 @@ FcCharSetGetLeaf(const FcCharSet *c, int i);
FcChar16 *
FcCharSetGetNumbers(const FcCharSet *c);
FcBool
FcCharSetRead (int fd, FcCache metadata);
FcBool
FcCharSetWrite (int fd, FcCache *metadata);
/* fcdbg.c */
void
FcValueListPrint (const FcValueListPtr l);
......@@ -668,6 +658,12 @@ FcFontSetPrepareSerialize (FcFontSet * s);
FcBool
FcFontSetSerialize (FcFontSet * s);
FcBool
FcFontSetRead(int fd, FcConfig * config, FcCache metadata);
FcBool
FcFontSetWrite(int fd, FcConfig * config, FcCache * metadata);
/* fcgram.y */
int
FcConfigparse (void);
......@@ -766,6 +762,12 @@ FcLangSetPtrCreateDynamic (FcLangSet *l);
void
FcLangSetPtrDestroy (FcLangSetPtr li);
FcBool
FcLangSetRead (int fd, FcCache metadata);
FcBool
FcLangSetWrite (int fd, FcCache *metadata);
/* fclist.c */
FcBool
......@@ -774,18 +776,6 @@ FcListPatternMatchAny (const FcPattern *p,
/* fcmatch.c */
/* fcmmap.c */
void
FcCacheClearStatic(void);
FcBool
FcCachePrepareSerialize(FcConfig * config);
FcBool
FcCacheSerialize (FcConfig * config);
/* fcname.c */
FcBool
......@@ -824,7 +814,10 @@ FcObjectPtr
FcObjectStaticName (const char *name);
FcBool
FcObjectPrepareSerialize (FcObjectPtr si);
FcObjectRead (int fd, FcCache metadata);
FcBool
FcObjectWrite (int fd, FcCache * metadata);
const char *
FcObjectPtrU (FcObjectPtr p);
......@@ -832,9 +825,6 @@ FcObjectPtrU (FcObjectPtr p);
int
FcObjectPtrCompare (FcObjectPtr a, FcObjectPtr b);
FcObjectPtr
FcObjectPtrCreateDynamic (const char * s);
void
FcObjectPtrDestroy (FcObjectPtr p);
......@@ -865,6 +855,24 @@ FcValueListSerialize(FcValueList *pi);
FcPattern *
FcPatternSerialize (FcPattern * p);
FcBool
FcPatternRead (int fd, FcCache metadata);
FcBool
FcPatternWrite (int fd, FcCache *metadata);
FcBool
FcPatternEltRead (int fd, FcCache metadata);
FcBool
FcPatternEltWrite (int fd, FcCache *metadata);
FcBool
FcValueListRead (int fd, FcCache metadata);
FcBool
FcValueListWrite (int fd, FcCache *metadata);
/* fcrender.c */
/* fcmatrix.c */
......@@ -892,6 +900,12 @@ FcMatrixPtrCreateDynamic (FcMatrix *m);
void
FcMatrixClearStatic (void);
FcBool
FcMatrixWrite (int fd, FcCache *metadata);
FcBool
FcMatrixRead (int fd, FcCache metadata);
/* fcstr.c */
FcStrSet *
FcStrSetPtrU (const FcStrSetPtr set);
......@@ -938,6 +952,12 @@ FcStrBufData (FcStrBuf *buf, const FcChar8 *s, int len);
FcStrSetPtr
FcStrSetSerialize (FcStrSet *set);
FcBool
FcStrSetRead (int fd, FcCache metadata);
FcBool
FcStrSetWrite (int fd, FcCache *metadata);
int
FcStrCmpIgnoreBlanksAndCase (const FcChar8 *s1, const FcChar8 *s2);
......
......@@ -774,3 +774,31 @@ FcLangSetSerialize(FcLangSet *l)
new.u.stat = p;
return new;
}
FcBool
FcLangSetWrite (int fd, FcCache *metadata)
{
metadata->langsets_length = langset_ptr;
metadata->langsets_offset = FcCacheNextOffset(fd);
if (langset_ptr > 0)
{
lseek (fd, metadata->langsets_offset, SEEK_SET);
return write(fd, langsets,
metadata->langsets_length * sizeof(FcLangSet)) != -1;
}
return FcTrue;
}
FcBool
FcLangSetRead (int fd, FcCache metadata)
{
langsets = mmap(NULL,
metadata.langsets_length * sizeof (FcLangSet),
PROT_READ,
MAP_SHARED, fd, metadata.langsets_offset);
if (langsets == MAP_FAILED)
return FcFalse;
langset_count = langset_ptr = metadata.langsets_length;
return FcTrue;
}
......@@ -25,6 +25,7 @@
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/mman.h>
#include "fcint.h"
FcMatrix _id = { 1, 0, 0, 1 };
......@@ -182,3 +183,31 @@ FcMatrixSerialize(FcMatrix *m)
return new;
}
FcBool
FcMatrixRead (int fd, FcCache metadata)
{
matrices = mmap(NULL,
metadata.matrices_length * sizeof (FcMatrix),
PROT_READ,
MAP_SHARED, fd, metadata.matrices_offset);
if (matrices == MAP_FAILED)
return FcFalse;
matrix_count = matrix_ptr = metadata.matrices_length;
return FcTrue;
}
FcBool
FcMatrixWrite (int fd, FcCache *metadata)
{
metadata->matrices_length = matrix_ptr;
metadata->matrices_offset = FcCacheNextOffset(fd);
if (matrix_ptr > 0)
{
lseek(fd, metadata->matrices_offset, SEEK_SET);
return write(fd, matrices,
metadata->matrices_length * sizeof(FcMatrix)) != -1;
}
return FcTrue;
}
This diff is collapsed.
......@@ -25,6 +25,7 @@
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <sys/mman.h>
#include "fcint.h"