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