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