1*71dafaa1Schristos.\" $OpenBSD: ohash_init.3,v 1.14 2007/05/31 19:19:30 jmc Exp $ 2*71dafaa1Schristos.\" Copyright (c) 1999 Marc Espie <espie@openbsd.org> 3*71dafaa1Schristos.\" 4*71dafaa1Schristos.\" Permission to use, copy, modify, and distribute this software for any 5*71dafaa1Schristos.\" purpose with or without fee is hereby granted, provided that the above 6*71dafaa1Schristos.\" copyright notice and this permission notice appear in all copies. 7*71dafaa1Schristos.\" 8*71dafaa1Schristos.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9*71dafaa1Schristos.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10*71dafaa1Schristos.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11*71dafaa1Schristos.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12*71dafaa1Schristos.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13*71dafaa1Schristos.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14*71dafaa1Schristos.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15*71dafaa1Schristos.\" 16*71dafaa1Schristos.Dd $Mdocdate: May 31 2007 $ 17*71dafaa1Schristos.Dt OPEN_HASH 3 18*71dafaa1Schristos.Os 19*71dafaa1Schristos.Sh NAME 20*71dafaa1Schristos.Nm ohash_init , 21*71dafaa1Schristos.Nm ohash_delete , 22*71dafaa1Schristos.Nm ohash_lookup_interval , 23*71dafaa1Schristos.Nm ohash_lookup_memory , 24*71dafaa1Schristos.Nm ohash_find , 25*71dafaa1Schristos.Nm ohash_remove , 26*71dafaa1Schristos.Nm ohash_insert , 27*71dafaa1Schristos.Nm ohash_first , 28*71dafaa1Schristos.Nm ohash_next , 29*71dafaa1Schristos.Nm ohash_entries 30*71dafaa1Schristos.Nd light-weight open hashing 31*71dafaa1Schristos.Sh SYNOPSIS 32*71dafaa1Schristos.Fd #include <stdint.h> 33*71dafaa1Schristos.Fd #include <stddef.h> 34*71dafaa1Schristos.Fd #include <ohash.h> 35*71dafaa1Schristos.Ft void 36*71dafaa1Schristos.Fn ohash_init "struct ohash *h" "unsigned int size" "struct ohash_info *info" 37*71dafaa1Schristos.Ft void 38*71dafaa1Schristos.Fn ohash_delete "struct ohash *h" 39*71dafaa1Schristos.Ft "unsigned int" 40*71dafaa1Schristos.Fn ohash_lookup_interval "struct ohash *h" "const char *start" "const char *end" "uint32_t hv" 41*71dafaa1Schristos.Ft "unsigned int" 42*71dafaa1Schristos.Fn ohash_lookup_memory "struct ohash *h" "const char *k" "size_t s" "uint32_t hv" 43*71dafaa1Schristos.Ft void * 44*71dafaa1Schristos.Fn ohash_find "struct ohash *h" "unsigned int i" 45*71dafaa1Schristos.Ft void * 46*71dafaa1Schristos.Fn ohash_remove "struct ohash *h" "unsigned int i" 47*71dafaa1Schristos.Ft void * 48*71dafaa1Schristos.Fn ohash_insert "struct ohash *h" "unsigned int i" "void *p" 49*71dafaa1Schristos.Ft void * 50*71dafaa1Schristos.Fn ohash_first "struct ohash *h" "unsigned int *i" 51*71dafaa1Schristos.Ft void * 52*71dafaa1Schristos.Fn ohash_next "struct ohash *h" "unsigned int *i" 53*71dafaa1Schristos.Ft "unsigned int" 54*71dafaa1Schristos.Fn ohash_entries "struct ohash *h" 55*71dafaa1Schristos.Sh DESCRIPTION 56*71dafaa1SchristosThese functions have been designed as a fast, extensible alternative to 57*71dafaa1Schristosthe usual hash table functions. 58*71dafaa1SchristosThey provide storage and retrieval of records indexed by keys, 59*71dafaa1Schristoswhere a key is a contiguous sequence of bytes at a fixed position in 60*71dafaa1Schristoseach record. 61*71dafaa1SchristosKeys can either be NUL-terminated strings or fixed-size memory areas. 62*71dafaa1SchristosAll functions take a pointer to an ohash structure as the 63*71dafaa1Schristos.Fa h 64*71dafaa1Schristosfunction argument. 65*71dafaa1SchristosStorage for this structure should be provided by user code. 66*71dafaa1Schristos.Pp 67*71dafaa1Schristos.Fn ohash_init 68*71dafaa1Schristosinitializes the table to store roughly 2 to the power 69*71dafaa1Schristos.Fa size 70*71dafaa1Schristoselements. 71*71dafaa1Schristos.Fa info 72*71dafaa1Schristosholds the position of the key in each record, and two pointers to 73*71dafaa1Schristos.Xr calloc 3 74*71dafaa1Schristosand 75*71dafaa1Schristos.Xr free 3 Ns -like 76*71dafaa1Schristosfunctions, to use for managing the table internal storage. 77*71dafaa1Schristos.Pp 78*71dafaa1Schristos.Fn ohash_delete 79*71dafaa1Schristosfrees storage internal to 80*71dafaa1Schristos.Fa h . 81*71dafaa1SchristosElements themselves should be freed by the user first, using for instance 82*71dafaa1Schristos.Fn ohash_first 83*71dafaa1Schristosand 84*71dafaa1Schristos.Fn ohash_next . 85*71dafaa1Schristos.Pp 86*71dafaa1Schristos.Fn ohash_lookup_interval 87*71dafaa1Schristosand 88*71dafaa1Schristos.Fn ohash_lookup_memory 89*71dafaa1Schristosare the basic look-up element functions. 90*71dafaa1SchristosThe hashing function result is provided by the user as 91*71dafaa1Schristos.Fa hv . 92*71dafaa1SchristosThese return a 93*71dafaa1Schristos.Qq slot 94*71dafaa1Schristosin the ohash table 95*71dafaa1Schristos.Fa h , 96*71dafaa1Schristosto be used with 97*71dafaa1Schristos.Fn ohash_find , 98*71dafaa1Schristos.Fn ohash_insert , 99*71dafaa1Schristosor 100*71dafaa1Schristos.Fn ohash_remove . 101*71dafaa1SchristosThis slot is only valid up to the next call to 102*71dafaa1Schristos.Fn ohash_insert 103*71dafaa1Schristosor 104*71dafaa1Schristos.Fn ohash_remove . 105*71dafaa1Schristos.Pp 106*71dafaa1Schristos.Fn ohash_lookup_interval 107*71dafaa1Schristoshandles string-like keys. 108*71dafaa1Schristos.Fn ohash_lookup_interval 109*71dafaa1Schristosassumes the key is the interval between 110*71dafaa1Schristos.Fa start 111*71dafaa1Schristosand 112*71dafaa1Schristos.Fa end , 113*71dafaa1Schristosexclusive, 114*71dafaa1Schristosthough the actual elements stored in the table should only contain 115*71dafaa1SchristosNUL-terminated keys. 116*71dafaa1Schristos.Pp 117*71dafaa1Schristos.Fn ohash_lookup_memory 118*71dafaa1Schristosassumes the key is the memory area starting at 119*71dafaa1Schristos.Fa k 120*71dafaa1Schristosof size 121*71dafaa1Schristos.Fa s . 122*71dafaa1SchristosAll bytes are significant in key comparison. 123*71dafaa1Schristos.Pp 124*71dafaa1Schristos.Fn ohash_find 125*71dafaa1Schristosretrieves an element from a slot 126*71dafaa1Schristos.Fa i 127*71dafaa1Schristosreturned by the 128*71dafaa1Schristos.Fn ohash_lookup* 129*71dafaa1Schristosfunctions. 130*71dafaa1SchristosIt returns 131*71dafaa1Schristos.Dv NULL 132*71dafaa1Schristosif the slot is empty. 133*71dafaa1Schristos.Pp 134*71dafaa1Schristos.Fn ohash_insert 135*71dafaa1Schristosinserts a new element 136*71dafaa1Schristos.Fa p 137*71dafaa1Schristosat slot 138*71dafaa1Schristos.Fa i . 139*71dafaa1SchristosSlot 140*71dafaa1Schristos.Fa i 141*71dafaa1Schristosmust be empty and element 142*71dafaa1Schristos.Fa p 143*71dafaa1Schristosmust have a key corresponding to the 144*71dafaa1Schristos.Fn ohash_lookup* 145*71dafaa1Schristoscall. 146*71dafaa1Schristos.Pp 147*71dafaa1Schristos.Fn ohash_remove 148*71dafaa1Schristosremoves the element at slot 149*71dafaa1Schristos.Fa i . 150*71dafaa1SchristosIt returns the removed element, for user code to dispose of, or 151*71dafaa1Schristos.Dv NULL 152*71dafaa1Schristosif the slot was empty. 153*71dafaa1Schristos.Pp 154*71dafaa1Schristos.Fn ohash_first 155*71dafaa1Schristosand 156*71dafaa1Schristos.Fn ohash_next 157*71dafaa1Schristoscan be used to access all elements in an ohash table, like this: 158*71dafaa1Schristos.Bd -literal -offset indent 159*71dafaa1Schristosfor (n = ohash_first(h, &i); n != NULL; n = ohash_next(h, &i)) 160*71dafaa1Schristos do_something_with(n); 161*71dafaa1Schristos.Ed 162*71dafaa1Schristos.Pp 163*71dafaa1Schristos.Fa i 164*71dafaa1Schristospoints to an auxiliary unsigned integer used to record the current position 165*71dafaa1Schristosin the ohash table. 166*71dafaa1SchristosThose functions are safe to use even while entries are added to/removed 167*71dafaa1Schristosfrom the table, but in such a case they don't guarantee that new entries 168*71dafaa1Schristoswill be returned. 169*71dafaa1SchristosAs a special case, they can safely be used to free elements in the table. 170*71dafaa1Schristos.Pp 171*71dafaa1Schristos.Fn ohash_entries 172*71dafaa1Schristosreturns the number of elements in the hash table. 173*71dafaa1Schristos.Sh STORAGE HANDLING 174*71dafaa1SchristosOnly 175*71dafaa1Schristos.Fn ohash_init , 176*71dafaa1Schristos.Fn ohash_insert , 177*71dafaa1Schristos.Fn ohash_remove 178*71dafaa1Schristosand 179*71dafaa1Schristos.Fn ohash_delete 180*71dafaa1Schristosmay call the user-supplied memory functions. 181*71dafaa1SchristosIt is the responsibility of the user memory allocation code to verify 182*71dafaa1Schristosthat those calls did not fail. 183*71dafaa1Schristos.Pp 184*71dafaa1SchristosIf memory allocation fails, 185*71dafaa1Schristos.Fn ohash_init 186*71dafaa1Schristosreturns a useless hash table. 187*71dafaa1Schristos.Fn ohash_insert 188*71dafaa1Schristosand 189*71dafaa1Schristos.Fn ohash_remove 190*71dafaa1Schristosstill perform the requested operation, but the returned table should be 191*71dafaa1Schristosconsidered read-only. 192*71dafaa1SchristosIt can still be accessed by 193*71dafaa1Schristos.Fn ohash_lookup* , 194*71dafaa1Schristos.Fn ohash_find , 195*71dafaa1Schristos.Fn ohash_first 196*71dafaa1Schristosand 197*71dafaa1Schristos.Fn ohash_next 198*71dafaa1Schristosto dump relevant information to disk before aborting. 199*71dafaa1Schristos.Sh THREAD SAFETY 200*71dafaa1SchristosThe open hashing functions are not thread-safe by design. 201*71dafaa1SchristosIn particular, in a threaded environment, there is no guarantee that a 202*71dafaa1Schristos.Qq slot 203*71dafaa1Schristoswill not move between a 204*71dafaa1Schristos.Fn ohash_lookup* 205*71dafaa1Schristosand a 206*71dafaa1Schristos.Fn ohash_find , 207*71dafaa1Schristos.Fn ohash_insert 208*71dafaa1Schristosor 209*71dafaa1Schristos.Fn ohash_remove 210*71dafaa1Schristoscall. 211*71dafaa1Schristos.Pp 212*71dafaa1SchristosMulti-threaded applications should explicitly protect ohash table access. 213*71dafaa1Schristos.Sh SEE ALSO 214*71dafaa1Schristos.Xr ohash_interval 3 215*71dafaa1Schristos.Rs 216*71dafaa1Schristos.%A Donald E. Knuth 217*71dafaa1Schristos.%B The Art of Computer Programming 218*71dafaa1Schristos.%V Vol. 3 219*71dafaa1Schristos.%P pp 506-550 220*71dafaa1Schristos.%D 1973 221*71dafaa1Schristos.Re 222*71dafaa1Schristos.Sh STANDARDS 223*71dafaa1SchristosThose functions are completely non-standard and should be avoided in 224*71dafaa1Schristosportable programs. 225*71dafaa1Schristos.Sh HISTORY 226*71dafaa1SchristosThose functions were designed and written for 227*71dafaa1Schristos.Ox 228*71dafaa1Schristos.Xr make 1 229*71dafaa1Schristosby Marc Espie in 1999. 230