1fec65c98Schristos /* $OpenBSD: ohash.h,v 1.2 2014/06/02 18:52:03 deraadt Exp $ */ 270f041f9Sjoerg 370f041f9Sjoerg /* Copyright (c) 1999, 2004 Marc Espie <espie@openbsd.org> 470f041f9Sjoerg * 570f041f9Sjoerg * Permission to use, copy, modify, and distribute this software for any 670f041f9Sjoerg * purpose with or without fee is hereby granted, provided that the above 770f041f9Sjoerg * copyright notice and this permission notice appear in all copies. 870f041f9Sjoerg * 970f041f9Sjoerg * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 1070f041f9Sjoerg * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 1170f041f9Sjoerg * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 1270f041f9Sjoerg * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 1370f041f9Sjoerg * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 1470f041f9Sjoerg * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 1570f041f9Sjoerg * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 1670f041f9Sjoerg */ 1770f041f9Sjoerg 18fec65c98Schristos #ifndef OHASH_H 19fec65c98Schristos #define OHASH_H 20fec65c98Schristos 2170f041f9Sjoerg /* Open hashing support. 2270f041f9Sjoerg * Open hashing was chosen because it is much lighter than other hash 2370f041f9Sjoerg * techniques, and more efficient in most cases. 2470f041f9Sjoerg */ 2570f041f9Sjoerg 26fec65c98Schristos /* user-visible data structure */ 2770f041f9Sjoerg struct ohash_info { 2870f041f9Sjoerg ptrdiff_t key_offset; 2970f041f9Sjoerg void *data; /* user data */ 30fec65c98Schristos void *(*calloc)(size_t, size_t, void *); 31fec65c98Schristos void (*free)(void *, void *); 3270f041f9Sjoerg void *(*alloc)(size_t, void *); 3370f041f9Sjoerg }; 3470f041f9Sjoerg 3570f041f9Sjoerg struct _ohash_record; 3670f041f9Sjoerg 37fec65c98Schristos /* private structure. It's there just so you can do a sizeof */ 3870f041f9Sjoerg struct ohash { 3970f041f9Sjoerg struct _ohash_record *t; 4070f041f9Sjoerg struct ohash_info info; 4170f041f9Sjoerg unsigned int size; 4270f041f9Sjoerg unsigned int total; 4370f041f9Sjoerg unsigned int deleted; 4470f041f9Sjoerg }; 4570f041f9Sjoerg 4670f041f9Sjoerg /* For this to be tweakable, we use small primitives, and leave part of the 4770f041f9Sjoerg * logic to the client application. e.g., hashing is left to the client 4870f041f9Sjoerg * application. We also provide a simple table entry lookup that yields 4970f041f9Sjoerg * a hashing table index (opaque) to be used in find/insert/remove. 5070f041f9Sjoerg * The keys are stored at a known position in the client data. 5170f041f9Sjoerg */ 5270f041f9Sjoerg void ohash_init(struct ohash *, unsigned, struct ohash_info *); 5370f041f9Sjoerg void ohash_delete(struct ohash *); 5470f041f9Sjoerg 5570f041f9Sjoerg unsigned int ohash_lookup_interval(struct ohash *, const char *, 5670f041f9Sjoerg const char *, uint32_t); 5770f041f9Sjoerg unsigned int ohash_lookup_memory(struct ohash *, const char *, 58fec65c98Schristos size_t, uint32_t); 5970f041f9Sjoerg void *ohash_find(struct ohash *, unsigned int); 6070f041f9Sjoerg void *ohash_remove(struct ohash *, unsigned int); 6170f041f9Sjoerg void *ohash_insert(struct ohash *, unsigned int, void *); 6270f041f9Sjoerg void *ohash_first(struct ohash *, unsigned int *); 6370f041f9Sjoerg void *ohash_next(struct ohash *, unsigned int *); 6470f041f9Sjoerg unsigned int ohash_entries(struct ohash *); 6570f041f9Sjoerg 6670f041f9Sjoerg void *ohash_create_entry(struct ohash_info *, const char *, const char **); 6770f041f9Sjoerg uint32_t ohash_interval(const char *, const char **); 6870f041f9Sjoerg 6970f041f9Sjoerg unsigned int ohash_qlookupi(struct ohash *, const char *, const char **); 7070f041f9Sjoerg unsigned int ohash_qlookup(struct ohash *, const char *); 71*9ff1f2acSchristos 7270f041f9Sjoerg #endif 73