From e5f5b81d65a5ed9b34910a58707b870d677c6183 Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Tue, 29 Dec 2020 23:07:18 +0000 Subject: Refactor libdcache, expose dcache_clean_expired() --- src/caches/dcache-internal.h | 18 ++++++++++++++++++ src/caches/dcache_add.c | 28 ++-------------------------- src/caches/dcache_clean_expired.c | 20 ++++++++++++++++++++ src/caches/dcache_delete.c | 19 +++++++++++++++++++ src/caches/dcache_free.c | 3 +-- src/caches/dcache_save.c | 3 ++- src/caches/dcache_search.c | 4 +--- src/caches/deps-lib/dcache | 2 ++ 8 files changed, 65 insertions(+), 32 deletions(-) create mode 100644 src/caches/dcache-internal.h create mode 100644 src/caches/dcache_clean_expired.c create mode 100644 src/caches/dcache_delete.c (limited to 'src/caches') diff --git a/src/caches/dcache-internal.h b/src/caches/dcache-internal.h new file mode 100644 index 0000000..4fdfa4c --- /dev/null +++ b/src/caches/dcache-internal.h @@ -0,0 +1,18 @@ +/* ISC license. */ + +#ifndef S6DNS_DCACHE_INTERNAL_H +#define S6DNS_DCACHE_INTERNAL_H + +#include + +#include +#include + +#include + +#define DNODE(z, i) GENSETDYN_P(dcache_node_t, &(z)->storage, i) +#define DCACHE_NODE_OVERHEAD (32 + sizeof(dcache_node_t) + 3 * sizeof(avlnode)) + +extern void dcache_delete (dcache_t *, uint32_t) ; + +#endif diff --git a/src/caches/dcache_add.c b/src/caches/dcache_add.c index bbe5fea..b69b1ca 100644 --- a/src/caches/dcache_add.c +++ b/src/caches/dcache_add.c @@ -12,9 +12,7 @@ #include #include - -#define DNODE(z, i) GENSETDYN_P(dcache_node_t, &(z)->storage, i) -#define DCACHE_NODE_OVERHEAD (32 + sizeof(dcache_node_t) + 3 * sizeof(avlnode)) +#include "dcache-internal.h" static void uniquify (avltree const *tree, tain_t *stamp) { @@ -24,17 +22,6 @@ static void uniquify (avltree const *tree, tain_t *stamp) tain_add(stamp, stamp, &nano) ; } -static void dcache_delete (dcache_t *z, uint32_t i) -{ - dcache_node_t *y = DNODE(z, i) ; - avltree_delete(&z->by_expire, &y->expire) ; - avltree_delete(&z->by_entry, &y->entry) ; - avltree_delete(&z->by_key, &y->key) ; - alloc_free(y->key.s) ; - z->size -= DCACHE_NODE_OVERHEAD + y->key.len + y->datalen ; - gensetdyn_delete(&z->storage, i) ; -} - static inline void dcache_gc_by_entry (dcache_t *z, uint64_t max) { while (z->size > max) @@ -45,17 +32,6 @@ static inline void dcache_gc_by_entry (dcache_t *z, uint64_t max) } } -static inline void dcache_gc_by_expire (dcache_t *z, tain_t const *stamp) -{ - for (;;) - { - uint32_t i ; - if (!avltree_min(&z->by_expire, &i)) break ; - if (tain_less(stamp, &DNODE(z, i)->expire)) break ; - dcache_delete(z, i) ; - } -} - static inline int dcache_add_node (dcache_t *z, dcache_node_t const *node) { uint32_t i ; @@ -104,7 +80,7 @@ int dcache_add (dcache_t *z, uint64_t max, char const *key, uint16_t keylen, cha { uint64_t size = DCACHE_NODE_OVERHEAD + keylen + datalen ; if (size > max) return (errno = EINVAL, 0) ; - if (z->size > max - size) dcache_gc_by_expire(z, stamp) ; + if (z->size > max - size) dcache_clean_expired(z, stamp) ; if (z->size > max - size) dcache_gc_by_entry(z, max - size) ; return dcache_add_unbounded(z, key, keylen, data, datalen, expire, stamp) ; } diff --git a/src/caches/dcache_clean_expired.c b/src/caches/dcache_clean_expired.c new file mode 100644 index 0000000..5308d8a --- /dev/null +++ b/src/caches/dcache_clean_expired.c @@ -0,0 +1,20 @@ +/* ISC license. */ + +#include + +#include +#include + +#include +#include "dcache-internal.h" + +void dcache_clean_expired (dcache_t *z, tain_t const *stamp) +{ + for (;;) + { + uint32_t i ; + if (!avltree_min(&z->by_expire, &i)) break ; + if (tain_less(stamp, &DNODE(z, i)->expire)) break ; + dcache_delete(z, i) ; + } +} diff --git a/src/caches/dcache_delete.c b/src/caches/dcache_delete.c new file mode 100644 index 0000000..e0dca4e --- /dev/null +++ b/src/caches/dcache_delete.c @@ -0,0 +1,19 @@ +/* ISC license. */ + +#include +#include +#include + +#include +#include "dcache-internal.h" + +void dcache_delete (dcache_t *z, uint32_t i) +{ + dcache_node_t *y = DNODE(z, i) ; + avltree_delete(&z->by_expire, &y->expire) ; + avltree_delete(&z->by_entry, &y->entry) ; + avltree_delete(&z->by_key, &y->key) ; + alloc_free(y->key.s) ; + z->size -= DCACHE_NODE_OVERHEAD + y->key.len + y->datalen ; + gensetdyn_delete(&z->storage, i) ; +} diff --git a/src/caches/dcache_free.c b/src/caches/dcache_free.c index 7937332..1ba8041 100644 --- a/src/caches/dcache_free.c +++ b/src/caches/dcache_free.c @@ -6,8 +6,6 @@ #include -static dcache_t const dcache_zero = DCACHE_ZERO ; - static void dcache_node_free (void *p) { alloc_free(((dcache_node_t *)p)->key.s) ; @@ -15,6 +13,7 @@ static void dcache_node_free (void *p) void dcache_free (dcache_t *z) { + static dcache_t const dcache_zero = DCACHE_ZERO ; avltree_free(&z->by_expire) ; avltree_free(&z->by_entry) ; avltree_free(&z->by_key) ; diff --git a/src/caches/dcache_save.c b/src/caches/dcache_save.c index 1ef35f0..04d1dac 100644 --- a/src/caches/dcache_save.c +++ b/src/caches/dcache_save.c @@ -1,6 +1,7 @@ /* ISC license. */ #include +#include #include #include @@ -57,7 +58,7 @@ int dcache_save (dcache_t const *z, char const *file) fd = open_excl(sa.s) ; if (fd == -1) goto err0 ; buffer_init(&b, &buffer_write, fd, buf, N) ; - if (!dcache_save_to_buffer(z, &b)) goto err2 ; + if (!dcache_save_to_buffer(z, &b) || fsync(fd) < 0) goto err2 ; fd_close(fd) ; if (rename(sa.s, file) == -1) goto err1 ; stralloc_free(&sa) ; diff --git a/src/caches/dcache_search.c b/src/caches/dcache_search.c index 0c1894a..7ca8324 100644 --- a/src/caches/dcache_search.c +++ b/src/caches/dcache_search.c @@ -2,12 +2,10 @@ #include -#include #include #include - -#define DNODE(z, i) GENSETDYN_P(dcache_node_t, &(z)->storage, i) +#include "dcache-internal.h" dcache_node_t *dcache_search (dcache_t *z, char const *key, uint16_t keylen) { diff --git a/src/caches/deps-lib/dcache b/src/caches/deps-lib/dcache index d842e17..fcc09a0 100644 --- a/src/caches/deps-lib/dcache +++ b/src/caches/deps-lib/dcache @@ -1,4 +1,6 @@ dcache_add.o +dcache_clean_expired.o +dcache_delete.o dcache_free.o dcache_init.o dcache_load.o -- cgit v1.2.3