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