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