Source-Changes-HG archive

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

[src/trunk]: src/sys/kern Add hash_list_size() and simplify slightly.



details:   https://anonhg.NetBSD.org/src/rev/151df54bf3b3
branches:  trunk
changeset: 779620:151df54bf3b3
user:      rmind <rmind%NetBSD.org@localhost>
date:      Tue Jun 05 20:51:36 2012 +0000

description:
Add hash_list_size() and simplify slightly.

diffstat:

 sys/kern/subr_hash.c |  81 +++++++++++++++++++++++----------------------------
 1 files changed, 37 insertions(+), 44 deletions(-)

diffs (128 lines):

diff -r 6d0301b4238d -r 151df54bf3b3 sys/kern/subr_hash.c
--- a/sys/kern/subr_hash.c      Tue Jun 05 19:35:44 2012 +0000
+++ b/sys/kern/subr_hash.c      Tue Jun 05 20:51:36 2012 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: subr_hash.c,v 1.4 2011/12/25 02:23:09 christos Exp $   */
+/*     $NetBSD: subr_hash.c,v 1.5 2012/06/05 20:51:36 rmind Exp $      */
 
 /*
  * Copyright (c) 1982, 1986, 1991, 1993
@@ -37,36 +37,19 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: subr_hash.c,v 1.4 2011/12/25 02:23:09 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_hash.c,v 1.5 2012/06/05 20:51:36 rmind Exp $");
 
 #include <sys/param.h>
 #include <sys/kmem.h>
 #include <sys/systm.h>
 
-/*
- * General routine to allocate a hash table.
- * Allocate enough memory to hold at least `elements' list-head pointers.
- * Return a pointer to the allocated space and set *hashmask to a pattern
- * suitable for masking a value to use as an index into the returned array.
- */
-void *
-hashinit(u_int elements, enum hashtype htype, bool waitok, u_long *hashmask)
+static size_t
+hash_list_size(enum hashtype htype)
 {
-       u_long hashsize, i;
        LIST_HEAD(, generic) *hashtbl_list;
        SLIST_HEAD(, generic) *hashtbl_slist;
        TAILQ_HEAD(, generic) *hashtbl_tailq;
        size_t esize;
-       void *p;
-       if (elements == 0)
-               panic("hashinit: bad cnt");
-
-#define MAXELEMENTS (1U << ((sizeof(elements) * NBBY) - 1))
-       if (elements > MAXELEMENTS)
-               elements = MAXELEMENTS;
-
-       for (hashsize = 1; hashsize < elements; hashsize <<= 1)
-               continue;
 
        switch (htype) {
        case HASH_LIST:
@@ -79,12 +62,40 @@
                esize = sizeof(*hashtbl_tailq);
                break;
        default:
-               panic("hashinit: invalid table type");
+               panic("hashdone: invalid table type");
        }
+       return esize;
+}
 
-       p = kmem_alloc(hashsize * esize, (waitok ? KM_SLEEP : KM_NOSLEEP));
+/*
+ * General routine to allocate a hash table.
+ * Allocate enough memory to hold at least `elements' list-head pointers.
+ * Return a pointer to the allocated space and set *hashmask to a pattern
+ * suitable for masking a value to use as an index into the returned array.
+ */
+void *
+hashinit(u_int elements, enum hashtype htype, bool waitok, u_long *hashmask)
+{
+       LIST_HEAD(, generic) *hashtbl_list;
+       SLIST_HEAD(, generic) *hashtbl_slist;
+       TAILQ_HEAD(, generic) *hashtbl_tailq;
+       u_long hashsize, i;
+       size_t esize;
+       void *p;
+
+       KASSERT(elements > 0);
+
+#define MAXELEMENTS (1U << ((sizeof(elements) * NBBY) - 1))
+       if (elements > MAXELEMENTS)
+               elements = MAXELEMENTS;
+
+       for (hashsize = 1; hashsize < elements; hashsize <<= 1)
+               continue;
+
+       esize = hash_list_size(htype);
+       p = kmem_alloc(hashsize * esize, waitok ? KM_SLEEP : KM_NOSLEEP);
        if (p == NULL)
-               return (NULL);
+               return NULL;
 
        switch (htype) {
        case HASH_LIST:
@@ -104,7 +115,7 @@
                break;
        }
        *hashmask = hashsize - 1;
-       return (p);
+       return p;
 }
 
 /*
@@ -113,24 +124,6 @@
 void
 hashdone(void *hashtbl, enum hashtype htype, u_long hashmask)
 {
-       LIST_HEAD(, generic) *hashtbl_list;
-       SLIST_HEAD(, generic) *hashtbl_slist;
-       TAILQ_HEAD(, generic) *hashtbl_tailq;
-       size_t esize;
-
-       switch (htype) {
-       case HASH_LIST:
-               esize = sizeof(*hashtbl_list);
-               break;
-       case HASH_SLIST:
-               esize = sizeof(*hashtbl_slist);
-               break;
-       case HASH_TAILQ:
-               esize = sizeof(*hashtbl_tailq);
-               break;
-       default:
-               panic("hashdone: invalid table type");
-       }
-
+       const size_t esize = hash_list_size(htype);
        kmem_free(hashtbl, esize * (hashmask + 1));
 }



Home | Main Index | Thread Index | Old Index