xref: /dflybsd-src/usr.bin/m4/lib/ohash.h (revision 4da66bbfa353d0fb44e7a3c17f7268748edba48b)
1*4afad3d8SPeter Avalos #ifndef OHASH_H
2*4afad3d8SPeter Avalos #define OHASH_H
3*4afad3d8SPeter Avalos /* $OpenBSD: ohash.h,v 1.8 2005/12/29 18:54:47 jaredy Exp $ */
4*4afad3d8SPeter Avalos /* ex:ts=8 sw=4:
5*4afad3d8SPeter Avalos  */
6*4afad3d8SPeter Avalos 
7*4afad3d8SPeter Avalos /* Copyright (c) 1999, 2004 Marc Espie <espie@openbsd.org>
8*4afad3d8SPeter Avalos  *
9*4afad3d8SPeter Avalos  * Permission to use, copy, modify, and distribute this software for any
10*4afad3d8SPeter Avalos  * purpose with or without fee is hereby granted, provided that the above
11*4afad3d8SPeter Avalos  * copyright notice and this permission notice appear in all copies.
12*4afad3d8SPeter Avalos  *
13*4afad3d8SPeter Avalos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14*4afad3d8SPeter Avalos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15*4afad3d8SPeter Avalos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16*4afad3d8SPeter Avalos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17*4afad3d8SPeter Avalos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18*4afad3d8SPeter Avalos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19*4afad3d8SPeter Avalos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20*4afad3d8SPeter Avalos  *
21*4afad3d8SPeter Avalos  * $FreeBSD: src/usr.bin/m4/lib/ohash.h,v 1.2 2012/11/17 01:54:24 svnexp Exp $
22*4afad3d8SPeter Avalos  */
23*4afad3d8SPeter Avalos 
24*4afad3d8SPeter Avalos /* Open hashing support.
25*4afad3d8SPeter Avalos  * Open hashing was chosen because it is much lighter than other hash
26*4afad3d8SPeter Avalos  * techniques, and more efficient in most cases.
27*4afad3d8SPeter Avalos  */
28*4afad3d8SPeter Avalos 
29*4afad3d8SPeter Avalos struct ohash_info {
30*4afad3d8SPeter Avalos 	ptrdiff_t key_offset;
31*4afad3d8SPeter Avalos 	void *data;	/* user data */
32*4afad3d8SPeter Avalos 	void *(*halloc)(size_t, void *);
33*4afad3d8SPeter Avalos 	void (*hfree)(void *, size_t, void *);
34*4afad3d8SPeter Avalos 	void *(*alloc)(size_t, void *);
35*4afad3d8SPeter Avalos };
36*4afad3d8SPeter Avalos 
37*4afad3d8SPeter Avalos struct _ohash_record;
38*4afad3d8SPeter Avalos 
39*4afad3d8SPeter Avalos struct ohash {
40*4afad3d8SPeter Avalos 	struct _ohash_record	*t;
41*4afad3d8SPeter Avalos 	struct ohash_info	info;
42*4afad3d8SPeter Avalos 	unsigned int		size;
43*4afad3d8SPeter Avalos 	unsigned int		total;
44*4afad3d8SPeter Avalos 	unsigned int		deleted;
45*4afad3d8SPeter Avalos };
46*4afad3d8SPeter Avalos 
47*4afad3d8SPeter Avalos /* For this to be tweakable, we use small primitives, and leave part of the
48*4afad3d8SPeter Avalos  * logic to the client application.  e.g., hashing is left to the client
49*4afad3d8SPeter Avalos  * application.  We also provide a simple table entry lookup that yields
50*4afad3d8SPeter Avalos  * a hashing table index (opaque) to be used in find/insert/remove.
51*4afad3d8SPeter Avalos  * The keys are stored at a known position in the client data.
52*4afad3d8SPeter Avalos  */
53*4afad3d8SPeter Avalos __BEGIN_DECLS
54*4afad3d8SPeter Avalos void ohash_init(struct ohash *, unsigned, struct ohash_info *);
55*4afad3d8SPeter Avalos void ohash_delete(struct ohash *);
56*4afad3d8SPeter Avalos 
57*4afad3d8SPeter Avalos unsigned int ohash_lookup_interval(struct ohash *, const char *,
58*4afad3d8SPeter Avalos 				   const char *, uint32_t);
59*4afad3d8SPeter Avalos unsigned int ohash_lookup_memory(struct ohash *, const char *,
60*4afad3d8SPeter Avalos 				 size_t, uint32_t);
61*4afad3d8SPeter Avalos void *ohash_find(struct ohash *, unsigned int);
62*4afad3d8SPeter Avalos void *ohash_remove(struct ohash *, unsigned int);
63*4afad3d8SPeter Avalos void *ohash_insert(struct ohash *, unsigned int, void *);
64*4afad3d8SPeter Avalos void *ohash_first(struct ohash *, unsigned int *);
65*4afad3d8SPeter Avalos void *ohash_next(struct ohash *, unsigned int *);
66*4afad3d8SPeter Avalos unsigned int ohash_entries(struct ohash *);
67*4afad3d8SPeter Avalos 
68*4afad3d8SPeter Avalos void *ohash_create_entry(struct ohash_info *, const char *, const char **);
69*4afad3d8SPeter Avalos uint32_t ohash_interval(const char *, const char **);
70*4afad3d8SPeter Avalos 
71*4afad3d8SPeter Avalos unsigned int ohash_qlookupi(struct ohash *, const char *, const char **);
72*4afad3d8SPeter Avalos unsigned int ohash_qlookup(struct ohash *, const char *);
73*4afad3d8SPeter Avalos __END_DECLS
74*4afad3d8SPeter Avalos #endif
75