1371584c2SYuri Pankov /* $OpenBSD: ohash.h,v 1.2 2014/06/02 18:52:03 deraadt Exp $ */ 2371584c2SYuri Pankov 3371584c2SYuri Pankov /* Copyright (c) 1999, 2004 Marc Espie <espie@openbsd.org> 4371584c2SYuri Pankov * 5371584c2SYuri Pankov * Permission to use, copy, modify, and distribute this software for any 6371584c2SYuri Pankov * purpose with or without fee is hereby granted, provided that the above 7371584c2SYuri Pankov * copyright notice and this permission notice appear in all copies. 8371584c2SYuri Pankov * 9371584c2SYuri Pankov * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10371584c2SYuri Pankov * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11371584c2SYuri Pankov * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12371584c2SYuri Pankov * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13371584c2SYuri Pankov * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14371584c2SYuri Pankov * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15371584c2SYuri Pankov * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16371584c2SYuri Pankov */ 17371584c2SYuri Pankov 18371584c2SYuri Pankov #ifndef OHASH_H 19371584c2SYuri Pankov #define OHASH_H 20371584c2SYuri Pankov 21*55fea89dSDan Cross /* Open hashing support. 22371584c2SYuri Pankov * Open hashing was chosen because it is much lighter than other hash 23371584c2SYuri Pankov * techniques, and more efficient in most cases. 24371584c2SYuri Pankov */ 25371584c2SYuri Pankov 26371584c2SYuri Pankov /* user-visible data structure */ 27371584c2SYuri Pankov struct ohash_info { 28371584c2SYuri Pankov ptrdiff_t key_offset; 29371584c2SYuri Pankov void *data; /* user data */ 30371584c2SYuri Pankov void *(*calloc)(size_t, size_t, void *); 31371584c2SYuri Pankov void (*free)(void *, void *); 32371584c2SYuri Pankov void *(*alloc)(size_t, void *); 33371584c2SYuri Pankov }; 34371584c2SYuri Pankov 35371584c2SYuri Pankov struct _ohash_record; 36371584c2SYuri Pankov 37371584c2SYuri Pankov /* private structure. It's there just so you can do a sizeof */ 38371584c2SYuri Pankov struct ohash { 39371584c2SYuri Pankov struct _ohash_record *t; 40371584c2SYuri Pankov struct ohash_info info; 41371584c2SYuri Pankov unsigned int size; 42371584c2SYuri Pankov unsigned int total; 43371584c2SYuri Pankov unsigned int deleted; 44371584c2SYuri Pankov }; 45371584c2SYuri Pankov 46371584c2SYuri Pankov /* For this to be tweakable, we use small primitives, and leave part of the 47371584c2SYuri Pankov * logic to the client application. e.g., hashing is left to the client 48371584c2SYuri Pankov * application. We also provide a simple table entry lookup that yields 49371584c2SYuri Pankov * a hashing table index (opaque) to be used in find/insert/remove. 50371584c2SYuri Pankov * The keys are stored at a known position in the client data. 51371584c2SYuri Pankov */ 52371584c2SYuri Pankov void ohash_init(struct ohash *, unsigned, struct ohash_info *); 53371584c2SYuri Pankov void ohash_delete(struct ohash *); 54371584c2SYuri Pankov 55371584c2SYuri Pankov unsigned int ohash_lookup_interval(struct ohash *, const char *, 56371584c2SYuri Pankov const char *, uint32_t); 57371584c2SYuri Pankov unsigned int ohash_lookup_memory(struct ohash *, const char *, 58371584c2SYuri Pankov size_t, uint32_t); 59371584c2SYuri Pankov void *ohash_find(struct ohash *, unsigned int); 60371584c2SYuri Pankov void *ohash_remove(struct ohash *, unsigned int); 61371584c2SYuri Pankov void *ohash_insert(struct ohash *, unsigned int, void *); 62371584c2SYuri Pankov void *ohash_first(struct ohash *, unsigned int *); 63371584c2SYuri Pankov void *ohash_next(struct ohash *, unsigned int *); 64371584c2SYuri Pankov unsigned int ohash_entries(struct ohash *); 65371584c2SYuri Pankov 66371584c2SYuri Pankov void *ohash_create_entry(struct ohash_info *, const char *, const char **); 67371584c2SYuri Pankov uint32_t ohash_interval(const char *, const char **); 68371584c2SYuri Pankov 69371584c2SYuri Pankov unsigned int ohash_qlookupi(struct ohash *, const char *, const char **); 70371584c2SYuri Pankov unsigned int ohash_qlookup(struct ohash *, const char *); 71371584c2SYuri Pankov 72371584c2SYuri Pankov #endif 73