hcreate.c revision 76613
1283625Sdim/* $NetBSD: hcreate.c,v 1.2 2001/02/19 21:26:04 ross Exp $ */
2283625Sdim/* $FreeBSD: head/lib/libc/stdlib/hcreate.c 76613 2001-05-15 07:08:20Z ru $ */
3283625Sdim
4283625Sdim/*
5283625Sdim * Copyright (c) 2001 Christopher G. Demetriou
6283625Sdim * All rights reserved.
7283625Sdim *
8283625Sdim * Redistribution and use in source and binary forms, with or without
9283625Sdim * modification, are permitted provided that the following conditions
10283625Sdim * are met:
11283625Sdim * 1. Redistributions of source code must retain the above copyright
12283625Sdim *    notice, this list of conditions and the following disclaimer.
13283625Sdim * 2. Redistributions in binary form must reproduce the above copyright
14283625Sdim *    notice, this list of conditions and the following disclaimer in the
15283625Sdim *    documentation and/or other materials provided with the distribution.
16283625Sdim * 3. All advertising materials mentioning features or use of this software
17283625Sdim *    must display the following acknowledgement:
18283625Sdim *          This product includes software developed for the
19283625Sdim *          NetBSD Project.  See http://www.netbsd.org/ for
20283625Sdim *          information about NetBSD.
21283625Sdim * 4. The name of the author may not be used to endorse or promote products
22283625Sdim *    derived from this software without specific prior written permission.
23283625Sdim *
24283625Sdim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25283625Sdim * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26283625Sdim * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27283625Sdim * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28283625Sdim * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29283625Sdim * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30283625Sdim * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31283625Sdim * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32283625Sdim * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33283625Sdim * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34283625Sdim *
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#include "namespace.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 */
67struct internal_entry {
68	SLIST_ENTRY(internal_entry) link;
69	ENTRY ent;
70};
71SLIST_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#define	MAX_BUCKETS_LG2	(sizeof (size_t) * 8 - 1 - 5)
81#define	MAX_BUCKETS	((size_t)1 << MAX_BUCKETS_LG2)
82
83/* Default hash function, from db/hash/hash_func.c */
84extern u_int32_t (*__default_hash)(const void *, size_t);
85
86static struct internal_head *htable;
87static size_t htablesize;
88
89int
90hcreate(size_t nel)
91{
92	size_t idx;
93	unsigned int p2;
94
95	/* Make sure this this isn't called when a table already exists. */
96	if (htable != NULL) {
97		errno = EINVAL;
98		return 0;
99	}
100
101	/* If nel is too small, make it min sized. */
102	if (nel < MIN_BUCKETS)
103		nel = MIN_BUCKETS;
104
105	/* If it's too large, cap it. */
106	if (nel > MAX_BUCKETS)
107		nel = MAX_BUCKETS;
108
109	/* If it's is not a power of two in size, round up. */
110	if ((nel & (nel - 1)) != 0) {
111		for (p2 = 0; nel != 0; p2++)
112			nel >>= 1;
113		nel = 1 << p2;
114	}
115
116	/* Allocate the table. */
117	htablesize = nel;
118	htable = malloc(htablesize * sizeof htable[0]);
119	if (htable == NULL) {
120		errno = ENOMEM;
121		return 0;
122	}
123
124	/* Initialize it. */
125	for (idx = 0; idx < htablesize; idx++)
126		SLIST_INIT(&htable[idx]);
127
128	return 1;
129}
130
131void
132hdestroy(void)
133{
134	struct internal_entry *ie;
135	size_t idx;
136
137	if (htable == NULL)
138		return;
139
140	for (idx = 0; idx < htablesize; idx++) {
141		while (!SLIST_EMPTY(&htable[idx])) {
142			ie = SLIST_FIRST(&htable[idx]);
143			SLIST_REMOVE_HEAD(&htable[idx], link);
144			free(ie->ent.key);
145			free(ie);
146		}
147	}
148	free(htable);
149	htable = NULL;
150}
151
152ENTRY *
153hsearch(ENTRY item, ACTION action)
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 = &htable[hashval & (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		return &ie->ent;
173	else if (action == FIND)
174		return NULL;
175
176	ie = malloc(sizeof *ie);
177	if (ie == NULL)
178		return NULL;
179	ie->ent.key = item.key;
180	ie->ent.data = item.data;
181
182	SLIST_INSERT_HEAD(head, ie, link);
183	return &ie->ent;
184}
185