Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libc/db/hash Introduce a HASH_BSIZE macro to return the ...



details:   https://anonhg.NetBSD.org/src/rev/9b0b389866e9
branches:  trunk
changeset: 341700:9b0b389866e9
user:      christos <christos%NetBSD.org@localhost>
date:      Wed Nov 18 18:22:42 2015 +0000

description:
Introduce a HASH_BSIZE macro to return the blocksize; in the 64K case this
returns 0xffff to avoid overflow. This is used where sizes are stored.

diffstat:

 lib/libc/db/hash/hash.c        |  12 ++++++------
 lib/libc/db/hash/hash.h        |   7 ++++++-
 lib/libc/db/hash/hash_bigkey.c |  16 ++++++++--------
 lib/libc/db/hash/hash_page.c   |  26 ++++++++++++--------------
 4 files changed, 32 insertions(+), 29 deletions(-)

diffs (225 lines):

diff -r 0214d669e715 -r 9b0b389866e9 lib/libc/db/hash/hash.c
--- a/lib/libc/db/hash/hash.c   Wed Nov 18 18:04:45 2015 +0000
+++ b/lib/libc/db/hash/hash.c   Wed Nov 18 18:22:42 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: hash.c,v 1.37 2015/11/18 13:00:46 christos Exp $       */
+/*     $NetBSD: hash.c,v 1.38 2015/11/18 18:22:42 christos Exp $       */
 
 /*-
  * Copyright (c) 1990, 1993, 1994
@@ -37,7 +37,7 @@
 #endif
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: hash.c,v 1.37 2015/11/18 13:00:46 christos Exp $");
+__RCSID("$NetBSD: hash.c,v 1.38 2015/11/18 18:22:42 christos Exp $");
 
 #include "namespace.h"
 #include <sys/param.h>
@@ -585,7 +585,7 @@
        hash_accesses++;
 #endif
 
-       off = hashp->BSIZE == MAX_BSIZE ? MAX_BSIZE - 1 : hashp->BSIZE;
+       off = HASH_BSIZE(hashp);
        size = key->size;
        kp = (char *)key->data;
        rbufp = __get_buf(hashp, __call_hash(hashp, kp, (int)size), NULL, 0);
@@ -617,7 +617,7 @@
                        bp = (uint16_t *)(void *)rbufp->page;
                        n = *bp++;
                        ndx = 1;
-                       off = hashp->BSIZE;
+                       off = HASH_BSIZE(hashp);
                } else if (bp[1] < REAL_KEY) {
                        if ((ndx =
                            __find_bigpair(hashp, rbufp, ndx, kp, (int)size)) > 0)
@@ -640,7 +640,7 @@
                                bp = (uint16_t *)(void *)rbufp->page;
                                n = *bp++;
                                ndx = 1;
-                               off = hashp->BSIZE;
+                               off = HASH_BSIZE(hashp);
                        } else {
                                save_bufp->flags &= ~BUF_PIN;
                                return (ERROR);
@@ -807,7 +807,7 @@
                if (hashp->cpage == NULL)
                        return (ERROR);
                key->data = (uint8_t *)hashp->cpage->page + bp[ndx];
-               key->size = (ndx > 1 ? bp[ndx - 1] : hashp->BSIZE) - bp[ndx];
+               key->size = (ndx > 1 ? bp[ndx - 1] : HASH_BSIZE(hashp)) - bp[ndx];
                data->data = (uint8_t *)hashp->cpage->page + bp[ndx + 1];
                data->size = bp[ndx] - bp[ndx + 1];
                hashp->cndx += 2;
diff -r 0214d669e715 -r 9b0b389866e9 lib/libc/db/hash/hash.h
--- a/lib/libc/db/hash/hash.h   Wed Nov 18 18:04:45 2015 +0000
+++ b/lib/libc/db/hash/hash.h   Wed Nov 18 18:22:42 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: hash.h,v 1.15 2008/08/26 21:18:38 joerg Exp $  */
+/*     $NetBSD: hash.h,v 1.16 2015/11/18 18:22:42 christos Exp $       */
 
 /*-
  * Copyright (c) 1990, 1993, 1994
@@ -123,6 +123,11 @@
  * Constants
  */
 #define        MAX_BSIZE               65536           /* 2^16 */
+/*
+ * Make it fit in uint16_t; a better way would be to store size - 1, but
+ * then we'd need to bump the version.
+ */
+#define HASH_BSIZE(hp) ((hp)->BSIZE == MAX_BSIZE ? MAX_BSIZE - 1 : (hp)->BSIZE)
 #define MIN_BUFFERS            6
 #define MINHDRSIZE             512
 #define DEF_BUFSIZE            65536           /* 64 K */
diff -r 0214d669e715 -r 9b0b389866e9 lib/libc/db/hash/hash_bigkey.c
--- a/lib/libc/db/hash/hash_bigkey.c    Wed Nov 18 18:04:45 2015 +0000
+++ b/lib/libc/db/hash/hash_bigkey.c    Wed Nov 18 18:22:42 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: hash_bigkey.c,v 1.24 2012/03/13 21:13:32 christos Exp $        */
+/*     $NetBSD: hash_bigkey.c,v 1.25 2015/11/18 18:22:42 christos Exp $        */
 
 /*-
  * Copyright (c) 1990, 1993, 1994
@@ -37,7 +37,7 @@
 #endif
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: hash_bigkey.c,v 1.24 2012/03/13 21:13:32 christos Exp $");
+__RCSID("$NetBSD: hash_bigkey.c,v 1.25 2015/11/18 18:22:42 christos Exp $");
 
 /*
  * PACKAGE: hash
@@ -274,10 +274,10 @@
                bufp->ovfl = NULL;
        n -= 2;
        bp[0] = n;
-       temp = hashp->BSIZE - PAGE_META(n);
+       temp = HASH_BSIZE(hashp) - PAGE_META(n);
        _DBFIT(temp, uint16_t);
        FREESPACE(bp) = (uint16_t)temp;
-       OFFSET(bp) = hashp->BSIZE;
+       OFFSET(bp) = HASH_BSIZE(hashp);
 
        bufp->flags |= BUF_MOD;
        if (rbufp)
@@ -309,9 +309,9 @@
        ksize = size;
        kkey = key;
 
-       for (bytes = hashp->BSIZE - bp[ndx];
+       for (bytes = HASH_BSIZE(hashp) - bp[ndx];
            bytes <= size && bp[ndx + 1] == PARTIAL_KEY;
-           bytes = hashp->BSIZE - bp[ndx]) {
+           bytes = HASH_BSIZE(hashp) - bp[ndx]) {
                if (memcmp(p + bp[ndx], kkey, (size_t)bytes))
                        return (-2);
                kkey += bytes;
@@ -479,7 +479,7 @@
 
        p = bufp->page;
        bp = (uint16_t *)(void *)p;
-       mylen = hashp->BSIZE - bp[1];
+       mylen = HASH_BSIZE(hashp) - bp[1];
        save_addr = bufp->addr;
 
        if (bp[2] == FULL_KEY_DATA) {           /* End of Data */
@@ -546,7 +546,7 @@
 
        p = bufp->page;
        bp = (uint16_t *)(void *)p;
-       mylen = hashp->BSIZE - bp[1];
+       mylen = HASH_BSIZE(hashp) - bp[1];
 
        save_addr = bufp->addr;
        totlen = len + mylen;
diff -r 0214d669e715 -r 9b0b389866e9 lib/libc/db/hash/hash_page.c
--- a/lib/libc/db/hash/hash_page.c      Wed Nov 18 18:04:45 2015 +0000
+++ b/lib/libc/db/hash/hash_page.c      Wed Nov 18 18:22:42 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: hash_page.c,v 1.27 2015/11/18 00:23:39 christos Exp $  */
+/*     $NetBSD: hash_page.c,v 1.28 2015/11/18 18:22:42 christos Exp $  */
 
 /*-
  * Copyright (c) 1990, 1993, 1994
@@ -37,7 +37,7 @@
 #endif
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: hash_page.c,v 1.27 2015/11/18 00:23:39 christos Exp $");
+__RCSID("$NetBSD: hash_page.c,v 1.28 2015/11/18 18:22:42 christos Exp $");
 
 /*
  * PACKAGE:  hashing
@@ -83,11 +83,9 @@
 #define        PAGE_INIT(P) { \
        ((uint16_t *)(void *)(P))[0] = 0; \
        temp = 3 * sizeof(uint16_t); \
-       _DIAGASSERT((size_t)hashp->BSIZE >= temp); \
-       ((uint16_t *)(void *)(P))[1] = (uint16_t)(hashp->BSIZE - temp); \
-       /* we should be really storing always length - 1 here... */ \
-       ((uint16_t *)(void *)(P))[2] = \
-           hashp->BSIZE == MAX_BSIZE ? MAX_BSIZE - 1 : hashp->BSIZE; \
+       _DIAGASSERT((size_t)HASH_BSIZE(hashp) >= temp); \
+       ((uint16_t *)(void *)(P))[1] = (uint16_t)(HASH_BSIZE(hashp) - temp); \
+       ((uint16_t *)(void *)(P))[2] = HASH_BSIZE(hashp); \
 }
 
 /*
@@ -147,7 +145,7 @@
        if (ndx != 1)
                newoff = bp[ndx - 1];
        else
-               newoff = hashp->BSIZE;
+               newoff = HASH_BSIZE(hashp);
        pairlen = newoff - bp[ndx + 1];
 
        if (ndx != (n - 1)) {
@@ -196,8 +194,8 @@
        char *op;
        size_t temp;
 
-       copyto = (uint16_t)hashp->BSIZE;
-       off = (uint16_t)hashp->BSIZE;
+       copyto = HASH_BSIZE(hashp);
+       off = HASH_BSIZE(hashp);
        old_bufp = __get_buf(hashp, obucket, NULL, 0);
        if (old_bufp == NULL)
                return (-1);
@@ -348,7 +346,7 @@
 
                        ino = (uint16_t *)(void *)bufp->page;
                        n = 1;
-                       scopyto = hashp->BSIZE;
+                       scopyto = HASH_BSIZE(hashp);
                        moved = 0;
 
                        if (last_bfp)
@@ -356,7 +354,7 @@
                        last_bfp = bufp;
                }
                /* Move regular sized pairs of there are any */
-               off = hashp->BSIZE;
+               off = HASH_BSIZE(hashp);
                for (n = 1; (n < ino[0]) && (ino[n + 1] >= REAL_KEY); n += 2) {
                        cino = (char *)(void *)ino;
                        key.data = (uint8_t *)cino + ino[n];
@@ -543,7 +541,7 @@
        size_t temp;
 
        fd = hashp->fp;
-       size = hashp->BSIZE;
+       size = HASH_BSIZE(hashp);
 
        if ((fd == -1) || !is_disk) {
                PAGE_INIT(p);
@@ -596,7 +594,7 @@
        int fd, page, size;
        ssize_t wsize;
 
-       size = hashp->BSIZE;
+       size = HASH_BSIZE(hashp);
        if ((hashp->fp == -1) && (hashp->fp = __dbtemp("_hash", NULL)) == -1)
                return (-1);
        fd = hashp->fp;



Home | Main Index | Thread Index | Old Index