xref: /freebsd/lib/libc/stdlib/hcreate.c (revision 1669d8afc64812c8d2d1d147ae1fd42ff441e1b1)
1 /* $NetBSD: hcreate.c,v 1.2 2001/02/19 21:26:04 ross Exp $ */
2 
3 /*
4  * Copyright (c) 2001 Christopher G. Demetriou
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *          This product includes software developed for the
18  *          NetBSD Project.  See http://www.netbsd.org/ for
19  *          information about NetBSD.
20  * 4. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
35  */
36 
37 /*
38  * hcreate() / hsearch() / hdestroy()
39  *
40  * SysV/XPG4 hash table functions.
41  *
42  * Implementation done based on NetBSD manual page and Solaris manual page,
43  * plus my own personal experience about how they're supposed to work.
44  *
45  * I tried to look at Knuth (as cited by the Solaris manual page), but
46  * nobody had a copy in the office, so...
47  */
48 
49 #include <sys/cdefs.h>
50 #if 0
51 #if defined(LIBC_SCCS) && !defined(lint)
52 __RCSID("$NetBSD: hcreate.c,v 1.2 2001/02/19 21:26:04 ross Exp $");
53 #endif /* LIBC_SCCS and not lint */
54 #endif
55 __FBSDID("$FreeBSD$");
56 
57 #include <sys/types.h>
58 #include <sys/queue.h>
59 #include <errno.h>
60 #include <search.h>
61 #include <stdlib.h>
62 #include <string.h>
63 
64 /*
65  * DO NOT MAKE THIS STRUCTURE LARGER THAN 32 BYTES (4 ptrs on 64-bit
66  * ptr machine) without adjusting MAX_BUCKETS_LG2 below.
67  */
68 struct internal_entry {
69 	SLIST_ENTRY(internal_entry) link;
70 	ENTRY ent;
71 };
72 SLIST_HEAD(internal_head, internal_entry);
73 
74 #define	MIN_BUCKETS_LG2	4
75 #define	MIN_BUCKETS	(1 << MIN_BUCKETS_LG2)
76 
77 /*
78  * max * sizeof internal_entry must fit into size_t.
79  * assumes internal_entry is <= 32 (2^5) bytes.
80  */
81 #define	MAX_BUCKETS_LG2	(sizeof (size_t) * 8 - 1 - 5)
82 #define	MAX_BUCKETS	((size_t)1 << MAX_BUCKETS_LG2)
83 
84 /* Default hash function, from db/hash/hash_func.c */
85 extern u_int32_t (*__default_hash)(const void *, size_t);
86 
87 static struct internal_head *htable;
88 static size_t htablesize;
89 
90 int
91 hcreate(size_t nel)
92 {
93 	size_t idx;
94 	unsigned int p2;
95 
96 	/* Make sure this this isn't called when a table already exists. */
97 	if (htable != NULL) {
98 		errno = EINVAL;
99 		return 0;
100 	}
101 
102 	/* If nel is too small, make it min sized. */
103 	if (nel < MIN_BUCKETS)
104 		nel = MIN_BUCKETS;
105 
106 	/* If it's too large, cap it. */
107 	if (nel > MAX_BUCKETS)
108 		nel = MAX_BUCKETS;
109 
110 	/* If it's is not a power of two in size, round up. */
111 	if ((nel & (nel - 1)) != 0) {
112 		for (p2 = 0; nel != 0; p2++)
113 			nel >>= 1;
114 		nel = 1 << p2;
115 	}
116 
117 	/* Allocate the table. */
118 	htablesize = nel;
119 	htable = malloc(htablesize * sizeof htable[0]);
120 	if (htable == NULL) {
121 		errno = ENOMEM;
122 		return 0;
123 	}
124 
125 	/* Initialize it. */
126 	for (idx = 0; idx < htablesize; idx++)
127 		SLIST_INIT(&htable[idx]);
128 
129 	return 1;
130 }
131 
132 void
133 hdestroy(void)
134 {
135 	struct internal_entry *ie;
136 	size_t idx;
137 
138 	if (htable == NULL)
139 		return;
140 
141 	for (idx = 0; idx < htablesize; idx++) {
142 		while (!SLIST_EMPTY(&htable[idx])) {
143 			ie = SLIST_FIRST(&htable[idx]);
144 			SLIST_REMOVE_HEAD(&htable[idx], link);
145 			free(ie->ent.key);
146 			free(ie);
147 		}
148 	}
149 	free(htable);
150 	htable = NULL;
151 }
152 
153 ENTRY *
154 hsearch(ENTRY item, ACTION action)
155 {
156 	struct internal_head *head;
157 	struct internal_entry *ie;
158 	uint32_t hashval;
159 	size_t len;
160 
161 	len = strlen(item.key);
162 	hashval = (*__default_hash)(item.key, len);
163 
164 	head = &htable[hashval & (htablesize - 1)];
165 	ie = SLIST_FIRST(head);
166 	while (ie != NULL) {
167 		if (strcmp(ie->ent.key, item.key) == 0)
168 			break;
169 		ie = SLIST_NEXT(ie, link);
170 	}
171 
172 	if (ie != NULL)
173 		return &ie->ent;
174 	else if (action == FIND)
175 		return NULL;
176 
177 	ie = malloc(sizeof *ie);
178 	if (ie == NULL)
179 		return NULL;
180 	ie->ent.key = item.key;
181 	ie->ent.data = item.data;
182 
183 	SLIST_INSERT_HEAD(head, ie, link);
184 	return &ie->ent;
185 }
186