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 <sys/cdefs.h> 27*2747eff1SEd Schouten __FBSDID("$FreeBSD$"); 28*2747eff1SEd Schouten 29*2747eff1SEd Schouten #include <search.h> 30*2747eff1SEd Schouten #include <stdlib.h> 31*2747eff1SEd Schouten 32*2747eff1SEd Schouten #include "hsearch.h" 33*2747eff1SEd Schouten 34*2747eff1SEd Schouten int 35*2747eff1SEd Schouten hcreate_r(size_t nel, struct hsearch_data *htab) 36*2747eff1SEd Schouten { 37*2747eff1SEd Schouten struct __hsearch *hsearch; 38*2747eff1SEd Schouten 39*2747eff1SEd Schouten /* 40*2747eff1SEd Schouten * Allocate a hash table object. Ignore the provided hint and start 41*2747eff1SEd Schouten * off with a table of sixteen entries. In most cases this hint is 42*2747eff1SEd Schouten * just a wild guess. Resizing the table dynamically if the use 43*2747eff1SEd Schouten * increases a threshold does not affect the worst-case running time. 44*2747eff1SEd Schouten */ 45*2747eff1SEd Schouten hsearch = malloc(sizeof(*hsearch)); 46*2747eff1SEd Schouten if (hsearch == NULL) 47*2747eff1SEd Schouten return 0; 48*2747eff1SEd Schouten hsearch->entries = calloc(16, sizeof(ENTRY)); 49*2747eff1SEd Schouten if (hsearch->entries == NULL) { 50*2747eff1SEd Schouten free(hsearch); 51*2747eff1SEd Schouten return 0; 52*2747eff1SEd Schouten } 53*2747eff1SEd Schouten 54*2747eff1SEd Schouten /* 55*2747eff1SEd Schouten * Pick a random initialization for the FNV-1a hashing. This makes it 56*2747eff1SEd Schouten * hard to come up with a fixed set of keys to force hash collisions. 57*2747eff1SEd Schouten */ 58*2747eff1SEd Schouten arc4random_buf(&hsearch->offset_basis, sizeof(hsearch->offset_basis)); 59*2747eff1SEd Schouten hsearch->index_mask = 0xf; 60*2747eff1SEd Schouten hsearch->entries_used = 0; 61*2747eff1SEd Schouten htab->__hsearch = hsearch; 62*2747eff1SEd Schouten return 1; 63*2747eff1SEd Schouten } 64