From 66251384705a5bd93928680448f847298389156c Mon Sep 17 00:00:00 2001 From: "Dr.Smile" Date: Fri, 20 Oct 2017 09:17:10 +0300 Subject: cache: cleanup --- libass/ass_cache.c | 127 ++++++++++++++++++++++++++------------------ libass/ass_cache.h | 10 ++-- libass/ass_cache_template.h | 18 ++----- libass/ass_utils.h | 16 +++--- 4 files changed, 92 insertions(+), 79 deletions(-) diff --git a/libass/ass_cache.c b/libass/ass_cache.c index f33babc..3f2cb19 100644 --- a/libass/ass_cache.c +++ b/libass/ass_cache.c @@ -38,37 +38,36 @@ #include "ass_cache_template.h" // font cache -static unsigned font_hash(void *buf, size_t len) +static uint32_t font_hash(void *buf, uint32_t hval) { ASS_FontDesc *desc = buf; - unsigned hval; - hval = fnv_32a_str(desc->family, FNV1_32A_INIT); + hval = fnv_32a_str(desc->family, hval); hval = fnv_32a_buf(&desc->bold, sizeof(desc->bold), hval); hval = fnv_32a_buf(&desc->italic, sizeof(desc->italic), hval); hval = fnv_32a_buf(&desc->vertical, sizeof(desc->vertical), hval); return hval; } -static unsigned font_compare(void *key1, void *key2, size_t key_size) +static bool font_compare(void *key1, void *key2) { ASS_FontDesc *a = key1; ASS_FontDesc *b = key2; if (strcmp(a->family, b->family) != 0) - return 0; + return false; if (a->bold != b->bold) - return 0; + return false; if (a->italic != b->italic) - return 0; + return false; if (a->vertical != b->vertical) - return 0; - return 1; + return false; + return true; } -static bool font_key_move(void *dst, void *src, size_t key_size) +static bool font_key_move(void *dst, void *src) { ASS_FontDesc *k = src; if (dst) - memcpy(dst, src, key_size); + memcpy(dst, src, sizeof(ASS_FontDesc)); else free(k->family); return true; @@ -93,29 +92,36 @@ const CacheDesc font_cache_desc = { // bitmap cache -static unsigned bitmap_hash(void *key, size_t key_size) +static uint32_t bitmap_hash(void *key, uint32_t hval) { BitmapHashKey *k = key; switch (k->type) { - case BITMAP_OUTLINE: return outline_bitmap_hash(&k->u, key_size); - case BITMAP_CLIP: return clip_bitmap_hash(&k->u, key_size); - default: return 0; + case BITMAP_OUTLINE: + return outline_bitmap_hash(&k->u, hval); + case BITMAP_CLIP: + return clip_bitmap_hash(&k->u, hval); + default: + return hval; } } -static unsigned bitmap_compare(void *a, void *b, size_t key_size) +static bool bitmap_compare(void *a, void *b) { BitmapHashKey *ak = a; BitmapHashKey *bk = b; - if (ak->type != bk->type) return 0; + if (ak->type != bk->type) + return false; switch (ak->type) { - case BITMAP_OUTLINE: return outline_bitmap_compare(&ak->u, &bk->u, key_size); - case BITMAP_CLIP: return clip_bitmap_compare(&ak->u, &bk->u, key_size); - default: return 0; + case BITMAP_OUTLINE: + return outline_bitmap_compare(&ak->u, &bk->u); + case BITMAP_CLIP: + return clip_bitmap_compare(&ak->u, &bk->u); + default: + return false; } } -static bool bitmap_key_move(void *dst, void *src, size_t key_size) +static bool bitmap_key_move(void *dst, void *src) { BitmapHashKey *d = dst, *s = src; if (!dst) { @@ -123,7 +129,7 @@ static bool bitmap_key_move(void *dst, void *src, size_t key_size) ass_cache_dec_ref(s->u.outline.outline); return true; } - memcpy(dst, src, key_size); + memcpy(dst, src, sizeof(BitmapHashKey)); if (s->type != BITMAP_CLIP) return true; d->u.clip.text = strdup(s->u.clip.text); @@ -139,8 +145,12 @@ static void bitmap_destruct(void *key, void *value) if (v->bm_o) ass_free_bitmap(v->bm_o); switch (k->type) { - case BITMAP_OUTLINE: ass_cache_dec_ref(k->u.outline.outline); break; - case BITMAP_CLIP: free(k->u.clip.text); break; + case BITMAP_OUTLINE: + ass_cache_dec_ref(k->u.outline.outline); + break; + case BITMAP_CLIP: + free(k->u.clip.text); + break; } } @@ -158,10 +168,10 @@ const CacheDesc bitmap_cache_desc = { // composite cache -static unsigned composite_hash(void *key, size_t key_size) +static uint32_t composite_hash(void *key, uint32_t hval) { CompositeHashKey *k = key; - unsigned hval = filter_hash(&k->filter, key_size); + hval = filter_hash(&k->filter, hval); for (size_t i = 0; i < k->bitmap_count; i++) { hval = fnv_32a_buf(&k->bitmaps[i].image, sizeof(k->bitmaps[i].image), hval); hval = fnv_32a_buf(&k->bitmaps[i].x, sizeof(k->bitmaps[i].x), hval); @@ -170,25 +180,25 @@ static unsigned composite_hash(void *key, size_t key_size) return hval; } -static unsigned composite_compare(void *a, void *b, size_t key_size) +static bool composite_compare(void *a, void *b) { CompositeHashKey *ak = a; CompositeHashKey *bk = b; if (ak->bitmap_count != bk->bitmap_count) - return 0; + return false; for (size_t i = 0; i < ak->bitmap_count; i++) { if (ak->bitmaps[i].image != bk->bitmaps[i].image || ak->bitmaps[i].x != bk->bitmaps[i].x || ak->bitmaps[i].y != bk->bitmaps[i].y) - return 0; + return false; } - return filter_compare(&ak->filter, &bk->filter, key_size); + return filter_compare(&ak->filter, &bk->filter); } -static bool composite_key_move(void *dst, void *src, size_t key_size) +static bool composite_key_move(void *dst, void *src) { if (dst) { - memcpy(dst, src, key_size); + memcpy(dst, src, sizeof(CompositeHashKey)); return true; } CompositeHashKey *k = src; @@ -227,29 +237,38 @@ const CacheDesc composite_cache_desc = { // outline cache -static unsigned outline_hash(void *key, size_t key_size) +static uint32_t outline_hash(void *key, uint32_t hval) { OutlineHashKey *k = key; switch (k->type) { - case OUTLINE_GLYPH: return glyph_hash(&k->u, key_size); - case OUTLINE_DRAWING: return drawing_hash(&k->u, key_size); - default: return outline_common_hash(&k->u, key_size); + case OUTLINE_GLYPH: + return glyph_hash(&k->u, hval); + case OUTLINE_DRAWING: + return drawing_hash(&k->u, hval); + default: + // unused, added to disable warning + return outline_common_hash(&k->u, hval); } } -static unsigned outline_compare(void *a, void *b, size_t key_size) +static bool outline_compare(void *a, void *b) { OutlineHashKey *ak = a; OutlineHashKey *bk = b; - if (ak->type != bk->type) return 0; + if (ak->type != bk->type) + return false; switch (ak->type) { - case OUTLINE_GLYPH: return glyph_compare(&ak->u, &bk->u, key_size); - case OUTLINE_DRAWING: return drawing_compare(&ak->u, &bk->u, key_size); - default: return outline_common_compare(&ak->u, &bk->u, key_size); + case OUTLINE_GLYPH: + return glyph_compare(&ak->u, &bk->u); + case OUTLINE_DRAWING: + return drawing_compare(&ak->u, &bk->u); + default: + // unused, added to disable warning + return outline_common_compare(&ak->u, &bk->u); } } -static bool outline_key_move(void *dst, void *src, size_t key_size) +static bool outline_key_move(void *dst, void *src) { OutlineHashKey *d = dst, *s = src; if (!dst) { @@ -257,7 +276,7 @@ static bool outline_key_move(void *dst, void *src, size_t key_size) ass_cache_dec_ref(s->u.glyph.font); return true; } - memcpy(dst, src, key_size); + memcpy(dst, src, sizeof(OutlineHashKey)); if (s->type != OUTLINE_DRAWING) return true; d->u.drawing.text = strdup(s->u.drawing.text); @@ -272,8 +291,12 @@ static void outline_destruct(void *key, void *value) outline_free(&v->border[0]); outline_free(&v->border[1]); switch (k->type) { - case OUTLINE_GLYPH: ass_cache_dec_ref(k->u.glyph.font); break; - case OUTLINE_DRAWING: free(k->u.drawing.text); break; + case OUTLINE_GLYPH: + ass_cache_dec_ref(k->u.glyph.font); + break; + case OUTLINE_DRAWING: + free(k->u.drawing.text); + break; } } @@ -291,11 +314,11 @@ const CacheDesc outline_cache_desc = { // glyph metric cache -static bool glyph_metrics_key_move(void *dst, void *src, size_t key_size) +static bool glyph_metrics_key_move(void *dst, void *src) { if (!dst) return true; - memcpy(dst, src, key_size); + memcpy(dst, src, sizeof(GlyphMetricsHashKey)); GlyphMetricsHashKey *k = src; ass_cache_inc_ref(k->font); return true; @@ -379,10 +402,10 @@ void *ass_cache_get(Cache *cache, void *key, void *priv) { const CacheDesc *desc = cache->desc; size_t key_offs = CACHE_ITEM_SIZE + align_cache(desc->value_size); - unsigned bucket = desc->hash_func(key, desc->key_size) % cache->buckets; + unsigned bucket = desc->hash_func(key, FNV1_32A_INIT) % cache->buckets; CacheItem *item = cache->map[bucket]; while (item) { - if (desc->compare_func(key, (char *) item + key_offs, desc->key_size)) { + if (desc->compare_func(key, (char *) item + key_offs)) { assert(item->size); if (!item->queue_prev || item->queue_next) { if (item->queue_prev) { @@ -396,7 +419,7 @@ void *ass_cache_get(Cache *cache, void *key, void *priv) item->queue_next = NULL; } cache->hits++; - desc->key_move_func(NULL, key, desc->key_size); + desc->key_move_func(NULL, key); item->ref_count++; return (char *) item + CACHE_ITEM_SIZE; } @@ -406,13 +429,13 @@ void *ass_cache_get(Cache *cache, void *key, void *priv) item = malloc(key_offs + desc->key_size); if (!item) { - desc->key_move_func(NULL, key, desc->key_size); + desc->key_move_func(NULL, key); return NULL; } item->cache = cache; item->desc = desc; void *new_key = (char *) item + key_offs; - if (!desc->key_move_func(new_key, key, desc->key_size)) { + if (!desc->key_move_func(new_key, key)) { free(item); return NULL; } diff --git a/libass/ass_cache.h b/libass/ass_cache.h index acfa4bb..453cefa 100644 --- a/libass/ass_cache.h +++ b/libass/ass_cache.h @@ -59,11 +59,11 @@ typedef struct { #include "ass_cache_template.h" // Type-specific function pointers -typedef unsigned(*HashFunction)(void *key, size_t key_size); -typedef unsigned(*HashCompare)(void *a, void *b, size_t key_size); -typedef bool(*CacheKeyMove)(void *dst, void *src, size_t key_size); -typedef size_t(*CacheValueConstructor)(void *key, void *value, void *priv); -typedef void(*CacheItemDestructor)(void *key, void *value); +typedef uint32_t (*HashFunction)(void *key, uint32_t hval); +typedef bool (*HashCompare)(void *a, void *b); +typedef bool (*CacheKeyMove)(void *dst, void *src); +typedef size_t (*CacheValueConstructor)(void *key, void *value, void *priv); +typedef void (*CacheItemDestructor)(void *key, void *value); // cache hash keys diff --git a/libass/ass_cache_template.h b/libass/ass_cache_template.h index b4a6864..2bb0228 100644 --- a/libass/ass_cache_template.h +++ b/libass/ass_cache_template.h @@ -8,15 +8,13 @@ char *member; #define VECTOR(member) \ ASS_Vector member; -#define BITMAPHASHKEY(member) \ - BitmapHashKey member; #define END(typedefnamename) \ } typedefnamename; #elif defined(CREATE_COMPARISON_FUNCTIONS) #undef CREATE_COMPARISON_FUNCTIONS #define START(funcname, structname) \ - static unsigned funcname##_compare(void *key1, void *key2, size_t key_size) \ + static bool funcname##_compare(void *key1, void *key2) \ { \ struct structname *a = key1; \ struct structname *b = key2; \ @@ -27,28 +25,21 @@ strcmp(a->member, b->member) == 0 && #define VECTOR(member) \ a->member.x == b->member.x && a->member.y == b->member.y && -#define BITMAPHASHKEY(member) \ - bitmap_compare(&a->member, &b->member, sizeof(a->member)) && #define END(typedefname) \ - 1; \ + true; \ } #elif defined(CREATE_HASH_FUNCTIONS) #undef CREATE_HASH_FUNCTIONS #define START(funcname, structname) \ - static unsigned funcname##_hash(void *buf, size_t len) \ + static uint32_t funcname##_hash(void *buf, uint32_t hval) \ { \ - struct structname *p = buf; \ - unsigned hval = FNV1_32A_INIT; + struct structname *p = buf; #define GENERIC(type, member) \ hval = fnv_32a_buf(&p->member, sizeof(p->member), hval); #define STRING(member) \ hval = fnv_32a_str(p->member, hval); #define VECTOR(member) GENERIC(, member.x); GENERIC(, member.y); -#define BITMAPHASHKEY(member) { \ - unsigned temp = bitmap_hash(&p->member, sizeof(p->member)); \ - hval = fnv_32a_buf(&temp, sizeof(temp), hval); \ - } #define END(typedefname) \ return hval; \ } @@ -142,5 +133,4 @@ END(FilterDesc) #undef GENERIC #undef STRING #undef VECTOR -#undef BITMAPHASHKEY #undef END diff --git a/libass/ass_utils.h b/libass/ass_utils.h index c2fcf30..008b3c7 100644 --- a/libass/ass_utils.h +++ b/libass/ass_utils.h @@ -173,26 +173,26 @@ static inline int rot_key(double a) #define FNV1_32A_INIT 0x811c9dc5U #define FNV1_32A_PRIME 16777619U -static inline unsigned fnv_32a_buf(void *buf, size_t len, unsigned hval) +static inline uint32_t fnv_32a_buf(void *buf, size_t len, uint32_t hval) { - unsigned char *bp = (unsigned char*)buf; + unsigned char *bp = (unsigned char *) buf; size_t n = (len + 3) / 4; switch (len % 4) { - case 0: do { hval ^= (unsigned) *bp++; hval *= FNV1_32A_PRIME; - case 3: hval ^= (unsigned) *bp++; hval *= FNV1_32A_PRIME; - case 2: hval ^= (unsigned) *bp++; hval *= FNV1_32A_PRIME; - case 1: hval ^= (unsigned) *bp++; hval *= FNV1_32A_PRIME; + case 0: do { hval ^= *bp++; hval *= FNV1_32A_PRIME; + case 3: hval ^= *bp++; hval *= FNV1_32A_PRIME; + case 2: hval ^= *bp++; hval *= FNV1_32A_PRIME; + case 1: hval ^= *bp++; hval *= FNV1_32A_PRIME; } while (--n > 0); } return hval; } -static inline unsigned fnv_32a_str(char *str, unsigned hval) +static inline uint32_t fnv_32a_str(const char *str, uint32_t hval) { unsigned char *s = (unsigned char *) str; while (*s) { - hval ^= (unsigned) *s++; + hval ^= *s++; hval *= FNV1_32A_PRIME; } return hval; -- cgit v1.2.3