xref: /freebsd/sys/kern/subr_hash.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
128993443SEd Schouten /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
428993443SEd Schouten  * Copyright (c) 1982, 1986, 1991, 1993
528993443SEd Schouten  *	The Regents of the University of California.  All rights reserved.
628993443SEd Schouten  * (c) UNIX System Laboratories, Inc.
728993443SEd Schouten  * All or some portions of this file are derived from material licensed
828993443SEd Schouten  * to the University of California by American Telephone and Telegraph
928993443SEd Schouten  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
1028993443SEd Schouten  * the permission of UNIX System Laboratories, Inc.
1128993443SEd Schouten  *
1228993443SEd Schouten  * Redistribution and use in source and binary forms, with or without
1328993443SEd Schouten  * modification, are permitted provided that the following conditions
1428993443SEd Schouten  * are met:
1528993443SEd Schouten  * 1. Redistributions of source code must retain the above copyright
1628993443SEd Schouten  *    notice, this list of conditions and the following disclaimer.
1728993443SEd Schouten  * 2. Redistributions in binary form must reproduce the above copyright
1828993443SEd Schouten  *    notice, this list of conditions and the following disclaimer in the
1928993443SEd Schouten  *    documentation and/or other materials provided with the distribution.
2069a28758SEd Maste  * 3. Neither the name of the University nor the names of its contributors
2128993443SEd Schouten  *    may be used to endorse or promote products derived from this software
2228993443SEd Schouten  *    without specific prior written permission.
2328993443SEd Schouten  *
2428993443SEd Schouten  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2528993443SEd Schouten  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2628993443SEd Schouten  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2728993443SEd Schouten  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2828993443SEd Schouten  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2928993443SEd Schouten  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3028993443SEd Schouten  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3128993443SEd Schouten  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3228993443SEd Schouten  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3328993443SEd Schouten  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3428993443SEd Schouten  * SUCH DAMAGE.
3528993443SEd Schouten  */
3628993443SEd Schouten 
3728993443SEd Schouten #include <sys/param.h>
3828993443SEd Schouten #include <sys/systm.h>
3928993443SEd Schouten #include <sys/malloc.h>
4028993443SEd Schouten 
41d5f0ea7cSSepherosa Ziehau static __inline int
hash_mflags(int flags)42d5f0ea7cSSepherosa Ziehau hash_mflags(int flags)
43d5f0ea7cSSepherosa Ziehau {
44d5f0ea7cSSepherosa Ziehau 
45d5f0ea7cSSepherosa Ziehau 	return ((flags & HASH_NOWAIT) ? M_NOWAIT : M_WAITOK);
46d5f0ea7cSSepherosa Ziehau }
47d5f0ea7cSSepherosa Ziehau 
4828993443SEd Schouten /*
4928993443SEd Schouten  * General routine to allocate a hash table with control of memory flags.
5028993443SEd Schouten  */
5128993443SEd Schouten void *
hashinit_flags(int elements,struct malloc_type * type,u_long * hashmask,int flags)5228993443SEd Schouten hashinit_flags(int elements, struct malloc_type *type, u_long *hashmask,
5328993443SEd Schouten     int flags)
5428993443SEd Schouten {
55*d821d364SPedro F. Giffuni 	long hashsize, i;
5628993443SEd Schouten 	LIST_HEAD(generic, generic) *hashtbl;
5728993443SEd Schouten 
5893a1b4c4SGleb Smirnoff 	KASSERT(elements > 0, ("%s: bad elements", __func__));
5928993443SEd Schouten 	/* Exactly one of HASH_WAITOK and HASH_NOWAIT must be set. */
6028993443SEd Schouten 	KASSERT((flags & HASH_WAITOK) ^ (flags & HASH_NOWAIT),
6128993443SEd Schouten 	    ("Bad flags (0x%x) passed to hashinit_flags", flags));
6228993443SEd Schouten 
6328993443SEd Schouten 	for (hashsize = 1; hashsize <= elements; hashsize <<= 1)
6428993443SEd Schouten 		continue;
6528993443SEd Schouten 	hashsize >>= 1;
6628993443SEd Schouten 
67d5f0ea7cSSepherosa Ziehau 	hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type,
68d5f0ea7cSSepherosa Ziehau 	    hash_mflags(flags));
6928993443SEd Schouten 	if (hashtbl != NULL) {
7028993443SEd Schouten 		for (i = 0; i < hashsize; i++)
7128993443SEd Schouten 			LIST_INIT(&hashtbl[i]);
7228993443SEd Schouten 		*hashmask = hashsize - 1;
7328993443SEd Schouten 	}
7428993443SEd Schouten 	return (hashtbl);
7528993443SEd Schouten }
7628993443SEd Schouten 
7728993443SEd Schouten /*
7828993443SEd Schouten  * Allocate and initialize a hash table with default flag: may sleep.
7928993443SEd Schouten  */
8028993443SEd Schouten void *
hashinit(int elements,struct malloc_type * type,u_long * hashmask)8128993443SEd Schouten hashinit(int elements, struct malloc_type *type, u_long *hashmask)
8228993443SEd Schouten {
8328993443SEd Schouten 
8428993443SEd Schouten 	return (hashinit_flags(elements, type, hashmask, HASH_WAITOK));
8528993443SEd Schouten }
8628993443SEd Schouten 
8728993443SEd Schouten void
hashdestroy(void * vhashtbl,struct malloc_type * type,u_long hashmask)8828993443SEd Schouten hashdestroy(void *vhashtbl, struct malloc_type *type, u_long hashmask)
8928993443SEd Schouten {
9028993443SEd Schouten 	LIST_HEAD(generic, generic) *hashtbl, *hp;
9128993443SEd Schouten 
9228993443SEd Schouten 	hashtbl = vhashtbl;
9328993443SEd Schouten 	for (hp = hashtbl; hp <= &hashtbl[hashmask]; hp++)
94029f99dcSBjoern A. Zeeb 		KASSERT(LIST_EMPTY(hp), ("%s: hashtbl %p not empty "
95029f99dcSBjoern A. Zeeb 		    "(malloc type %s)", __func__, hashtbl, type->ks_shortdesc));
9628993443SEd Schouten 	free(hashtbl, type);
9728993443SEd Schouten }
9828993443SEd Schouten 
9928993443SEd Schouten static const int primes[] = { 1, 13, 31, 61, 127, 251, 509, 761, 1021, 1531,
10028993443SEd Schouten 			2039, 2557, 3067, 3583, 4093, 4603, 5119, 5623, 6143,
10128993443SEd Schouten 			6653, 7159, 7673, 8191, 12281, 16381, 24571, 32749 };
10202abd400SPedro F. Giffuni #define	NPRIMES nitems(primes)
10328993443SEd Schouten 
10428993443SEd Schouten /*
105f8ce3dfaSSepherosa Ziehau  * General routine to allocate a prime number sized hash table with control of
106f8ce3dfaSSepherosa Ziehau  * memory flags.
10728993443SEd Schouten  */
10828993443SEd Schouten void *
phashinit_flags(int elements,struct malloc_type * type,u_long * nentries,int flags)109f8ce3dfaSSepherosa Ziehau phashinit_flags(int elements, struct malloc_type *type, u_long *nentries, int flags)
11028993443SEd Schouten {
111*d821d364SPedro F. Giffuni 	long hashsize, i;
11228993443SEd Schouten 	LIST_HEAD(generic, generic) *hashtbl;
11328993443SEd Schouten 
11493a1b4c4SGleb Smirnoff 	KASSERT(elements > 0, ("%s: bad elements", __func__));
115f8ce3dfaSSepherosa Ziehau 	/* Exactly one of HASH_WAITOK and HASH_NOWAIT must be set. */
116f8ce3dfaSSepherosa Ziehau 	KASSERT((flags & HASH_WAITOK) ^ (flags & HASH_NOWAIT),
117f8ce3dfaSSepherosa Ziehau 	    ("Bad flags (0x%x) passed to phashinit_flags", flags));
118f8ce3dfaSSepherosa Ziehau 
11928993443SEd Schouten 	for (i = 1, hashsize = primes[1]; hashsize <= elements;) {
12028993443SEd Schouten 		i++;
12128993443SEd Schouten 		if (i == NPRIMES)
12228993443SEd Schouten 			break;
12328993443SEd Schouten 		hashsize = primes[i];
12428993443SEd Schouten 	}
12528993443SEd Schouten 	hashsize = primes[i - 1];
126f8ce3dfaSSepherosa Ziehau 
127d5f0ea7cSSepherosa Ziehau 	hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type,
128d5f0ea7cSSepherosa Ziehau 	    hash_mflags(flags));
129f8ce3dfaSSepherosa Ziehau 	if (hashtbl == NULL)
130f8ce3dfaSSepherosa Ziehau 		return (NULL);
131f8ce3dfaSSepherosa Ziehau 
13228993443SEd Schouten 	for (i = 0; i < hashsize; i++)
13328993443SEd Schouten 		LIST_INIT(&hashtbl[i]);
13428993443SEd Schouten 	*nentries = hashsize;
13528993443SEd Schouten 	return (hashtbl);
13628993443SEd Schouten }
137f8ce3dfaSSepherosa Ziehau 
138f8ce3dfaSSepherosa Ziehau /*
139f8ce3dfaSSepherosa Ziehau  * Allocate and initialize a prime number sized hash table with default flag:
140f8ce3dfaSSepherosa Ziehau  * may sleep.
141f8ce3dfaSSepherosa Ziehau  */
142f8ce3dfaSSepherosa Ziehau void *
phashinit(int elements,struct malloc_type * type,u_long * nentries)143f8ce3dfaSSepherosa Ziehau phashinit(int elements, struct malloc_type *type, u_long *nentries)
144f8ce3dfaSSepherosa Ziehau {
145f8ce3dfaSSepherosa Ziehau 
146f8ce3dfaSSepherosa Ziehau 	return (phashinit_flags(elements, type, nentries, HASH_WAITOK));
147f8ce3dfaSSepherosa Ziehau }
148