1269162Sbapt/* $OpenBSD: src/lib/libutil/ohash.h,v 1.2 2014/06/02 18:52:03 deraadt Exp $ */
2228063Sbapt
3228063Sbapt/* Copyright (c) 1999, 2004 Marc Espie <espie@openbsd.org>
4228063Sbapt *
5228063Sbapt * Permission to use, copy, modify, and distribute this software for any
6228063Sbapt * purpose with or without fee is hereby granted, provided that the above
7228063Sbapt * copyright notice and this permission notice appear in all copies.
8228063Sbapt *
9228063Sbapt * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10228063Sbapt * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11228063Sbapt * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12228063Sbapt * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13228063Sbapt * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14228063Sbapt * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15228063Sbapt * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16228063Sbapt *
17228063Sbapt * $FreeBSD: releng/11.0/lib/libopenbsd/ohash.h 288629 2015-10-03 20:06:50Z bdrewery $
18228063Sbapt */
19228063Sbapt
20269162Sbapt#ifndef OHASH_H
21269162Sbapt#define OHASH_H
22269162Sbapt
23288629Sbdrewery#include <stddef.h>
24288629Sbdrewery
25228063Sbapt/* Open hashing support.
26228063Sbapt * Open hashing was chosen because it is much lighter than other hash
27228063Sbapt * techniques, and more efficient in most cases.
28228063Sbapt */
29228063Sbapt
30269162Sbapt/* user-visible data structure */
31228063Sbaptstruct ohash_info {
32228063Sbapt	ptrdiff_t key_offset;
33228063Sbapt	void *data;	/* user data */
34269162Sbapt	void *(*calloc)(size_t, size_t, void *);
35269162Sbapt	void (*free)(void *, void *);
36228063Sbapt	void *(*alloc)(size_t, void *);
37228063Sbapt};
38228063Sbapt
39228063Sbaptstruct _ohash_record;
40228063Sbapt
41269162Sbapt/* private structure. It's there just so you can do a sizeof */
42228063Sbaptstruct ohash {
43228063Sbapt	struct _ohash_record 	*t;
44228063Sbapt	struct ohash_info 	info;
45228063Sbapt	unsigned int 		size;
46228063Sbapt	unsigned int 		total;
47228063Sbapt	unsigned int 		deleted;
48228063Sbapt};
49228063Sbapt
50228063Sbapt/* For this to be tweakable, we use small primitives, and leave part of the
51228063Sbapt * logic to the client application.  e.g., hashing is left to the client
52228063Sbapt * application.  We also provide a simple table entry lookup that yields
53228063Sbapt * a hashing table index (opaque) to be used in find/insert/remove.
54228063Sbapt * The keys are stored at a known position in the client data.
55228063Sbapt */
56228063Sbapt__BEGIN_DECLS
57228063Sbaptvoid ohash_init(struct ohash *, unsigned, struct ohash_info *);
58228063Sbaptvoid ohash_delete(struct ohash *);
59228063Sbapt
60228063Sbaptunsigned int ohash_lookup_interval(struct ohash *, const char *,
61269162Sbapt	    const char *, uint32_t);
62228063Sbaptunsigned int ohash_lookup_memory(struct ohash *, const char *,
63269231Sbapt	    size_t, uint32_t);
64228063Sbaptvoid *ohash_find(struct ohash *, unsigned int);
65228063Sbaptvoid *ohash_remove(struct ohash *, unsigned int);
66228063Sbaptvoid *ohash_insert(struct ohash *, unsigned int, void *);
67228063Sbaptvoid *ohash_first(struct ohash *, unsigned int *);
68228063Sbaptvoid *ohash_next(struct ohash *, unsigned int *);
69228063Sbaptunsigned int ohash_entries(struct ohash *);
70228063Sbapt
71228063Sbaptvoid *ohash_create_entry(struct ohash_info *, const char *, const char **);
72269162Sbaptuint32_t ohash_interval(const char *, const char **);
73228063Sbapt
74228063Sbaptunsigned int ohash_qlookupi(struct ohash *, const char *, const char **);
75228063Sbaptunsigned int ohash_qlookup(struct ohash *, const char *);
76228063Sbapt__END_DECLS
77228063Sbapt#endif
78