xref: /minix3/usr.bin/m4/lib/ohash_lookup_memory.c (revision 2e8d1eda1b10b1eefcc41d19325e6baa0615ae5c)
1*2e8d1edaSArun Thomas /* $OpenBSD: ohash_lookup_memory.c,v 1.3 2006/01/16 15:52:25 espie Exp $ */
2*2e8d1edaSArun Thomas /* ex:ts=8 sw=4:
3*2e8d1edaSArun Thomas  */
4*2e8d1edaSArun Thomas 
5*2e8d1edaSArun Thomas /* Copyright (c) 1999, 2004 Marc Espie <espie@openbsd.org>
6*2e8d1edaSArun Thomas  *
7*2e8d1edaSArun Thomas  * Permission to use, copy, modify, and distribute this software for any
8*2e8d1edaSArun Thomas  * purpose with or without fee is hereby granted, provided that the above
9*2e8d1edaSArun Thomas  * copyright notice and this permission notice appear in all copies.
10*2e8d1edaSArun Thomas  *
11*2e8d1edaSArun Thomas  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12*2e8d1edaSArun Thomas  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13*2e8d1edaSArun Thomas  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14*2e8d1edaSArun Thomas  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15*2e8d1edaSArun Thomas  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16*2e8d1edaSArun Thomas  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17*2e8d1edaSArun Thomas  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18*2e8d1edaSArun Thomas  */
19*2e8d1edaSArun Thomas 
20*2e8d1edaSArun Thomas #include "ohash_int.h"
21*2e8d1edaSArun Thomas 
22*2e8d1edaSArun Thomas unsigned int
ohash_lookup_memory(struct ohash * h,const char * k,size_t size,uint32_t hv)23*2e8d1edaSArun Thomas ohash_lookup_memory(struct ohash *h, const char *k, size_t size, uint32_t hv)
24*2e8d1edaSArun Thomas {
25*2e8d1edaSArun Thomas 	unsigned int	i, incr;
26*2e8d1edaSArun Thomas 	unsigned int	empty;
27*2e8d1edaSArun Thomas 
28*2e8d1edaSArun Thomas #ifdef STATS_HASH
29*2e8d1edaSArun Thomas 	STAT_HASH_LOOKUP++;
30*2e8d1edaSArun Thomas #endif
31*2e8d1edaSArun Thomas 	empty = NONE;
32*2e8d1edaSArun Thomas 	i = hv % h->size;
33*2e8d1edaSArun Thomas 	incr = ((hv % (h->size-2)) & ~1) + 1;
34*2e8d1edaSArun Thomas 	while (h->t[i].p != NULL) {
35*2e8d1edaSArun Thomas #ifdef STATS_HASH
36*2e8d1edaSArun Thomas 		STAT_HASH_LENGTH++;
37*2e8d1edaSArun Thomas #endif
38*2e8d1edaSArun Thomas 		if (h->t[i].p == DELETED) {
39*2e8d1edaSArun Thomas 			if (empty == NONE)
40*2e8d1edaSArun Thomas 				empty = i;
41*2e8d1edaSArun Thomas 		} else if (h->t[i].hv == hv &&
42*2e8d1edaSArun Thomas 		    memcmp(h->t[i].p+h->info.key_offset, k, size) == 0) {
43*2e8d1edaSArun Thomas 		    	if (empty != NONE) {
44*2e8d1edaSArun Thomas 				h->t[empty].hv = hv;
45*2e8d1edaSArun Thomas 				h->t[empty].p = h->t[i].p;
46*2e8d1edaSArun Thomas 				h->t[i].p = DELETED;
47*2e8d1edaSArun Thomas 				return empty;
48*2e8d1edaSArun Thomas 			} else {
49*2e8d1edaSArun Thomas #ifdef STATS_HASH
50*2e8d1edaSArun Thomas 				STAT_HASH_POSITIVE++;
51*2e8d1edaSArun Thomas #endif
52*2e8d1edaSArun Thomas 			}	return i;
53*2e8d1edaSArun Thomas 		}
54*2e8d1edaSArun Thomas 		i += incr;
55*2e8d1edaSArun Thomas 		if (i >= h->size)
56*2e8d1edaSArun Thomas 			i -= h->size;
57*2e8d1edaSArun Thomas 	}
58*2e8d1edaSArun Thomas 
59*2e8d1edaSArun Thomas 	/* Found an empty position.  */
60*2e8d1edaSArun Thomas 	if (empty != NONE)
61*2e8d1edaSArun Thomas 		i = empty;
62*2e8d1edaSArun Thomas 	h->t[i].hv = hv;
63*2e8d1edaSArun Thomas 	return i;
64*2e8d1edaSArun Thomas }
65