/*-1* Copyright (c) 2015 Nuxi, https://nuxi.nl/2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6* 1. Redistributions of source code must retain the above copyright7* notice, this list of conditions and the following disclaimer.8* 2. Redistributions in binary form must reproduce the above copyright9* notice, this list of conditions and the following disclaimer in the10* documentation and/or other materials provided with the distribution.11*12* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND13* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE14* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE15* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE16* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL17* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS18* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)19* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT20* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY21* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF22* SUCH DAMAGE.23*/2425#include <errno.h>26#include <limits.h>27#include <search.h>28#include <stdint.h>29#include <stdlib.h>30#include <string.h>3132#include "hsearch.h"3334/*35* Look up an unused entry in the hash table for a given hash. For this36* implementation we use quadratic probing. Quadratic probing has the37* advantage of preventing primary clustering.38*/39static ENTRY *40hsearch_lookup_free(struct __hsearch *hsearch, size_t hash)41{42size_t index, i;4344for (index = hash, i = 0;; index += ++i) {45ENTRY *entry = &hsearch->entries[index & hsearch->index_mask];46if (entry->key == NULL)47return (entry);48}49}5051/*52* Computes an FNV-1a hash of the key. Depending on the pointer size, this53* either uses the 32- or 64-bit FNV prime.54*/55static size_t56hsearch_hash(size_t offset_basis, const char *str)57{58size_t hash;5960hash = offset_basis;61while (*str != '\0') {62hash ^= (uint8_t)*str++;63if (sizeof(size_t) * CHAR_BIT <= 32)64hash *= UINT32_C(16777619);65else66hash *= UINT64_C(1099511628211);67}68return (hash);69}7071int72hsearch_r(ENTRY item, ACTION action, ENTRY **retval, struct hsearch_data *htab)73{74struct __hsearch *hsearch;75ENTRY *entry, *old_entries, *new_entries;76size_t hash, index, i, old_hash, old_count, new_count;7778hsearch = htab->__hsearch;79hash = hsearch_hash(hsearch->offset_basis, item.key);8081/*82* Search the hash table for an existing entry for this key.83* Stop searching if we run into an unused hash table entry.84*/85for (index = hash, i = 0;; index += ++i) {86entry = &hsearch->entries[index & hsearch->index_mask];87if (entry->key == NULL)88break;89if (strcmp(entry->key, item.key) == 0) {90*retval = entry;91return (1);92}93}9495/* Only perform the insertion if action is set to ENTER. */96if (action == FIND) {97errno = ESRCH;98return (0);99}100101if (hsearch->entries_used * 2 >= hsearch->index_mask) {102/* Preserve the old hash table entries. */103old_count = hsearch->index_mask + 1;104old_entries = hsearch->entries;105106/*107* Allocate and install a new table if insertion would108* yield a hash table that is more than 50% used. By109* using 50% as a threshold, a lookup will only take up110* to two steps on average.111*/112new_count = (hsearch->index_mask + 1) * 2;113new_entries = calloc(new_count, sizeof(ENTRY));114if (new_entries == NULL)115return (0);116hsearch->entries = new_entries;117hsearch->index_mask = new_count - 1;118119/* Copy over the entries from the old table to the new table. */120for (i = 0; i < old_count; ++i) {121entry = &old_entries[i];122if (entry->key != NULL) {123old_hash = hsearch_hash(hsearch->offset_basis,124entry->key);125*hsearch_lookup_free(hsearch, old_hash) =126*entry;127}128}129130/* Destroy the old hash table entries. */131free(old_entries);132133/*134* Perform a new lookup for a free table entry, so that135* we insert the entry into the new hash table.136*/137hsearch = htab->__hsearch;138entry = hsearch_lookup_free(hsearch, hash);139}140141/* Insert the new entry into the hash table. */142*entry = item;143++hsearch->entries_used;144*retval = entry;145return (1);146}147148149