X-Git-Url: https://git.wh0rd.org/?a=blobdiff_plain;f=src%2Ffccache.c;h=7e2c1bec9363068b2b2ec31fb743a4c00fdec348;hb=cc104e6a910427db009be36ec34125962889ecb8;hp=af2c68fa88ebcd3348db24f9b270fc985edd7c5a;hpb=bc5e487f2a1ad9946aa5c6e19cd75794fc38d530;p=fontconfig.git diff --git a/src/fccache.c b/src/fccache.c index af2c68f..7e2c1be 100644 --- a/src/fccache.c +++ b/src/fccache.c @@ -28,6 +28,7 @@ #include #include #include +#include #if defined(HAVE_MMAP) || defined(__CYGWIN__) # include # include @@ -134,7 +135,7 @@ FcDirCacheOpenFile (const FcChar8 *cache_file, struct stat *file_stat) */ static FcBool FcDirCacheProcess (FcConfig *config, const FcChar8 *dir, - FcBool (*callback) (int fd, off_t size, void *closure), + FcBool (*callback) (int fd, struct stat *stat, void *closure), void *closure, FcChar8 **cache_file_ret) { int fd = -1; @@ -162,7 +163,7 @@ FcDirCacheProcess (FcConfig *config, const FcChar8 *dir, if (fd >= 0) { if (dir_stat.st_mtime <= file_stat.st_mtime) { - ret = (*callback) (fd, file_stat.st_size, closure); + ret = (*callback) (fd, &file_stat, closure); if (ret) { if (cache_file_ret) @@ -184,24 +185,266 @@ FcDirCacheProcess (FcConfig *config, const FcChar8 *dir, #define FC_CACHE_MIN_MMAP 1024 +/* + * Skip list element, make sure the 'next' pointer is the last thing + * in the structure, it will be allocated large enough to hold all + * of the necessary pointers + */ + +typedef struct _FcCacheSkip FcCacheSkip; + +struct _FcCacheSkip { + FcCache *cache; + int ref; + intptr_t size; + dev_t cache_dev; + ino_t cache_ino; + time_t cache_mtime; + FcCacheSkip *next[1]; +}; + +/* + * The head of the skip list; pointers for every possible level + * in the skip list, plus the largest level in the list + */ + +#define FC_CACHE_MAX_LEVEL 16 + +static FcCacheSkip *fcCacheChains[FC_CACHE_MAX_LEVEL]; +static int fcCacheMaxLevel; + +#if HAVE_RANDOM +# define FcRandom() random() +#else +# if HAVE_LRAND48 +# define FcRandom() lrand48() +# else +# if HAVE_RAND +# define FcRandom() rand() +# endif +# endif +#endif +/* + * Generate a random level number, distributed + * so that each level is 1/4 as likely as the one before + * + * Note that level numbers run 1 <= level <= MAX_LEVEL + */ +static int +random_level (void) +{ + /* tricky bit -- each bit is '1' 75% of the time */ + long int bits = FcRandom () | FcRandom (); + int level = 0; + + while (++level < FC_CACHE_MAX_LEVEL) + { + if (bits & 1) + break; + bits >>= 1; + } + return level; +} + +/* + * Insert cache into the list + */ +static FcBool +FcCacheInsert (FcCache *cache, struct stat *cache_stat) +{ + FcCacheSkip **update[FC_CACHE_MAX_LEVEL]; + FcCacheSkip *s, **next; + int i, level; + + /* + * Find links along each chain + */ + next = fcCacheChains; + for (i = fcCacheMaxLevel; --i >= 0; ) + { + for (; (s = next[i]); next = s->next) + if (s->cache > cache) + break; + update[i] = &next[i]; + } + + /* + * Create new list element + */ + level = random_level (); + if (level > fcCacheMaxLevel) + { + level = fcCacheMaxLevel + 1; + update[fcCacheMaxLevel] = &fcCacheChains[fcCacheMaxLevel]; + fcCacheMaxLevel = level; + } + + s = malloc (sizeof (FcCacheSkip) + (level - 1) * sizeof (FcCacheSkip *)); + if (!s) + return FcFalse; + + s->cache = cache; + s->size = cache->size; + s->ref = 1; + if (cache_stat) + { + s->cache_dev = cache_stat->st_dev; + s->cache_ino = cache_stat->st_ino; + s->cache_mtime = cache_stat->st_mtime; + } + else + { + s->cache_dev = 0; + s->cache_ino = 0; + s->cache_mtime = 0; + } + + /* + * Insert into all fcCacheChains + */ + for (i = 0; i < level; i++) + { + s->next[i] = *update[i]; + *update[i] = s; + } + return FcTrue; +} + +static FcCacheSkip * +FcCacheFindByAddr (void *object) +{ + int i; + FcCacheSkip **next = fcCacheChains; + FcCacheSkip *s; + + /* + * Walk chain pointers one level at a time + */ + for (i = fcCacheMaxLevel; --i >= 0;) + while (next[i] && (char *) object >= ((char *) next[i]->cache + next[i]->size)) + next = next[i]->next; + /* + * Here we are + */ + s = next[0]; + if (s && (char *) object < ((char *) s->cache + s->size)) + return s; + return NULL; +} + +static void +FcCacheRemove (FcCache *cache) +{ + FcCacheSkip **update[FC_CACHE_MAX_LEVEL]; + FcCacheSkip *s, **next; + int i; + + /* + * Find links along each chain + */ + next = fcCacheChains; + for (i = fcCacheMaxLevel; --i >= 0; ) + { + for (; (s = next[i]); next = s->next) + if (s->cache >= cache) + break; + update[i] = &next[i]; + } + s = next[0]; + for (i = 0; i < fcCacheMaxLevel && *update[i] == s; i++) + *update[i] = s->next[i]; + while (fcCacheMaxLevel > 0 && fcCacheChains[fcCacheMaxLevel - 1] == NULL) + fcCacheMaxLevel--; + free (s); +} + +static FcCache * +FcCacheFindByStat (struct stat *cache_stat) +{ + FcCacheSkip *s; + + for (s = fcCacheChains[0]; s; s = s->next[0]) + if (s->cache_dev == cache_stat->st_dev && + s->cache_ino == cache_stat->st_ino && + s->cache_mtime == cache_stat->st_mtime) + { + s->ref++; + return s->cache; + } + return NULL; +} + +static void +FcDirCacheDispose (FcCache *cache) +{ + switch (cache->magic) { + case FC_CACHE_MAGIC_ALLOC: + free (cache); + break; + case FC_CACHE_MAGIC_MMAP: +#if defined(HAVE_MMAP) || defined(__CYGWIN__) + munmap (cache, cache->size); +#elif defined(_WIN32) + UnmapViewOfFile (cache); +#endif + break; + } + FcCacheRemove (cache); +} + +void +FcCacheObjectReference (void *object) +{ + FcCacheSkip *skip = FcCacheFindByAddr (object); + + if (skip) + skip->ref++; +} + +void +FcCacheObjectDereference (void *object) +{ + FcCacheSkip *skip = FcCacheFindByAddr (object); + + if (skip) + { + skip->ref--; + if (skip->ref <= 0) + FcDirCacheDispose (skip->cache); + } +} + +void +FcCacheFini (void) +{ + int i; + + for (i = 0; i < FC_CACHE_MAX_LEVEL; i++) + assert (fcCacheChains[i] == NULL); + assert (fcCacheMaxLevel == 0); +} + /* * Map a cache file into memory */ static FcCache * -FcDirCacheMapFd (int fd, off_t size) +FcDirCacheMapFd (int fd, struct stat *fd_stat) { - FcCache *cache = NULL; + FcCache *cache; FcBool allocated = FcFalse; - if (size < sizeof (FcCache)) + if (fd_stat->st_size < sizeof (FcCache)) return NULL; + cache = FcCacheFindByStat (fd_stat); + if (cache) + return cache; /* * For small cache files, just read them into memory */ - if (size >= FC_CACHE_MIN_MMAP) + if (fd_stat->st_size >= FC_CACHE_MIN_MMAP) { #if defined(HAVE_MMAP) || defined(__CYGWIN__) - cache = mmap (0, size, PROT_READ, MAP_SHARED, fd, 0); + cache = mmap (0, fd_stat->st_size, PROT_READ, MAP_SHARED, fd, 0); #elif defined(_WIN32) { HANDLE hFileMap; @@ -219,11 +462,11 @@ FcDirCacheMapFd (int fd, off_t size) } if (!cache) { - cache = malloc (size); + cache = malloc (fd_stat->st_size); if (!cache) return NULL; - if (read (fd, cache, size) != size) + if (read (fd, cache, fd_stat->st_size) != fd_stat->st_size) { free (cache); return NULL; @@ -231,15 +474,16 @@ FcDirCacheMapFd (int fd, off_t size) allocated = FcTrue; } if (cache->magic != FC_CACHE_MAGIC_MMAP || - cache->version != FC_CACHE_CONTENT_VERSION || - cache->size != size) + cache->version < FC_CACHE_CONTENT_VERSION || + cache->size != fd_stat->st_size || + !FcCacheInsert (cache, fd_stat)) { if (allocated) free (cache); else { #if defined(HAVE_MMAP) || defined(__CYGWIN__) - munmap (cache, size); + munmap (cache, fd_stat->st_size); #elif defined(_WIN32) UnmapViewOfFile (cache); #endif @@ -254,27 +498,25 @@ FcDirCacheMapFd (int fd, off_t size) return cache; } +void +FcDirCacheReference (FcCache *cache, int nref) +{ + FcCacheSkip *skip = FcCacheFindByAddr (cache); + + if (skip) + skip->ref += nref; +} + void FcDirCacheUnload (FcCache *cache) { - switch (cache->magic) { - case FC_CACHE_MAGIC_ALLOC: - free (cache); - break; - case FC_CACHE_MAGIC_MMAP: -#if defined(HAVE_MMAP) || defined(__CYGWIN__) - munmap (cache, cache->size); -#elif defined(_WIN32) - UnmapViewOfFile (cache); -#endif - break; - } + FcCacheObjectDereference (cache); } static FcBool -FcDirCacheMapHelper (int fd, off_t size, void *closure) +FcDirCacheMapHelper (int fd, struct stat *fd_stat, void *closure) { - FcCache *cache = FcDirCacheMapFd (fd, size); + FcCache *cache = FcDirCacheMapFd (fd, fd_stat); if (!cache) return FcFalse; @@ -303,7 +545,7 @@ FcDirCacheLoadFile (const FcChar8 *cache_file, struct stat *file_stat) fd = FcDirCacheOpenFile (cache_file, file_stat); if (fd < 0) return NULL; - cache = FcDirCacheMapFd (fd, file_stat->st_size); + cache = FcDirCacheMapFd (fd, file_stat); close (fd); return cache; } @@ -313,21 +555,18 @@ FcDirCacheLoadFile (const FcChar8 *cache_file, struct stat *file_stat) * the magic number and the size field */ static FcBool -FcDirCacheValidateHelper (int fd, off_t size, void *closure) +FcDirCacheValidateHelper (int fd, struct stat *fd_stat, void *closure) { FcBool ret = FcTrue; FcCache c; - struct stat file_stat; if (read (fd, &c, sizeof (FcCache)) != sizeof (FcCache)) ret = FcFalse; else if (c.magic != FC_CACHE_MAGIC_MMAP) ret = FcFalse; - else if (c.version != FC_CACHE_CONTENT_VERSION) - ret = FcFalse; - else if (fstat (fd, &file_stat) < 0) + else if (c.version < FC_CACHE_CONTENT_VERSION) ret = FcFalse; - else if (file_stat.st_size != c.size) + else if (fd_stat->st_size != c.size) ret = FcFalse; return ret; } @@ -439,6 +678,8 @@ FcDirCacheBuild (FcFontSet *set, const FcChar8 *dir, FcStrSet *dirs) FcSerializeDestroy (serialize); + FcCacheInsert (cache, NULL); + return cache; bail2: @@ -575,6 +816,59 @@ FcDirCacheWrite (FcCache *cache, FcConfig *config) return FcFalse; } +/* + * Hokey little macro trick to permit the definitions of C functions + * with the same name as CPP macros + */ +#define args(x...) (x) + +const FcChar8 * +FcCacheDir args(const FcCache *c) +{ + return FcCacheDir (c); +} + +FcFontSet * +FcCacheCopySet args(const FcCache *c) +{ + FcFontSet *old = FcCacheSet (c); + FcFontSet *new = FcFontSetCreate (); + int i; + + if (!new) + return NULL; + for (i = 0; i < old->nfont; i++) + { + FcPattern *font = FcFontSetFont (old, i); + + FcPatternReference (font); + if (!FcFontSetAdd (new, font)) + { + FcFontSetDestroy (new); + return NULL; + } + } + return new; +} + +const FcChar8 * +FcCacheSubdir args(const FcCache *c, int i) +{ + return FcCacheSubdir (c, i); +} + +int +FcCacheNumSubdir args(const FcCache *c) +{ + return c->dirs_count; +} + +int +FcCacheNumFont args(const FcCache *c) +{ + return FcCacheSet(c)->nfont; +} + /* * This code implements the MD5 message-digest algorithm. * The algorithm is due to Ron Rivest. This code was @@ -818,3 +1112,6 @@ static void MD5Transform(FcChar32 buf[4], FcChar32 in[16]) buf[2] += c; buf[3] += d; } +#define __fccache__ +#include "fcaliastail.h" +#undef __fccache__