summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/caches/dcache-internal.h18
-rw-r--r--src/caches/dcache_add.c28
-rw-r--r--src/caches/dcache_clean_expired.c20
-rw-r--r--src/caches/dcache_delete.c19
-rw-r--r--src/caches/dcache_free.c3
-rw-r--r--src/caches/dcache_save.c3
-rw-r--r--src/caches/dcache_search.c4
-rw-r--r--src/caches/deps-lib/dcache2
-rw-r--r--src/include/s6-dns/dcache.h2
9 files changed, 67 insertions, 32 deletions
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 <stdint.h>
+
+#include <skalibs/avlnode.h>
+#include <skalibs/gensetdyn.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))
+
+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 <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) ;
}
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 <stdint.h>
+
+#include <skalibs/tai.h>
+#include <skalibs/avltree.h>
+
+#include <s6-dns/dcache.h>
+#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 <skalibs/alloc.h>
+#include <skalibs/gensetdyn.h>
+#include <skalibs/avltree.h>
+
+#include <s6-dns/dcache.h>
+#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 <s6-dns/dcache.h>
-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 <stdint.h>
+#include <unistd.h>
#include <stdio.h>
#include <skalibs/posixplz.h>
@@ -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 <stdint.h>
-#include <skalibs/gensetdyn.h>
#include <skalibs/avltree.h>
#include <s6-dns/dcache.h>
-
-#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
diff --git a/src/include/s6-dns/dcache.h b/src/include/s6-dns/dcache.h
index 177102e..5209750 100644
--- a/src/include/s6-dns/dcache.h
+++ b/src/include/s6-dns/dcache.h
@@ -44,6 +44,8 @@ extern void dcache_init (dcache_t *, uint64_t) ;
extern dcache_node_t *dcache_search (dcache_t *, char const *, uint16_t) ;
extern int dcache_add (dcache_t *, uint64_t, char const *, uint16_t, char const *, uint16_t, tain_t const *, tain_t const *) ;
#define dcache_add_g(d, max, key, keylen, data, datalen, expire) dcache_add(d, max, key, keylen, data, datalen, (expire), &STAMP)
+extern void dcache_clean_expired (dcache_t *, tain_t const *) ;
+#define dcache_clean_expired_g(d) dcache_clean_expired((d), &STAMP)
extern void dcache_free (dcache_t *) ;
extern int dcache_save (dcache_t const *, char const *) ;