From 30497b80dd25447df4c3a820d0f7f849dc725114 Mon Sep 17 00:00:00 2001 From: Grigori Goronzy Date: Mon, 27 Jun 2011 19:42:56 +0200 Subject: [PATCH] cache: fix size tracking and emptying --- libass/ass_cache.c | 8 +++++--- libass/ass_cache.h | 46 +++++++++++++++++++++++----------------------- 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/libass/ass_cache.c b/libass/ass_cache.c index 15f7376..8d42e61 100644 --- a/libass/ass_cache.c +++ b/libass/ass_cache.c @@ -229,6 +229,8 @@ void *ass_cache_put(Cache *cache, void *key, void *value) cache->items++; if (cache->size_func) cache->cache_size += cache->size_func(value, cache->value_size); + else + cache->cache_size++; return (*item)->value; } @@ -248,12 +250,12 @@ void *ass_cache_get(Cache *cache, void *key) return NULL; } -size_t ass_cache_empty(Cache *cache, size_t max_size) +int ass_cache_empty(Cache *cache, size_t max_size) { int i; if (cache->cache_size < max_size) - return cache->cache_size; + return 0; for (i = 0; i < cache->buckets; i++) { CacheItem *item = cache->map[i]; @@ -268,7 +270,7 @@ size_t ass_cache_empty(Cache *cache, size_t max_size) cache->items = cache->hits = cache->misses = cache->cache_size = 0; - return 0; + return 1; } void ass_cache_stats(Cache *cache, size_t *size, unsigned *hits, diff --git a/libass/ass_cache.h b/libass/ass_cache.h index 0ad7cae..05903e7 100644 --- a/libass/ass_cache.h +++ b/libass/ass_cache.h @@ -26,6 +26,28 @@ typedef struct cache Cache; +// cache values + +typedef struct { + Bitmap *bm; // the actual bitmaps + Bitmap *bm_o; + Bitmap *bm_s; +} BitmapHashValue; + +typedef struct { + unsigned char *a; + unsigned char *b; +} CompositeHashValue; + +typedef struct { + FT_Library lib; + FT_Outline *outline; + FT_Outline *border; + FT_BBox bbox_scaled; // bbox after scaling, but before rotation + FT_Vector advance; // 26.6, advance distance to the next outline in line + int asc, desc; // ascender/descender +} OutlineHashValue; + // Create definitions for bitmap, outline and composite hash keys #define CREATE_STRUCT_DEFINITIONS #include "ass_cache_template.h" @@ -49,34 +71,12 @@ typedef struct outline_hash_key { } u; } OutlineHashKey; -// cache values - -typedef struct { - Bitmap *bm; // the actual bitmaps - Bitmap *bm_o; - Bitmap *bm_s; -} BitmapHashValue; - -typedef struct { - unsigned char *a; - unsigned char *b; -} CompositeHashValue; - -typedef struct { - FT_Library lib; - FT_Outline *outline; - FT_Outline *border; - FT_BBox bbox_scaled; // bbox after scaling, but before rotation - FT_Vector advance; // 26.6, advance distance to the next outline in line - int asc, desc; // ascender/descender -} OutlineHashValue; - Cache *ass_cache_create(HashFunction hash_func, HashCompare compare_func, CacheItemDestructor destruct_func, ItemSize size_func, size_t key_size, size_t value_size); void *ass_cache_put(Cache *cache, void *key, void *value); void *ass_cache_get(Cache *cache, void *key); -size_t ass_cache_empty(Cache *cache, size_t max_size); +int ass_cache_empty(Cache *cache, size_t max_size); void ass_cache_stats(Cache *cache, size_t *size, unsigned *hits, unsigned *misses, unsigned *count); void ass_cache_done(Cache *cache); -- 2.40.0