Home | History | Annotate | Line # | Download | only in stdlib
      1 /* $NetBSD: hcreate.c,v 1.13 2022/03/13 01:44:37 kre 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,v 1.2 2000/06/14 15:57:33 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 #include <sys/cdefs.h>
     45 #if defined(LIBC_SCCS) && !defined(lint)
     46 __RCSID("$NetBSD: hcreate.c,v 1.13 2022/03/13 01:44:37 kre Exp $");
     47 #endif /* LIBC_SCCS and not lint */
     48 
     49 #if !defined(lint)
     50 __COPYRIGHT("@(#) Copyright (c) 2001\
     51  Christopher G. Demetriou.  All rights reserved.");
     52 #endif /* not lint */
     53 
     54 #include "namespace.h"
     55 #include <assert.h>
     56 #include <errno.h>
     57 #include <inttypes.h>
     58 #include <search.h>
     59 #include <stdlib.h>
     60 #include <string.h>
     61 #include <sys/queue.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 #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 */
     84 extern u_int32_t (*__default_hash)(const void *, size_t);
     85 
     86 static struct hsearch_data htable;
     87 
     88 int
     89 hcreate(size_t nel)
     90 {
     91 	_DIAGASSERT(htable.table == NULL);
     92 
     93 	/* Make sure this isn't called when a table already exists. */
     94 	if (htable.table != NULL) {
     95 		errno = EINVAL;
     96 		return 0;
     97 	}
     98 	return hcreate_r(nel, &htable);
     99 }
    100 
    101 int
    102 hcreate_r(size_t nel, struct hsearch_data *head)
    103 {
    104 	struct internal_head *table;
    105 	size_t idx;
    106 	unsigned int p2;
    107 
    108 	/* If nel is too small, make it min sized. */
    109 	if (nel < MIN_BUCKETS)
    110 		nel = MIN_BUCKETS;
    111 
    112 	/* If it's too large, cap it. */
    113 	if (nel > MAX_BUCKETS)
    114 		nel = MAX_BUCKETS;
    115 
    116 	/* If it's is not a power of two in size, round up. */
    117 	if ((nel & (nel - 1)) != 0) {
    118 		for (p2 = 0; nel != 0; p2++)
    119 			nel >>= 1;
    120 		_DIAGASSERT(p2 <= MAX_BUCKETS_LG2);
    121 		nel = 1 << p2;
    122 	}
    123 
    124 	/* Allocate the table. */
    125 	head->size = nel;
    126 	head->filled = 0;
    127 	table = NULL;
    128 	errno = reallocarr(&table, nel, sizeof(*table));
    129 	if (errno)
    130 		return 0;
    131 	head->table = (void *)table;
    132 
    133 	/* Initialize it. */
    134 	for (idx = 0; idx < nel; idx++)
    135 		SLIST_INIT(&table[idx]);
    136 
    137 	return 1;
    138 }
    139 
    140 void
    141 hdestroy1(void (*freekey)(void *), void (*freedata)(void *))
    142 {
    143 	_DIAGASSERT(htable.table != NULL);
    144 	hdestroy1_r(&htable, freekey, freedata);
    145 }
    146 
    147 void
    148 hdestroy(void)
    149 {
    150 	hdestroy1(NULL, NULL);
    151 }
    152 
    153 void
    154 hdestroy1_r(struct hsearch_data *head, void (*freekey)(void *),
    155     void (*freedata)(void *))
    156 {
    157 	struct internal_entry *ie;
    158 	size_t idx;
    159 	void *p;
    160 	struct internal_head *table;
    161 
    162 	if (head == NULL)
    163 		return;
    164 
    165 	p = head->table;
    166 	head->table = NULL;
    167 	table = p;
    168 
    169 	for (idx = 0; idx < head->size; idx++) {
    170 		while (!SLIST_EMPTY(&table[idx])) {
    171 			ie = SLIST_FIRST(&table[idx]);
    172 			SLIST_REMOVE_HEAD(&table[idx], link);
    173 			if (freekey)
    174 				(*freekey)(ie->ent.key);
    175 			if (freedata)
    176 				(*freedata)(ie->ent.data);
    177 			free(ie);
    178 		}
    179 	}
    180 	free(table);
    181 }
    182 
    183 void
    184 hdestroy_r(struct hsearch_data *head)
    185 {
    186 	hdestroy1_r(head, NULL, NULL);
    187 }
    188 
    189 ENTRY *
    190 hsearch(ENTRY item, ACTION action)
    191 {
    192 	ENTRY *ep;
    193 	_DIAGASSERT(htable.table != NULL);
    194 	(void)hsearch_r(item, action, &ep, &htable);
    195 	return ep;
    196 }
    197 
    198 int
    199 hsearch_r(ENTRY item, ACTION action, ENTRY **itemp, struct hsearch_data *head)
    200 {
    201 	struct internal_head *table, *chain;
    202 	struct internal_entry *ie;
    203 	uint32_t hashval;
    204 	size_t len;
    205 	void *p;
    206 
    207 	_DIAGASSERT(item.key != NULL);
    208 	_DIAGASSERT(action == ENTER || action == FIND);
    209 
    210 	p = head->table;
    211 	table = p;
    212 
    213 	len = strlen(item.key);
    214 	hashval = (*__default_hash)(item.key, len);
    215 
    216 	chain = &table[hashval & (head->size - 1)];
    217 	ie = SLIST_FIRST(chain);
    218 	while (ie != NULL) {
    219 		if (strcmp(ie->ent.key, item.key) == 0)
    220 			break;
    221 		ie = SLIST_NEXT(ie, link);
    222 	}
    223 
    224 	if (ie != NULL) {
    225 		*itemp = &ie->ent;
    226 		return 1;
    227 	} else if (action == FIND) {
    228 		*itemp = NULL;
    229 		errno = ESRCH;
    230 		return 1;
    231 	}
    232 
    233 	ie = malloc(sizeof *ie);
    234 	if (ie == NULL)
    235 		return 0;
    236 	ie->ent.key = item.key;
    237 	ie->ent.data = item.data;
    238 
    239 	SLIST_INSERT_HEAD(chain, ie, link);
    240 	*itemp = &ie->ent;
    241 	head->filled++;
    242 	return 1;
    243 }
    244