aboutsummaryrefslogtreecommitdiff
path: root/src/cache.c
blob: 1ffb31c5df60e1e898b81725c73d5e4838dc4d7d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <uthash.h>

#include "compiler.h"
#include "utils.h"
#include "cache.h"

struct cache_entry {
	char *key;
	void *value;
	UT_hash_handle hh;
};

struct cache {
	cache_getter_t getter;
	cache_free_t free;
	void *user_data;
	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) {
	struct cache_entry *e;
	HASH_FIND_STR(c->entries, key, e);
	if (e) {
		return e->value;
	}

	int tmperr;
	if (!err) {
		err = &tmperr;
	}

	*err = 0;
	e = ccalloc(1, struct cache_entry);
	e->key = strdup(key);
	e->value = c->getter(c->user_data, key, err);
	if (*err) {
		free(e->key);
		free(e);
		return NULL;
	}

	HASH_ADD_STR(c->entries, key, e);
	return e->value;
}

static inline void _cache_invalidate(struct cache *c, struct cache_entry *e) {
	if (c->free) {
		c->free(c->user_data, e->value);
	}
	free(e->key);
	HASH_DEL(c->entries, e);
	free(e);
}

void cache_invalidate(struct cache *c, const char *key) {
	struct cache_entry *e;
	HASH_FIND_STR(c->entries, key, e);

	if (e) {
		_cache_invalidate(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);
	}
}

void *cache_free(struct cache *c) {
	void *ret = c->user_data;
	cache_invalidate_all(c);
	free(c);
	return ret;
}

struct cache *new_cache(void *ud, cache_getter_t getter, cache_free_t f) {
	auto c = ccalloc(1, struct cache);
	c->user_data = ud;
	c->getter = getter;
	c->free = f;
	return c;
}