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