* PERFORMANCE OF THIS SOFTWARE.
*/
+#include <fcntl.h>
+#include <sys/mman.h>
+#include <sys/utsname.h>
#include "fcint.h"
-/*
- * POSIX has broken stdio so that getc must do thread-safe locking,
- * this is a serious performance problem for applications doing large
- * amounts of IO with getc (as is done here). If available, use
- * the getc_unlocked varient instead.
- */
-
-#if defined(getc_unlocked) || defined(_IO_getc_unlocked)
-#define GETC(f) getc_unlocked(f)
-#define PUTC(c,f) putc_unlocked(c,f)
-#else
-#define GETC(f) getc(f)
-#define PUTC(c,f) putc(c,f)
-#endif
+#define PAGESIZE 8192
-#define FC_DBG_CACHE_REF 1024
+static FcBool force;
static FcChar8 *
-FcCacheReadString (FILE *f, FcChar8 *dest, int len)
+FcCacheReadString (int fd, FcChar8 *dest, int len)
{
- int c;
+ FcChar8 c;
FcBool escape;
- FcChar8 *d;
int size;
int i;
- while ((c = GETC (f)) != EOF)
- if (c == '"')
- break;
- if (c == EOF)
- return FcFalse;
if (len == 0)
return FcFalse;
size = len;
i = 0;
- d = dest;
escape = FcFalse;
- while ((c = GETC (f)) != EOF)
+ while (read (fd, &c, 1) == 1)
{
if (!escape)
{
}
if (i == size)
{
- FcChar8 *new = malloc (size * 2); /* freed in caller */
- if (!new)
- break;
- memcpy (new, d, size);
- size *= 2;
- if (d != dest)
- free (d);
- d = new;
+ dest[i++] = 0;
+ return dest;
}
- d[i++] = c;
+ dest[i++] = c;
if (c == '\0')
- return d;
+ return dest;
escape = FcFalse;
}
- if (d != dest)
- free (d);
return 0;
}
static FcBool
-FcCacheReadUlong (FILE *f, unsigned long *dest)
-{
- unsigned long t;
- int c;
-
- while ((c = GETC (f)) != EOF)
- {
- if (!isspace (c))
- break;
- }
- if (c == EOF)
- return FcFalse;
- t = 0;
- for (;;)
- {
- if (c == EOF || isspace (c))
- break;
- if (!isdigit (c))
- return FcFalse;
- t = t * 10 + (c - '0');
- c = GETC (f);
- }
- *dest = t;
- return FcTrue;
-}
-
-static FcBool
-FcCacheReadInt (FILE *f, int *dest)
-{
- unsigned long t;
- FcBool ret;
-
- ret = FcCacheReadUlong (f, &t);
- if (ret)
- *dest = (int) t;
- return ret;
-}
-
-static FcBool
-FcCacheReadTime (FILE *f, time_t *dest)
-{
- unsigned long t;
- FcBool ret;
-
- ret = FcCacheReadUlong (f, &t);
- if (ret)
- *dest = (time_t) t;
- return ret;
-}
-
-static FcBool
-FcCacheWriteChars (FILE *f, const FcChar8 *chars)
-{
- FcChar8 c;
- while ((c = *chars++))
- {
- switch (c) {
- case '"':
- case '\\':
- if (PUTC ('\\', f) == EOF)
- return FcFalse;
- /* fall through */
- default:
- if (PUTC (c, f) == EOF)
- return FcFalse;
- }
- }
- return FcTrue;
-}
-
-static FcBool
-FcCacheWriteString (FILE *f, const FcChar8 *string)
-{
-
- if (PUTC ('"', f) == EOF)
- return FcFalse;
- if (!FcCacheWriteChars (f, string))
- return FcFalse;
- if (PUTC ('"', f) == EOF)
- return FcFalse;
- return FcTrue;
-}
-
-static FcBool
-FcCacheWritePath (FILE *f, const FcChar8 *dir, const FcChar8 *file)
+FcCacheWriteString (int fd, const FcChar8 *chars)
{
- if (PUTC ('"', f) == EOF)
- return FcFalse;
- if (dir)
- if (!FcCacheWriteChars (f, dir))
- return FcFalse;
-#ifdef _WIN32
- if (dir &&
- dir[strlen((const char *) dir) - 1] != '/' &&
- dir[strlen((const char *) dir) - 1] != '\\')
- {
- if (!FcCacheWriteChars (f, "\\"))
- return FcFalse;
- }
-#else
- if (dir && dir[strlen((const char *) dir) - 1] != '/')
- if (PUTC ('/', f) == EOF)
- return FcFalse;
-#endif
- if (!FcCacheWriteChars (f, file))
+ if (write (fd, chars, strlen(chars)+1) != strlen(chars)+1)
return FcFalse;
- if (PUTC ('"', f) == EOF)
- return FcFalse;
- return FcTrue;
-}
-
-static FcBool
-FcCacheWriteUlong (FILE *f, unsigned long t)
-{
- int pow;
- unsigned long temp, digit;
-
- temp = t;
- pow = 1;
- while (temp >= 10)
- {
- temp /= 10;
- pow *= 10;
- }
- temp = t;
- while (pow)
- {
- digit = temp / pow;
- if (PUTC ((char) digit + '0', f) == EOF)
- return FcFalse;
- temp = temp - pow * digit;
- pow = pow / 10;
- }
return FcTrue;
}
-static FcBool
-FcCacheWriteInt (FILE *f, int i)
-{
- return FcCacheWriteUlong (f, (unsigned long) i);
-}
-
-static FcBool
-FcCacheWriteTime (FILE *f, time_t t)
-{
- return FcCacheWriteUlong (f, (unsigned long) t);
-}
-
-static FcBool
-FcCacheFontSetAdd (FcFontSet *set,
- FcStrSet *dirs,
- const FcChar8 *dir,
- int dir_len,
- const FcChar8 *file,
- const FcChar8 *name,
- FcConfig *config)
-{
- FcChar8 path_buf[8192], *path;
- int len;
- FcBool ret = FcFalse;
- FcPattern *font;
- FcPattern *frozen;
-
- path = path_buf;
- len = (dir_len + 1 + strlen ((const char *) file) + 1);
- if (len > sizeof (path_buf))
- {
- path = malloc (len); /* freed down below */
- if (!path)
- return FcFalse;
- }
- strncpy ((char *) path, (const char *) dir, dir_len);
-#ifdef _WIN32
- if (dir[dir_len - 1] != '/' && dir[dir_len - 1] != '\\' )
- path[dir_len++] = '\\';
-#else
- if (dir[dir_len - 1] != '/')
- path[dir_len++] = '/';
-#endif
- strcpy ((char *) path + dir_len, (const char *) file);
- if (config && !FcConfigAcceptFilename (config, path))
- ret = FcTrue;
- else if (!FcStrCmp (name, FC_FONT_FILE_DIR))
- {
- if (FcDebug () & FC_DBG_CACHEV)
- printf (" dir cache dir \"%s\"\n", path);
- ret = FcStrSetAdd (dirs, path);
- }
- else if (!FcStrCmp (name, FC_FONT_FILE_INVALID))
- {
- ret = FcTrue;
- }
- else
- {
- font = FcNameParse (name);
- if (font)
- {
- FcChar8 *family;
-
- if (FcDebug () & FC_DBG_CACHEV)
- printf (" dir cache file \"%s\"\n", file);
- ret = FcPatternAddString (font, FC_FILE, path);
- /*
- * Make sure the pattern has the file name as well as
- * already containing at least one family name.
- */
- if (ret &&
- FcPatternGetString (font, FC_FAMILY, 0, &family) == FcResultMatch &&
- (!config || FcConfigAcceptFont (config, font)))
- {
- frozen = FcPatternFreeze (font);
- ret = (frozen != 0);
- if (ret)
- ret = FcFontSetAdd (set, frozen);
- }
- FcPatternDestroy (font);
- }
- }
- if (path != path_buf) free (path);
- return ret;
-
-}
-
-static unsigned int
-FcCacheHash (const FcChar8 *string, int len)
-{
- unsigned int h = 0;
- FcChar8 c;
-
- while (len-- && (c = *string++))
- h = (h << 1) ^ c;
- return h;
-}
-
+#if 0
/*
* Verify the saved timestamp for a file
*/
return i;
}
-FcGlobalCacheDir *
-FcGlobalCacheDirGet (FcGlobalCache *cache,
- const FcChar8 *dir,
- int len,
- FcBool create_missing)
-{
- unsigned int hash = FcCacheHash (dir, len);
- FcGlobalCacheDir *d, **prev;
-
- for (prev = &cache->ents[hash % FC_GLOBAL_CACHE_DIR_HASH_SIZE];
- (d = *prev);
- prev = &(*prev)->next)
- {
- if (d->info.hash == hash && d->len == len &&
- !strncmp ((const char *) d->info.file,
- (const char *) dir, len))
- break;
- }
- if (!(d = *prev))
- {
- int i;
- if (!create_missing)
- return 0;
- d = malloc (sizeof (FcGlobalCacheDir) + len + 1);
- if (!d)
- return 0;
- FcMemAlloc (FC_MEM_CACHE, sizeof (FcGlobalCacheDir) + len + 1);
- d->next = *prev;
- *prev = d;
- d->info.hash = hash;
- d->info.file = (FcChar8 *) (d + 1);
- strncpy ((char *) d->info.file, (const char *) dir, len);
- d->info.file[len] = '\0';
- d->info.time = 0;
- d->info.referenced = FcFalse;
- d->len = len;
- for (i = 0; i < FC_GLOBAL_CACHE_FILE_HASH_SIZE; i++)
- d->ents[i] = 0;
- d->subdirs = 0;
- }
- return d;
-}
-
-static FcGlobalCacheInfo *
-FcGlobalCacheDirAdd (FcGlobalCache *cache,
- const FcChar8 *dir,
- time_t time,
- FcBool replace,
- FcBool create_missing)
-{
- FcGlobalCacheDir *d;
- FcFilePathInfo i;
- FcGlobalCacheSubdir *subdir;
- FcGlobalCacheDir *parent;
-
- i = FcFilePathInfoGet (dir);
- parent = FcGlobalCacheDirGet (cache, i.dir, i.dir_len, create_missing);
- /*
- * Tricky here -- directories containing fonts.cache-1 files
- * need entries only when the parent doesn't have a cache file.
- * That is, when the parent already exists in the cache, is
- * referenced and has a "real" timestamp. The time of 0 is
- * special and marks directories which got stuck in the
- * global cache for this very reason. Yes, it could
- * use a separate boolean field, and probably should.
- */
- if (!parent || (!create_missing &&
- (!parent->info.referenced ||
- (parent->info.time == 0))))
- return 0;
- /*
- * Add this directory to the cache
- */
- d = FcGlobalCacheDirGet (cache, dir, strlen ((const char *) dir), FcTrue);
- if (!d)
- return 0;
- d->info.time = time;
- /*
- * Add this directory to the subdirectory list of the parent
- */
- subdir = malloc (sizeof (FcGlobalCacheSubdir));
- if (!subdir)
- return 0;
- FcMemAlloc (FC_MEM_CACHE, sizeof (FcGlobalCacheSubdir));
- subdir->ent = d;
- subdir->next = parent->subdirs;
- parent->subdirs = subdir;
- return &d->info;
-}
-
-static void
-FcGlobalCacheDirDestroy (FcGlobalCacheDir *d)
-{
- FcGlobalCacheFile *f, *next;
- int h;
- FcGlobalCacheSubdir *s, *nexts;
-
- for (h = 0; h < FC_GLOBAL_CACHE_FILE_HASH_SIZE; h++)
- for (f = d->ents[h]; f; f = next)
- {
- next = f->next;
- FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheFile) +
- strlen ((char *) f->info.file) + 1 +
- strlen ((char *) f->name) + 1);
- free (f);
- }
- for (s = d->subdirs; s; s = nexts)
- {
- nexts = s->next;
- FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheSubdir));
- free (s);
- }
- FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheDir) + d->len + 1);
- free (d);
-}
-
-/*
- * If the parent is in the global cache and referenced, add
- * an entry for 'dir' to the global cache. This is used
- * for directories with fonts.cache files
- */
-
-void
-FcGlobalCacheReferenceSubdir (FcGlobalCache *cache,
- const FcChar8 *dir)
-{
- FcGlobalCacheInfo *info;
- info = FcGlobalCacheDirAdd (cache, dir, 0, FcFalse, FcFalse);
- if (info && !info->referenced)
- {
- info->referenced = FcTrue;
- cache->referenced++;
- }
-}
-
-/*
- * Check to see if the global cache contains valid data for 'dir'.
- * If so, scan the global cache for files and directories in 'dir'.
- * else, return False.
- */
-FcBool
-FcGlobalCacheScanDir (FcFontSet *set,
- FcStrSet *dirs,
- FcGlobalCache *cache,
- const FcChar8 *dir,
- FcConfig *config)
-{
- FcGlobalCacheDir *d = FcGlobalCacheDirGet (cache, dir,
- strlen ((const char *) dir),
- FcFalse);
- FcGlobalCacheFile *f;
- int h;
- int dir_len;
- FcGlobalCacheSubdir *subdir;
- FcBool any_in_cache = FcFalse;
-
- if (FcDebug() & FC_DBG_CACHE)
- printf ("FcGlobalCacheScanDir %s\n", dir);
-
- if (!d)
- {
- if (FcDebug () & FC_DBG_CACHE)
- printf ("\tNo dir cache entry\n");
- return FcFalse;
- }
-
- /*
- * See if the timestamp recorded in the global cache
- * matches the directory time, if not, return False
- */
- if (!FcGlobalCacheCheckTime (d->info.file, &d->info))
- {
- if (FcDebug () & FC_DBG_CACHE)
- printf ("\tdir cache entry time mismatch\n");
- return FcFalse;
- }
-
- /*
- * Add files from 'dir' to the fontset
- */
- dir_len = strlen ((const char *) dir);
- for (h = 0; h < FC_GLOBAL_CACHE_FILE_HASH_SIZE; h++)
- for (f = d->ents[h]; f; f = f->next)
- {
- if (FcDebug() & FC_DBG_CACHEV)
- printf ("FcGlobalCacheScanDir add file %s\n", f->info.file);
- any_in_cache = FcTrue;
- if (!FcCacheFontSetAdd (set, dirs, dir, dir_len,
- f->info.file, f->name, config))
- {
- cache->broken = FcTrue;
- return FcFalse;
- }
- FcGlobalCacheReferenced (cache, &f->info);
- }
- /*
- * Add directories in 'dir' to 'dirs'
- */
- for (subdir = d->subdirs; subdir; subdir = subdir->next)
- {
- FcFilePathInfo info = FcFilePathInfoGet (subdir->ent->info.file);
-
- any_in_cache = FcTrue;
- if (!FcCacheFontSetAdd (set, dirs, dir, dir_len,
- info.base, FC_FONT_FILE_DIR, config))
- {
- cache->broken = FcTrue;
- return FcFalse;
- }
- FcGlobalCacheReferenced (cache, &subdir->ent->info);
- }
-
- FcGlobalCacheReferenced (cache, &d->info);
-
- /*
- * To recover from a bug in previous versions of fontconfig,
- * return FcFalse if no entries in the cache were found
- * for this directory. This will cause any empty directories
- * to get rescanned every time fontconfig is initialized. This
- * might get removed at some point when the older cache files are
- * presumably fixed.
- */
- return any_in_cache;
-}
-
-/*
- * Locate the cache entry for a particular file
- */
-FcGlobalCacheFile *
-FcGlobalCacheFileGet (FcGlobalCache *cache,
- const FcChar8 *file,
- int id,
- int *count)
-{
- FcFilePathInfo i = FcFilePathInfoGet (file);
- FcGlobalCacheDir *d = FcGlobalCacheDirGet (cache, i.dir,
- i.dir_len, FcFalse);
- FcGlobalCacheFile *f, *match = 0;
- int max = -1;
-
- if (!d)
- return 0;
- for (f = d->ents[i.base_hash % FC_GLOBAL_CACHE_FILE_HASH_SIZE]; f; f = f->next)
- {
- if (f->info.hash == i.base_hash &&
- !strcmp ((const char *) f->info.file, (const char *) i.base))
- {
- if (f->id == id)
- match = f;
- if (f->id > max)
- max = f->id;
- }
- }
- if (count)
- *count = max + 1;
- return match;
-}
-
-/*
- * Add a file entry to the cache
- */
-static FcGlobalCacheInfo *
-FcGlobalCacheFileAdd (FcGlobalCache *cache,
- const FcChar8 *path,
- int id,
- time_t time,
- const FcChar8 *name,
- FcBool replace)
-{
- FcFilePathInfo i = FcFilePathInfoGet (path);
- FcGlobalCacheDir *d = FcGlobalCacheDirGet (cache, i.dir,
- i.dir_len, FcTrue);
- FcGlobalCacheFile *f, **prev;
- int size;
-
- if (!d)
- return 0;
- for (prev = &d->ents[i.base_hash % FC_GLOBAL_CACHE_FILE_HASH_SIZE];
- (f = *prev);
- prev = &(*prev)->next)
- {
- if (f->info.hash == i.base_hash &&
- f->id == id &&
- !strcmp ((const char *) f->info.file, (const char *) i.base))
- {
- break;
- }
- }
- if (*prev)
- {
- if (!replace)
- return 0;
-
- f = *prev;
- if (f->info.referenced)
- cache->referenced--;
- *prev = f->next;
- FcMemFree (FC_MEM_CACHE, sizeof (FcGlobalCacheFile) +
- strlen ((char *) f->info.file) + 1 +
- strlen ((char *) f->name) + 1);
- free (f);
- }
- size = (sizeof (FcGlobalCacheFile) +
- strlen ((char *) i.base) + 1 +
- strlen ((char *) name) + 1);
- f = malloc (size);
- if (!f)
- return 0;
- FcMemAlloc (FC_MEM_CACHE, size);
- f->next = *prev;
- *prev = f;
- f->info.hash = i.base_hash;
- f->info.file = (FcChar8 *) (f + 1);
- f->info.time = time;
- f->info.referenced = FcFalse;
- f->id = id;
- f->name = f->info.file + strlen ((char *) i.base) + 1;
- strcpy ((char *) f->info.file, (const char *) i.base);
- strcpy ((char *) f->name, (const char *) name);
- return &f->info;
-}
-
FcGlobalCache *
FcGlobalCacheCreate (void)
{
bail0:
return FcFalse;
}
+#endif
-FcBool
-FcDirCacheValid (const FcChar8 *dir)
+/*
+ * Find the next presumably-mmapable offset after the current file
+ * pointer.
+ */
+int
+FcCacheNextOffset(int fd)
{
- FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
- struct stat file_stat, dir_stat;
+ off_t w;
+ w = lseek(fd, 0, SEEK_END);
- if (stat ((char *) dir, &dir_stat) < 0)
- {
- FcStrFree (cache_file);
- return FcFalse;
- }
- if (stat ((char *) cache_file, &file_stat) < 0)
- {
- FcStrFree (cache_file);
- return FcFalse;
- }
- FcStrFree (cache_file);
- /*
- * If the directory has been modified more recently than
- * the cache file, the cache is not valid
- */
- if (dir_stat.st_mtime - file_stat.st_mtime > 0)
- return FcFalse;
- return FcTrue;
+ if (w % PAGESIZE == 0)
+ return w;
+ else
+ return ((w / PAGESIZE)+1)*PAGESIZE;
}
-FcBool
-FcDirCacheReadDir (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir, FcConfig *config)
+/* will go away once we use config->cache */
+#define CACHE_DEFAULT_TMPDIR "/tmp"
+#define CACHE_DEFAULT_NAME "/fontconfig-mmap"
+static char *
+FcCacheFilename(void)
{
- FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
- FILE *f;
- FcChar8 *base;
- int id;
- int dir_len;
- FcChar8 file_buf[8192], *file;
- FcChar8 name_buf[8192], *name;
- FcBool ret = FcFalse;
+ struct utsname b;
+ static char * name = 0;
- if (!cache_file)
- goto bail0;
-
- if (FcDebug () & FC_DBG_CACHE)
- printf ("FcDirCacheReadDir cache_file \"%s\"\n", cache_file);
-
- f = fopen ((char *) cache_file, "r");
- if (!f)
- {
- if (FcDebug () & FC_DBG_CACHE)
- printf (" no cache file\n");
- goto bail1;
- }
+ if (name)
+ return name;
- if (!FcDirCacheValid (dir))
- {
- if (FcDebug () & FC_DBG_CACHE)
- printf (" cache file older than directory\n");
- goto bail2;
- }
-
- base = (FcChar8 *) strrchr ((char *) cache_file, '/');
- if (!base)
- goto bail2;
- base++;
- dir_len = base - cache_file;
-
- file = 0;
- name = 0;
- while ((file = FcCacheReadString (f, file_buf, sizeof (file_buf))) &&
- FcCacheReadInt (f, &id) &&
- (name = FcCacheReadString (f, name_buf, sizeof (name_buf))))
+ if (uname(&b) == -1)
+ name = CACHE_DEFAULT_NAME;
+ else
{
- if (!FcCacheFontSetAdd (set, dirs, cache_file, dir_len,
- file, name, config))
- goto bail3;
- if (file != file_buf)
- free (file);
- if (name != name_buf)
- free (name);
- file = name = 0;
- }
- if (FcDebug () & FC_DBG_CACHE)
- printf (" cache loaded\n");
-
- ret = FcTrue;
-bail3:
- if (file && file != file_buf)
- free (file);
- if (name && name != name_buf)
- free (name);
-bail2:
- fclose (f);
-bail1:
- FcStrFree (cache_file);
-bail0:
- return ret;
-}
+ char * tmpname = getenv("TMPDIR");
+ char * logname = getenv("LOGNAME");
+ if (!tmpname)
+ tmpname = CACHE_DEFAULT_TMPDIR;
-/*
- * return the path from the directory containing 'cache' to 'file'
- */
-
-static const FcChar8 *
-FcFileBaseName (const FcChar8 *cache, const FcChar8 *file)
-{
- const FcChar8 *cache_slash;
-
- cache_slash = FcStrLastSlash (cache);
- if (cache_slash && !strncmp ((const char *) cache, (const char *) file,
- (cache_slash + 1) - cache))
- return file + ((cache_slash + 1) - cache);
- return file;
-}
-
-FcBool
-FcDirCacheWriteDir (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
-{
- FcChar8 *cache_file = FcStrPlus (dir, (FcChar8 *) "/" FC_DIR_CACHE_FILE);
- FcPattern *font;
- FILE *f;
- FcChar8 *name;
- const FcChar8 *file, *base;
- int n;
- int id;
- FcBool ret;
- FcStrList *list;
-
- if (!cache_file)
- goto bail0;
- if (FcDebug () & FC_DBG_CACHE)
- printf ("FcDirCacheWriteDir cache_file \"%s\"\n", cache_file);
-
- f = fopen ((char *) cache_file, "w");
- if (!f)
- {
- if (FcDebug () & FC_DBG_CACHE)
- printf (" can't create \"%s\"\n", cache_file);
- goto bail1;
- }
-
- list = FcStrListCreate (dirs);
- if (!list)
- goto bail2;
-
- while ((dir = FcStrListNext (list)))
- {
- base = FcFileBaseName (cache_file, dir);
- if (!FcCacheWriteString (f, base))
- goto bail3;
- if (PUTC (' ', f) == EOF)
- goto bail3;
- if (!FcCacheWriteInt (f, 0))
- goto bail3;
- if (PUTC (' ', f) == EOF)
- goto bail3;
- if (!FcCacheWriteString (f, FC_FONT_FILE_DIR))
- goto bail3;
- if (PUTC ('\n', f) == EOF)
- goto bail3;
+ name = malloc(strlen(CACHE_DEFAULT_NAME) +
+ strlen(tmpname) +
+ (logname ? strlen(logname) : 0) + 5);
+ strcpy(name, tmpname);
+ strcat(name, CACHE_DEFAULT_NAME);
+ strcat(name, "-");
+ strcat(name, logname ? logname : "");
}
-
- for (n = 0; n < set->nfont; n++)
- {
- font = set->fonts[n];
- if (FcPatternGetString (font, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
- goto bail3;
- base = FcFileBaseName (cache_file, file);
- if (FcPatternGetInteger (font, FC_INDEX, 0, &id) != FcResultMatch)
- goto bail3;
- if (FcDebug () & FC_DBG_CACHEV)
- printf (" write file \"%s\"\n", base);
- if (!FcCacheWriteString (f, base))
- goto bail3;
- if (PUTC (' ', f) == EOF)
- goto bail3;
- if (!FcCacheWriteInt (f, id))
- goto bail3;
- if (PUTC (' ', f) == EOF)
- goto bail3;
- name = FcNameUnparse (font);
- if (!name)
- goto bail3;
- ret = FcCacheWriteString (f, name);
- FcStrFree (name);
- if (!ret)
- goto bail3;
- if (PUTC ('\n', f) == EOF)
- goto bail3;
- }
-
- FcStrListDone (list);
-
- if (fclose (f) == EOF)
- goto bail1;
-
- FcStrFree (cache_file);
-
- if (FcDebug () & FC_DBG_CACHE)
- printf (" cache written\n");
- return FcTrue;
-
-bail3:
- FcStrListDone (list);
-bail2:
- fclose (f);
-bail1:
- unlink ((char *) cache_file);
- FcStrFree (cache_file);
-bail0:
- return FcFalse;
+ return name;
}
+/*
+ * Wipe out static state.
+ */
void
FcCacheClearStatic()
{
FcLangSetClearStatic();
}
+/*
+ * Trigger the counting phase: this tells us how much to allocate.
+ */
FcBool
FcCachePrepareSerialize (FcConfig * config)
{
return FcTrue;
}
+/* allocate and populate static structures */
FcBool
FcCacheSerialize (FcConfig * config)
{
return FcFalse;
return FcTrue;
}
+
+/* get the current arch name */
+/* caller is responsible for freeing returned pointer */
+static char *
+FcCacheGetCurrentArch (void)
+{
+ struct utsname b;
+ char * current_arch_machine_name;
+
+ if (uname(&b) == -1)
+ return FcFalse;
+ current_arch_machine_name = strdup(b.machine);
+ /* if (getenv ("FAKE_ARCH")) // testing purposes
+ current_arch_machine_name = strdup(getenv("FAKE_ARCH")); */
+ return current_arch_machine_name;
+}
+
+/* return the address of the segment for the provided arch,
+ * or -1 if arch not found */
+static off_t
+FcCacheSkipToArch (int fd, const char * arch)
+{
+ char candidate_arch_machine_name[64], bytes_to_skip[7];
+ off_t current_arch_start = 0;
+
+ /* skip arches that are not the current arch */
+ while (1)
+ {
+ long bs;
+
+ lseek (fd, current_arch_start, SEEK_SET);
+ if (FcCacheReadString (fd, candidate_arch_machine_name,
+ sizeof (candidate_arch_machine_name)) == 0)
+ break;
+ if (FcCacheReadString (fd, bytes_to_skip, 7) == 0)
+ break;
+ bs = a64l(bytes_to_skip);
+ if (bs == 0)
+ break;
+
+ if (strcmp (candidate_arch_machine_name, arch)==0)
+ break;
+ current_arch_start += bs;
+ }
+
+ if (strcmp (candidate_arch_machine_name, arch)!=0)
+ return -1;
+
+ return current_arch_start;
+}
+
+/* Cuts out the segment at the file pointer (moves everything else
+ * down to cover it), and leaves the file pointer at the end of the
+ * file. */
+#define BUF_SIZE 8192
+
+static FcBool
+FcCacheMoveDown (int fd, off_t start)
+{
+ char * buf = malloc (BUF_SIZE);
+ char candidate_arch_machine_name[64], bytes_to_skip[7];
+ long bs; off_t pos;
+ int c, bytes_skipped;
+
+ if (!buf)
+ return FcFalse;
+
+ lseek (fd, start, SEEK_SET);
+ if (FcCacheReadString (fd, candidate_arch_machine_name,
+ sizeof (candidate_arch_machine_name)) == 0)
+ goto done;
+ if (FcCacheReadString (fd, bytes_to_skip, 7) == 0)
+ goto done;
+
+ bs = a64l(bytes_to_skip);
+ if (bs == 0)
+ goto done;
+
+ bytes_skipped = 0;
+ do
+ {
+ lseek (fd, start+bs+bytes_skipped, SEEK_SET);
+ if ((c = read (fd, buf, BUF_SIZE)) <= 0)
+ break;
+ lseek (fd, start+bytes_skipped, SEEK_SET);
+ if (write (fd, buf, c) < 0)
+ goto bail;
+ bytes_skipped += c;
+ }
+ while (c > 0);
+ lseek (fd, start+bytes_skipped, SEEK_SET);
+
+ done:
+ free (buf);
+ return FcTrue;
+
+ bail:
+ free (buf);
+ return FcFalse;
+}
+
+/* read serialized state from the cache file */
+FcBool
+FcCacheRead (FcConfig *config)
+{
+ int fd, i;
+ FcCache metadata;
+ char * current_arch_machine_name;
+ char candidate_arch_machine_name[64], bytes_in_block[7];
+ off_t current_arch_start = 0;
+
+ if (force)
+ return FcFalse;
+
+ fd = open(FcCacheFilename(), O_RDONLY);
+ if (fd == -1)
+ return FcFalse;
+
+ current_arch_machine_name = FcCacheGetCurrentArch();
+ current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
+ if (current_arch_start < 0)
+ goto bail;
+
+ lseek (fd, current_arch_start, SEEK_SET);
+ if (FcCacheReadString (fd, candidate_arch_machine_name,
+ sizeof (candidate_arch_machine_name)) == 0)
+ goto bail;
+ if (FcCacheReadString (fd, bytes_in_block, 7) == 0)
+ goto bail;
+
+ // sanity check for endianness issues
+ read(fd, &metadata, sizeof(FcCache));
+ if (metadata.magic != FC_CACHE_MAGIC)
+ goto bail;
+
+ if (!FcObjectRead(fd, metadata)) goto bail1;
+ if (!FcStrSetRead(fd, metadata)) goto bail1;
+ if (!FcCharSetRead(fd, metadata)) goto bail1;
+ if (!FcMatrixRead(fd, metadata)) goto bail1;
+ if (!FcLangSetRead(fd, metadata)) goto bail1;
+ if (!FcValueListRead(fd, metadata)) goto bail1;
+ if (!FcPatternEltRead(fd, metadata)) goto bail1;
+ if (!FcPatternRead(fd, metadata)) goto bail1;
+ if (!FcFontSetRead(fd, config, metadata)) goto bail1;
+ close(fd);
+ free (current_arch_machine_name);
+ return FcTrue;
+
+ bail1:
+ for (i = FcSetSystem; i <= FcSetApplication; i++)
+ config->fonts[i] = 0;
+ close(fd);
+ bail:
+ free (current_arch_machine_name);
+ return FcFalse;
+}
+
+/* write serialized state to the cache file */
+FcBool
+FcCacheWrite (FcConfig * config)
+{
+ int fd;
+ FcCache metadata;
+ off_t current_arch_start = 0, truncate_to;
+ char * current_arch_machine_name, bytes_written[7] = "dedbef";
+
+ if (!FcCachePrepareSerialize (config))
+ return FcFalse;
+
+ if (!FcCacheSerialize (config))
+ return FcFalse;
+
+ fd = open(FcCacheFilename(), O_RDWR | O_CREAT, 0666);
+ if (fd == -1)
+ return FcFalse;
+
+ current_arch_machine_name = FcCacheGetCurrentArch();
+ current_arch_start = FcCacheSkipToArch(fd, current_arch_machine_name);
+ if (current_arch_start < 0)
+ current_arch_start = FcCacheNextOffset (fd);
+
+ if (!FcCacheMoveDown(fd, current_arch_start))
+ goto bail;
+
+ current_arch_start = lseek(fd, 0, SEEK_CUR);
+ if (ftruncate (fd, current_arch_start) == -1)
+ goto bail;
+
+ /* reserve space for arch, count & metadata */
+ if (!FcCacheWriteString (fd, current_arch_machine_name))
+ goto bail;
+ if (!FcCacheWriteString (fd, bytes_written))
+ goto bail;
+ memset (&metadata, 0, sizeof(FcCache));
+ metadata.magic = FC_CACHE_MAGIC;
+ write(fd, &metadata, sizeof(FcCache));
+
+ if (!FcFontSetWrite(fd, config, &metadata)) goto bail;
+ if (!FcPatternWrite(fd, &metadata)) goto bail;
+ if (!FcPatternEltWrite(fd, &metadata)) goto bail;
+ if (!FcValueListWrite(fd, &metadata)) goto bail;
+ if (!FcLangSetWrite(fd, &metadata)) goto bail;
+ if (!FcCharSetWrite(fd, &metadata)) goto bail;
+ if (!FcMatrixWrite(fd, &metadata)) goto bail;
+ if (!FcStrSetWrite(fd, &metadata)) goto bail;
+ if (!FcObjectWrite(fd, &metadata)) goto bail;
+
+ /* now write the address of the next offset */
+ truncate_to = FcCacheNextOffset(fd) - current_arch_start;
+ lseek(fd, current_arch_start + strlen(current_arch_machine_name)+1,
+ SEEK_SET);
+ strcpy (bytes_written, l64a(truncate_to));
+ if (!FcCacheWriteString (fd, bytes_written))
+ goto bail;
+
+ /* now rewrite metadata & truncate file */
+ if (write(fd, &metadata, sizeof(FcCache)) != sizeof (FcCache))
+ goto bail;
+ if (ftruncate (fd, current_arch_start + truncate_to) == -1)
+ goto bail;
+
+ close(fd);
+ return FcTrue;
+
+ bail:
+ free (current_arch_machine_name);
+ return FcFalse;
+}
+
+/* if true, ignore the cache file */
+void
+FcCacheForce (FcBool f)
+{
+ force = f;
+}
v.type = FcTypeVoid;
break;
case FcTypeCharSet:
- v.u.ci = FcCharSetPtrCreateDynamic
- (FcCharSetCopy (FcCharSetPtrU(v.u.ci)));
+ v.u.ci = FcCharSetCopyPtr (v.u.ci);
if (!FcCharSetPtrU(v.u.ci))
v.type = FcTypeVoid;
break;
switch (pei.storage)
{
case FcStorageStatic:
- if (pei.u.stat == 0) return 0;
return &fcpatternelts[pei.u.stat];
case FcStorageDynamic:
return pei.u.dyn;
fcvaluelist_count = 0;
}
+static FcBool
+FcObjectPrepareSerialize (FcObjectPtr si);
static FcObjectPtr
FcObjectSerialize (FcObjectPtr si);
fcpatternelt_ptr = 0;
}
- p = FcPatternCreate();
+ p = &fcpatterns[fcpattern_ptr++];
elts = fcpatternelt_ptr;
nep = &fcpatternelts[elts];
if (!nep)
return FcFalse;
+
fcpatternelt_ptr += old->num;
-
+
for (e = FcPatternEltU(old->elts), i=0; i < old->num; i++, e++)
{
v = e->values;
}
nep[i].values = nv_head;
- nep[i].object = FcObjectSerialize
- (FcObjectStaticName(FcObjectPtrU(e->object)));
+ nep[i].object = FcObjectSerialize (e->object);
}
-
+
+ p->elts = old->elts;
p->elts = FcPatternEltPtrCreateStatic(elts);
p->size = old->num;
+ p->num = old->num;
p->ref = FC_REF_CONSTANT;
return p;
free (fcpatterns);
bail:
return 0;
- }
+}
+
+FcBool
+FcPatternRead (int fd, FcCache metadata)
+{
+ fcpatterns = mmap(NULL,
+ metadata.pattern_length * sizeof (FcPattern),
+ PROT_READ,
+ MAP_SHARED, fd, metadata.pattern_offset);
+ if (fcpatterns == MAP_FAILED)
+ return FcFalse;
+ fcpattern_count = fcpattern_ptr = metadata.pattern_length;
+
+ return FcTrue;
+}
+
+FcBool
+FcPatternWrite (int fd, FcCache *metadata)
+{
+ int c = fcpattern_ptr;
+ off_t w = FcCacheNextOffset(fd);
+
+ metadata->pattern_offset = w;
+ metadata->pattern_length = c;
+
+ if (c > 0)
+ {
+ lseek(fd, w, SEEK_SET);
+ return write(fd, fcpatterns, c*sizeof(FcPattern)) != -1;
+ }
+ return FcTrue;
+}
+
+FcBool
+FcPatternEltRead (int fd, FcCache metadata)
+{
+ fcpatternelts = mmap(NULL,
+ metadata.patternelt_length * sizeof (FcPatternElt),
+ PROT_READ,
+ MAP_SHARED, fd, metadata.patternelt_offset);
+ if (fcpatternelts == MAP_FAILED)
+ return FcFalse;
+ fcpatternelt_count = fcpatternelt_ptr = metadata.patternelt_length;
+
+ return FcTrue;
+}
+
+FcBool
+FcPatternEltWrite (int fd, FcCache *metadata)
+{
+ int c = fcpatternelt_ptr;
+ off_t w = FcCacheNextOffset(fd);
+
+ metadata->patternelt_offset = w;
+ metadata->patternelt_length = c;
+
+ if (c > 0)
+ {
+ lseek(fd, w, SEEK_SET);
+ return write(fd, fcpatternelts, c*sizeof(FcPatternElt)) != -1;
+ }
+ return FcTrue;
+}
FcValueListPtr
FcValueListSerialize(FcValueList *pi)
switch (v->type)
{
case FcTypeString:
- if (FcObjectPtrU(v->u.si))
+ /* this departs from the usual convention of dereferencing
+ * foo before serialization; FcObjectSerialize does the
+ * translation itself. */
+ /* also, v->u.si is 0 iff the string is null. */
+ /* also, have to update the old pi */
+ if (v->u.si)
{
- FcObjectPtr si =
- FcObjectSerialize(FcObjectStaticName(FcObjectPtrU(v->u.si)));
- if (!FcObjectPtrU(v->u.si))
+ FcObjectPtr si = FcObjectSerialize(v->u.si);
+ if (!FcObjectPtrU(si))
return FcValueListPtrCreateDynamic(pi);
v->u.si = si;
+ pi->value.u.si = si;
}
break;
case FcTypeMatrix:
if (FcCharSetPtrU(v->u.ci))
{
FcCharSetPtr ci = FcCharSetSerialize(FcCharSetPtrU(v->u.ci));
- if (!FcCharSetPtrU(v->u.ci))
+ if (!FcCharSetPtrU(ci))
return FcValueListPtrCreateDynamic(pi);
v->u.ci = ci;
}
if (FcLangSetPtrU(v->u.li))
{
FcLangSetPtr li = FcLangSetSerialize(FcLangSetPtrU(v->u.li));
- if (!FcLangSetPtrU(v->u.li))
+ if (!FcLangSetPtrU(li))
return FcValueListPtrCreateDynamic(pi);
v->u.li = li;
}
return new;
}
+FcBool
+FcValueListRead (int fd, FcCache metadata)
+{
+ fcvaluelists = mmap(NULL,
+ metadata.valuelist_length * sizeof (FcValueList),
+ PROT_READ,
+ MAP_SHARED, fd, metadata.valuelist_offset);
+ if (fcvaluelists == MAP_FAILED)
+ return FcFalse;
+ fcvaluelist_count = fcvaluelist_ptr = metadata.valuelist_length;
+
+ return FcTrue;
+}
+
+FcBool
+FcValueListWrite (int fd, FcCache *metadata)
+{
+ metadata->valuelist_offset = FcCacheNextOffset(fd);
+ metadata->valuelist_length = fcvaluelist_ptr;
+
+ if (fcvaluelist_ptr > 0)
+ {
+ lseek(fd, metadata->valuelist_offset, SEEK_SET);
+ return write(fd, fcvaluelists,
+ fcvaluelist_ptr * sizeof(FcValueList)) != -1;
+ }
+ return FcTrue;
+}
+
FcValueList *
FcValueListPtrU (FcValueListPtr pi)
{
switch (pi.storage)
{
case FcStorageStatic:
- if (pi.u.stat == 0) return 0;
return &fcvaluelists[pi.u.stat];
case FcStorageDynamic:
return pi.u.dyn;
objectptr_alloc = s;
}
- size = sizeof (struct objectBucket) + strlen (name) + 1;
+ size = sizeof (struct objectBucket) + sizeof (char *);
b = malloc (size);
if (!b)
return 0;
const char *
FcObjectPtrU (FcObjectPtr si)
{
+ if (si == 0)
+ return 0;
+
if (objectptr_indices[si] > 0)
return &objectcontent_static_buf[objectptr_indices[si]];
else
/* Hmm. This will have a terrible effect on the memory size,
* because the mmapped strings now get reallocated on the heap.
* Is it all worth it? (Of course, the Serialization codepath is
- * not problematic.) */
+ * not problematic, because the program quits just afterwards.) */
static FcBool
FcObjectPtrConvertToStatic(FcBool renumber)
{
new_indices = malloc (active_count * sizeof(int));
if (!new_indices)
goto bail2;
+ new_indices[0] = 0;
+ new_static_buf[0] = 0;
FcMemAlloc (FC_MEM_STATICSTR, new_static_bytes);
FcMemFree (FC_MEM_STATICSTR, objectptr_count * sizeof (int));
int n = FcObjectStaticName(fixed_length_buf+i*(longest_string+1));
if (renumber)
{
- object_old_id_to_new[n] = i;
- new_indices[i] = p-new_static_buf;
+ object_old_id_to_new[n] = i+1;
+ new_indices[i+1] = p-new_static_buf;
}
else
new_indices[n] = p-new_static_buf;
object_old_id_to_new = 0;
}
-static FcObjectPtr
-FcObjectSerialize (FcObjectPtr si)
-{
- if (objectptr_first_serialization)
- if (!FcObjectPtrConvertToStatic(FcTrue))
- return 0;
-
- return object_old_id_to_new[si];
-}
-
/* In the pre-serialization phase, mark the used strings with
* -1 in the mapping array. */
/* The first call to the serialization phase assigns actual
* static indices to the strings (sweep). */
-FcBool
+static FcBool
FcObjectPrepareSerialize (FcObjectPtr si)
{
if (object_old_id_to_new == 0)
return FcFalse;
}
+static FcObjectPtr
+FcObjectSerialize (FcObjectPtr si)
+{
+ if (objectptr_first_serialization)
+ if (!FcObjectPtrConvertToStatic(FcTrue))
+ return 0;
+
+ return object_old_id_to_new[si];
+}
+
+FcBool
+FcObjectRead (int fd, FcCache metadata)
+{
+ /* do we have to merge strings?
+ * it's possible to merge dynamic strings, as long as we only store
+ * static strings to disk and as long as all static strings have lower
+ * ids than any dynamic strings. */
+
+ objectcontent_dynamic_count = 1;
+ objectcontent_dynamic_alloc = 0;
+ objectcontent_dynamic = 0;
+ objectcontent_dynamic_refcount = 0;
+
+ /* well, we do need to allocate dynamic strings all the time,
+ * so this would just have to be converted. It takes 1.4k on
+ * my system. - PL */
+/* objectptr_indices = mmap(NULL, */
+/* metadata.object_length * sizeof (int), */
+/* PROT_READ, */
+/* MAP_SHARED, fd, metadata.object_offset); */
+/* if (objectptr_indices == MAP_FAILED) */
+/* goto bail; */
+
+ objectptr_count = metadata.object_length;
+ objectptr_alloc = metadata.object_length;
+ objectptr_indices = malloc (metadata.object_length * sizeof (int));
+ if (!objectptr_indices)
+ goto bail;
+ FcMemAlloc (FC_MEM_STATICSTR, metadata.object_length * sizeof (int));
+ lseek (fd, metadata.object_offset, SEEK_SET);
+ read (fd, objectptr_indices, metadata.object_length * sizeof (int));
+
+ objectcontent_static_buf =
+ mmap(NULL,
+ metadata.objectcontent_length * sizeof (char),
+ PROT_READ,
+ MAP_SHARED, fd, metadata.objectcontent_offset);
+ if (objectptr_indices == MAP_FAILED)
+ goto bail1;
+ objectcontent_static_bytes = metadata.objectcontent_length;
+
+ FcObjectRebuildStaticNameHashtable ();
+
+ return FcTrue;
+
+ bail1:
+ /*munmap(objectptr_indices, metadata.object_length * sizeof(int));*/
+ free (objectptr_indices);
+ bail:
+ return FcFalse;
+}
+
+FcBool
+FcObjectWrite (int fd, FcCache * metadata)
+{
+ /* there should be no dynamic strings:
+ * serialize ought to have zapped 'em. */
+ if (objectcontent_dynamic_alloc)
+ return FcFalse;
+
+ metadata->object_length = objectptr_count;
+ metadata->object_offset = FcCacheNextOffset(fd);
+ lseek(fd, metadata->object_offset, SEEK_SET);
+ if (write (fd, objectptr_indices,
+ metadata->object_length * sizeof (int)) == -1)
+ return FcFalse;
+
+ metadata->objectcontent_length = objectcontent_static_bytes;
+ metadata->objectcontent_offset = FcCacheNextOffset(fd);
+ lseek(fd, metadata->objectcontent_offset, SEEK_SET);
+ if (write (fd, objectcontent_static_buf,
+ metadata->objectcontent_length * sizeof (char)) == -1)
+ return FcFalse;
+
+ return FcTrue;
+}
+
static void
FcObjectStaticNameFini (void)
{