ohash.h revision 269162
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: head/usr.bin/m4/lib/ohash.h 269162 2014-07-27 22:54:13Z bapt $ 18228063Sbapt */ 19228063Sbapt 20269162Sbapt#ifndef OHASH_H 21269162Sbapt#define OHASH_H 22269162Sbapt 23228063Sbapt/* Open hashing support. 24228063Sbapt * Open hashing was chosen because it is much lighter than other hash 25228063Sbapt * techniques, and more efficient in most cases. 26228063Sbapt */ 27228063Sbapt 28269162Sbapt/* user-visible data structure */ 29228063Sbaptstruct ohash_info { 30228063Sbapt ptrdiff_t key_offset; 31228063Sbapt void *data; /* user data */ 32269162Sbapt void *(*calloc)(size_t, size_t, void *); 33269162Sbapt void (*free)(void *, void *); 34228063Sbapt void *(*alloc)(size_t, void *); 35228063Sbapt}; 36228063Sbapt 37228063Sbaptstruct _ohash_record; 38228063Sbapt 39269162Sbapt/* private structure. It's there just so you can do a sizeof */ 40228063Sbaptstruct ohash { 41228063Sbapt struct _ohash_record *t; 42228063Sbapt struct ohash_info info; 43228063Sbapt unsigned int size; 44228063Sbapt unsigned int total; 45228063Sbapt unsigned int deleted; 46228063Sbapt}; 47228063Sbapt 48228063Sbapt/* For this to be tweakable, we use small primitives, and leave part of the 49228063Sbapt * logic to the client application. e.g., hashing is left to the client 50228063Sbapt * application. We also provide a simple table entry lookup that yields 51228063Sbapt * a hashing table index (opaque) to be used in find/insert/remove. 52228063Sbapt * The keys are stored at a known position in the client data. 53228063Sbapt */ 54228063Sbapt__BEGIN_DECLS 55228063Sbaptvoid ohash_init(struct ohash *, unsigned, struct ohash_info *); 56228063Sbaptvoid ohash_delete(struct ohash *); 57228063Sbapt 58228063Sbaptunsigned int ohash_lookup_interval(struct ohash *, const char *, 59269162Sbapt const char *, uint32_t); 60228063Sbaptunsigned int ohash_lookup_memory(struct ohash *, const char *, 61269162Sbapt size_t, uint32_t) 62269162Sbapt __attribute__ ((__bounded__(__string__,2,3))); 63228063Sbaptvoid *ohash_find(struct ohash *, unsigned int); 64228063Sbaptvoid *ohash_remove(struct ohash *, unsigned int); 65228063Sbaptvoid *ohash_insert(struct ohash *, unsigned int, void *); 66228063Sbaptvoid *ohash_first(struct ohash *, unsigned int *); 67228063Sbaptvoid *ohash_next(struct ohash *, unsigned int *); 68228063Sbaptunsigned int ohash_entries(struct ohash *); 69228063Sbapt 70228063Sbaptvoid *ohash_create_entry(struct ohash_info *, const char *, const char **); 71269162Sbaptuint32_t ohash_interval(const char *, const char **); 72228063Sbapt 73228063Sbaptunsigned int ohash_qlookupi(struct ohash *, const char *, const char **); 74228063Sbaptunsigned int ohash_qlookup(struct ohash *, const char *); 75228063Sbapt__END_DECLS 76228063Sbapt#endif 77