Commit a63b9c62 authored by Alexander Larsson's avatar Alexander Larsson Committed by Akira TAGOH
Browse files

Add FcCacheAllocate() helper

This lets you allocate a chunk of memory that will be freed when the cache
is freed.

https://bugs.freedesktop.org/show_bug.cgi?id=106618
parent 94080c3d
......@@ -443,6 +443,7 @@ struct _FcCacheSkip {
FcCache *cache;
FcRef ref;
intptr_t size;
void *allocated;
dev_t cache_dev;
ino_t cache_ino;
time_t cache_mtime;
......@@ -568,6 +569,7 @@ FcCacheInsert (FcCache *cache, struct stat *cache_stat)
s->cache = cache;
s->size = cache->size;
s->allocated = NULL;
FcRefInit (&s->ref, 1);
if (cache_stat)
{
......@@ -642,6 +644,7 @@ FcCacheRemoveUnlocked (FcCache *cache)
FcCacheSkip **update[FC_CACHE_MAX_LEVEL];
FcCacheSkip *s, **next;
int i;
void *allocated;
/*
* Find links along each chain
......@@ -659,6 +662,15 @@ FcCacheRemoveUnlocked (FcCache *cache)
*update[i] = s->next[i];
while (fcCacheMaxLevel > 0 && fcCacheChains[fcCacheMaxLevel - 1] == NULL)
fcCacheMaxLevel--;
allocated = s->allocated;
while (allocated)
{
/* First element in allocated chunk is the free list */
next = *(void **)allocated;
free (allocated);
allocated = next;
}
free (s);
}
......@@ -728,6 +740,30 @@ FcCacheObjectDereference (void *object)
unlock_cache ();
}
void *
FcCacheAllocate (FcCache *cache, size_t len)
{
FcCacheSkip *skip;
void *allocated = NULL;
lock_cache ();
skip = FcCacheFindByAddrUnlocked (cache);
if (skip)
{
void *chunk = malloc (sizeof (void *) + len);
if (chunk)
{
/* First element in allocated chunk is the free list */
*(void **)chunk = skip->allocated;
skip->allocated = chunk;
/* Return the rest */
allocated = ((FcChar8 *)chunk) + sizeof (void *);
}
}
unlock_cache ();
return allocated;
}
void
FcCacheFini (void)
{
......
......@@ -617,9 +617,13 @@ FcCacheObjectReference (void *object);
FcPrivate void
FcCacheObjectDereference (void *object);
FcPrivate void *
FcCacheAllocate (FcCache *cache, size_t len);
FcPrivate void
FcCacheFini (void);
FcPrivate void
FcDirCacheReference (FcCache *cache, int nref);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment