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