1 0d6fb46a 2022-01-09 op /* $OpenBSD: ohash.c,v 1.1 2014/06/02 18:52:03 deraadt Exp $ */
3 0d6fb46a 2022-01-09 op /* Copyright (c) 1999, 2004 Marc Espie <espie@openbsd.org>
5 0d6fb46a 2022-01-09 op * Permission to use, copy, modify, and distribute this software for any
6 0d6fb46a 2022-01-09 op * purpose with or without fee is hereby granted, provided that the above
7 0d6fb46a 2022-01-09 op * copyright notice and this permission notice appear in all copies.
9 0d6fb46a 2022-01-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 0d6fb46a 2022-01-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 0d6fb46a 2022-01-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 0d6fb46a 2022-01-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 0d6fb46a 2022-01-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 0d6fb46a 2022-01-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 0d6fb46a 2022-01-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 0d6fb46a 2022-01-09 op #include "compat.h"
20 0d6fb46a 2022-01-09 op #include <stddef.h>
21 0d6fb46a 2022-01-09 op #include <stdint.h>
22 0d6fb46a 2022-01-09 op #include <stdlib.h>
23 0d6fb46a 2022-01-09 op #include <string.h>
24 0d6fb46a 2022-01-09 op #include <limits.h>
25 0d6fb46a 2022-01-09 op #include "ohash.h"
27 0d6fb46a 2022-01-09 op struct _ohash_record {
29 0d6fb46a 2022-01-09 op const char *p;
32 0d6fb46a 2022-01-09 op #define DELETED ((const char *)h)
33 0d6fb46a 2022-01-09 op #define NONE (h->size)
35 0d6fb46a 2022-01-09 op /* Don't bother changing the hash table if the change is small enough. */
36 0d6fb46a 2022-01-09 op #define MINSIZE (1UL << 4)
37 0d6fb46a 2022-01-09 op #define MINDELETED 4
39 0d6fb46a 2022-01-09 op static void ohash_resize(struct ohash *);
42 0d6fb46a 2022-01-09 op /* This handles the common case of variable length keys, where the
43 0d6fb46a 2022-01-09 op * key is stored at the end of the record.
46 0d6fb46a 2022-01-09 op ohash_create_entry(struct ohash_info *i, const char *start, const char **end)
51 0d6fb46a 2022-01-09 op *end = start + strlen(start);
52 0d6fb46a 2022-01-09 op p = (i->alloc)(i->key_offset + (*end - start) + 1, i->data);
54 0d6fb46a 2022-01-09 op memcpy(p+i->key_offset, start, *end-start);
55 0d6fb46a 2022-01-09 op p[i->key_offset + (*end - start)] = '\0';
57 0d6fb46a 2022-01-09 op return (void *)p;
60 0d6fb46a 2022-01-09 op /* hash_delete only frees the hash structure. Use hash_first/hash_next
61 0d6fb46a 2022-01-09 op * to free entries as well. */
63 0d6fb46a 2022-01-09 op ohash_delete(struct ohash *h)
65 0d6fb46a 2022-01-09 op (h->info.free)(h->t, h->info.data);
66 0d6fb46a 2022-01-09 op #ifndef NDEBUG
72 0d6fb46a 2022-01-09 op ohash_resize(struct ohash *h)
74 0d6fb46a 2022-01-09 op struct _ohash_record *n;
76 0d6fb46a 2022-01-09 op unsigned int j;
77 0d6fb46a 2022-01-09 op unsigned int i, incr;
79 0d6fb46a 2022-01-09 op if (4 * h->deleted < h->total) {
80 0d6fb46a 2022-01-09 op if (h->size >= (UINT_MAX >> 1U))
81 0d6fb46a 2022-01-09 op ns = UINT_MAX;
83 0d6fb46a 2022-01-09 op ns = h->size << 1U;
84 0d6fb46a 2022-01-09 op } else if (3 * h->deleted > 2 * h->total)
85 0d6fb46a 2022-01-09 op ns = h->size >> 1U;
88 0d6fb46a 2022-01-09 op if (ns < MINSIZE)
90 0d6fb46a 2022-01-09 op #ifdef STATS_HASH
91 0d6fb46a 2022-01-09 op STAT_HASH_EXPAND++;
92 0d6fb46a 2022-01-09 op STAT_HASH_SIZE += ns - h->size;
95 0d6fb46a 2022-01-09 op n = (h->info.calloc)(ns, sizeof(struct _ohash_record), h->info.data);
99 0d6fb46a 2022-01-09 op for (j = 0; j < h->size; j++) {
100 0d6fb46a 2022-01-09 op if (h->t[j].p != NULL && h->t[j].p != DELETED) {
101 0d6fb46a 2022-01-09 op i = h->t[j].hv % ns;
102 0d6fb46a 2022-01-09 op incr = ((h->t[j].hv % (ns - 2)) & ~1) + 1;
103 0d6fb46a 2022-01-09 op while (n[i].p != NULL) {
108 0d6fb46a 2022-01-09 op n[i].hv = h->t[j].hv;
109 0d6fb46a 2022-01-09 op n[i].p = h->t[j].p;
112 0d6fb46a 2022-01-09 op (h->info.free)(h->t, h->info.data);
114 0d6fb46a 2022-01-09 op h->size = ns;
115 0d6fb46a 2022-01-09 op h->total -= h->deleted;
116 0d6fb46a 2022-01-09 op h->deleted = 0;
120 0d6fb46a 2022-01-09 op ohash_remove(struct ohash *h, unsigned int i)
122 0d6fb46a 2022-01-09 op void *result = (void *)h->t[i].p;
124 0d6fb46a 2022-01-09 op if (result == NULL || result == DELETED)
127 0d6fb46a 2022-01-09 op #ifdef STATS_HASH
128 0d6fb46a 2022-01-09 op STAT_HASH_ENTRIES--;
130 0d6fb46a 2022-01-09 op h->t[i].p = DELETED;
131 0d6fb46a 2022-01-09 op h->deleted++;
132 0d6fb46a 2022-01-09 op if (h->deleted >= MINDELETED && 4 * h->deleted > h->total)
133 0d6fb46a 2022-01-09 op ohash_resize(h);
134 0d6fb46a 2022-01-09 op return result;
138 0d6fb46a 2022-01-09 op ohash_find(struct ohash *h, unsigned int i)
140 0d6fb46a 2022-01-09 op if (h->t[i].p == DELETED)
143 0d6fb46a 2022-01-09 op return (void *)h->t[i].p;
147 0d6fb46a 2022-01-09 op ohash_insert(struct ohash *h, unsigned int i, void *p)
149 0d6fb46a 2022-01-09 op #ifdef STATS_HASH
150 0d6fb46a 2022-01-09 op STAT_HASH_ENTRIES++;
152 0d6fb46a 2022-01-09 op if (h->t[i].p == DELETED) {
153 0d6fb46a 2022-01-09 op h->deleted--;
154 0d6fb46a 2022-01-09 op h->t[i].p = p;
156 0d6fb46a 2022-01-09 op h->t[i].p = p;
157 0d6fb46a 2022-01-09 op /* Arbitrary resize boundary. Tweak if not efficient enough. */
158 0d6fb46a 2022-01-09 op if (++h->total * 4 > h->size * 3)
159 0d6fb46a 2022-01-09 op ohash_resize(h);
165 0d6fb46a 2022-01-09 op ohash_entries(struct ohash *h)
167 0d6fb46a 2022-01-09 op return h->total - h->deleted;
171 0d6fb46a 2022-01-09 op ohash_first(struct ohash *h, unsigned int *pos)
174 0d6fb46a 2022-01-09 op return ohash_next(h, pos);
178 0d6fb46a 2022-01-09 op ohash_next(struct ohash *h, unsigned int *pos)
180 0d6fb46a 2022-01-09 op for (; *pos < h->size; (*pos)++)
181 0d6fb46a 2022-01-09 op if (h->t[*pos].p != DELETED && h->t[*pos].p != NULL)
182 0d6fb46a 2022-01-09 op return (void *)h->t[(*pos)++].p;
187 0d6fb46a 2022-01-09 op ohash_init(struct ohash *h, unsigned int size, struct ohash_info *info)
189 0d6fb46a 2022-01-09 op h->size = 1UL << size;
190 0d6fb46a 2022-01-09 op if (h->size < MINSIZE)
191 0d6fb46a 2022-01-09 op h->size = MINSIZE;
192 0d6fb46a 2022-01-09 op #ifdef STATS_HASH
193 0d6fb46a 2022-01-09 op STAT_HASH_CREATION++;
194 0d6fb46a 2022-01-09 op STAT_HASH_SIZE += h->size;
196 0d6fb46a 2022-01-09 op /* Copy info so that caller may free it. */
197 0d6fb46a 2022-01-09 op h->info.key_offset = info->key_offset;
198 0d6fb46a 2022-01-09 op h->info.calloc = info->calloc;
199 0d6fb46a 2022-01-09 op h->info.free = info->free;
200 0d6fb46a 2022-01-09 op h->info.alloc = info->alloc;
201 0d6fb46a 2022-01-09 op h->info.data = info->data;
202 0d6fb46a 2022-01-09 op h->t = (h->info.calloc)(h->size, sizeof(struct _ohash_record),
203 0d6fb46a 2022-01-09 op h->info.data);
204 0d6fb46a 2022-01-09 op h->total = h->deleted = 0;
208 0d6fb46a 2022-01-09 op ohash_interval(const char *s, const char **e)
213 0d6fb46a 2022-01-09 op *e = s + strlen(s);
218 0d6fb46a 2022-01-09 op while (s != *e)
219 0d6fb46a 2022-01-09 op k = ((k << 2) | (k >> 30)) ^ *s++;
224 0d6fb46a 2022-01-09 op ohash_lookup_interval(struct ohash *h, const char *start, const char *end,
227 0d6fb46a 2022-01-09 op unsigned int i, incr;
228 0d6fb46a 2022-01-09 op unsigned int empty;
230 0d6fb46a 2022-01-09 op #ifdef STATS_HASH
231 0d6fb46a 2022-01-09 op STAT_HASH_LOOKUP++;
233 0d6fb46a 2022-01-09 op empty = NONE;
234 0d6fb46a 2022-01-09 op i = hv % h->size;
235 0d6fb46a 2022-01-09 op incr = ((hv % (h->size-2)) & ~1) + 1;
236 0d6fb46a 2022-01-09 op while (h->t[i].p != NULL) {
237 0d6fb46a 2022-01-09 op #ifdef STATS_HASH
238 0d6fb46a 2022-01-09 op STAT_HASH_LENGTH++;
240 0d6fb46a 2022-01-09 op if (h->t[i].p == DELETED) {
241 0d6fb46a 2022-01-09 op if (empty == NONE)
243 0d6fb46a 2022-01-09 op } else if (h->t[i].hv == hv &&
244 0d6fb46a 2022-01-09 op strncmp(h->t[i].p+h->info.key_offset, start,
245 0d6fb46a 2022-01-09 op end - start) == 0 &&
246 0d6fb46a 2022-01-09 op (h->t[i].p+h->info.key_offset)[end-start] == '\0') {
247 0d6fb46a 2022-01-09 op if (empty != NONE) {
248 0d6fb46a 2022-01-09 op h->t[empty].hv = hv;
249 0d6fb46a 2022-01-09 op h->t[empty].p = h->t[i].p;
250 0d6fb46a 2022-01-09 op h->t[i].p = DELETED;
251 0d6fb46a 2022-01-09 op return empty;
253 0d6fb46a 2022-01-09 op #ifdef STATS_HASH
254 0d6fb46a 2022-01-09 op STAT_HASH_POSITIVE++;
260 0d6fb46a 2022-01-09 op if (i >= h->size)
261 0d6fb46a 2022-01-09 op i -= h->size;
264 0d6fb46a 2022-01-09 op /* Found an empty position. */
265 0d6fb46a 2022-01-09 op if (empty != NONE)
267 0d6fb46a 2022-01-09 op h->t[i].hv = hv;
272 0d6fb46a 2022-01-09 op ohash_lookup_memory(struct ohash *h, const char *k, size_t size, uint32_t hv)
274 0d6fb46a 2022-01-09 op unsigned int i, incr;
275 0d6fb46a 2022-01-09 op unsigned int empty;
277 0d6fb46a 2022-01-09 op #ifdef STATS_HASH
278 0d6fb46a 2022-01-09 op STAT_HASH_LOOKUP++;
280 0d6fb46a 2022-01-09 op empty = NONE;
281 0d6fb46a 2022-01-09 op i = hv % h->size;
282 0d6fb46a 2022-01-09 op incr = ((hv % (h->size-2)) & ~1) + 1;
283 0d6fb46a 2022-01-09 op while (h->t[i].p != NULL) {
284 0d6fb46a 2022-01-09 op #ifdef STATS_HASH
285 0d6fb46a 2022-01-09 op STAT_HASH_LENGTH++;
287 0d6fb46a 2022-01-09 op if (h->t[i].p == DELETED) {
288 0d6fb46a 2022-01-09 op if (empty == NONE)
290 0d6fb46a 2022-01-09 op } else if (h->t[i].hv == hv &&
291 0d6fb46a 2022-01-09 op memcmp(h->t[i].p+h->info.key_offset, k, size) == 0) {
292 0d6fb46a 2022-01-09 op if (empty != NONE) {
293 0d6fb46a 2022-01-09 op h->t[empty].hv = hv;
294 0d6fb46a 2022-01-09 op h->t[empty].p = h->t[i].p;
295 0d6fb46a 2022-01-09 op h->t[i].p = DELETED;
296 0d6fb46a 2022-01-09 op return empty;
298 0d6fb46a 2022-01-09 op #ifdef STATS_HASH
299 0d6fb46a 2022-01-09 op STAT_HASH_POSITIVE++;
304 0d6fb46a 2022-01-09 op if (i >= h->size)
305 0d6fb46a 2022-01-09 op i -= h->size;
308 0d6fb46a 2022-01-09 op /* Found an empty position. */
309 0d6fb46a 2022-01-09 op if (empty != NONE)
311 0d6fb46a 2022-01-09 op h->t[i].hv = hv;
316 0d6fb46a 2022-01-09 op ohash_qlookup(struct ohash *h, const char *s)
318 0d6fb46a 2022-01-09 op const char *e = NULL;
319 0d6fb46a 2022-01-09 op return ohash_qlookupi(h, s, &e);
323 0d6fb46a 2022-01-09 op ohash_qlookupi(struct ohash *h, const char *s, const char **e)
327 0d6fb46a 2022-01-09 op hv = ohash_interval(s, e);
328 0d6fb46a 2022-01-09 op return ohash_lookup_interval(h, s, *e, hv);