From 153a646667ff5ae7578a6027938524db91c40821 Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Thu, 5 Feb 2015 23:56:14 +0000 Subject: - Add siovec_trunc - Add openwritevnclose - replace buffer_read/write with fd_readsv/writesv - add localtmn_from_tain_g - refactor cdb_make - rewrite buffer_getvall and buffer_putvall, with a bugfix --- src/libstddjb/cdb_make.c | 73 +++++++++++++++++------------------------------- 1 file changed, 25 insertions(+), 48 deletions(-) (limited to 'src/libstddjb/cdb_make.c') diff --git a/src/libstddjb/cdb_make.c b/src/libstddjb/cdb_make.c index 0ec3423..203d24c 100644 --- a/src/libstddjb/cdb_make.c +++ b/src/libstddjb/cdb_make.c @@ -4,26 +4,19 @@ #include #include #include -#include +#include #include #include +#include #include #include -static void cdb_make_free (struct cdb_make *c) -{ - struct cdb_make zero = CDB_MAKE_ZERO ; - genalloc_free(diuint32, &c->hplist) ; - *c = zero ; -} - int cdb_make_start (struct cdb_make *c, int fd) { c->hplist = genalloc_zero ; - c->fd = fd ; c->pos = 2048 ; - buffer_init(&c->b, &buffer_write, fd, c->buf, BUFFER_OUTSIZE) ; - return seek_set(fd, c->pos) ; + buffer_init(&c->b, &fd_writesv, fd, c->buf, BUFFER_OUTSIZE) ; + return (int)lseek(fd, c->pos, SEEK_SET) ; } static int posplus (struct cdb_make *c, uint32 len) @@ -34,41 +27,30 @@ static int posplus (struct cdb_make *c, uint32 len) return 1 ; } -static int cdb_make_addend (struct cdb_make *c, unsigned int keylen, unsigned int datalen, uint32 h) +static inline int cdb_make_addend (struct cdb_make *c, unsigned int keylen, unsigned int datalen, uint32 h) { - diuint32 blah = { h, c->pos } ; - if (!genalloc_append(diuint32, &c->hplist, &blah) || !posplus(c, 8) || !posplus(c, keylen) || !posplus(c, datalen)) - { - cdb_make_free(c) ; - return -1 ; - } - return 0 ; + diuint32 blah = { .left = h, .right = c->pos } ; + return genalloc_append(diuint32, &c->hplist, &blah) && posplus(c, 8) && posplus(c, keylen) && posplus(c, datalen) ; } -static int cdb_make_addbegin (struct cdb_make *c, unsigned int keylen, unsigned int datalen) +static inline int cdb_make_addbegin (struct cdb_make *c, unsigned int keylen, unsigned int datalen) { char buf[8] ; - if ((keylen > 0xffffffff) || (datalen > 0xffffffff)) - { - errno = ENOMEM ; - goto err ; - } - uint32_pack(buf, keylen) ; - uint32_pack(buf + 4, datalen) ; - if (buffer_put(&c->b, buf, 8) < 0) goto err ; - return 0 ; -err: - cdb_make_free(c) ; - return -1 ; + uint32_pack(buf, (uint32)keylen) ; + uint32_pack(buf + 4, (uint32)datalen) ; + return buffer_put(&c->b, buf, 8) ; } int cdb_make_add (struct cdb_make *c, char const *key, unsigned int keylen, char const *data, unsigned int datalen) { - if ((cdb_make_addbegin(c, keylen, datalen) < 0) - || (buffer_put(&c->b, key, keylen) < 0) - || (buffer_put(&c->b, data, datalen) < 0) - || (cdb_make_addend(c, keylen, datalen, cdb_hash(key, keylen)) < 0)) + if (cdb_make_addbegin(c, keylen, datalen) < 0 + || buffer_put(&c->b, key, keylen) < 0 + || buffer_put(&c->b, data, datalen) < 0 + || !cdb_make_addend(c, keylen, datalen, cdb_hash(key, keylen))) + { + genalloc_free(diuint32, &c->hplist) ; return -1 ; + } return 0 ; } @@ -82,7 +64,7 @@ int cdb_make_finish (struct cdb_make *c) register unsigned int i = 0 ; register diuint32 *hp = genalloc_s(diuint32, &c->hplist) ; - for ( ; i < 256 ; i++) count[i] = 0 ; + for (; i < 256 ; i++) count[i] = 0 ; for (i = 0 ; i < n ; i++) ++count[hp[i].left & 255] ; { @@ -115,7 +97,7 @@ int cdb_make_finish (struct cdb_make *c) uint32_pack(final + (i << 3), c->pos) ; uint32_pack(final + (i << 3) + 4, len) ; - for ( ; j < len ; j++) hp[j].left = hp[j].right = 0 ; + for (; j < len ; j++) hp[j].left = hp[j].right = 0 ; for (j = 0 ; j < k ; j++) { register uint32 where = (p->left >> 8) % len ; @@ -127,19 +109,14 @@ int cdb_make_finish (struct cdb_make *c) { uint32_pack(buf, hp[j].left) ; uint32_pack(buf + 4, hp[j].right) ; - if (buffer_put(&c->b, buf, 8) < 0) goto err0 ; - if (!posplus(c, 8)) goto err0 ; + if (buffer_put(&c->b, buf, 8) < 0) return -1 ; + if (!posplus(c, 8)) return -1 ; } } } - if (!buffer_flush(&c->b)) goto err0 ; - if (seek_begin(c->fd) == -1) goto err0 ; - if (buffer_putflush(&c->b, final, 2048) < 0) goto err0 ; - cdb_make_free(c) ; + if (!buffer_flush(&c->b)) return -1 ; + if (lseek(buffer_fd(&c->b), 0, SEEK_SET) < 0) return -1 ; + if (buffer_putflush(&c->b, final, 2048) < 0) return -1 ; return 0 ; - -err0: - cdb_make_free(c) ; - return -1 ; } -- cgit v1.2.3