diff --git a/src/atom.c b/src/atom.c index 0272dc8..bf94232 100644 --- a/src/atom.c +++ b/src/atom.c @@ -29,7 +29,8 @@ static inline void *atom_getter(void *ud, const char *atom_name, int *err) { struct atom *init_atoms(xcb_connection_t *c) { auto atoms = ccalloc(1, struct atom); atoms->c = new_cache((void *)c, atom_getter, NULL); -#define ATOM_GET(x) atoms->a##x = (xcb_atom_t)(intptr_t)cache_get(atoms->c, #x, NULL) +#define ATOM_GET(x) \ + atoms->a##x = (xcb_atom_t)(intptr_t)cache_get_or_fetch(atoms->c, #x, NULL) LIST_APPLY(ATOM_GET, SEP_COLON, ATOM_LIST1); LIST_APPLY(ATOM_GET, SEP_COLON, ATOM_LIST2); #undef ATOM_GET diff --git a/src/atom.h b/src/atom.h index 5ea7701..f984e66 100644 --- a/src/atom.h +++ b/src/atom.h @@ -61,7 +61,7 @@ struct atom { struct atom *init_atoms(xcb_connection_t *); static inline xcb_atom_t get_atom(struct atom *a, const char *key) { - return (xcb_atom_t)(intptr_t)cache_get(a->c, key, NULL); + return (xcb_atom_t)(intptr_t)cache_get_or_fetch(a->c, key, NULL); } static inline void destroy_atoms(struct atom *a) { diff --git a/src/cache.c b/src/cache.c index 1ffb31c..8c5d91d 100644 --- a/src/cache.c +++ b/src/cache.c @@ -17,20 +17,19 @@ struct cache { struct cache_entry *entries; }; -void cache_set(struct cache *c, const char *key, void *data) { - struct cache_entry *e = NULL; - HASH_FIND_STR(c->entries, key, e); - CHECK(!e); - - e = ccalloc(1, struct cache_entry); - e->key = strdup(key); - e->value = data; - HASH_ADD_STR(c->entries, key, e); -} - -void *cache_get(struct cache *c, const char *key, int *err) { +static inline struct cache_entry *cache_get_entry(struct cache *c, const char *key) { struct cache_entry *e; HASH_FIND_STR(c->entries, key, e); + return e; +} + +void *cache_get(struct cache *c, const char *key) { + struct cache_entry *e = cache_get_entry(c, key); + return e ? e->value : NULL; +} + +void *cache_get_or_fetch(struct cache *c, const char *key, int *err) { + struct cache_entry *e = cache_get_entry(c, key); if (e) { return e->value; } @@ -54,7 +53,7 @@ void *cache_get(struct cache *c, const char *key, int *err) { return e->value; } -static inline void _cache_invalidate(struct cache *c, struct cache_entry *e) { +static inline void cache_invalidate_impl(struct cache *c, struct cache_entry *e) { if (c->free) { c->free(c->user_data, e->value); } @@ -68,14 +67,14 @@ void cache_invalidate(struct cache *c, const char *key) { HASH_FIND_STR(c->entries, key, e); if (e) { - _cache_invalidate(c, e); + cache_invalidate_impl(c, e); } } void cache_invalidate_all(struct cache *c) { struct cache_entry *e, *tmpe; HASH_ITER(hh, c->entries, e, tmpe) { - _cache_invalidate(c, e); + cache_invalidate_impl(c, e); } } diff --git a/src/cache.h b/src/cache.h index 3ca054f..2ac1a01 100644 --- a/src/cache.h +++ b/src/cache.h @@ -11,9 +11,13 @@ typedef void (*cache_free_t)(void *user_data, void *data); /// `user_data` will be passed to `getter` and `f` when they are called. struct cache *new_cache(void *user_data, cache_getter_t getter, cache_free_t f); -/// Fetch a value from the cache. If the value doesn't present in the cache yet, the +/// Get a value from the cache. If the value doesn't present in the cache yet, the /// getter will be called, and the returned value will be stored into the cache. -void *cache_get(struct cache *, const char *key, int *err); +void *cache_get_or_fetch(struct cache *, const char *key, int *err); + +/// Get a value from the cache. If the value doesn't present in the cache, NULL will be +/// returned. +void *cache_get(struct cache *, const char *key); /// Invalidate a value in the cache. void cache_invalidate(struct cache *, const char *key); @@ -24,9 +28,3 @@ void cache_invalidate_all(struct cache *); /// Invalidate all values in the cache and free it. Returns the user data passed to /// `new_cache` void *cache_free(struct cache *); - -/// Insert a key-value pair into the cache. Only used for internal testing. Takes -/// ownership of `data` -/// -/// If `key` already exists in the cache, this function will abort the program. -void cache_set(struct cache *c, const char *key, void *data);