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