1228063Sbapt#ifndef OHASH_H
2228063Sbapt#define OHASH_H
3228063Sbapt/* $OpenBSD: ohash.h,v 1.8 2005/12/29 18:54:47 jaredy Exp $ */
4228063Sbapt/* ex:ts=8 sw=4:
5228063Sbapt */
6228063Sbapt
7228063Sbapt/* Copyright (c) 1999, 2004 Marc Espie <espie@openbsd.org>
8228063Sbapt *
9228063Sbapt * Permission to use, copy, modify, and distribute this software for any
10228063Sbapt * purpose with or without fee is hereby granted, provided that the above
11228063Sbapt * copyright notice and this permission notice appear in all copies.
12228063Sbapt *
13228063Sbapt * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14228063Sbapt * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15228063Sbapt * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16228063Sbapt * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17228063Sbapt * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18228063Sbapt * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19228063Sbapt * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20228063Sbapt *
21228063Sbapt * $FreeBSD$
22228063Sbapt */
23228063Sbapt
24228063Sbapt/* Open hashing support.
25228063Sbapt * Open hashing was chosen because it is much lighter than other hash
26228063Sbapt * techniques, and more efficient in most cases.
27228063Sbapt */
28228063Sbapt
29228063Sbaptstruct ohash_info {
30228063Sbapt	ptrdiff_t key_offset;
31228063Sbapt	void *data;	/* user data */
32228063Sbapt	void *(*halloc)(size_t, void *);
33228063Sbapt	void (*hfree)(void *, size_t, void *);
34228063Sbapt	void *(*alloc)(size_t, void *);
35228063Sbapt};
36228063Sbapt
37228063Sbaptstruct _ohash_record;
38228063Sbapt
39228063Sbaptstruct ohash {
40228063Sbapt	struct _ohash_record 	*t;
41228063Sbapt	struct ohash_info 	info;
42228063Sbapt	unsigned int 		size;
43228063Sbapt	unsigned int 		total;
44228063Sbapt	unsigned int 		deleted;
45228063Sbapt};
46228063Sbapt
47228063Sbapt/* For this to be tweakable, we use small primitives, and leave part of the
48228063Sbapt * logic to the client application.  e.g., hashing is left to the client
49228063Sbapt * application.  We also provide a simple table entry lookup that yields
50228063Sbapt * a hashing table index (opaque) to be used in find/insert/remove.
51228063Sbapt * The keys are stored at a known position in the client data.
52228063Sbapt */
53228063Sbapt__BEGIN_DECLS
54228063Sbaptvoid ohash_init(struct ohash *, unsigned, struct ohash_info *);
55228063Sbaptvoid ohash_delete(struct ohash *);
56228063Sbapt
57228063Sbaptunsigned int ohash_lookup_interval(struct ohash *, const char *,
58228063Sbapt	    const char *, u_int32_t);
59228063Sbaptunsigned int ohash_lookup_memory(struct ohash *, const char *,
60228063Sbapt	    size_t, u_int32_t);
61228063Sbaptvoid *ohash_find(struct ohash *, unsigned int);
62228063Sbaptvoid *ohash_remove(struct ohash *, unsigned int);
63228063Sbaptvoid *ohash_insert(struct ohash *, unsigned int, void *);
64228063Sbaptvoid *ohash_first(struct ohash *, unsigned int *);
65228063Sbaptvoid *ohash_next(struct ohash *, unsigned int *);
66228063Sbaptunsigned int ohash_entries(struct ohash *);
67228063Sbapt
68228063Sbaptvoid *ohash_create_entry(struct ohash_info *, const char *, const char **);
69228063Sbaptu_int32_t ohash_interval(const char *, const char **);
70228063Sbapt
71228063Sbaptunsigned int ohash_qlookupi(struct ohash *, const char *, const char **);
72228063Sbaptunsigned int ohash_qlookup(struct ohash *, const char *);
73228063Sbapt__END_DECLS
74228063Sbapt#endif
75228063Sbapt
76