xref: /freebsd/lib/libopenbsd/ohash.h (revision eb69d1f144a6fcc765d1b9d44a5ae8082353e70b)
1 /* $OpenBSD: src/lib/libutil/ohash.h,v 1.2 2014/06/02 18:52:03 deraadt Exp $ */
2 
3 /* Copyright (c) 1999, 2004 Marc Espie <espie@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  *
17  * $FreeBSD$
18  */
19 
20 #ifndef OHASH_H
21 #define OHASH_H
22 
23 #include <stddef.h>
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 /* user-visible data structure */
31 struct ohash_info {
32 	ptrdiff_t key_offset;
33 	void *data;	/* user data */
34 	void *(*calloc)(size_t, size_t, void *);
35 	void (*free)(void *, void *);
36 	void *(*alloc)(size_t, void *);
37 };
38 
39 struct _ohash_record;
40 
41 /* private structure. It's there just so you can do a sizeof */
42 struct ohash {
43 	struct _ohash_record 	*t;
44 	struct ohash_info 	info;
45 	unsigned int 		size;
46 	unsigned int 		total;
47 	unsigned int 		deleted;
48 };
49 
50 /* For this to be tweakable, we use small primitives, and leave part of the
51  * logic to the client application.  e.g., hashing is left to the client
52  * application.  We also provide a simple table entry lookup that yields
53  * a hashing table index (opaque) to be used in find/insert/remove.
54  * The keys are stored at a known position in the client data.
55  */
56 __BEGIN_DECLS
57 void ohash_init(struct ohash *, unsigned, struct ohash_info *);
58 void ohash_delete(struct ohash *);
59 
60 unsigned int ohash_lookup_interval(struct ohash *, const char *,
61 	    const char *, uint32_t);
62 unsigned int ohash_lookup_memory(struct ohash *, const char *,
63 	    size_t, uint32_t);
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 uint32_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