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 #include <sys/cdefs.h>
46 #if 0
47 #if defined(LIBC_SCCS) && !defined(lint)
48 __RCSID("$NetBSD: hcreate.c,v 1.2 2001/02/19 21:26:04 ross Exp $");
49 #endif /* LIBC_SCCS and not lint */
50 #endif
51 
52 #include <sys/types.h>
53 #include <sys/queue.h>
54 #include <errno.h>
55 #include <search.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include "db_local.h"
59 #include "hash.h"
60 #include "page.h"
61 #include "extern.h"
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 #ifdef __MSP430X_LARGE__
81 /* 20-bit size_t.  */
82 #define	MAX_BUCKETS_LG2	(20 - 1 - 5)
83 #else
84 #define	MAX_BUCKETS_LG2	(sizeof (size_t) * 8 - 1 - 5)
85 #endif
86 #define	MAX_BUCKETS	((size_t)1 << MAX_BUCKETS_LG2)
87 
88 int
hcreate_r(size_t nel,struct hsearch_data * htab)89 hcreate_r(size_t nel, struct hsearch_data *htab)
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 (htab->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 	htab->htablesize = nel;
117 	htab->htable = malloc(htab->htablesize * sizeof htab->htable[0]);
118 	if (htab->htable == NULL) {
119 		errno = ENOMEM;
120 		return 0;
121 	}
122 
123 	/* Initialize it. */
124 	for (idx = 0; idx < htab->htablesize; idx++)
125 		SLIST_INIT(&(htab->htable[idx]));
126 
127 	return 1;
128 }
129 
130 void
hdestroy_r(struct hsearch_data * htab)131 hdestroy_r(struct hsearch_data *htab)
132 {
133 #if 0
134 	struct internal_entry *ie;
135 	size_t idx;
136 #endif
137 	if (htab->htable == NULL)
138 		return;
139 
140 #if 0
141 	for (idx = 0; idx < htab->htablesize; idx++) {
142 		while (!SLIST_EMPTY(&(htab->htable[idx]))) {
143 			ie = SLIST_FIRST(&(htab->htable[idx]));
144 			SLIST_REMOVE_HEAD(&(htab->htable[idx]), link);
145                         free(ie->ent.key);
146 			free(ie);
147 		}
148 	}
149 #endif
150 	free(htab->htable);
151 	htab->htable = NULL;
152 }
153 
154 int
hsearch_r(ENTRY item,ACTION action,ENTRY ** retval,struct hsearch_data * htab)155 hsearch_r(ENTRY item, ACTION action, ENTRY **retval, struct hsearch_data *htab)
156 {
157 	struct internal_head *head;
158 	struct internal_entry *ie;
159 	__uint32_t hashval;
160 	size_t len;
161 
162 	len = strlen(item.key);
163 	hashval = __default_hash(item.key, len);
164 
165         head = &(htab->htable[hashval & (htab->htablesize - 1)]);
166 	ie = SLIST_FIRST(head);
167 	while (ie != NULL) {
168 		if (strcmp(ie->ent.key, item.key) == 0)
169 			break;
170 		ie = SLIST_NEXT(ie, link);
171 	}
172 
173 	if (ie != NULL)
174           {
175             *retval = &ie->ent;
176             return 1;
177           }
178 	else if (action == FIND)
179           {
180             *retval = NULL;
181             return 0;
182           }
183 
184 	ie = malloc(sizeof *ie);
185 	if (ie == NULL)
186           {
187             *retval = NULL;
188             return 0;
189           }
190 	ie->ent.key = item.key;
191 	ie->ent.data = item.data;
192 
193 	SLIST_INSERT_HEAD(head, ie, link);
194         *retval = &ie->ent;
195 	return 1;
196 }
197