19bd82b5cSJoseph Koshy.\" 29bd82b5cSJoseph Koshy.\" Copyright (c) 2004 Joseph Koshy 39bd82b5cSJoseph Koshy.\" All rights reserved. 49bd82b5cSJoseph Koshy.\" 59bd82b5cSJoseph Koshy.\" Redistribution and use in source and binary forms, with or without 69bd82b5cSJoseph Koshy.\" modification, are permitted provided that the following conditions 79bd82b5cSJoseph Koshy.\" are met: 89bd82b5cSJoseph Koshy.\" 1. Redistributions of source code must retain the above copyright 99bd82b5cSJoseph Koshy.\" notice, this list of conditions and the following disclaimer. 109bd82b5cSJoseph Koshy.\" 2. Redistributions in binary form must reproduce the above copyright 119bd82b5cSJoseph Koshy.\" notice, this list of conditions and the following disclaimer in the 129bd82b5cSJoseph Koshy.\" documentation and/or other materials provided with the distribution. 139bd82b5cSJoseph Koshy.\" 149bd82b5cSJoseph Koshy.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' 159bd82b5cSJoseph Koshy.\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 169bd82b5cSJoseph Koshy.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 179bd82b5cSJoseph Koshy.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE 189bd82b5cSJoseph Koshy.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 199bd82b5cSJoseph Koshy.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 209bd82b5cSJoseph Koshy.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 219bd82b5cSJoseph Koshy.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 229bd82b5cSJoseph Koshy.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 239bd82b5cSJoseph Koshy.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 249bd82b5cSJoseph Koshy.\" POSSIBILITY OF SUCH DAMAGE. 259bd82b5cSJoseph Koshy.\" 269bd82b5cSJoseph Koshy.\" $FreeBSD$ 279bd82b5cSJoseph Koshy.\" 28*aa0445b7SBenjamin Kaduk.Dd January 23, 2016 299bd82b5cSJoseph Koshy.Dt HASHINIT 9 309bd82b5cSJoseph Koshy.Os 319bd82b5cSJoseph Koshy.Sh NAME 32b939bb36SRandall Stewart.Nm hashinit , hashinit_flags , hashdestroy , phashinit 339bd82b5cSJoseph Koshy.Nd manage kernel hash tables 349bd82b5cSJoseph Koshy.Sh SYNOPSIS 359bd82b5cSJoseph Koshy.In sys/malloc.h 369bd82b5cSJoseph Koshy.In sys/systm.h 379bd82b5cSJoseph Koshy.In sys/queue.h 389bd82b5cSJoseph Koshy.Ft "void *" 399bd82b5cSJoseph Koshy.Fn hashinit "int nelements" "struct malloc_type *type" "u_long *hashmask" 409bd82b5cSJoseph Koshy.Ft void 41b902c5bcSRuslan Ermilov.Fo hashinit_flags 42b902c5bcSRuslan Ermilov.Fa "int nelements" "struct malloc_type *type" "u_long *hashmask" "int flags" 43b902c5bcSRuslan Ermilov.Fc 44b939bb36SRandall Stewart.Ft void 459bd82b5cSJoseph Koshy.Fn hashdestroy "void *hashtbl" "struct malloc_type *type" "u_long hashmask" 469bd82b5cSJoseph Koshy.Ft "void *" 479bd82b5cSJoseph Koshy.Fn phashinit "int nelements" "struct malloc_type *type" "u_long *nentries" 489bd82b5cSJoseph Koshy.Sh DESCRIPTION 499bd82b5cSJoseph KoshyThe 50b939bb36SRandall Stewart.Fn hashinit , 51b939bb36SRandall Stewart.Fn hashinit_flags 529bd82b5cSJoseph Koshyand 539bd82b5cSJoseph Koshy.Fn phashinit 549bd82b5cSJoseph Koshyfunctions allocate space for hash tables of size given by the argument 559bd82b5cSJoseph Koshy.Fa nelements . 569bd82b5cSJoseph Koshy.Pp 579bd82b5cSJoseph KoshyThe 589bd82b5cSJoseph Koshy.Fn hashinit 599bd82b5cSJoseph Koshyfunction allocates hash tables that are sized to largest power of two 609bd82b5cSJoseph Koshyless than or equal to argument 619bd82b5cSJoseph Koshy.Fa nelements . 629bd82b5cSJoseph KoshyThe 639bd82b5cSJoseph Koshy.Fn phashinit 649bd82b5cSJoseph Koshyfunction allocates hash tables that are sized to the largest prime 659bd82b5cSJoseph Koshynumber less than or equal to argument 669bd82b5cSJoseph Koshy.Fa nelements . 67b939bb36SRandall StewartThe 68b939bb36SRandall Stewart.Fn hashinit_flags 69b902c5bcSRuslan Ermilovfunction operates like 70b939bb36SRandall Stewart.Fn hashinit 71b939bb36SRandall Stewartbut also accepts an additional argument 72b939bb36SRandall Stewart.Fa flags 73b939bb36SRandall Stewartwhich control various options during allocation. 749bd82b5cSJoseph KoshyAllocated hash tables are contiguous arrays of 759bd82b5cSJoseph Koshy.Xr LIST_HEAD 3 769bd82b5cSJoseph Koshyentries, allocated using 779bd82b5cSJoseph Koshy.Xr malloc 9 , 789bd82b5cSJoseph Koshyand initialized using 799bd82b5cSJoseph Koshy.Xr LIST_INIT 3 . 809bd82b5cSJoseph KoshyThe malloc arena to be used for allocation is pointed to by argument 819bd82b5cSJoseph Koshy.Fa type . 829bd82b5cSJoseph Koshy.Pp 839bd82b5cSJoseph KoshyThe 849bd82b5cSJoseph Koshy.Fn hashdestroy 859bd82b5cSJoseph Koshyfunction frees the space occupied by the hash table pointed to by argument 869bd82b5cSJoseph Koshy.Fa hashtbl . 879bd82b5cSJoseph KoshyArgument 889bd82b5cSJoseph Koshy.Fa type 899bd82b5cSJoseph Koshydetermines the malloc arena to use when freeing space. 909bd82b5cSJoseph KoshyThe argument 919bd82b5cSJoseph Koshy.Fa hashmask 929bd82b5cSJoseph Koshyshould be the bit mask returned by the call to 939bd82b5cSJoseph Koshy.Fn hashinit 949bd82b5cSJoseph Koshythat allocated the hash table. 95b939bb36SRandall StewartThe argument 96b939bb36SRandall Stewart.Fa flags 97b939bb36SRandall Stewartmust be used with one of the following values. 98b939bb36SRandall Stewart.Pp 99b939bb36SRandall Stewart.Bl -tag -width ".Dv HASH_NOWAIT" -offset indent -compact 100b939bb36SRandall Stewart.It Dv HASH_NOWAIT 101b939bb36SRandall StewartAny malloc performed by the 102b939bb36SRandall Stewart.Fn hashinit_flags 103b939bb36SRandall Stewartfunction will not be allowed to wait, and therefore may fail. 104b939bb36SRandall Stewart.It Dv HASH_WAITOK 105*aa0445b7SBenjamin KadukAny malloc performed by 106b939bb36SRandall Stewart.Fn hashinit_flags 107b939bb36SRandall Stewartfunction is allowed to wait for memory. 108*aa0445b7SBenjamin KadukThis is also the behavior of 109*aa0445b7SBenjamin Kaduk.Fn hashinit . 110b939bb36SRandall Stewart.El 1119cbda590SRuslan Ermilov.Sh IMPLEMENTATION NOTES 1129cbda590SRuslan ErmilovThe largest prime hash value chosen by 1139cbda590SRuslan Ermilov.Fn phashinit 1149cbda590SRuslan Ermilovis 32749. 1159bd82b5cSJoseph Koshy.Sh RETURN VALUES 1169bd82b5cSJoseph KoshyThe 1179bd82b5cSJoseph Koshy.Fn hashinit 1189bd82b5cSJoseph Koshyfunction returns a pointer to an allocated hash table and sets the 1199bd82b5cSJoseph Koshylocation pointed to by 1209bd82b5cSJoseph Koshy.Fa hashmask 1219bd82b5cSJoseph Koshyto the bit mask to be used for computing the correct slot in the 1229bd82b5cSJoseph Koshyhash table. 1239bd82b5cSJoseph Koshy.Pp 1249bd82b5cSJoseph KoshyThe 1259bd82b5cSJoseph Koshy.Fn phashinit 1269bd82b5cSJoseph Koshyfunction returns a pointer to an allocated hash table and sets the 1279bd82b5cSJoseph Koshylocation pointed to by 1289bd82b5cSJoseph Koshy.Fa nentries 1299bd82b5cSJoseph Koshyto the number of rows in the hash table. 1309bd82b5cSJoseph Koshy.Sh EXAMPLES 1319bd82b5cSJoseph KoshyA typical example is shown below: 1329bd82b5cSJoseph Koshy.Bd -literal -offset indent 1339bd82b5cSJoseph Koshy\&... 1349bd82b5cSJoseph Koshystatic LIST_HEAD(foo, foo) *footable; 1359bd82b5cSJoseph Koshystatic u_long foomask; 1369bd82b5cSJoseph Koshy\&... 1379bd82b5cSJoseph Koshyfootable = hashinit(32, M_FOO, &foomask); 1389bd82b5cSJoseph Koshy.Ed 1399bd82b5cSJoseph Koshy.Pp 1409bd82b5cSJoseph KoshyHere we allocate a hash table with 32 entries from the malloc arena 1419bd82b5cSJoseph Koshypointed to by 1429bd82b5cSJoseph Koshy.Dv M_FOO . 1439bd82b5cSJoseph KoshyThe mask for the allocated hash table is returned in 1449bd82b5cSJoseph Koshy.Va foomask . 1459bd82b5cSJoseph KoshyA subsequent call to 1469bd82b5cSJoseph Koshy.Fn hashdestroy 1479bd82b5cSJoseph Koshyuses the value in 1489bd82b5cSJoseph Koshy.Va foomask : 1499bd82b5cSJoseph Koshy.Bd -literal -offset indent 1509bd82b5cSJoseph Koshy\&... 1519bd82b5cSJoseph Koshyhashdestroy(footable, M_FOO, foomask); 1529bd82b5cSJoseph Koshy.Ed 1539bd82b5cSJoseph Koshy.Sh DIAGNOSTICS 1549bd82b5cSJoseph KoshyThe 1559bd82b5cSJoseph Koshy.Fn hashinit 1569bd82b5cSJoseph Koshyand 1579bd82b5cSJoseph Koshy.Fn phashinit 1589bd82b5cSJoseph Koshyfunctions will panic if argument 1599bd82b5cSJoseph Koshy.Fa nelements 1609bd82b5cSJoseph Koshyis less than or equal to zero. 1619bd82b5cSJoseph Koshy.Pp 1629bd82b5cSJoseph KoshyThe 1639bd82b5cSJoseph Koshy.Fn hashdestroy 1649bd82b5cSJoseph Koshyfunction will panic if the hash table 1659bd82b5cSJoseph Koshypointed to by 1669bd82b5cSJoseph Koshy.Fa hashtbl 1679bd82b5cSJoseph Koshyis not empty. 1689cbda590SRuslan Ermilov.Sh SEE ALSO 1699cbda590SRuslan Ermilov.Xr LIST_HEAD 3 , 1709cbda590SRuslan Ermilov.Xr malloc 9 1719bd82b5cSJoseph Koshy.Sh BUGS 1729bd82b5cSJoseph KoshyThere is no 1739bd82b5cSJoseph Koshy.Fn phashdestroy 1749bd82b5cSJoseph Koshyfunction, and using 1759bd82b5cSJoseph Koshy.Fn hashdestroy 1769bd82b5cSJoseph Koshyto free a hash table allocated by 1779bd82b5cSJoseph Koshy.Fn phashinit 1789bd82b5cSJoseph Koshyusually has grave consequences. 179