1 /* $OpenBSD: hcreate.c,v 1.6 2015/09/10 18:13:46 guenther Exp $ */ 2 /* $NetBSD: hcreate.c,v 1.5 2004/04/23 02:48:12 simonb Exp $ */ 3 4 /* 5 * Copyright (c) 2001 Christopher G. Demetriou 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed for the 19 * NetBSD Project. See http://www.NetBSD.org/ for 20 * information about NetBSD. 21 * 4. The name of the author may not be used to endorse or promote products 22 * derived from this software without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 * 35 * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>> 36 */ 37 38 /* 39 * hcreate() / hsearch() / hdestroy() 40 * 41 * SysV/XPG4 hash table functions. 42 * 43 * Implementation done based on NetBSD manual page and Solaris manual page, 44 * plus my own personal experience about how they're supposed to work. 45 * 46 * I tried to look at Knuth (as cited by the Solaris manual page), but 47 * nobody had a copy in the office, so... 48 */ 49 50 #include <assert.h> 51 #include <errno.h> 52 #include <stdint.h> 53 #include <search.h> 54 #include <stdlib.h> 55 #include <string.h> 56 #include <sys/queue.h> 57 58 #ifndef _DIAGASSERT 59 #define _DIAGASSERT(x) 60 #endif 61 62 /* 63 * DO NOT MAKE THIS STRUCTURE LARGER THAN 32 BYTES (4 ptrs on 64-bit 64 * ptr machine) without adjusting MAX_BUCKETS_LG2 below. 65 */ 66 struct internal_entry { 67 SLIST_ENTRY(internal_entry) link; 68 ENTRY ent; 69 }; 70 SLIST_HEAD(internal_head, internal_entry); 71 72 #define MIN_BUCKETS_LG2 4 73 #define MIN_BUCKETS (1 << MIN_BUCKETS_LG2) 74 75 /* 76 * max * sizeof internal_entry must fit into size_t. 77 * assumes internal_entry is <= 32 (2^5) bytes. 78 */ 79 #define MAX_BUCKETS_LG2 (sizeof (size_t) * 8 - 1 - 5) 80 #define MAX_BUCKETS ((size_t)1 << MAX_BUCKETS_LG2) 81 82 /* Default hash function, from db/hash/hash_func.c */ 83 extern u_int32_t (*__default_hash)(const void *, size_t); 84 85 static struct internal_head *htable; 86 static size_t htablesize; 87 88 int 89 hcreate(size_t nel) 90 { 91 size_t idx; 92 unsigned int p2; 93 94 /* Make sure this isn't called when a table already exists. */ 95 _DIAGASSERT(htable == NULL); 96 if (htable != NULL) { 97 errno = EINVAL; 98 return 0; 99 } 100 101 /* If nel is too small, make it min sized. */ 102 if (nel < MIN_BUCKETS) 103 nel = MIN_BUCKETS; 104 105 /* If it's too large, cap it. */ 106 if (nel > MAX_BUCKETS) 107 nel = MAX_BUCKETS; 108 109 /* If it's is not a power of two in size, round up. */ 110 if ((nel & (nel - 1)) != 0) { 111 for (p2 = 0; nel != 0; p2++) 112 nel >>= 1; 113 _DIAGASSERT(p2 <= MAX_BUCKETS_LG2); 114 nel = 1 << p2; 115 } 116 117 /* Allocate the table. */ 118 htablesize = nel; 119 htable = calloc(htablesize, sizeof htable[0]); 120 if (htable == NULL) { 121 errno = ENOMEM; 122 return 0; 123 } 124 125 /* Initialize it. */ 126 for (idx = 0; idx < htablesize; idx++) 127 SLIST_INIT(&htable[idx]); 128 129 return 1; 130 } 131 132 void 133 hdestroy(void) 134 { 135 struct internal_entry *ie; 136 size_t idx; 137 138 _DIAGASSERT(htable != NULL); 139 if (htable == NULL) 140 return; 141 142 for (idx = 0; idx < htablesize; idx++) { 143 while (!SLIST_EMPTY(&htable[idx])) { 144 ie = SLIST_FIRST(&htable[idx]); 145 SLIST_REMOVE_HEAD(&htable[idx], link); 146 free(ie->ent.key); 147 free(ie); 148 } 149 } 150 free(htable); 151 htable = NULL; 152 } 153 154 ENTRY * 155 hsearch(ENTRY item, ACTION action) 156 { 157 struct internal_head *head; 158 struct internal_entry *ie; 159 uint32_t hashval; 160 size_t len; 161 162 _DIAGASSERT(htable != NULL); 163 _DIAGASSERT(item.key != NULL); 164 _DIAGASSERT(action == ENTER || action == FIND); 165 166 len = strlen(item.key); 167 hashval = (*__default_hash)(item.key, len); 168 169 head = &htable[hashval & (htablesize - 1)]; 170 ie = SLIST_FIRST(head); 171 while (ie != NULL) { 172 if (strcmp(ie->ent.key, item.key) == 0) 173 break; 174 ie = SLIST_NEXT(ie, link); 175 } 176 177 if (ie != NULL) 178 return &ie->ent; 179 else if (action == FIND) 180 return NULL; 181 182 ie = malloc(sizeof *ie); 183 if (ie == NULL) 184 return NULL; 185 ie->ent.key = item.key; 186 ie->ent.data = item.data; 187 188 SLIST_INSERT_HEAD(head, ie, link); 189 return &ie->ent; 190 } 191