1*0a6a1f1dSLionel Sambuc #ifndef OHASH_H 2*0a6a1f1dSLionel Sambuc #define OHASH_H 3*0a6a1f1dSLionel Sambuc /* $OpenBSD: ohash.h,v 1.9 2006/01/16 15:52:25 espie Exp $ */ 4*0a6a1f1dSLionel Sambuc /* ex:ts=8 sw=4: 5*0a6a1f1dSLionel Sambuc */ 6*0a6a1f1dSLionel Sambuc 7*0a6a1f1dSLionel Sambuc /* Copyright (c) 1999, 2004 Marc Espie <espie@openbsd.org> 8*0a6a1f1dSLionel Sambuc * 9*0a6a1f1dSLionel Sambuc * Permission to use, copy, modify, and distribute this software for any 10*0a6a1f1dSLionel Sambuc * purpose with or without fee is hereby granted, provided that the above 11*0a6a1f1dSLionel Sambuc * copyright notice and this permission notice appear in all copies. 12*0a6a1f1dSLionel Sambuc * 13*0a6a1f1dSLionel Sambuc * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 14*0a6a1f1dSLionel Sambuc * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 15*0a6a1f1dSLionel Sambuc * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 16*0a6a1f1dSLionel Sambuc * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 17*0a6a1f1dSLionel Sambuc * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 18*0a6a1f1dSLionel Sambuc * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 19*0a6a1f1dSLionel Sambuc * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 20*0a6a1f1dSLionel Sambuc */ 21*0a6a1f1dSLionel Sambuc 22*0a6a1f1dSLionel Sambuc /* Open hashing support. 23*0a6a1f1dSLionel Sambuc * Open hashing was chosen because it is much lighter than other hash 24*0a6a1f1dSLionel Sambuc * techniques, and more efficient in most cases. 25*0a6a1f1dSLionel Sambuc */ 26*0a6a1f1dSLionel Sambuc 27*0a6a1f1dSLionel Sambuc struct ohash_info { 28*0a6a1f1dSLionel Sambuc ptrdiff_t key_offset; 29*0a6a1f1dSLionel Sambuc void *data; /* user data */ 30*0a6a1f1dSLionel Sambuc void *(*halloc)(size_t, void *); 31*0a6a1f1dSLionel Sambuc void (*hfree)(void *, size_t, void *); 32*0a6a1f1dSLionel Sambuc void *(*alloc)(size_t, void *); 33*0a6a1f1dSLionel Sambuc }; 34*0a6a1f1dSLionel Sambuc 35*0a6a1f1dSLionel Sambuc struct _ohash_record; 36*0a6a1f1dSLionel Sambuc 37*0a6a1f1dSLionel Sambuc struct ohash { 38*0a6a1f1dSLionel Sambuc struct _ohash_record *t; 39*0a6a1f1dSLionel Sambuc struct ohash_info info; 40*0a6a1f1dSLionel Sambuc unsigned int size; 41*0a6a1f1dSLionel Sambuc unsigned int total; 42*0a6a1f1dSLionel Sambuc unsigned int deleted; 43*0a6a1f1dSLionel Sambuc }; 44*0a6a1f1dSLionel Sambuc 45*0a6a1f1dSLionel Sambuc /* For this to be tweakable, we use small primitives, and leave part of the 46*0a6a1f1dSLionel Sambuc * logic to the client application. e.g., hashing is left to the client 47*0a6a1f1dSLionel Sambuc * application. We also provide a simple table entry lookup that yields 48*0a6a1f1dSLionel Sambuc * a hashing table index (opaque) to be used in find/insert/remove. 49*0a6a1f1dSLionel Sambuc * The keys are stored at a known position in the client data. 50*0a6a1f1dSLionel Sambuc */ 51*0a6a1f1dSLionel Sambuc __BEGIN_DECLS 52*0a6a1f1dSLionel Sambuc void ohash_init(struct ohash *, unsigned, struct ohash_info *); 53*0a6a1f1dSLionel Sambuc void ohash_delete(struct ohash *); 54*0a6a1f1dSLionel Sambuc 55*0a6a1f1dSLionel Sambuc unsigned int ohash_lookup_interval(struct ohash *, const char *, 56*0a6a1f1dSLionel Sambuc const char *, uint32_t); 57*0a6a1f1dSLionel Sambuc unsigned int ohash_lookup_memory(struct ohash *, const char *, 58*0a6a1f1dSLionel Sambuc size_t, uint32_t) 59*0a6a1f1dSLionel Sambuc __attribute__ ((__bounded__(__string__,2,3))); 60*0a6a1f1dSLionel Sambuc void *ohash_find(struct ohash *, unsigned int); 61*0a6a1f1dSLionel Sambuc void *ohash_remove(struct ohash *, unsigned int); 62*0a6a1f1dSLionel Sambuc void *ohash_insert(struct ohash *, unsigned int, void *); 63*0a6a1f1dSLionel Sambuc void *ohash_first(struct ohash *, unsigned int *); 64*0a6a1f1dSLionel Sambuc void *ohash_next(struct ohash *, unsigned int *); 65*0a6a1f1dSLionel Sambuc unsigned int ohash_entries(struct ohash *); 66*0a6a1f1dSLionel Sambuc 67*0a6a1f1dSLionel Sambuc void *ohash_create_entry(struct ohash_info *, const char *, const char **); 68*0a6a1f1dSLionel Sambuc uint32_t ohash_interval(const char *, const char **); 69*0a6a1f1dSLionel Sambuc 70*0a6a1f1dSLionel Sambuc unsigned int ohash_qlookupi(struct ohash *, const char *, const char **); 71*0a6a1f1dSLionel Sambuc unsigned int ohash_qlookup(struct ohash *, const char *); 72*0a6a1f1dSLionel Sambuc __END_DECLS 73*0a6a1f1dSLionel Sambuc #endif 74