xref: /freebsd-src/lib/libc/stdlib/hsearch_r.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
1*2747eff1SEd Schouten /*-
2*2747eff1SEd Schouten  * Copyright (c) 2015 Nuxi, https://nuxi.nl/
3*2747eff1SEd Schouten  *
4*2747eff1SEd Schouten  * Redistribution and use in source and binary forms, with or without
5*2747eff1SEd Schouten  * modification, are permitted provided that the following conditions
6*2747eff1SEd Schouten  * are met:
7*2747eff1SEd Schouten  * 1. Redistributions of source code must retain the above copyright
8*2747eff1SEd Schouten  *    notice, this list of conditions and the following disclaimer.
9*2747eff1SEd Schouten  * 2. Redistributions in binary form must reproduce the above copyright
10*2747eff1SEd Schouten  *    notice, this list of conditions and the following disclaimer in the
11*2747eff1SEd Schouten  *    documentation and/or other materials provided with the distribution.
12*2747eff1SEd Schouten  *
13*2747eff1SEd Schouten  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14*2747eff1SEd Schouten  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15*2747eff1SEd Schouten  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16*2747eff1SEd Schouten  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17*2747eff1SEd Schouten  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18*2747eff1SEd Schouten  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19*2747eff1SEd Schouten  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20*2747eff1SEd Schouten  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21*2747eff1SEd Schouten  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22*2747eff1SEd Schouten  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23*2747eff1SEd Schouten  * SUCH DAMAGE.
24*2747eff1SEd Schouten  */
25*2747eff1SEd Schouten 
26*2747eff1SEd Schouten #include <errno.h>
27*2747eff1SEd Schouten #include <limits.h>
28*2747eff1SEd Schouten #include <search.h>
29*2747eff1SEd Schouten #include <stdint.h>
30*2747eff1SEd Schouten #include <stdlib.h>
31*2747eff1SEd Schouten #include <string.h>
32*2747eff1SEd Schouten 
33*2747eff1SEd Schouten #include "hsearch.h"
34*2747eff1SEd Schouten 
35*2747eff1SEd Schouten /*
36*2747eff1SEd Schouten  * Look up an unused entry in the hash table for a given hash. For this
37*2747eff1SEd Schouten  * implementation we use quadratic probing. Quadratic probing has the
38*2747eff1SEd Schouten  * advantage of preventing primary clustering.
39*2747eff1SEd Schouten  */
40*2747eff1SEd Schouten static ENTRY *
hsearch_lookup_free(struct __hsearch * hsearch,size_t hash)41*2747eff1SEd Schouten hsearch_lookup_free(struct __hsearch *hsearch, size_t hash)
42*2747eff1SEd Schouten {
43*2747eff1SEd Schouten 	size_t index, i;
44*2747eff1SEd Schouten 
45*2747eff1SEd Schouten 	for (index = hash, i = 0;; index += ++i) {
46*2747eff1SEd Schouten 		ENTRY *entry = &hsearch->entries[index & hsearch->index_mask];
47*2747eff1SEd Schouten 		if (entry->key == NULL)
48*2747eff1SEd Schouten 			return (entry);
49*2747eff1SEd Schouten 	}
50*2747eff1SEd Schouten }
51*2747eff1SEd Schouten 
52*2747eff1SEd Schouten /*
53*2747eff1SEd Schouten  * Computes an FNV-1a hash of the key. Depending on the pointer size, this
54*2747eff1SEd Schouten  * either uses the 32- or 64-bit FNV prime.
55*2747eff1SEd Schouten  */
56*2747eff1SEd Schouten static size_t
hsearch_hash(size_t offset_basis,const char * str)57*2747eff1SEd Schouten hsearch_hash(size_t offset_basis, const char *str)
58*2747eff1SEd Schouten {
59*2747eff1SEd Schouten 	size_t hash;
60*2747eff1SEd Schouten 
61*2747eff1SEd Schouten 	hash = offset_basis;
62*2747eff1SEd Schouten 	while (*str != '\0') {
63*2747eff1SEd Schouten 		hash ^= (uint8_t)*str++;
64*2747eff1SEd Schouten 		if (sizeof(size_t) * CHAR_BIT <= 32)
65*2747eff1SEd Schouten 			hash *= UINT32_C(16777619);
66*2747eff1SEd Schouten 		else
67*2747eff1SEd Schouten 			hash *= UINT64_C(1099511628211);
68*2747eff1SEd Schouten 	}
69*2747eff1SEd Schouten 	return (hash);
70*2747eff1SEd Schouten }
71*2747eff1SEd Schouten 
72*2747eff1SEd Schouten int
hsearch_r(ENTRY item,ACTION action,ENTRY ** retval,struct hsearch_data * htab)73*2747eff1SEd Schouten hsearch_r(ENTRY item, ACTION action, ENTRY **retval, struct hsearch_data *htab)
74*2747eff1SEd Schouten {
75*2747eff1SEd Schouten 	struct __hsearch *hsearch;
76*2747eff1SEd Schouten 	ENTRY *entry, *old_entries, *new_entries;
77*2747eff1SEd Schouten 	size_t hash, index, i, old_hash, old_count, new_count;
78*2747eff1SEd Schouten 
79*2747eff1SEd Schouten 	hsearch = htab->__hsearch;
80*2747eff1SEd Schouten 	hash = hsearch_hash(hsearch->offset_basis, item.key);
81*2747eff1SEd Schouten 
82*2747eff1SEd Schouten 	/*
83*2747eff1SEd Schouten 	 * Search the hash table for an existing entry for this key.
84*2747eff1SEd Schouten 	 * Stop searching if we run into an unused hash table entry.
85*2747eff1SEd Schouten 	 */
86*2747eff1SEd Schouten 	for (index = hash, i = 0;; index += ++i) {
87*2747eff1SEd Schouten 		entry = &hsearch->entries[index & hsearch->index_mask];
88*2747eff1SEd Schouten 		if (entry->key == NULL)
89*2747eff1SEd Schouten 			break;
90*2747eff1SEd Schouten 		if (strcmp(entry->key, item.key) == 0) {
91*2747eff1SEd Schouten 			*retval = entry;
92*2747eff1SEd Schouten 			return (1);
93*2747eff1SEd Schouten 		}
94*2747eff1SEd Schouten 	}
95*2747eff1SEd Schouten 
96*2747eff1SEd Schouten 	/* Only perform the insertion if action is set to ENTER. */
97*2747eff1SEd Schouten 	if (action == FIND) {
98*2747eff1SEd Schouten 		errno = ESRCH;
99*2747eff1SEd Schouten 		return (0);
100*2747eff1SEd Schouten 	}
101*2747eff1SEd Schouten 
102*2747eff1SEd Schouten 	if (hsearch->entries_used * 2 >= hsearch->index_mask) {
103*2747eff1SEd Schouten 		/* Preserve the old hash table entries. */
104*2747eff1SEd Schouten 		old_count = hsearch->index_mask + 1;
105*2747eff1SEd Schouten 		old_entries = hsearch->entries;
106*2747eff1SEd Schouten 
107*2747eff1SEd Schouten 		/*
108*2747eff1SEd Schouten 		 * Allocate and install a new table if insertion would
109*2747eff1SEd Schouten 		 * yield a hash table that is more than 50% used. By
110*2747eff1SEd Schouten 		 * using 50% as a threshold, a lookup will only take up
111*2747eff1SEd Schouten 		 * to two steps on average.
112*2747eff1SEd Schouten 		 */
113*2747eff1SEd Schouten 		new_count = (hsearch->index_mask + 1) * 2;
114*2747eff1SEd Schouten 		new_entries = calloc(new_count, sizeof(ENTRY));
115*2747eff1SEd Schouten 		if (new_entries == NULL)
116*2747eff1SEd Schouten 			return (0);
117*2747eff1SEd Schouten 		hsearch->entries = new_entries;
118*2747eff1SEd Schouten 		hsearch->index_mask = new_count - 1;
119*2747eff1SEd Schouten 
120*2747eff1SEd Schouten 		/* Copy over the entries from the old table to the new table. */
121*2747eff1SEd Schouten 		for (i = 0; i < old_count; ++i) {
122*2747eff1SEd Schouten 			entry = &old_entries[i];
123*2747eff1SEd Schouten 			if (entry->key != NULL) {
124*2747eff1SEd Schouten 				old_hash = hsearch_hash(hsearch->offset_basis,
125*2747eff1SEd Schouten 				    entry->key);
126*2747eff1SEd Schouten 				*hsearch_lookup_free(hsearch, old_hash) =
127*2747eff1SEd Schouten 				    *entry;
128*2747eff1SEd Schouten 			}
129*2747eff1SEd Schouten 		}
130*2747eff1SEd Schouten 
131*2747eff1SEd Schouten 		/* Destroy the old hash table entries. */
132*2747eff1SEd Schouten 		free(old_entries);
133*2747eff1SEd Schouten 
134*2747eff1SEd Schouten 		/*
135*2747eff1SEd Schouten 		 * Perform a new lookup for a free table entry, so that
136*2747eff1SEd Schouten 		 * we insert the entry into the new hash table.
137*2747eff1SEd Schouten 		 */
138*2747eff1SEd Schouten 		hsearch = htab->__hsearch;
139*2747eff1SEd Schouten 		entry = hsearch_lookup_free(hsearch, hash);
140*2747eff1SEd Schouten 	}
141*2747eff1SEd Schouten 
142*2747eff1SEd Schouten 	/* Insert the new entry into the hash table. */
143*2747eff1SEd Schouten 	*entry = item;
144*2747eff1SEd Schouten 	++hsearch->entries_used;
145*2747eff1SEd Schouten 	*retval = entry;
146*2747eff1SEd Schouten 	return (1);
147*2747eff1SEd Schouten }
148