xref: /netbsd-src/lib/libc/stdlib/hcreate.c (revision e9fdd3c8488e9c963b359609930a5d64498c6d58)
1*e9fdd3c8Skre /* $NetBSD: hcreate.c,v 1.13 2022/03/13 01:44:37 kre Exp $ */
251a7af15Scgd 
351a7af15Scgd /*
451a7af15Scgd  * Copyright (c) 2001 Christopher G. Demetriou
551a7af15Scgd  * All rights reserved.
651a7af15Scgd  *
751a7af15Scgd  * Redistribution and use in source and binary forms, with or without
851a7af15Scgd  * modification, are permitted provided that the following conditions
951a7af15Scgd  * are met:
1051a7af15Scgd  * 1. Redistributions of source code must retain the above copyright
1151a7af15Scgd  *    notice, this list of conditions and the following disclaimer.
1251a7af15Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1351a7af15Scgd  *    notice, this list of conditions and the following disclaimer in the
1451a7af15Scgd  *    documentation and/or other materials provided with the distribution.
15fb9e5ffdSchristos  * 3. The name of the author may not be used to endorse or promote products
1651a7af15Scgd  *    derived from this software without specific prior written permission.
1751a7af15Scgd  *
1851a7af15Scgd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1951a7af15Scgd  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2051a7af15Scgd  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2151a7af15Scgd  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2251a7af15Scgd  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2351a7af15Scgd  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2451a7af15Scgd  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2551a7af15Scgd  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2651a7af15Scgd  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2751a7af15Scgd  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2851a7af15Scgd  *
2951a7af15Scgd  * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
3051a7af15Scgd  */
3151a7af15Scgd 
3251a7af15Scgd /*
3351a7af15Scgd  * hcreate() / hsearch() / hdestroy()
3451a7af15Scgd  *
3551a7af15Scgd  * SysV/XPG4 hash table functions.
3651a7af15Scgd  *
3751a7af15Scgd  * Implementation done based on NetBSD manual page and Solaris manual page,
3851a7af15Scgd  * plus my own personal experience about how they're supposed to work.
3951a7af15Scgd  *
4051a7af15Scgd  * I tried to look at Knuth (as cited by the Solaris manual page), but
4151a7af15Scgd  * nobody had a copy in the office, so...
4251a7af15Scgd  */
4351a7af15Scgd 
4451a7af15Scgd #include <sys/cdefs.h>
4551a7af15Scgd #if defined(LIBC_SCCS) && !defined(lint)
46*e9fdd3c8Skre __RCSID("$NetBSD: hcreate.c,v 1.13 2022/03/13 01:44:37 kre Exp $");
4751a7af15Scgd #endif /* LIBC_SCCS and not lint */
4851a7af15Scgd 
4951a7af15Scgd #if !defined(lint)
508897ce05Slukem __COPYRIGHT("@(#) Copyright (c) 2001\
518897ce05Slukem  Christopher G. Demetriou.  All rights reserved.");
5251a7af15Scgd #endif /* not lint */
5351a7af15Scgd 
5451a7af15Scgd #include "namespace.h"
5551a7af15Scgd #include <assert.h>
5651a7af15Scgd #include <errno.h>
5751a7af15Scgd #include <inttypes.h>
5851a7af15Scgd #include <search.h>
5951a7af15Scgd #include <stdlib.h>
6051a7af15Scgd #include <string.h>
6151a7af15Scgd #include <sys/queue.h>
6251a7af15Scgd 
6351a7af15Scgd /*
6451a7af15Scgd  * DO NOT MAKE THIS STRUCTURE LARGER THAN 32 BYTES (4 ptrs on 64-bit
6551a7af15Scgd  * ptr machine) without adjusting MAX_BUCKETS_LG2 below.
6651a7af15Scgd  */
6751a7af15Scgd struct internal_entry {
6851a7af15Scgd 	SLIST_ENTRY(internal_entry) link;
6951a7af15Scgd 	ENTRY ent;
7051a7af15Scgd };
7151a7af15Scgd SLIST_HEAD(internal_head, internal_entry);
7251a7af15Scgd 
7351a7af15Scgd #define	MIN_BUCKETS_LG2	4
7451a7af15Scgd #define	MIN_BUCKETS	(1 << MIN_BUCKETS_LG2)
7551a7af15Scgd 
7651a7af15Scgd /*
7751a7af15Scgd  * max * sizeof internal_entry must fit into size_t.
7851a7af15Scgd  * assumes internal_entry is <= 32 (2^5) bytes.
7951a7af15Scgd  */
8051a7af15Scgd #define	MAX_BUCKETS_LG2	(sizeof (size_t) * 8 - 1 - 5)
8168725996Sross #define	MAX_BUCKETS	((size_t)1 << MAX_BUCKETS_LG2)
8251a7af15Scgd 
8351a7af15Scgd /* Default hash function, from db/hash/hash_func.c */
8451a7af15Scgd extern u_int32_t (*__default_hash)(const void *, size_t);
8551a7af15Scgd 
8605845f98Schristos static struct hsearch_data htable;
8751a7af15Scgd 
8851a7af15Scgd int
hcreate(size_t nel)8951a7af15Scgd hcreate(size_t nel)
9051a7af15Scgd {
9105845f98Schristos 	_DIAGASSERT(htable.table == NULL);
9251a7af15Scgd 
9304680727Ssimonb 	/* Make sure this isn't called when a table already exists. */
9405845f98Schristos 	if (htable.table != NULL) {
9551a7af15Scgd 		errno = EINVAL;
9651a7af15Scgd 		return 0;
9751a7af15Scgd 	}
9805845f98Schristos 	return hcreate_r(nel, &htable);
9905845f98Schristos }
10005845f98Schristos 
10105845f98Schristos int
hcreate_r(size_t nel,struct hsearch_data * head)10205845f98Schristos hcreate_r(size_t nel, struct hsearch_data *head)
10305845f98Schristos {
10405845f98Schristos 	struct internal_head *table;
10505845f98Schristos 	size_t idx;
10605845f98Schristos 	unsigned int p2;
10751a7af15Scgd 
10851a7af15Scgd 	/* If nel is too small, make it min sized. */
10951a7af15Scgd 	if (nel < MIN_BUCKETS)
11051a7af15Scgd 		nel = MIN_BUCKETS;
11151a7af15Scgd 
11251a7af15Scgd 	/* If it's too large, cap it. */
11351a7af15Scgd 	if (nel > MAX_BUCKETS)
11451a7af15Scgd 		nel = MAX_BUCKETS;
11551a7af15Scgd 
11651a7af15Scgd 	/* If it's is not a power of two in size, round up. */
11751a7af15Scgd 	if ((nel & (nel - 1)) != 0) {
11851a7af15Scgd 		for (p2 = 0; nel != 0; p2++)
11951a7af15Scgd 			nel >>= 1;
12051a7af15Scgd 		_DIAGASSERT(p2 <= MAX_BUCKETS_LG2);
12151a7af15Scgd 		nel = 1 << p2;
12251a7af15Scgd 	}
12351a7af15Scgd 
12451a7af15Scgd 	/* Allocate the table. */
12505845f98Schristos 	head->size = nel;
12605845f98Schristos 	head->filled = 0;
127*e9fdd3c8Skre 	table = NULL;
1283249d3dcSchristos 	errno = reallocarr(&table, nel, sizeof(*table));
1293249d3dcSchristos 	if (errno)
13051a7af15Scgd 		return 0;
1313249d3dcSchristos 	head->table = (void *)table;
13251a7af15Scgd 
13351a7af15Scgd 	/* Initialize it. */
13405845f98Schristos 	for (idx = 0; idx < nel; idx++)
13505845f98Schristos 		SLIST_INIT(&table[idx]);
13651a7af15Scgd 
13751a7af15Scgd 	return 1;
13851a7af15Scgd }
13951a7af15Scgd 
14051a7af15Scgd void
hdestroy1(void (* freekey)(void *),void (* freedata)(void *))1416030f04aSchristos hdestroy1(void (*freekey)(void *), void (*freedata)(void *))
14251a7af15Scgd {
14305845f98Schristos 	_DIAGASSERT(htable.table != NULL);
1446030f04aSchristos 	hdestroy1_r(&htable, freekey, freedata);
14505845f98Schristos }
14605845f98Schristos 
14705845f98Schristos void
hdestroy(void)148842ee049Schristos hdestroy(void)
149842ee049Schristos {
1506030f04aSchristos 	hdestroy1(NULL, NULL);
151842ee049Schristos }
152842ee049Schristos 
153842ee049Schristos void
hdestroy1_r(struct hsearch_data * head,void (* freekey)(void *),void (* freedata)(void *))1546030f04aSchristos hdestroy1_r(struct hsearch_data *head, void (*freekey)(void *),
1556030f04aSchristos     void (*freedata)(void *))
15605845f98Schristos {
15751a7af15Scgd 	struct internal_entry *ie;
15851a7af15Scgd 	size_t idx;
15905845f98Schristos 	void *p;
16005845f98Schristos 	struct internal_head *table;
16151a7af15Scgd 
16205845f98Schristos 	if (head == NULL)
16351a7af15Scgd 		return;
16451a7af15Scgd 
16505845f98Schristos 	p = head->table;
16605845f98Schristos 	head->table = NULL;
16705845f98Schristos 	table = p;
16805845f98Schristos 
16905845f98Schristos 	for (idx = 0; idx < head->size; idx++) {
17005845f98Schristos 		while (!SLIST_EMPTY(&table[idx])) {
17105845f98Schristos 			ie = SLIST_FIRST(&table[idx]);
17205845f98Schristos 			SLIST_REMOVE_HEAD(&table[idx], link);
1736030f04aSchristos 			if (freekey)
1746030f04aSchristos 				(*freekey)(ie->ent.key);
1756030f04aSchristos 			if (freedata)
1766030f04aSchristos 				(*freedata)(ie->ent.data);
17751a7af15Scgd 			free(ie);
17851a7af15Scgd 		}
17951a7af15Scgd 	}
18005845f98Schristos 	free(table);
18151a7af15Scgd }
18251a7af15Scgd 
183842ee049Schristos void
hdestroy_r(struct hsearch_data * head)184842ee049Schristos hdestroy_r(struct hsearch_data *head)
185842ee049Schristos {
1866030f04aSchristos 	hdestroy1_r(head, NULL, NULL);
187842ee049Schristos }
188842ee049Schristos 
18951a7af15Scgd ENTRY *
hsearch(ENTRY item,ACTION action)19051a7af15Scgd hsearch(ENTRY item, ACTION action)
19151a7af15Scgd {
19205845f98Schristos 	ENTRY *ep;
19305845f98Schristos 	_DIAGASSERT(htable.table != NULL);
19405845f98Schristos 	(void)hsearch_r(item, action, &ep, &htable);
19505845f98Schristos 	return ep;
19605845f98Schristos }
19705845f98Schristos 
19805845f98Schristos int
hsearch_r(ENTRY item,ACTION action,ENTRY ** itemp,struct hsearch_data * head)19905845f98Schristos hsearch_r(ENTRY item, ACTION action, ENTRY **itemp, struct hsearch_data *head)
20005845f98Schristos {
20105845f98Schristos 	struct internal_head *table, *chain;
20251a7af15Scgd 	struct internal_entry *ie;
20351a7af15Scgd 	uint32_t hashval;
20451a7af15Scgd 	size_t len;
20505845f98Schristos 	void *p;
20651a7af15Scgd 
2072e118c9dSlukem 	_DIAGASSERT(item.key != NULL);
2082e118c9dSlukem 	_DIAGASSERT(action == ENTER || action == FIND);
20951a7af15Scgd 
21005845f98Schristos 	p = head->table;
21105845f98Schristos 	table = p;
21205845f98Schristos 
21351a7af15Scgd 	len = strlen(item.key);
21451a7af15Scgd 	hashval = (*__default_hash)(item.key, len);
21551a7af15Scgd 
21605845f98Schristos 	chain = &table[hashval & (head->size - 1)];
21705845f98Schristos 	ie = SLIST_FIRST(chain);
21851a7af15Scgd 	while (ie != NULL) {
21951a7af15Scgd 		if (strcmp(ie->ent.key, item.key) == 0)
22051a7af15Scgd 			break;
22151a7af15Scgd 		ie = SLIST_NEXT(ie, link);
22251a7af15Scgd 	}
22351a7af15Scgd 
22405845f98Schristos 	if (ie != NULL) {
22505845f98Schristos 		*itemp = &ie->ent;
22605845f98Schristos 		return 1;
22705845f98Schristos 	} else if (action == FIND) {
22805845f98Schristos 		*itemp = NULL;
22905845f98Schristos 		errno = ESRCH;
23005845f98Schristos 		return 1;
23105845f98Schristos 	}
23251a7af15Scgd 
23351a7af15Scgd 	ie = malloc(sizeof *ie);
23451a7af15Scgd 	if (ie == NULL)
23505845f98Schristos 		return 0;
23651a7af15Scgd 	ie->ent.key = item.key;
23751a7af15Scgd 	ie->ent.data = item.data;
23851a7af15Scgd 
23905845f98Schristos 	SLIST_INSERT_HEAD(chain, ie, link);
24005845f98Schristos 	*itemp = &ie->ent;
24105845f98Schristos 	head->filled++;
24205845f98Schristos 	return 1;
24351a7af15Scgd }
244