1070c62a6SFranco Fichtner /* $OpenBSD: ohash.h,v 1.2 2014/06/02 18:52:03 deraadt Exp $ */ 2070c62a6SFranco Fichtner 3070c62a6SFranco Fichtner /* Copyright (c) 1999, 2004 Marc Espie <espie@openbsd.org> 4070c62a6SFranco Fichtner * 5070c62a6SFranco Fichtner * Permission to use, copy, modify, and distribute this software for any 6070c62a6SFranco Fichtner * purpose with or without fee is hereby granted, provided that the above 7070c62a6SFranco Fichtner * copyright notice and this permission notice appear in all copies. 8070c62a6SFranco Fichtner * 9070c62a6SFranco Fichtner * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10070c62a6SFranco Fichtner * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11070c62a6SFranco Fichtner * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12070c62a6SFranco Fichtner * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13070c62a6SFranco Fichtner * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14070c62a6SFranco Fichtner * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15070c62a6SFranco Fichtner * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16070c62a6SFranco Fichtner */ 17070c62a6SFranco Fichtner 18070c62a6SFranco Fichtner #ifndef OHASH_H 19070c62a6SFranco Fichtner #define OHASH_H 20070c62a6SFranco Fichtner 21070c62a6SFranco Fichtner /* Open hashing support. 22070c62a6SFranco Fichtner * Open hashing was chosen because it is much lighter than other hash 23070c62a6SFranco Fichtner * techniques, and more efficient in most cases. 24070c62a6SFranco Fichtner */ 25070c62a6SFranco Fichtner 26070c62a6SFranco Fichtner /* user-visible data structure */ 27070c62a6SFranco Fichtner struct ohash_info { 28070c62a6SFranco Fichtner ptrdiff_t key_offset; 29070c62a6SFranco Fichtner void *data; /* user data */ 30070c62a6SFranco Fichtner void *(*calloc)(size_t, size_t, void *); 31070c62a6SFranco Fichtner void (*free)(void *, void *); 32070c62a6SFranco Fichtner void *(*alloc)(size_t, void *); 33070c62a6SFranco Fichtner }; 34070c62a6SFranco Fichtner 35070c62a6SFranco Fichtner struct _ohash_record; 36070c62a6SFranco Fichtner 37070c62a6SFranco Fichtner /* private structure. It's there just so you can do a sizeof */ 38070c62a6SFranco Fichtner struct ohash { 39070c62a6SFranco Fichtner struct _ohash_record *t; 40070c62a6SFranco Fichtner struct ohash_info info; 41070c62a6SFranco Fichtner unsigned int size; 42070c62a6SFranco Fichtner unsigned int total; 43070c62a6SFranco Fichtner unsigned int deleted; 44070c62a6SFranco Fichtner }; 45070c62a6SFranco Fichtner 46070c62a6SFranco Fichtner /* For this to be tweakable, we use small primitives, and leave part of the 47070c62a6SFranco Fichtner * logic to the client application. e.g., hashing is left to the client 48070c62a6SFranco Fichtner * application. We also provide a simple table entry lookup that yields 49070c62a6SFranco Fichtner * a hashing table index (opaque) to be used in find/insert/remove. 50070c62a6SFranco Fichtner * The keys are stored at a known position in the client data. 51070c62a6SFranco Fichtner */ 52070c62a6SFranco Fichtner void ohash_init(struct ohash *, unsigned, struct ohash_info *); 53070c62a6SFranco Fichtner void ohash_delete(struct ohash *); 54070c62a6SFranco Fichtner 55070c62a6SFranco Fichtner unsigned int ohash_lookup_interval(struct ohash *, const char *, 56070c62a6SFranco Fichtner const char *, uint32_t); 57070c62a6SFranco Fichtner unsigned int ohash_lookup_memory(struct ohash *, const char *, 58070c62a6SFranco Fichtner size_t, uint32_t); 59070c62a6SFranco Fichtner void *ohash_find(struct ohash *, unsigned int); 60070c62a6SFranco Fichtner void *ohash_remove(struct ohash *, unsigned int); 61070c62a6SFranco Fichtner void *ohash_insert(struct ohash *, unsigned int, void *); 62070c62a6SFranco Fichtner void *ohash_first(struct ohash *, unsigned int *); 63070c62a6SFranco Fichtner void *ohash_next(struct ohash *, unsigned int *); 64070c62a6SFranco Fichtner unsigned int ohash_entries(struct ohash *); 65070c62a6SFranco Fichtner 66070c62a6SFranco Fichtner void *ohash_create_entry(struct ohash_info *, const char *, const char **); 67070c62a6SFranco Fichtner uint32_t ohash_interval(const char *, const char **); 68070c62a6SFranco Fichtner 69070c62a6SFranco Fichtner unsigned int ohash_qlookupi(struct ohash *, const char *, const char **); 70070c62a6SFranco Fichtner unsigned int ohash_qlookup(struct ohash *, const char *); 71*54ba9607SSascha Wildner 72070c62a6SFranco Fichtner #endif 73