1292767Sed/*- 2292767Sed * Copyright (c) 2015 Nuxi, https://nuxi.nl/ 3292767Sed * 4292767Sed * Redistribution and use in source and binary forms, with or without 5292767Sed * modification, are permitted provided that the following conditions 6292767Sed * are met: 7292767Sed * 1. Redistributions of source code must retain the above copyright 8292767Sed * notice, this list of conditions and the following disclaimer. 9292767Sed * 2. Redistributions in binary form must reproduce the above copyright 10292767Sed * notice, this list of conditions and the following disclaimer in the 11292767Sed * documentation and/or other materials provided with the distribution. 12292767Sed * 13292767Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14292767Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15292767Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16292767Sed * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17292767Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18292767Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19292767Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20292767Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21292767Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22292767Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23292767Sed * SUCH DAMAGE. 24292767Sed */ 25292767Sed 26292767Sed#include <sys/cdefs.h> 27292767Sed__FBSDID("$FreeBSD$"); 28292767Sed 29292767Sed#include <search.h> 30292767Sed#include <stdbool.h> 31292767Sed#include <stddef.h> 32292767Sed 33292767Sed/* 34292767Sed * Thread unsafe interface: use a single process-wide hash table and 35292767Sed * forward calls to *_r() functions. 36292767Sed */ 37292767Sed 38292767Sedstatic struct hsearch_data global_hashtable; 39292767Sedstatic bool global_hashtable_initialized = false; 40292767Sed 41292767Sedint 42292767Sedhcreate(size_t nel) 43292767Sed{ 44292767Sed 45292767Sed return (1); 46292767Sed} 47292767Sed 48292767Sedvoid 49292767Sedhdestroy(void) 50292767Sed{ 51292767Sed 52292767Sed /* Destroy global hash table if present. */ 53292767Sed if (global_hashtable_initialized) { 54292767Sed hdestroy_r(&global_hashtable); 55292767Sed global_hashtable_initialized = false; 56292767Sed } 57292767Sed} 58292767Sed 59292767SedENTRY * 60292767Sedhsearch(ENTRY item, ACTION action) 61292767Sed{ 62292767Sed ENTRY *retval; 63292767Sed 64292767Sed /* Create global hash table if needed. */ 65292767Sed if (!global_hashtable_initialized) { 66292767Sed if (hcreate_r(0, &global_hashtable) == 0) 67292767Sed return (NULL); 68292767Sed global_hashtable_initialized = true; 69292767Sed } 70292767Sed if (hsearch_r(item, action, &retval, &global_hashtable) == 0) 71292767Sed return (NULL); 72292767Sed return (retval); 73292767Sed} 74