subr_hash.c revision 1.1.6.2 1 1.1.6.2 skrll /* $NetBSD: subr_hash.c,v 1.1.6.2 2007/08/15 13:49:12 skrll Exp $ */
2 1.1.6.2 skrll
3 1.1.6.2 skrll /*
4 1.1.6.2 skrll * Copyright (c) 1982, 1986, 1991, 1993
5 1.1.6.2 skrll * The Regents of the University of California. All rights reserved.
6 1.1.6.2 skrll * (c) UNIX System Laboratories, Inc.
7 1.1.6.2 skrll * All or some portions of this file are derived from material licensed
8 1.1.6.2 skrll * to the University of California by American Telephone and Telegraph
9 1.1.6.2 skrll * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 1.1.6.2 skrll * the permission of UNIX System Laboratories, Inc.
11 1.1.6.2 skrll *
12 1.1.6.2 skrll * Redistribution and use in source and binary forms, with or without
13 1.1.6.2 skrll * modification, are permitted provided that the following conditions
14 1.1.6.2 skrll * are met:
15 1.1.6.2 skrll * 1. Redistributions of source code must retain the above copyright
16 1.1.6.2 skrll * notice, this list of conditions and the following disclaimer.
17 1.1.6.2 skrll * 2. Redistributions in binary form must reproduce the above copyright
18 1.1.6.2 skrll * notice, this list of conditions and the following disclaimer in the
19 1.1.6.2 skrll * documentation and/or other materials provided with the distribution.
20 1.1.6.2 skrll * 3. Neither the name of the University nor the names of its contributors
21 1.1.6.2 skrll * may be used to endorse or promote products derived from this software
22 1.1.6.2 skrll * without specific prior written permission.
23 1.1.6.2 skrll *
24 1.1.6.2 skrll * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1.6.2 skrll * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1.6.2 skrll * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1.6.2 skrll * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1.6.2 skrll * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1.6.2 skrll * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1.6.2 skrll * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1.6.2 skrll * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1.6.2 skrll * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1.6.2 skrll * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1.6.2 skrll * SUCH DAMAGE.
35 1.1.6.2 skrll *
36 1.1.6.2 skrll * @(#)kern_subr.c 8.4 (Berkeley) 2/14/95
37 1.1.6.2 skrll */
38 1.1.6.2 skrll
39 1.1.6.2 skrll #include <sys/cdefs.h>
40 1.1.6.2 skrll __KERNEL_RCSID(0, "$NetBSD: subr_hash.c,v 1.1.6.2 2007/08/15 13:49:12 skrll Exp $");
41 1.1.6.2 skrll
42 1.1.6.2 skrll #include <sys/param.h>
43 1.1.6.2 skrll #include <sys/malloc.h>
44 1.1.6.2 skrll #include <sys/systm.h>
45 1.1.6.2 skrll
46 1.1.6.2 skrll /*
47 1.1.6.2 skrll * General routine to allocate a hash table.
48 1.1.6.2 skrll * Allocate enough memory to hold at least `elements' list-head pointers.
49 1.1.6.2 skrll * Return a pointer to the allocated space and set *hashmask to a pattern
50 1.1.6.2 skrll * suitable for masking a value to use as an index into the returned array.
51 1.1.6.2 skrll */
52 1.1.6.2 skrll void *
53 1.1.6.2 skrll hashinit(u_int elements, enum hashtype htype, struct malloc_type *mtype,
54 1.1.6.2 skrll int mflags, u_long *hashmask)
55 1.1.6.2 skrll {
56 1.1.6.2 skrll u_long hashsize, i;
57 1.1.6.2 skrll LIST_HEAD(, generic) *hashtbl_list;
58 1.1.6.2 skrll TAILQ_HEAD(, generic) *hashtbl_tailq;
59 1.1.6.2 skrll size_t esize;
60 1.1.6.2 skrll void *p;
61 1.1.6.2 skrll
62 1.1.6.2 skrll if (elements == 0)
63 1.1.6.2 skrll panic("hashinit: bad cnt");
64 1.1.6.2 skrll for (hashsize = 1; hashsize < elements; hashsize <<= 1)
65 1.1.6.2 skrll continue;
66 1.1.6.2 skrll
67 1.1.6.2 skrll switch (htype) {
68 1.1.6.2 skrll case HASH_LIST:
69 1.1.6.2 skrll esize = sizeof(*hashtbl_list);
70 1.1.6.2 skrll break;
71 1.1.6.2 skrll case HASH_TAILQ:
72 1.1.6.2 skrll esize = sizeof(*hashtbl_tailq);
73 1.1.6.2 skrll break;
74 1.1.6.2 skrll default:
75 1.1.6.2 skrll #ifdef DIAGNOSTIC
76 1.1.6.2 skrll panic("hashinit: invalid table type");
77 1.1.6.2 skrll #else
78 1.1.6.2 skrll return NULL;
79 1.1.6.2 skrll #endif
80 1.1.6.2 skrll }
81 1.1.6.2 skrll
82 1.1.6.2 skrll if ((p = malloc(hashsize * esize, mtype, mflags)) == NULL)
83 1.1.6.2 skrll return (NULL);
84 1.1.6.2 skrll
85 1.1.6.2 skrll switch (htype) {
86 1.1.6.2 skrll case HASH_LIST:
87 1.1.6.2 skrll hashtbl_list = p;
88 1.1.6.2 skrll for (i = 0; i < hashsize; i++)
89 1.1.6.2 skrll LIST_INIT(&hashtbl_list[i]);
90 1.1.6.2 skrll break;
91 1.1.6.2 skrll case HASH_TAILQ:
92 1.1.6.2 skrll hashtbl_tailq = p;
93 1.1.6.2 skrll for (i = 0; i < hashsize; i++)
94 1.1.6.2 skrll TAILQ_INIT(&hashtbl_tailq[i]);
95 1.1.6.2 skrll break;
96 1.1.6.2 skrll }
97 1.1.6.2 skrll *hashmask = hashsize - 1;
98 1.1.6.2 skrll return (p);
99 1.1.6.2 skrll }
100 1.1.6.2 skrll
101 1.1.6.2 skrll /*
102 1.1.6.2 skrll * Free memory from hash table previosly allocated via hashinit().
103 1.1.6.2 skrll */
104 1.1.6.2 skrll void
105 1.1.6.2 skrll hashdone(void *hashtbl, struct malloc_type *mtype)
106 1.1.6.2 skrll {
107 1.1.6.2 skrll
108 1.1.6.2 skrll free(hashtbl, mtype);
109 1.1.6.2 skrll }
110