1*b5745d08Sguenther /* $OpenBSD: hcreate.c,v 1.7 2016/05/29 20:47:49 guenther Exp $ */
246f8fdc8Smillert /* $NetBSD: hcreate.c,v 1.5 2004/04/23 02:48:12 simonb Exp $ */
346f8fdc8Smillert
446f8fdc8Smillert /*
546f8fdc8Smillert * Copyright (c) 2001 Christopher G. Demetriou
646f8fdc8Smillert * All rights reserved.
746f8fdc8Smillert *
846f8fdc8Smillert * Redistribution and use in source and binary forms, with or without
946f8fdc8Smillert * modification, are permitted provided that the following conditions
1046f8fdc8Smillert * are met:
1146f8fdc8Smillert * 1. Redistributions of source code must retain the above copyright
1246f8fdc8Smillert * notice, this list of conditions and the following disclaimer.
1346f8fdc8Smillert * 2. Redistributions in binary form must reproduce the above copyright
1446f8fdc8Smillert * notice, this list of conditions and the following disclaimer in the
1546f8fdc8Smillert * documentation and/or other materials provided with the distribution.
1646f8fdc8Smillert * 3. All advertising materials mentioning features or use of this software
1746f8fdc8Smillert * must display the following acknowledgement:
1846f8fdc8Smillert * This product includes software developed for the
1946f8fdc8Smillert * NetBSD Project. See http://www.NetBSD.org/ for
2046f8fdc8Smillert * information about NetBSD.
2146f8fdc8Smillert * 4. The name of the author may not be used to endorse or promote products
2246f8fdc8Smillert * derived from this software without specific prior written permission.
2346f8fdc8Smillert *
2446f8fdc8Smillert * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2546f8fdc8Smillert * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2646f8fdc8Smillert * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2746f8fdc8Smillert * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2846f8fdc8Smillert * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2946f8fdc8Smillert * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3046f8fdc8Smillert * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3146f8fdc8Smillert * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3246f8fdc8Smillert * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3346f8fdc8Smillert * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3446f8fdc8Smillert *
3546f8fdc8Smillert * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
3646f8fdc8Smillert */
3746f8fdc8Smillert
3846f8fdc8Smillert /*
3946f8fdc8Smillert * hcreate() / hsearch() / hdestroy()
4046f8fdc8Smillert *
4146f8fdc8Smillert * SysV/XPG4 hash table functions.
4246f8fdc8Smillert *
4346f8fdc8Smillert * Implementation done based on NetBSD manual page and Solaris manual page,
4446f8fdc8Smillert * plus my own personal experience about how they're supposed to work.
4546f8fdc8Smillert *
4646f8fdc8Smillert * I tried to look at Knuth (as cited by the Solaris manual page), but
4746f8fdc8Smillert * nobody had a copy in the office, so...
4846f8fdc8Smillert */
4946f8fdc8Smillert
5046f8fdc8Smillert #include <assert.h>
5146f8fdc8Smillert #include <errno.h>
52bb2e986cSguenther #include <stdint.h>
5346f8fdc8Smillert #include <search.h>
5446f8fdc8Smillert #include <stdlib.h>
5546f8fdc8Smillert #include <string.h>
5646f8fdc8Smillert #include <sys/queue.h>
5746f8fdc8Smillert
58*b5745d08Sguenther #include <db.h> /* for __default_hash */
59*b5745d08Sguenther
6046f8fdc8Smillert #ifndef _DIAGASSERT
61c919a5c6Sespie #define _DIAGASSERT(x)
6246f8fdc8Smillert #endif
6346f8fdc8Smillert
6446f8fdc8Smillert /*
6546f8fdc8Smillert * DO NOT MAKE THIS STRUCTURE LARGER THAN 32 BYTES (4 ptrs on 64-bit
6646f8fdc8Smillert * ptr machine) without adjusting MAX_BUCKETS_LG2 below.
6746f8fdc8Smillert */
6846f8fdc8Smillert struct internal_entry {
6946f8fdc8Smillert SLIST_ENTRY(internal_entry) link;
7046f8fdc8Smillert ENTRY ent;
7146f8fdc8Smillert };
7246f8fdc8Smillert SLIST_HEAD(internal_head, internal_entry);
7346f8fdc8Smillert
7446f8fdc8Smillert #define MIN_BUCKETS_LG2 4
7546f8fdc8Smillert #define MIN_BUCKETS (1 << MIN_BUCKETS_LG2)
7646f8fdc8Smillert
7746f8fdc8Smillert /*
7846f8fdc8Smillert * max * sizeof internal_entry must fit into size_t.
7946f8fdc8Smillert * assumes internal_entry is <= 32 (2^5) bytes.
8046f8fdc8Smillert */
8146f8fdc8Smillert #define MAX_BUCKETS_LG2 (sizeof (size_t) * 8 - 1 - 5)
8246f8fdc8Smillert #define MAX_BUCKETS ((size_t)1 << MAX_BUCKETS_LG2)
8346f8fdc8Smillert
8446f8fdc8Smillert static struct internal_head *htable;
8546f8fdc8Smillert static size_t htablesize;
8646f8fdc8Smillert
8746f8fdc8Smillert int
hcreate(size_t nel)8846f8fdc8Smillert hcreate(size_t nel)
8946f8fdc8Smillert {
9046f8fdc8Smillert size_t idx;
9146f8fdc8Smillert unsigned int p2;
9246f8fdc8Smillert
9346f8fdc8Smillert /* Make sure this isn't called when a table already exists. */
9446f8fdc8Smillert _DIAGASSERT(htable == NULL);
9546f8fdc8Smillert if (htable != NULL) {
9646f8fdc8Smillert errno = EINVAL;
9746f8fdc8Smillert return 0;
9846f8fdc8Smillert }
9946f8fdc8Smillert
10046f8fdc8Smillert /* If nel is too small, make it min sized. */
10146f8fdc8Smillert if (nel < MIN_BUCKETS)
10246f8fdc8Smillert nel = MIN_BUCKETS;
10346f8fdc8Smillert
10446f8fdc8Smillert /* If it's too large, cap it. */
10546f8fdc8Smillert if (nel > MAX_BUCKETS)
10646f8fdc8Smillert nel = MAX_BUCKETS;
10746f8fdc8Smillert
10846f8fdc8Smillert /* If it's is not a power of two in size, round up. */
10946f8fdc8Smillert if ((nel & (nel - 1)) != 0) {
11046f8fdc8Smillert for (p2 = 0; nel != 0; p2++)
11146f8fdc8Smillert nel >>= 1;
11246f8fdc8Smillert _DIAGASSERT(p2 <= MAX_BUCKETS_LG2);
11346f8fdc8Smillert nel = 1 << p2;
11446f8fdc8Smillert }
11546f8fdc8Smillert
11646f8fdc8Smillert /* Allocate the table. */
11746f8fdc8Smillert htablesize = nel;
1181ed98fdfSderaadt htable = calloc(htablesize, sizeof htable[0]);
11946f8fdc8Smillert if (htable == NULL) {
12046f8fdc8Smillert errno = ENOMEM;
12146f8fdc8Smillert return 0;
12246f8fdc8Smillert }
12346f8fdc8Smillert
12446f8fdc8Smillert /* Initialize it. */
12546f8fdc8Smillert for (idx = 0; idx < htablesize; idx++)
12646f8fdc8Smillert SLIST_INIT(&htable[idx]);
12746f8fdc8Smillert
12846f8fdc8Smillert return 1;
12946f8fdc8Smillert }
13046f8fdc8Smillert
13146f8fdc8Smillert void
hdestroy(void)13246f8fdc8Smillert hdestroy(void)
13346f8fdc8Smillert {
13446f8fdc8Smillert struct internal_entry *ie;
13546f8fdc8Smillert size_t idx;
13646f8fdc8Smillert
13746f8fdc8Smillert _DIAGASSERT(htable != NULL);
13846f8fdc8Smillert if (htable == NULL)
13946f8fdc8Smillert return;
14046f8fdc8Smillert
14146f8fdc8Smillert for (idx = 0; idx < htablesize; idx++) {
14246f8fdc8Smillert while (!SLIST_EMPTY(&htable[idx])) {
14346f8fdc8Smillert ie = SLIST_FIRST(&htable[idx]);
14446f8fdc8Smillert SLIST_REMOVE_HEAD(&htable[idx], link);
14546f8fdc8Smillert free(ie->ent.key);
14646f8fdc8Smillert free(ie);
14746f8fdc8Smillert }
14846f8fdc8Smillert }
14946f8fdc8Smillert free(htable);
15046f8fdc8Smillert htable = NULL;
15146f8fdc8Smillert }
15246f8fdc8Smillert
15346f8fdc8Smillert ENTRY *
hsearch(ENTRY item,ACTION action)15446f8fdc8Smillert hsearch(ENTRY item, ACTION action)
15546f8fdc8Smillert {
15646f8fdc8Smillert struct internal_head *head;
15746f8fdc8Smillert struct internal_entry *ie;
15846f8fdc8Smillert uint32_t hashval;
15946f8fdc8Smillert size_t len;
16046f8fdc8Smillert
16146f8fdc8Smillert _DIAGASSERT(htable != NULL);
16246f8fdc8Smillert _DIAGASSERT(item.key != NULL);
16346f8fdc8Smillert _DIAGASSERT(action == ENTER || action == FIND);
16446f8fdc8Smillert
16546f8fdc8Smillert len = strlen(item.key);
166*b5745d08Sguenther hashval = __default_hash(item.key, len);
16746f8fdc8Smillert
16846f8fdc8Smillert head = &htable[hashval & (htablesize - 1)];
16946f8fdc8Smillert ie = SLIST_FIRST(head);
17046f8fdc8Smillert while (ie != NULL) {
17146f8fdc8Smillert if (strcmp(ie->ent.key, item.key) == 0)
17246f8fdc8Smillert break;
17346f8fdc8Smillert ie = SLIST_NEXT(ie, link);
17446f8fdc8Smillert }
17546f8fdc8Smillert
17646f8fdc8Smillert if (ie != NULL)
17746f8fdc8Smillert return &ie->ent;
17846f8fdc8Smillert else if (action == FIND)
17946f8fdc8Smillert return NULL;
18046f8fdc8Smillert
18146f8fdc8Smillert ie = malloc(sizeof *ie);
18246f8fdc8Smillert if (ie == NULL)
18346f8fdc8Smillert return NULL;
18446f8fdc8Smillert ie->ent.key = item.key;
18546f8fdc8Smillert ie->ent.data = item.data;
18646f8fdc8Smillert
18746f8fdc8Smillert SLIST_INSERT_HEAD(head, ie, link);
18846f8fdc8Smillert return &ie->ent;
18946f8fdc8Smillert }
190