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
36 * Look up an unused entry in the hash table for a given hash. For this
37 * implementation we use quadratic probing. Quadratic probing has the
38 * advantage of preventing primary clustering.
41 hsearch_lookup_free(struct __hsearch *hsearch, size_t hash)
45 for (index = hash, i = 0;; index += ++i) {
46 ENTRY *entry = &hsearch->entries[index & hsearch->index_mask];
47 if (entry->key == NULL)
53 * Computes an FNV-1a hash of the key. Depending on the pointer size, this
54 * either uses the 32- or 64-bit FNV prime.
57 hsearch_hash(size_t offset_basis, const char *str)
62 while (*str != '\0') {
63 hash ^= (uint8_t)*str++;
64 if (sizeof(size_t) * CHAR_BIT <= 32)
65 hash *= UINT32_C(16777619);
67 hash *= UINT64_C(1099511628211);
73 hsearch_r(ENTRY item, ACTION action, ENTRY **retval, struct hsearch_data *htab)
75 struct __hsearch *hsearch;
76 ENTRY *entry, *old_entries, *new_entries;
77 size_t hash, index, i, old_hash, old_count, new_count;
79 hsearch = htab->__hsearch;
80 hash = hsearch_hash(hsearch->offset_basis, item.key);
83 * Search the hash table for an existing entry for this key.
84 * Stop searching if we run into an unused hash table entry.
86 for (index = hash, i = 0;; index += ++i) {
87 entry = &hsearch->entries[index & hsearch->index_mask];
88 if (entry->key == NULL)
90 if (strcmp(entry->key, item.key) == 0) {
96 /* Only perform the insertion if action is set to ENTER. */
102 if (hsearch->entries_used * 2 >= hsearch->index_mask) {
103 /* Preserve the old hash table entries. */
104 old_count = hsearch->index_mask + 1;
105 old_entries = hsearch->entries;
108 * Allocate and install a new table if insertion would
109 * yield a hash table that is more than 50% used. By
110 * using 50% as a threshold, a lookup will only take up
111 * to two steps on average.
113 new_count = (hsearch->index_mask + 1) * 2;
114 new_entries = calloc(new_count, sizeof(ENTRY));
115 if (new_entries == NULL)
117 hsearch->entries = new_entries;
118 hsearch->index_mask = new_count - 1;
120 /* Copy over the entries from the old table to the new table. */
121 for (i = 0; i < old_count; ++i) {
122 entry = &old_entries[i];
123 if (entry->key != NULL) {
124 old_hash = hsearch_hash(hsearch->offset_basis,
126 *hsearch_lookup_free(hsearch, old_hash) =
131 /* Destroy the old hash table entries. */
135 * Perform a new lookup for a free table entry, so that
136 * we insert the entry into the new hash table.
138 hsearch = htab->__hsearch;
139 entry = hsearch_lookup_free(hsearch, hash);
142 /* Insert the new entry into the hash table. */
144 ++hsearch->entries_used;