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. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * <<Id: LICENSE_GC,v 1.1 2001/10/01 23:24:05 cgd Exp>>
30 */
31
32 /*
33 * hcreate() / hsearch() / hdestroy()
34 *
35 * SysV/XPG4 hash table functions.
36 *
37 * Implementation done based on NetBSD manual page and Solaris manual page,
38 * plus my own personal experience about how they're supposed to work.
39 *
40 * I tried to look at Knuth (as cited by the Solaris manual page), but
41 * nobody had a copy in the office, so...
42 */
43
44 #define _DEFAULT_SOURCE
45 #if 0
46 #if defined(LIBC_SCCS) && !defined(lint)
47 #endif /* LIBC_SCCS and not lint */
48 #endif
49
50 #include <sys/types.h>
51 #include <sys/queue.h>
52 #include <errno.h>
53 #include <search.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include "db_local.h"
57 #include "hash.h"
58 #include "page.h"
59 #include "extern.h"
60
61 /*
62 * DO NOT MAKE THIS STRUCTURE LARGER THAN 32 BYTES (4 ptrs on 64-bit
63 * ptr machine) without adjusting MAX_BUCKETS_LG2 below.
64 */
65 struct internal_entry {
66 SLIST_ENTRY(internal_entry) link;
67 ENTRY ent;
68 };
69 SLIST_HEAD(internal_head, internal_entry);
70
71 #define MIN_BUCKETS_LG2 4
72 #define MIN_BUCKETS (1 << MIN_BUCKETS_LG2)
73
74 /*
75 * max * sizeof internal_entry must fit into size_t.
76 * assumes internal_entry is <= 32 (2^5) bytes.
77 */
78 #ifdef __MSP430X_LARGE__
79 /* 20-bit size_t. */
80 #define MAX_BUCKETS_LG2 (20 - 1 - 5)
81 #else
82 #define MAX_BUCKETS_LG2 (sizeof (size_t) * 8 - 1 - 5)
83 #endif
84 #define MAX_BUCKETS ((size_t)1 << MAX_BUCKETS_LG2)
85
86 int
hcreate_r(size_t nel,struct hsearch_data * htab)87 hcreate_r(size_t nel, struct hsearch_data *htab)
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 (htab->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 htab->htablesize = nel;
115 htab->htable = malloc(htab->htablesize * sizeof htab->htable[0]);
116 if (htab->htable == NULL) {
117 errno = ENOMEM;
118 return 0;
119 }
120
121 /* Initialize it. */
122 for (idx = 0; idx < htab->htablesize; idx++)
123 SLIST_INIT(&(htab->htable[idx]));
124
125 return 1;
126 }
127
128 void
hdestroy_r(struct hsearch_data * htab)129 hdestroy_r(struct hsearch_data *htab)
130 {
131 #if 0
132 struct internal_entry *ie;
133 size_t idx;
134 #endif
135 if (htab->htable == NULL)
136 return;
137
138 #if 0
139 for (idx = 0; idx < htab->htablesize; idx++) {
140 while (!SLIST_EMPTY(&(htab->htable[idx]))) {
141 ie = SLIST_FIRST(&(htab->htable[idx]));
142 SLIST_REMOVE_HEAD(&(htab->htable[idx]), link);
143 free(ie->ent.key);
144 free(ie);
145 }
146 }
147 #endif
148 free(htab->htable);
149 htab->htable = NULL;
150 }
151
152 int
hsearch_r(ENTRY item,ACTION action,ENTRY ** retval,struct hsearch_data * htab)153 hsearch_r(ENTRY item, ACTION action, ENTRY **retval, struct hsearch_data *htab)
154 {
155 struct internal_head *head;
156 struct internal_entry *ie;
157 __uint32_t hashval;
158 size_t len;
159
160 len = strlen(item.key);
161 hashval = __default_hash(item.key, len);
162
163 head = &(htab->htable[hashval & (htab->htablesize - 1)]);
164 ie = SLIST_FIRST(head);
165 while (ie != NULL) {
166 if (strcmp(ie->ent.key, item.key) == 0)
167 break;
168 ie = SLIST_NEXT(ie, link);
169 }
170
171 if (ie != NULL)
172 {
173 *retval = &ie->ent;
174 return 1;
175 }
176 else if (action == FIND)
177 {
178 *retval = NULL;
179 return 0;
180 }
181
182 ie = malloc(sizeof *ie);
183 if (ie == NULL)
184 {
185 *retval = NULL;
186 return 0;
187 }
188 ie->ent.key = item.key;
189 ie->ent.data = item.data;
190
191 SLIST_INSERT_HEAD(head, ie, link);
192 *retval = &ie->ent;
193 return 1;
194 }
195