xref: /openbsd-src/lib/libutil/ohash.c (revision 41e21db3e57d5ef3698c2fe00fa94d4c4ac06d9f)
1*41e21db3Sderaadt /* $OpenBSD: ohash.c,v 1.1 2014/06/02 18:52:03 deraadt Exp $ */
2*41e21db3Sderaadt 
3*41e21db3Sderaadt /* Copyright (c) 1999, 2004 Marc Espie <espie@openbsd.org>
4*41e21db3Sderaadt  *
5*41e21db3Sderaadt  * Permission to use, copy, modify, and distribute this software for any
6*41e21db3Sderaadt  * purpose with or without fee is hereby granted, provided that the above
7*41e21db3Sderaadt  * copyright notice and this permission notice appear in all copies.
8*41e21db3Sderaadt  *
9*41e21db3Sderaadt  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10*41e21db3Sderaadt  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*41e21db3Sderaadt  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12*41e21db3Sderaadt  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*41e21db3Sderaadt  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*41e21db3Sderaadt  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15*41e21db3Sderaadt  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16*41e21db3Sderaadt  */
17*41e21db3Sderaadt 
18*41e21db3Sderaadt #include <stddef.h>
19*41e21db3Sderaadt #include <stdint.h>
20*41e21db3Sderaadt #include <stdlib.h>
21*41e21db3Sderaadt #include <string.h>
22*41e21db3Sderaadt #include <limits.h>
23*41e21db3Sderaadt #include "ohash.h"
24*41e21db3Sderaadt 
25*41e21db3Sderaadt struct _ohash_record {
26*41e21db3Sderaadt 	uint32_t	hv;
27*41e21db3Sderaadt 	const char	*p;
28*41e21db3Sderaadt };
29*41e21db3Sderaadt 
30*41e21db3Sderaadt #define DELETED		((const char *)h)
31*41e21db3Sderaadt #define NONE		(h->size)
32*41e21db3Sderaadt 
33*41e21db3Sderaadt /* Don't bother changing the hash table if the change is small enough.  */
34*41e21db3Sderaadt #define MINSIZE		(1UL << 4)
35*41e21db3Sderaadt #define MINDELETED	4
36*41e21db3Sderaadt 
37*41e21db3Sderaadt static void ohash_resize(struct ohash *);
38*41e21db3Sderaadt 
39*41e21db3Sderaadt 
40*41e21db3Sderaadt /* This handles the common case of variable length keys, where the
41*41e21db3Sderaadt  * key is stored at the end of the record.
42*41e21db3Sderaadt  */
43*41e21db3Sderaadt void *
ohash_create_entry(struct ohash_info * i,const char * start,const char ** end)44*41e21db3Sderaadt ohash_create_entry(struct ohash_info *i, const char *start, const char **end)
45*41e21db3Sderaadt {
46*41e21db3Sderaadt 	char *p;
47*41e21db3Sderaadt 
48*41e21db3Sderaadt 	if (!*end)
49*41e21db3Sderaadt 		*end = start + strlen(start);
50*41e21db3Sderaadt 	p = (i->alloc)(i->key_offset + (*end - start) + 1, i->data);
51*41e21db3Sderaadt 	if (p) {
52*41e21db3Sderaadt 		memcpy(p+i->key_offset, start, *end-start);
53*41e21db3Sderaadt 		p[i->key_offset + (*end - start)] = '\0';
54*41e21db3Sderaadt 	}
55*41e21db3Sderaadt 	return (void *)p;
56*41e21db3Sderaadt }
57*41e21db3Sderaadt 
58*41e21db3Sderaadt /* hash_delete only frees the hash structure. Use hash_first/hash_next
59*41e21db3Sderaadt  * to free entries as well.  */
60*41e21db3Sderaadt void
ohash_delete(struct ohash * h)61*41e21db3Sderaadt ohash_delete(struct ohash *h)
62*41e21db3Sderaadt {
63*41e21db3Sderaadt 	(h->info.free)(h->t, h->info.data);
64*41e21db3Sderaadt #ifndef NDEBUG
65*41e21db3Sderaadt 	h->t = NULL;
66*41e21db3Sderaadt #endif
67*41e21db3Sderaadt }
68*41e21db3Sderaadt 
69*41e21db3Sderaadt static void
ohash_resize(struct ohash * h)70*41e21db3Sderaadt ohash_resize(struct ohash *h)
71*41e21db3Sderaadt {
72*41e21db3Sderaadt 	struct _ohash_record *n;
73*41e21db3Sderaadt 	size_t ns;
74*41e21db3Sderaadt 	unsigned int	j;
75*41e21db3Sderaadt 	unsigned int	i, incr;
76*41e21db3Sderaadt 
77*41e21db3Sderaadt 	if (4 * h->deleted < h->total) {
78*41e21db3Sderaadt 		if (h->size >= (UINT_MAX >> 1U))
79*41e21db3Sderaadt 			ns = UINT_MAX;
80*41e21db3Sderaadt 		else
81*41e21db3Sderaadt 			ns = h->size << 1U;
82*41e21db3Sderaadt 	} else if (3 * h->deleted > 2 * h->total)
83*41e21db3Sderaadt 		ns = h->size >> 1U;
84*41e21db3Sderaadt 	else
85*41e21db3Sderaadt 		ns = h->size;
86*41e21db3Sderaadt 	if (ns < MINSIZE)
87*41e21db3Sderaadt 		ns = MINSIZE;
88*41e21db3Sderaadt #ifdef STATS_HASH
89*41e21db3Sderaadt 	STAT_HASH_EXPAND++;
90*41e21db3Sderaadt 	STAT_HASH_SIZE += ns - h->size;
91*41e21db3Sderaadt #endif
92*41e21db3Sderaadt 
93*41e21db3Sderaadt 	n = (h->info.calloc)(ns, sizeof(struct _ohash_record), h->info.data);
94*41e21db3Sderaadt 	if (!n)
95*41e21db3Sderaadt 		return;
96*41e21db3Sderaadt 
97*41e21db3Sderaadt 	for (j = 0; j < h->size; j++) {
98*41e21db3Sderaadt 		if (h->t[j].p != NULL && h->t[j].p != DELETED) {
99*41e21db3Sderaadt 			i = h->t[j].hv % ns;
100*41e21db3Sderaadt 			incr = ((h->t[j].hv % (ns - 2)) & ~1) + 1;
101*41e21db3Sderaadt 			while (n[i].p != NULL) {
102*41e21db3Sderaadt 				i += incr;
103*41e21db3Sderaadt 				if (i >= ns)
104*41e21db3Sderaadt 					i -= ns;
105*41e21db3Sderaadt 			}
106*41e21db3Sderaadt 			n[i].hv = h->t[j].hv;
107*41e21db3Sderaadt 			n[i].p = h->t[j].p;
108*41e21db3Sderaadt 		}
109*41e21db3Sderaadt 	}
110*41e21db3Sderaadt 	(h->info.free)(h->t, h->info.data);
111*41e21db3Sderaadt 	h->t = n;
112*41e21db3Sderaadt 	h->size = ns;
113*41e21db3Sderaadt 	h->total -= h->deleted;
114*41e21db3Sderaadt 	h->deleted = 0;
115*41e21db3Sderaadt }
116*41e21db3Sderaadt 
117*41e21db3Sderaadt void *
ohash_remove(struct ohash * h,unsigned int i)118*41e21db3Sderaadt ohash_remove(struct ohash *h, unsigned int i)
119*41e21db3Sderaadt {
120*41e21db3Sderaadt 	void		*result = (void *)h->t[i].p;
121*41e21db3Sderaadt 
122*41e21db3Sderaadt 	if (result == NULL || result == DELETED)
123*41e21db3Sderaadt 		return NULL;
124*41e21db3Sderaadt 
125*41e21db3Sderaadt #ifdef STATS_HASH
126*41e21db3Sderaadt 	STAT_HASH_ENTRIES--;
127*41e21db3Sderaadt #endif
128*41e21db3Sderaadt 	h->t[i].p = DELETED;
129*41e21db3Sderaadt 	h->deleted++;
130*41e21db3Sderaadt 	if (h->deleted >= MINDELETED && 4 * h->deleted > h->total)
131*41e21db3Sderaadt 		ohash_resize(h);
132*41e21db3Sderaadt 	return result;
133*41e21db3Sderaadt }
134*41e21db3Sderaadt 
135*41e21db3Sderaadt void *
ohash_find(struct ohash * h,unsigned int i)136*41e21db3Sderaadt ohash_find(struct ohash *h, unsigned int i)
137*41e21db3Sderaadt {
138*41e21db3Sderaadt 	if (h->t[i].p == DELETED)
139*41e21db3Sderaadt 		return NULL;
140*41e21db3Sderaadt 	else
141*41e21db3Sderaadt 		return (void *)h->t[i].p;
142*41e21db3Sderaadt }
143*41e21db3Sderaadt 
144*41e21db3Sderaadt void *
ohash_insert(struct ohash * h,unsigned int i,void * p)145*41e21db3Sderaadt ohash_insert(struct ohash *h, unsigned int i, void *p)
146*41e21db3Sderaadt {
147*41e21db3Sderaadt #ifdef STATS_HASH
148*41e21db3Sderaadt 	STAT_HASH_ENTRIES++;
149*41e21db3Sderaadt #endif
150*41e21db3Sderaadt 	if (h->t[i].p == DELETED) {
151*41e21db3Sderaadt 		h->deleted--;
152*41e21db3Sderaadt 		h->t[i].p = p;
153*41e21db3Sderaadt 	} else {
154*41e21db3Sderaadt 		h->t[i].p = p;
155*41e21db3Sderaadt 		/* Arbitrary resize boundary.  Tweak if not efficient enough.  */
156*41e21db3Sderaadt 		if (++h->total * 4 > h->size * 3)
157*41e21db3Sderaadt 			ohash_resize(h);
158*41e21db3Sderaadt 	}
159*41e21db3Sderaadt 	return p;
160*41e21db3Sderaadt }
161*41e21db3Sderaadt 
162*41e21db3Sderaadt unsigned int
ohash_entries(struct ohash * h)163*41e21db3Sderaadt ohash_entries(struct ohash *h)
164*41e21db3Sderaadt {
165*41e21db3Sderaadt 	return h->total - h->deleted;
166*41e21db3Sderaadt }
167*41e21db3Sderaadt 
168*41e21db3Sderaadt void *
ohash_first(struct ohash * h,unsigned int * pos)169*41e21db3Sderaadt ohash_first(struct ohash *h, unsigned int *pos)
170*41e21db3Sderaadt {
171*41e21db3Sderaadt 	*pos = 0;
172*41e21db3Sderaadt 	return ohash_next(h, pos);
173*41e21db3Sderaadt }
174*41e21db3Sderaadt 
175*41e21db3Sderaadt void *
ohash_next(struct ohash * h,unsigned int * pos)176*41e21db3Sderaadt ohash_next(struct ohash *h, unsigned int *pos)
177*41e21db3Sderaadt {
178*41e21db3Sderaadt 	for (; *pos < h->size; (*pos)++)
179*41e21db3Sderaadt 		if (h->t[*pos].p != DELETED && h->t[*pos].p != NULL)
180*41e21db3Sderaadt 			return (void *)h->t[(*pos)++].p;
181*41e21db3Sderaadt 	return NULL;
182*41e21db3Sderaadt }
183*41e21db3Sderaadt 
184*41e21db3Sderaadt void
ohash_init(struct ohash * h,unsigned int size,struct ohash_info * info)185*41e21db3Sderaadt ohash_init(struct ohash *h, unsigned int size, struct ohash_info *info)
186*41e21db3Sderaadt {
187*41e21db3Sderaadt 	h->size = 1UL << size;
188*41e21db3Sderaadt 	if (h->size < MINSIZE)
189*41e21db3Sderaadt 		h->size = MINSIZE;
190*41e21db3Sderaadt #ifdef STATS_HASH
191*41e21db3Sderaadt 	STAT_HASH_CREATION++;
192*41e21db3Sderaadt 	STAT_HASH_SIZE += h->size;
193*41e21db3Sderaadt #endif
194*41e21db3Sderaadt 	/* Copy info so that caller may free it.  */
195*41e21db3Sderaadt 	h->info.key_offset = info->key_offset;
196*41e21db3Sderaadt 	h->info.calloc = info->calloc;
197*41e21db3Sderaadt 	h->info.free = info->free;
198*41e21db3Sderaadt 	h->info.alloc = info->alloc;
199*41e21db3Sderaadt 	h->info.data = info->data;
200*41e21db3Sderaadt 	h->t = (h->info.calloc)(h->size, sizeof(struct _ohash_record),
201*41e21db3Sderaadt 		    h->info.data);
202*41e21db3Sderaadt 	h->total = h->deleted = 0;
203*41e21db3Sderaadt }
204*41e21db3Sderaadt 
205*41e21db3Sderaadt uint32_t
ohash_interval(const char * s,const char ** e)206*41e21db3Sderaadt ohash_interval(const char *s, const char **e)
207*41e21db3Sderaadt {
208*41e21db3Sderaadt 	uint32_t k;
209*41e21db3Sderaadt 
210*41e21db3Sderaadt 	if (!*e)
211*41e21db3Sderaadt 		*e = s + strlen(s);
212*41e21db3Sderaadt 	if (s == *e)
213*41e21db3Sderaadt 		k = 0;
214*41e21db3Sderaadt 	else
215*41e21db3Sderaadt 		k = *s++;
216*41e21db3Sderaadt 	while (s != *e)
217*41e21db3Sderaadt 		k =  ((k << 2) | (k >> 30)) ^ *s++;
218*41e21db3Sderaadt 	return k;
219*41e21db3Sderaadt }
220*41e21db3Sderaadt 
221*41e21db3Sderaadt unsigned int
ohash_lookup_interval(struct ohash * h,const char * start,const char * end,uint32_t hv)222*41e21db3Sderaadt ohash_lookup_interval(struct ohash *h, const char *start, const char *end,
223*41e21db3Sderaadt     uint32_t hv)
224*41e21db3Sderaadt {
225*41e21db3Sderaadt 	unsigned int	i, incr;
226*41e21db3Sderaadt 	unsigned int	empty;
227*41e21db3Sderaadt 
228*41e21db3Sderaadt #ifdef STATS_HASH
229*41e21db3Sderaadt 	STAT_HASH_LOOKUP++;
230*41e21db3Sderaadt #endif
231*41e21db3Sderaadt 	empty = NONE;
232*41e21db3Sderaadt 	i = hv % h->size;
233*41e21db3Sderaadt 	incr = ((hv % (h->size-2)) & ~1) + 1;
234*41e21db3Sderaadt 	while (h->t[i].p != NULL) {
235*41e21db3Sderaadt #ifdef STATS_HASH
236*41e21db3Sderaadt 		STAT_HASH_LENGTH++;
237*41e21db3Sderaadt #endif
238*41e21db3Sderaadt 		if (h->t[i].p == DELETED) {
239*41e21db3Sderaadt 			if (empty == NONE)
240*41e21db3Sderaadt 				empty = i;
241*41e21db3Sderaadt 		} else if (h->t[i].hv == hv &&
242*41e21db3Sderaadt 		    strncmp(h->t[i].p+h->info.key_offset, start,
243*41e21db3Sderaadt 			end - start) == 0 &&
244*41e21db3Sderaadt 		    (h->t[i].p+h->info.key_offset)[end-start] == '\0') {
245*41e21db3Sderaadt 			if (empty != NONE) {
246*41e21db3Sderaadt 				h->t[empty].hv = hv;
247*41e21db3Sderaadt 				h->t[empty].p = h->t[i].p;
248*41e21db3Sderaadt 				h->t[i].p = DELETED;
249*41e21db3Sderaadt 				return empty;
250*41e21db3Sderaadt 			} else {
251*41e21db3Sderaadt #ifdef STATS_HASH
252*41e21db3Sderaadt 				STAT_HASH_POSITIVE++;
253*41e21db3Sderaadt #endif
254*41e21db3Sderaadt 				return i;
255*41e21db3Sderaadt 			}
256*41e21db3Sderaadt 		}
257*41e21db3Sderaadt 		i += incr;
258*41e21db3Sderaadt 		if (i >= h->size)
259*41e21db3Sderaadt 			i -= h->size;
260*41e21db3Sderaadt 	}
261*41e21db3Sderaadt 
262*41e21db3Sderaadt 	/* Found an empty position.  */
263*41e21db3Sderaadt 	if (empty != NONE)
264*41e21db3Sderaadt 		i = empty;
265*41e21db3Sderaadt 	h->t[i].hv = hv;
266*41e21db3Sderaadt 	return i;
267*41e21db3Sderaadt }
268*41e21db3Sderaadt 
269*41e21db3Sderaadt unsigned int
ohash_lookup_memory(struct ohash * h,const char * k,size_t size,uint32_t hv)270*41e21db3Sderaadt ohash_lookup_memory(struct ohash *h, const char *k, size_t size, uint32_t hv)
271*41e21db3Sderaadt {
272*41e21db3Sderaadt 	unsigned int	i, incr;
273*41e21db3Sderaadt 	unsigned int	empty;
274*41e21db3Sderaadt 
275*41e21db3Sderaadt #ifdef STATS_HASH
276*41e21db3Sderaadt 	STAT_HASH_LOOKUP++;
277*41e21db3Sderaadt #endif
278*41e21db3Sderaadt 	empty = NONE;
279*41e21db3Sderaadt 	i = hv % h->size;
280*41e21db3Sderaadt 	incr = ((hv % (h->size-2)) & ~1) + 1;
281*41e21db3Sderaadt 	while (h->t[i].p != NULL) {
282*41e21db3Sderaadt #ifdef STATS_HASH
283*41e21db3Sderaadt 		STAT_HASH_LENGTH++;
284*41e21db3Sderaadt #endif
285*41e21db3Sderaadt 		if (h->t[i].p == DELETED) {
286*41e21db3Sderaadt 			if (empty == NONE)
287*41e21db3Sderaadt 				empty = i;
288*41e21db3Sderaadt 		} else if (h->t[i].hv == hv &&
289*41e21db3Sderaadt 		    memcmp(h->t[i].p+h->info.key_offset, k, size) == 0) {
290*41e21db3Sderaadt 			if (empty != NONE) {
291*41e21db3Sderaadt 				h->t[empty].hv = hv;
292*41e21db3Sderaadt 				h->t[empty].p = h->t[i].p;
293*41e21db3Sderaadt 				h->t[i].p = DELETED;
294*41e21db3Sderaadt 				return empty;
295*41e21db3Sderaadt 			} else {
296*41e21db3Sderaadt #ifdef STATS_HASH
297*41e21db3Sderaadt 				STAT_HASH_POSITIVE++;
298*41e21db3Sderaadt #endif
299*41e21db3Sderaadt 			}	return i;
300*41e21db3Sderaadt 		}
301*41e21db3Sderaadt 		i += incr;
302*41e21db3Sderaadt 		if (i >= h->size)
303*41e21db3Sderaadt 			i -= h->size;
304*41e21db3Sderaadt 	}
305*41e21db3Sderaadt 
306*41e21db3Sderaadt 	/* Found an empty position.  */
307*41e21db3Sderaadt 	if (empty != NONE)
308*41e21db3Sderaadt 		i = empty;
309*41e21db3Sderaadt 	h->t[i].hv = hv;
310*41e21db3Sderaadt 	return i;
311*41e21db3Sderaadt }
312*41e21db3Sderaadt 
313*41e21db3Sderaadt unsigned int
ohash_qlookup(struct ohash * h,const char * s)314*41e21db3Sderaadt ohash_qlookup(struct ohash *h, const char *s)
315*41e21db3Sderaadt {
316*41e21db3Sderaadt 	const char *e = NULL;
317*41e21db3Sderaadt 	return ohash_qlookupi(h, s, &e);
318*41e21db3Sderaadt }
319*41e21db3Sderaadt 
320*41e21db3Sderaadt unsigned int
ohash_qlookupi(struct ohash * h,const char * s,const char ** e)321*41e21db3Sderaadt ohash_qlookupi(struct ohash *h, const char *s, const char **e)
322*41e21db3Sderaadt {
323*41e21db3Sderaadt 	uint32_t hv;
324*41e21db3Sderaadt 
325*41e21db3Sderaadt 	hv = ohash_interval(s, e);
326*41e21db3Sderaadt 	return ohash_lookup_interval(h, s, *e, hv);
327*41e21db3Sderaadt }
328