Initial import from FreeBSD RELENG_4:
[dragonfly.git] / lib / libcr / stdlib / hcreate.c
1 /* $NetBSD: hcreate.c,v 1.2 2001/02/19 21:26:04 ross Exp $ */
2 /* $FreeBSD: src/lib/libc/stdlib/hcreate.c,v 1.1.2.2 2001/10/02 11:22:56 ru 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 <sys/cdefs.h>
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
55 #include <sys/types.h>
56 #include <sys/queue.h>
57 #include <errno.h>
58 #include <search.h>
59 #include <stdlib.h>
60 #include <string.h>
61
62 /*
63  * DO NOT MAKE THIS STRUCTURE LARGER THAN 32 BYTES (4 ptrs on 64-bit
64  * ptr machine) without adjusting MAX_BUCKETS_LG2 below.
65  */
66 struct internal_entry {
67         SLIST_ENTRY(internal_entry) link;
68         ENTRY ent;
69 };
70 SLIST_HEAD(internal_head, internal_entry);
71
72 #define MIN_BUCKETS_LG2 4
73 #define MIN_BUCKETS     (1 << MIN_BUCKETS_LG2)
74
75 /*
76  * max * sizeof internal_entry must fit into size_t.
77  * assumes internal_entry is <= 32 (2^5) bytes.
78  */
79 #define MAX_BUCKETS_LG2 (sizeof (size_t) * 8 - 1 - 5)
80 #define MAX_BUCKETS     ((size_t)1 << MAX_BUCKETS_LG2)
81
82 /* Default hash function, from db/hash/hash_func.c */
83 extern u_int32_t (*__default_hash)(const void *, size_t);
84
85 static struct internal_head *htable;
86 static size_t htablesize;
87
88 int
89 hcreate(size_t nel)
90 {
91         size_t idx;
92         unsigned int p2;
93
94         /* Make sure this this isn't called when a table already exists. */
95         if (htable != NULL) {
96                 errno = EINVAL;
97                 return 0;
98         }
99
100         /* If nel is too small, make it min sized. */
101         if (nel < MIN_BUCKETS)
102                 nel = MIN_BUCKETS;
103
104         /* If it's too large, cap it. */
105         if (nel > MAX_BUCKETS)
106                 nel = MAX_BUCKETS;
107
108         /* If it's is not a power of two in size, round up. */
109         if ((nel & (nel - 1)) != 0) {
110                 for (p2 = 0; nel != 0; p2++)
111                         nel >>= 1;
112                 nel = 1 << p2;
113         }
114         
115         /* Allocate the table. */
116         htablesize = nel;
117         htable = malloc(htablesize * sizeof htable[0]);
118         if (htable == NULL) {
119                 errno = ENOMEM;
120                 return 0;
121         }
122
123         /* Initialize it. */
124         for (idx = 0; idx < htablesize; idx++)
125                 SLIST_INIT(&htable[idx]);
126
127         return 1;
128 }
129
130 void
131 hdestroy(void)
132 {
133         struct internal_entry *ie;
134         size_t idx;
135
136         if (htable == NULL)
137                 return;
138
139         for (idx = 0; idx < htablesize; idx++) {
140                 while (!SLIST_EMPTY(&htable[idx])) {
141                         ie = SLIST_FIRST(&htable[idx]);
142                         SLIST_REMOVE_HEAD(&htable[idx], link);
143                         free(ie->ent.key);
144                         free(ie);
145                 }
146         }
147         free(htable);
148         htable = NULL;
149 }
150
151 ENTRY *
152 hsearch(ENTRY item, ACTION action)
153 {
154         struct internal_head *head;
155         struct internal_entry *ie;
156         uint32_t hashval;
157         size_t len;
158
159         len = strlen(item.key);
160         hashval = (*__default_hash)(item.key, len);
161
162         head = &htable[hashval & (htablesize - 1)];
163         ie = SLIST_FIRST(head);
164         while (ie != NULL) {
165                 if (strcmp(ie->ent.key, item.key) == 0)
166                         break;
167                 ie = SLIST_NEXT(ie, link);
168         }
169
170         if (ie != NULL)
171                 return &ie->ent;
172         else if (action == FIND)
173                 return NULL;
174
175         ie = malloc(sizeof *ie);
176         if (ie == NULL)
177                 return NULL;
178         ie->ent.key = item.key;
179         ie->ent.data = item.data;
180
181         SLIST_INSERT_HEAD(head, ie, link);
182         return &ie->ent;
183 }