2016-07-06 08:20:48 +03:00
|
|
|
/* $NetBSD: subr_hash.c,v 1.7 2016/07/06 05:20:48 ozaki-r Exp $ */
|
2007-07-28 16:53:52 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1982, 1986, 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
* (c) UNIX System Laboratories, Inc.
|
|
|
|
* All or some portions of this file are derived from material licensed
|
|
|
|
* to the University of California by American Telephone and Telegraph
|
|
|
|
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
|
|
|
|
* the permission of UNIX System Laboratories, Inc.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* @(#)kern_subr.c 8.4 (Berkeley) 2/14/95
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2016-07-06 08:20:48 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: subr_hash.c,v 1.7 2016/07/06 05:20:48 ozaki-r Exp $");
|
2007-07-28 16:53:52 +04:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
2014-05-30 01:15:55 +04:00
|
|
|
#include <sys/bitops.h>
|
2008-05-05 21:11:16 +04:00
|
|
|
#include <sys/kmem.h>
|
2007-07-28 16:53:52 +04:00
|
|
|
#include <sys/systm.h>
|
2016-07-06 08:20:48 +03:00
|
|
|
#include <sys/pslist.h>
|
2007-07-28 16:53:52 +04:00
|
|
|
|
2012-06-06 00:51:36 +04:00
|
|
|
static size_t
|
|
|
|
hash_list_size(enum hashtype htype)
|
|
|
|
{
|
|
|
|
LIST_HEAD(, generic) *hashtbl_list;
|
|
|
|
SLIST_HEAD(, generic) *hashtbl_slist;
|
|
|
|
TAILQ_HEAD(, generic) *hashtbl_tailq;
|
2016-07-06 08:20:48 +03:00
|
|
|
struct pslist_head *hashtbl_pslist;
|
2012-06-06 00:51:36 +04:00
|
|
|
size_t esize;
|
|
|
|
|
|
|
|
switch (htype) {
|
|
|
|
case HASH_LIST:
|
|
|
|
esize = sizeof(*hashtbl_list);
|
|
|
|
break;
|
2016-07-06 08:20:48 +03:00
|
|
|
case HASH_PSLIST:
|
|
|
|
esize = sizeof(*hashtbl_pslist);
|
|
|
|
break;
|
2012-06-06 00:51:36 +04:00
|
|
|
case HASH_SLIST:
|
|
|
|
esize = sizeof(*hashtbl_slist);
|
|
|
|
break;
|
|
|
|
case HASH_TAILQ:
|
|
|
|
esize = sizeof(*hashtbl_tailq);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
panic("hashdone: invalid table type");
|
|
|
|
}
|
|
|
|
return esize;
|
|
|
|
}
|
|
|
|
|
2007-07-28 16:53:52 +04:00
|
|
|
/*
|
|
|
|
* 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 *
|
2008-05-05 21:11:16 +04:00
|
|
|
hashinit(u_int elements, enum hashtype htype, bool waitok, u_long *hashmask)
|
2007-07-28 16:53:52 +04:00
|
|
|
{
|
|
|
|
LIST_HEAD(, generic) *hashtbl_list;
|
2008-03-18 00:16:03 +03:00
|
|
|
SLIST_HEAD(, generic) *hashtbl_slist;
|
2007-07-28 16:53:52 +04:00
|
|
|
TAILQ_HEAD(, generic) *hashtbl_tailq;
|
2016-07-06 08:20:48 +03:00
|
|
|
struct pslist_head *hashtbl_pslist;
|
2012-06-06 00:51:36 +04:00
|
|
|
u_long hashsize, i;
|
2007-07-28 16:53:52 +04:00
|
|
|
size_t esize;
|
|
|
|
void *p;
|
2012-06-06 00:51:36 +04:00
|
|
|
|
|
|
|
KASSERT(elements > 0);
|
2011-12-25 06:23:09 +04:00
|
|
|
|
|
|
|
#define MAXELEMENTS (1U << ((sizeof(elements) * NBBY) - 1))
|
|
|
|
if (elements > MAXELEMENTS)
|
|
|
|
elements = MAXELEMENTS;
|
|
|
|
|
2014-05-30 01:15:55 +04:00
|
|
|
hashsize = 1UL << (ilog2(elements - 1) + 1);
|
2012-06-06 00:51:36 +04:00
|
|
|
esize = hash_list_size(htype);
|
2014-05-30 01:15:55 +04:00
|
|
|
|
2012-06-06 00:51:36 +04:00
|
|
|
p = kmem_alloc(hashsize * esize, waitok ? KM_SLEEP : KM_NOSLEEP);
|
2008-05-05 21:11:16 +04:00
|
|
|
if (p == NULL)
|
2012-06-06 00:51:36 +04:00
|
|
|
return NULL;
|
2007-07-28 16:53:52 +04:00
|
|
|
|
|
|
|
switch (htype) {
|
|
|
|
case HASH_LIST:
|
|
|
|
hashtbl_list = p;
|
|
|
|
for (i = 0; i < hashsize; i++)
|
|
|
|
LIST_INIT(&hashtbl_list[i]);
|
|
|
|
break;
|
2016-07-06 08:20:48 +03:00
|
|
|
case HASH_PSLIST:
|
|
|
|
hashtbl_pslist = p;
|
|
|
|
for (i = 0; i < hashsize; i++)
|
|
|
|
PSLIST_INIT(&hashtbl_pslist[i]);
|
|
|
|
break;
|
2008-03-18 00:16:03 +03:00
|
|
|
case HASH_SLIST:
|
|
|
|
hashtbl_slist = p;
|
|
|
|
for (i = 0; i < hashsize; i++)
|
|
|
|
SLIST_INIT(&hashtbl_slist[i]);
|
|
|
|
break;
|
2007-07-28 16:53:52 +04:00
|
|
|
case HASH_TAILQ:
|
|
|
|
hashtbl_tailq = p;
|
|
|
|
for (i = 0; i < hashsize; i++)
|
|
|
|
TAILQ_INIT(&hashtbl_tailq[i]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*hashmask = hashsize - 1;
|
2012-06-06 00:51:36 +04:00
|
|
|
return p;
|
2007-07-28 16:53:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free memory from hash table previosly allocated via hashinit().
|
|
|
|
*/
|
|
|
|
void
|
2008-05-05 21:11:16 +04:00
|
|
|
hashdone(void *hashtbl, enum hashtype htype, u_long hashmask)
|
2007-07-28 16:53:52 +04:00
|
|
|
{
|
2012-06-06 00:51:36 +04:00
|
|
|
const size_t esize = hash_list_size(htype);
|
2008-05-05 21:11:16 +04:00
|
|
|
kmem_free(hashtbl, esize * (hashmask + 1));
|
2007-07-28 16:53:52 +04:00
|
|
|
}
|