14d9c8bcbapt/* $OpenBSD: src/lib/libutil/ohash.h,v 1.2 2014/06/02 18:52:03 deraadt Exp $ */
20d5c584bapt
30d5c584bapt/* Copyright (c) 1999, 2004 Marc Espie <espie@openbsd.org>
40d5c584bapt *
50d5c584bapt * Permission to use, copy, modify, and distribute this software for any
60d5c584bapt * purpose with or without fee is hereby granted, provided that the above
70d5c584bapt * copyright notice and this permission notice appear in all copies.
80d5c584bapt *
90d5c584bapt * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
100d5c584bapt * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
110d5c584bapt * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
120d5c584bapt * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
130d5c584bapt * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
140d5c584bapt * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
150d5c584bapt * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
160d5c584bapt *
170d5c584bapt * $FreeBSD$
180d5c584bapt */
190d5c584bapt
204d9c8bcbapt#ifndef OHASH_H
214d9c8bcbapt#define OHASH_H
224d9c8bcbapt
23c4aef72bdrewery#include <stddef.h>
24c4aef72bdrewery
250d5c584bapt/* Open hashing support.
260d5c584bapt * Open hashing was chosen because it is much lighter than other hash
270d5c584bapt * techniques, and more efficient in most cases.
280d5c584bapt */
290d5c584bapt
304d9c8bcbapt/* user-visible data structure */
310d5c584baptstruct ohash_info {
320d5c584bapt	ptrdiff_t key_offset;
330d5c584bapt	void *data;	/* user data */
344d9c8bcbapt	void *(*calloc)(size_t, size_t, void *);
354d9c8bcbapt	void (*free)(void *, void *);
360d5c584bapt	void *(*alloc)(size_t, void *);
370d5c584bapt};
380d5c584bapt
390d5c584baptstruct _ohash_record;
400d5c584bapt
414d9c8bcbapt/* private structure. It's there just so you can do a sizeof */
420d5c584baptstruct ohash {
430d5c584bapt	struct _ohash_record 	*t;
440d5c584bapt	struct ohash_info 	info;
450d5c584bapt	unsigned int 		size;
460d5c584bapt	unsigned int 		total;
470d5c584bapt	unsigned int 		deleted;
480d5c584bapt};
490d5c584bapt
500d5c584bapt/* For this to be tweakable, we use small primitives, and leave part of the
510d5c584bapt * logic to the client application.  e.g., hashing is left to the client
520d5c584bapt * application.  We also provide a simple table entry lookup that yields
530d5c584bapt * a hashing table index (opaque) to be used in find/insert/remove.
540d5c584bapt * The keys are stored at a known position in the client data.
550d5c584bapt */
560d5c584bapt__BEGIN_DECLS
570d5c584baptvoid ohash_init(struct ohash *, unsigned, struct ohash_info *);
580d5c584baptvoid ohash_delete(struct ohash *);
590d5c584bapt
600d5c584baptunsigned int ohash_lookup_interval(struct ohash *, const char *,
614d9c8bcbapt	    const char *, uint32_t);
620d5c584baptunsigned int ohash_lookup_memory(struct ohash *, const char *,
6342f5f53bapt	    size_t, uint32_t);
640d5c584baptvoid *ohash_find(struct ohash *, unsigned int);
650d5c584baptvoid *ohash_remove(struct ohash *, unsigned int);
660d5c584baptvoid *ohash_insert(struct ohash *, unsigned int, void *);
670d5c584baptvoid *ohash_first(struct ohash *, unsigned int *);
680d5c584baptvoid *ohash_next(struct ohash *, unsigned int *);
690d5c584baptunsigned int ohash_entries(struct ohash *);
700d5c584bapt
710d5c584baptvoid *ohash_create_entry(struct ohash_info *, const char *, const char **);
724d9c8bcbaptuint32_t ohash_interval(const char *, const char **);
730d5c584bapt
740d5c584baptunsigned int ohash_qlookupi(struct ohash *, const char *, const char **);
750d5c584baptunsigned int ohash_qlookup(struct ohash *, const char *);
760d5c584bapt__END_DECLS
770d5c584bapt#endif
78