xref: /openbsd-src/lib/libutil/ohash.h (revision 41e21db3e57d5ef3698c2fe00fa94d4c4ac06d9f)
1*41e21db3Sderaadt /* $OpenBSD: ohash.h,v 1.2 2014/06/02 18:52:03 deraadt Exp $ */
242dcb487Sespie 
342dcb487Sespie /* Copyright (c) 1999, 2004 Marc Espie <espie@openbsd.org>
442dcb487Sespie  *
542dcb487Sespie  * Permission to use, copy, modify, and distribute this software for any
642dcb487Sespie  * purpose with or without fee is hereby granted, provided that the above
742dcb487Sespie  * copyright notice and this permission notice appear in all copies.
842dcb487Sespie  *
942dcb487Sespie  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1042dcb487Sespie  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1142dcb487Sespie  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1242dcb487Sespie  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1342dcb487Sespie  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1442dcb487Sespie  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1542dcb487Sespie  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1642dcb487Sespie  */
1742dcb487Sespie 
18*41e21db3Sderaadt #ifndef OHASH_H
19*41e21db3Sderaadt #define OHASH_H
20*41e21db3Sderaadt 
2142dcb487Sespie /* Open hashing support.
2242dcb487Sespie  * Open hashing was chosen because it is much lighter than other hash
2342dcb487Sespie  * techniques, and more efficient in most cases.
2442dcb487Sespie  */
2542dcb487Sespie 
2642dcb487Sespie /* user-visible data structure */
2742dcb487Sespie struct ohash_info {
2842dcb487Sespie 	ptrdiff_t key_offset;
2942dcb487Sespie 	void *data;	/* user data */
3042dcb487Sespie 	void *(*calloc)(size_t, size_t, void *);
3142dcb487Sespie 	void (*free)(void *, void *);
3242dcb487Sespie 	void *(*alloc)(size_t, void *);
3342dcb487Sespie };
3442dcb487Sespie 
3542dcb487Sespie struct _ohash_record;
3642dcb487Sespie 
3742dcb487Sespie /* private structure. It's there just so you can do a sizeof */
3842dcb487Sespie struct ohash {
3942dcb487Sespie 	struct _ohash_record 	*t;
4042dcb487Sespie 	struct ohash_info 	info;
4142dcb487Sespie 	unsigned int 		size;
4242dcb487Sespie 	unsigned int 		total;
4342dcb487Sespie 	unsigned int 		deleted;
4442dcb487Sespie };
4542dcb487Sespie 
4642dcb487Sespie /* For this to be tweakable, we use small primitives, and leave part of the
4742dcb487Sespie  * logic to the client application.  e.g., hashing is left to the client
4842dcb487Sespie  * application.  We also provide a simple table entry lookup that yields
4942dcb487Sespie  * a hashing table index (opaque) to be used in find/insert/remove.
5042dcb487Sespie  * The keys are stored at a known position in the client data.
5142dcb487Sespie  */
5242dcb487Sespie __BEGIN_DECLS
5342dcb487Sespie void ohash_init(struct ohash *, unsigned, struct ohash_info *);
5442dcb487Sespie void ohash_delete(struct ohash *);
5542dcb487Sespie 
5642dcb487Sespie unsigned int ohash_lookup_interval(struct ohash *, const char *,
5742dcb487Sespie 	    const char *, uint32_t);
5842dcb487Sespie unsigned int ohash_lookup_memory(struct ohash *, const char *,
5942dcb487Sespie 	    size_t, uint32_t)
6042dcb487Sespie 		__attribute__ ((__bounded__(__string__,2,3)));
6142dcb487Sespie void *ohash_find(struct ohash *, unsigned int);
6242dcb487Sespie void *ohash_remove(struct ohash *, unsigned int);
6342dcb487Sespie void *ohash_insert(struct ohash *, unsigned int, void *);
6442dcb487Sespie void *ohash_first(struct ohash *, unsigned int *);
6542dcb487Sespie void *ohash_next(struct ohash *, unsigned int *);
6642dcb487Sespie unsigned int ohash_entries(struct ohash *);
6742dcb487Sespie 
6842dcb487Sespie void *ohash_create_entry(struct ohash_info *, const char *, const char **);
6942dcb487Sespie uint32_t ohash_interval(const char *, const char **);
7042dcb487Sespie 
7142dcb487Sespie unsigned int ohash_qlookupi(struct ohash *, const char *, const char **);
7242dcb487Sespie unsigned int ohash_qlookup(struct ohash *, const char *);
7342dcb487Sespie __END_DECLS
7442dcb487Sespie #endif
75