diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2021-07-24 15:41:18 +0000 |
---|---|---|
committer | Laurent Bercot <ska@appnovation.com> | 2021-07-24 15:41:18 +0000 |
commit | 4414a4b9d8495320e54eaae05572a30b25b4401c (patch) | |
tree | a65e3e411a0ef4a92b00b626d43ef03d12be5fbf | |
parent | d6f7078aab3050629a63d9b9d92f30e1ca5a9aa1 (diff) | |
download | skalibs-4414a4b9d8495320e54eaae05572a30b25b4401c.tar.xz |
Hide cdb_find_state in the common case
Signed-off-by: Laurent Bercot <ska@appnovation.com>
-rw-r--r-- | package/deps.mak | 3 | ||||
-rw-r--r-- | src/include/skalibs/cdb.h | 3 | ||||
-rw-r--r-- | src/libstddjb/cdb_find.c | 54 | ||||
-rw-r--r-- | src/libstddjb/cdb_findnext.c | 57 |
4 files changed, 64 insertions, 53 deletions
diff --git a/package/deps.mak b/package/deps.mak index 3b17e98..7e47e69 100644 --- a/package/deps.mak +++ b/package/deps.mak @@ -291,7 +291,8 @@ src/libstddjb/cbuffer_unget.o src/libstddjb/cbuffer_unget.lo: src/libstddjb/cbuf src/libstddjb/cbuffer_unput.o src/libstddjb/cbuffer_unput.lo: src/libstddjb/cbuffer_unput.c src/include/skalibs/cbuffer.h src/libstddjb/cbuffer_wpeek.o src/libstddjb/cbuffer_wpeek.lo: src/libstddjb/cbuffer_wpeek.c src/include/skalibs/cbuffer.h src/libstddjb/cbuffer_wseek.o src/libstddjb/cbuffer_wseek.lo: src/libstddjb/cbuffer_wseek.c src/include/skalibs/cbuffer.h -src/libstddjb/cdb_find.o src/libstddjb/cdb_find.lo: src/libstddjb/cdb_find.c src/libstddjb/cdb-internal.h src/include/skalibs/cdb.h src/include/skalibs/uint32.h +src/libstddjb/cdb_find.o src/libstddjb/cdb_find.lo: src/libstddjb/cdb_find.c src/include/skalibs/cdb.h +src/libstddjb/cdb_findnext.o src/libstddjb/cdb_findnext.lo: src/libstddjb/cdb_findnext.c src/libstddjb/cdb-internal.h src/include/skalibs/cdb.h src/include/skalibs/uint32.h src/libstddjb/cdb_free.o src/libstddjb/cdb_free.lo: src/libstddjb/cdb_free.c src/include/skalibs/cdb.h src/include/skalibs/posixplz.h src/libstddjb/cdb_hash.o src/libstddjb/cdb_hash.lo: src/libstddjb/cdb_hash.c src/libstddjb/cdb-internal.h src/libstddjb/cdb_init.o src/libstddjb/cdb_init.lo: src/libstddjb/cdb_init.c src/include/skalibs/cdb.h src/include/skalibs/djbunix.h diff --git a/src/include/skalibs/cdb.h b/src/include/skalibs/cdb.h index 73a13ce..42fcb18 100644 --- a/src/include/skalibs/cdb.h +++ b/src/include/skalibs/cdb.h @@ -40,7 +40,8 @@ extern int cdb_init_at (cdb *, int, char const *) ; extern int cdb_init_fromfd (cdb *, int) ; #define cdb_findstart(d) ((d)->loop = 0) -extern int cdb_find (cdb const *, cdb_data *, char const *, uint32_t, cdb_find_state *) ; +extern int cdb_findnext (cdb const *, cdb_data *, char const *, uint32_t, cdb_find_state *) ; +extern int cdb_find (cdb const *, cdb_data *, char const *, uint32_t) ; #define CDB_TRAVERSE_INIT() 2048 #define cdb_traverse_init(pos) (*(pos) = 2048) diff --git a/src/libstddjb/cdb_find.c b/src/libstddjb/cdb_find.c index af642cc..ebf900d 100644 --- a/src/libstddjb/cdb_find.c +++ b/src/libstddjb/cdb_find.c @@ -1,57 +1,9 @@ /* ISC license. */ -#include <stdint.h> -#include <string.h> - -#include <skalibs/uint32.h> #include <skalibs/cdb.h> -#include "cdb-internal.h" -int cdb_find (cdb const *c, cdb_data *out, char const *key, uint32_t len, cdb_find_state *d) +int cdb_find (cdb const *c, cdb_data *out, char const *key, uint32_t len) { - if (!d->loop) - { - uint32_t u = cdb_hash(key, len) ; - char const *p = cdb_p(c, 8, (u << 3) & 2047) ; - if (!p) return -1 ; - uint32_unpack(p + 4, &d->hslots) ; - if (!d->hslots) return 0 ; - uint32_unpack(p, &d->hpos) ; - d->khash = u ; - u >>= 8 ; - u %= d->hslots ; - u <<= 3 ; - d->kpos = d->hpos + u ; - } - - while (d->loop < d->hslots) - { - uint32_t pos, u ; - char const *p = cdb_p(c, 8, d->kpos) ; - if (!p) return -1 ; - uint32_unpack(p + 4, &pos) ; - if (!pos) return 0 ; - d->loop++ ; - d->kpos += 8 ; - if (d->kpos == d->hpos + (d->hslots << 3)) d->kpos = d->hpos ; - uint32_unpack(p, &u) ; - if (u == d->khash) - { - p = cdb_p(c, 8, pos) ; - if (!p) return -1 ; - uint32_unpack(p, &u) ; - if (u == len) - { - char const *k = cdb_p(c, len, pos + 8) ; - if (!k) return -1 ; - if (!memcmp(key, k, len)) - { - uint32_unpack(p + 4, &out->len) ; - out->s = c->map + pos + 8 + len ; - return 1 ; - } - } - } - } - return 0 ; + cdb_find_state cfs = CDB_FIND_STATE_ZERO ; + return cdb_findnext(c, out, key, len, &cfs) ; } diff --git a/src/libstddjb/cdb_findnext.c b/src/libstddjb/cdb_findnext.c new file mode 100644 index 0000000..6cd376c --- /dev/null +++ b/src/libstddjb/cdb_findnext.c @@ -0,0 +1,57 @@ +/* ISC license. */ + +#include <stdint.h> +#include <string.h> + +#include <skalibs/uint32.h> +#include <skalibs/cdb.h> +#include "cdb-internal.h" + +int cdb_findnext (cdb const *c, cdb_data *out, char const *key, uint32_t len, cdb_find_state *d) +{ + if (!d->loop) + { + uint32_t u = cdb_hash(key, len) ; + char const *p = cdb_p(c, 8, (u << 3) & 2047) ; + if (!p) return -1 ; + uint32_unpack(p + 4, &d->hslots) ; + if (!d->hslots) return 0 ; + uint32_unpack(p, &d->hpos) ; + d->khash = u ; + u >>= 8 ; + u %= d->hslots ; + u <<= 3 ; + d->kpos = d->hpos + u ; + } + + while (d->loop < d->hslots) + { + uint32_t pos, u ; + char const *p = cdb_p(c, 8, d->kpos) ; + if (!p) return -1 ; + uint32_unpack(p + 4, &pos) ; + if (!pos) return 0 ; + d->loop++ ; + d->kpos += 8 ; + if (d->kpos == d->hpos + (d->hslots << 3)) d->kpos = d->hpos ; + uint32_unpack(p, &u) ; + if (u == d->khash) + { + p = cdb_p(c, 8, pos) ; + if (!p) return -1 ; + uint32_unpack(p, &u) ; + if (u == len) + { + char const *k = cdb_p(c, len, pos + 8) ; + if (!k) return -1 ; + if (!memcmp(key, k, len)) + { + uint32_unpack(p + 4, &out->len) ; + out->s = c->map + pos + 8 + len ; + return 1 ; + } + } + } + } + return 0 ; +} |