1274876Sbapt/* $OpenBSD: ohash.h,v 1.2 2014/06/02 18:52:03 deraadt Exp $ */ 2274876Sbapt 3274876Sbapt/* Copyright (c) 1999, 2004 Marc Espie <espie@openbsd.org> 4274876Sbapt * 5274876Sbapt * Permission to use, copy, modify, and distribute this software for any 6274876Sbapt * purpose with or without fee is hereby granted, provided that the above 7274876Sbapt * copyright notice and this permission notice appear in all copies. 8274876Sbapt * 9274876Sbapt * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10274876Sbapt * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11274876Sbapt * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12274876Sbapt * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13274876Sbapt * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14274876Sbapt * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15274876Sbapt * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16274876Sbapt */ 17274876Sbapt 18274876Sbapt#ifndef OHASH_H 19274876Sbapt#define OHASH_H 20274876Sbapt 21274876Sbapt/* Open hashing support. 22274876Sbapt * Open hashing was chosen because it is much lighter than other hash 23274876Sbapt * techniques, and more efficient in most cases. 24274876Sbapt */ 25274876Sbapt 26274876Sbapt/* user-visible data structure */ 27274876Sbaptstruct ohash_info { 28274876Sbapt ptrdiff_t key_offset; 29274876Sbapt void *data; /* user data */ 30274876Sbapt void *(*calloc)(size_t, size_t, void *); 31274876Sbapt void (*free)(void *, void *); 32274876Sbapt void *(*alloc)(size_t, void *); 33274876Sbapt}; 34274876Sbapt 35274876Sbaptstruct _ohash_record; 36274876Sbapt 37274876Sbapt/* private structure. It's there just so you can do a sizeof */ 38274876Sbaptstruct ohash { 39274876Sbapt struct _ohash_record *t; 40274876Sbapt struct ohash_info info; 41274876Sbapt unsigned int size; 42274876Sbapt unsigned int total; 43274876Sbapt unsigned int deleted; 44274876Sbapt}; 45274876Sbapt 46274876Sbapt/* For this to be tweakable, we use small primitives, and leave part of the 47274876Sbapt * logic to the client application. e.g., hashing is left to the client 48274876Sbapt * application. We also provide a simple table entry lookup that yields 49274876Sbapt * a hashing table index (opaque) to be used in find/insert/remove. 50274876Sbapt * The keys are stored at a known position in the client data. 51274876Sbapt */ 52274876Sbaptvoid ohash_init(struct ohash *, unsigned, struct ohash_info *); 53274876Sbaptvoid ohash_delete(struct ohash *); 54274876Sbapt 55274876Sbaptunsigned int ohash_lookup_interval(struct ohash *, const char *, 56274876Sbapt const char *, uint32_t); 57274876Sbaptunsigned int ohash_lookup_memory(struct ohash *, const char *, 58274876Sbapt size_t, uint32_t); 59274876Sbaptvoid *ohash_find(struct ohash *, unsigned int); 60274876Sbaptvoid *ohash_remove(struct ohash *, unsigned int); 61274876Sbaptvoid *ohash_insert(struct ohash *, unsigned int, void *); 62274876Sbaptvoid *ohash_first(struct ohash *, unsigned int *); 63274876Sbaptvoid *ohash_next(struct ohash *, unsigned int *); 64274876Sbaptunsigned int ohash_entries(struct ohash *); 65274876Sbapt 66274876Sbaptvoid *ohash_create_entry(struct ohash_info *, const char *, const char **); 67274876Sbaptuint32_t ohash_interval(const char *, const char **); 68274876Sbapt 69274876Sbaptunsigned int ohash_qlookupi(struct ohash *, const char *, const char **); 70274876Sbaptunsigned int ohash_qlookup(struct ohash *, const char *); 71294113Sbapt 72274876Sbapt#endif 73