Home | History | Annotate | Line # | Download | only in kern
subr_hash.c revision 1.5.2.2
      1  1.5.2.1       tls /*	$NetBSD: subr_hash.c,v 1.5.2.2 2017/12/03 11:38:45 jdolecek Exp $	*/
      2      1.1     pooka 
      3      1.1     pooka /*
      4      1.1     pooka  * Copyright (c) 1982, 1986, 1991, 1993
      5      1.1     pooka  *	The Regents of the University of California.  All rights reserved.
      6      1.1     pooka  * (c) UNIX System Laboratories, Inc.
      7      1.1     pooka  * All or some portions of this file are derived from material licensed
      8      1.1     pooka  * to the University of California by American Telephone and Telegraph
      9      1.1     pooka  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10      1.1     pooka  * the permission of UNIX System Laboratories, Inc.
     11      1.1     pooka  *
     12      1.1     pooka  * Redistribution and use in source and binary forms, with or without
     13      1.1     pooka  * modification, are permitted provided that the following conditions
     14      1.1     pooka  * are met:
     15      1.1     pooka  * 1. Redistributions of source code must retain the above copyright
     16      1.1     pooka  *    notice, this list of conditions and the following disclaimer.
     17      1.1     pooka  * 2. Redistributions in binary form must reproduce the above copyright
     18      1.1     pooka  *    notice, this list of conditions and the following disclaimer in the
     19      1.1     pooka  *    documentation and/or other materials provided with the distribution.
     20      1.1     pooka  * 3. Neither the name of the University nor the names of its contributors
     21      1.1     pooka  *    may be used to endorse or promote products derived from this software
     22      1.1     pooka  *    without specific prior written permission.
     23      1.1     pooka  *
     24      1.1     pooka  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25      1.1     pooka  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26      1.1     pooka  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27      1.1     pooka  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28      1.1     pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29      1.1     pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30      1.1     pooka  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31      1.1     pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32      1.1     pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33      1.1     pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34      1.1     pooka  * SUCH DAMAGE.
     35      1.1     pooka  *
     36      1.1     pooka  *	@(#)kern_subr.c	8.4 (Berkeley) 2/14/95
     37      1.1     pooka  */
     38      1.1     pooka 
     39      1.1     pooka #include <sys/cdefs.h>
     40  1.5.2.1       tls __KERNEL_RCSID(0, "$NetBSD: subr_hash.c,v 1.5.2.2 2017/12/03 11:38:45 jdolecek Exp $");
     41      1.1     pooka 
     42      1.1     pooka #include <sys/param.h>
     43  1.5.2.1       tls #include <sys/bitops.h>
     44      1.3        ad #include <sys/kmem.h>
     45      1.1     pooka #include <sys/systm.h>
     46  1.5.2.2  jdolecek #include <sys/pslist.h>
     47      1.1     pooka 
     48      1.5     rmind static size_t
     49      1.5     rmind hash_list_size(enum hashtype htype)
     50      1.5     rmind {
     51      1.5     rmind 	LIST_HEAD(, generic) *hashtbl_list;
     52      1.5     rmind 	SLIST_HEAD(, generic) *hashtbl_slist;
     53      1.5     rmind 	TAILQ_HEAD(, generic) *hashtbl_tailq;
     54  1.5.2.2  jdolecek 	struct pslist_head *hashtbl_pslist;
     55      1.5     rmind 	size_t esize;
     56      1.5     rmind 
     57      1.5     rmind 	switch (htype) {
     58      1.5     rmind 	case HASH_LIST:
     59      1.5     rmind 		esize = sizeof(*hashtbl_list);
     60      1.5     rmind 		break;
     61  1.5.2.2  jdolecek 	case HASH_PSLIST:
     62  1.5.2.2  jdolecek 		esize = sizeof(*hashtbl_pslist);
     63  1.5.2.2  jdolecek 		break;
     64      1.5     rmind 	case HASH_SLIST:
     65      1.5     rmind 		esize = sizeof(*hashtbl_slist);
     66      1.5     rmind 		break;
     67      1.5     rmind 	case HASH_TAILQ:
     68      1.5     rmind 		esize = sizeof(*hashtbl_tailq);
     69      1.5     rmind 		break;
     70      1.5     rmind 	default:
     71      1.5     rmind 		panic("hashdone: invalid table type");
     72      1.5     rmind 	}
     73      1.5     rmind 	return esize;
     74      1.5     rmind }
     75      1.5     rmind 
     76      1.1     pooka /*
     77      1.1     pooka  * General routine to allocate a hash table.
     78      1.1     pooka  * Allocate enough memory to hold at least `elements' list-head pointers.
     79      1.1     pooka  * Return a pointer to the allocated space and set *hashmask to a pattern
     80      1.1     pooka  * suitable for masking a value to use as an index into the returned array.
     81      1.1     pooka  */
     82      1.1     pooka void *
     83      1.3        ad hashinit(u_int elements, enum hashtype htype, bool waitok, u_long *hashmask)
     84      1.1     pooka {
     85      1.1     pooka 	LIST_HEAD(, generic) *hashtbl_list;
     86      1.2     rmind 	SLIST_HEAD(, generic) *hashtbl_slist;
     87      1.1     pooka 	TAILQ_HEAD(, generic) *hashtbl_tailq;
     88  1.5.2.2  jdolecek 	struct pslist_head *hashtbl_pslist;
     89      1.5     rmind 	u_long hashsize, i;
     90      1.1     pooka 	size_t esize;
     91      1.1     pooka 	void *p;
     92      1.5     rmind 
     93      1.5     rmind 	KASSERT(elements > 0);
     94      1.4  christos 
     95      1.4  christos #define MAXELEMENTS (1U << ((sizeof(elements) * NBBY) - 1))
     96      1.4  christos 	if (elements > MAXELEMENTS)
     97      1.4  christos 		elements = MAXELEMENTS;
     98      1.4  christos 
     99  1.5.2.1       tls 	hashsize = 1UL << (ilog2(elements - 1) + 1);
    100      1.5     rmind 	esize = hash_list_size(htype);
    101  1.5.2.1       tls 
    102      1.5     rmind 	p = kmem_alloc(hashsize * esize, waitok ? KM_SLEEP : KM_NOSLEEP);
    103      1.3        ad 	if (p == NULL)
    104      1.5     rmind 		return NULL;
    105      1.1     pooka 
    106      1.1     pooka 	switch (htype) {
    107      1.1     pooka 	case HASH_LIST:
    108      1.1     pooka 		hashtbl_list = p;
    109      1.1     pooka 		for (i = 0; i < hashsize; i++)
    110      1.1     pooka 			LIST_INIT(&hashtbl_list[i]);
    111      1.1     pooka 		break;
    112  1.5.2.2  jdolecek 	case HASH_PSLIST:
    113  1.5.2.2  jdolecek 		hashtbl_pslist = p;
    114  1.5.2.2  jdolecek 		for (i = 0; i < hashsize; i++)
    115  1.5.2.2  jdolecek 			PSLIST_INIT(&hashtbl_pslist[i]);
    116  1.5.2.2  jdolecek 		break;
    117      1.2     rmind 	case HASH_SLIST:
    118      1.2     rmind 		hashtbl_slist = p;
    119      1.2     rmind 		for (i = 0; i < hashsize; i++)
    120      1.2     rmind 			SLIST_INIT(&hashtbl_slist[i]);
    121      1.2     rmind 		break;
    122      1.1     pooka 	case HASH_TAILQ:
    123      1.1     pooka 		hashtbl_tailq = p;
    124      1.1     pooka 		for (i = 0; i < hashsize; i++)
    125      1.1     pooka 			TAILQ_INIT(&hashtbl_tailq[i]);
    126      1.1     pooka 		break;
    127      1.1     pooka 	}
    128      1.1     pooka 	*hashmask = hashsize - 1;
    129      1.5     rmind 	return p;
    130      1.1     pooka }
    131      1.1     pooka 
    132      1.1     pooka /*
    133      1.1     pooka  * Free memory from hash table previosly allocated via hashinit().
    134      1.1     pooka  */
    135      1.1     pooka void
    136      1.3        ad hashdone(void *hashtbl, enum hashtype htype, u_long hashmask)
    137      1.1     pooka {
    138      1.5     rmind 	const size_t esize = hash_list_size(htype);
    139      1.3        ad 	kmem_free(hashtbl, esize * (hashmask + 1));
    140      1.1     pooka }
    141