mirror of
https://github.com/yshui/picom.git
synced 2024-11-11 13:51:02 -05:00
cache: slight refactor
Added a pure version of `cache_get` which does not change the cache, and renamed the old `cache_get` to `cache_get_or_fetch`. Remove unused `cache_set`, and remove prefix underscore from function names. Signed-off-by: Yuxuan Shui <yshuiv7@gmail.com>
This commit is contained in:
parent
bb097730c7
commit
0dcca2228e
4 changed files with 23 additions and 25 deletions
|
@ -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
|
||||
|
|
|
@ -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) {
|
||||
|
|
29
src/cache.c
29
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
14
src/cache.h
14
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);
|
||||
|
|
Loading…
Reference in a new issue