2 * Copyright (c) 2015 Nuxi, https://nuxi.nl/
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
37 * Look up an unused entry in the hash table for a given hash. For this
38 * implementation we use quadratic probing. Quadratic probing has the
39 * advantage of preventing primary clustering.
42 hsearch_lookup_free(struct __hsearch *hsearch, size_t hash)
46 for (index = hash, i = 0;; index += ++i) {
47 ENTRY *entry = &hsearch->entries[index & hsearch->index_mask];
48 if (entry->key == NULL)
54 * Computes an FNV-1a hash of the key. Depending on the pointer size, this
55 * either uses the 32- or 64-bit FNV prime.
58 hsearch_hash(size_t offset_basis, const char *str)
63 while (*str != '\0') {
64 hash ^= (uint8_t)*str++;
65 if (sizeof(size_t) * CHAR_BIT <= 32)
66 hash *= UINT32_C(16777619);
68 hash *= UINT64_C(1099511628211);
74 hsearch_r(ENTRY item, ACTION action, ENTRY **retval, struct hsearch_data *htab)
76 struct __hsearch *hsearch;
77 ENTRY *entry, *old_entries, *new_entries;
78 size_t hash, index, i, old_hash, old_count, new_count;
80 hsearch = htab->__hsearch;
81 hash = hsearch_hash(hsearch->offset_basis, item.key);
84 * Search the hash table for an existing entry for this key.
85 * Stop searching if we run into an unused hash table entry.
87 for (index = hash, i = 0;; index += ++i) {
88 entry = &hsearch->entries[index & hsearch->index_mask];
89 if (entry->key == NULL)
91 if (strcmp(entry->key, item.key) == 0) {
97 /* Only perform the insertion if action is set to ENTER. */
103 if (hsearch->entries_used * 2 >= hsearch->index_mask) {
104 /* Preserve the old hash table entries. */
105 old_count = hsearch->index_mask + 1;
106 old_entries = hsearch->entries;
109 * Allocate and install a new table if insertion would
110 * yield a hash table that is more than 50% used. By
111 * using 50% as a threshold, a lookup will only take up
112 * to two steps on average.
114 new_count = (hsearch->index_mask + 1) * 2;
115 new_entries = calloc(new_count, sizeof(ENTRY));
116 if (new_entries == NULL)
118 hsearch->entries = new_entries;
119 hsearch->index_mask = new_count - 1;
121 /* Copy over the entries from the old table to the new table. */
122 for (i = 0; i < old_count; ++i) {
123 entry = &old_entries[i];
124 if (entry->key != NULL) {
125 old_hash = hsearch_hash(hsearch->offset_basis,
127 *hsearch_lookup_free(hsearch, old_hash) =
132 /* Destroy the old hash table entries. */
136 * Perform a new lookup for a free table entry, so that
137 * we insert the entry into the new hash table.
139 hsearch = htab->__hsearch;
140 entry = hsearch_lookup_free(hsearch, hash);
143 /* Insert the new entry into the hash table. */
145 ++hsearch->entries_used;