summaryrefslogtreecommitdiff
path: root/src/caches/dcache_add.c
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2020-12-29 23:07:18 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2020-12-29 23:07:18 +0000
commite5f5b81d65a5ed9b34910a58707b870d677c6183 (patch)
tree6e87cfda96222560751e200ca5ef7dd8d7cd65f4 /src/caches/dcache_add.c
parent5b063a4db3e631d40c05b77ecc7554ebdf5fff92 (diff)
downloads6-dns-e5f5b81d65a5ed9b34910a58707b870d677c6183.tar.xz
Refactor libdcache, expose dcache_clean_expired()
Diffstat (limited to 'src/caches/dcache_add.c')
-rw-r--r--src/caches/dcache_add.c28
1 files changed, 2 insertions, 26 deletions
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 <skalibs/avltree.h>
#include <s6-dns/dcache.h>
-
-#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) ;
}