xref: /freebsd/share/man/man9/hashinit.9 (revision fa9896e082a1046ff4fbc75fcba4d18d1f2efc19)
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.\"
26*f8ce3dfaSSepherosa Ziehau.Dd April 29, 2016
279bd82b5cSJoseph Koshy.Dt HASHINIT 9
289bd82b5cSJoseph Koshy.Os
299bd82b5cSJoseph Koshy.Sh NAME
30*f8ce3dfaSSepherosa Ziehau.Nm hashinit , hashinit_flags ,  hashdestroy , phashinit , phashinit_flags
319bd82b5cSJoseph Koshy.Nd manage kernel hash tables
329bd82b5cSJoseph Koshy.Sh SYNOPSIS
339bd82b5cSJoseph Koshy.In sys/malloc.h
349bd82b5cSJoseph Koshy.In sys/systm.h
359bd82b5cSJoseph Koshy.In sys/queue.h
369bd82b5cSJoseph Koshy.Ft "void *"
379bd82b5cSJoseph Koshy.Fn hashinit "int nelements" "struct malloc_type *type" "u_long *hashmask"
389bd82b5cSJoseph Koshy.Ft void
39b902c5bcSRuslan Ermilov.Fo hashinit_flags
40b902c5bcSRuslan Ermilov.Fa "int nelements" "struct malloc_type *type" "u_long *hashmask" "int flags"
41b902c5bcSRuslan Ermilov.Fc
42b939bb36SRandall Stewart.Ft void
439bd82b5cSJoseph Koshy.Fn hashdestroy "void *hashtbl" "struct malloc_type *type" "u_long hashmask"
449bd82b5cSJoseph Koshy.Ft "void *"
459bd82b5cSJoseph Koshy.Fn phashinit "int nelements" "struct malloc_type *type" "u_long *nentries"
46*f8ce3dfaSSepherosa Ziehau.Fn phashinit_flags "int nelements" "struct malloc_type *type" "u_long *nentries" "int flags"
479bd82b5cSJoseph Koshy.Sh DESCRIPTION
489bd82b5cSJoseph KoshyThe
49b939bb36SRandall Stewart.Fn hashinit ,
50*f8ce3dfaSSepherosa Ziehau.Fn hashinit_flags ,
519bd82b5cSJoseph Koshy.Fn phashinit
52*f8ce3dfaSSepherosa Ziehauand
53*f8ce3dfaSSepherosa Ziehau.Fn phashinit_flags
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.
74*f8ce3dfaSSepherosa Ziehau.Fn phashinit_flags
75*f8ce3dfaSSepherosa Ziehaufunction operates like
76*f8ce3dfaSSepherosa Ziehau.Fn phashinit
77*f8ce3dfaSSepherosa Ziehaubut also accepts an additional argument
78*f8ce3dfaSSepherosa Ziehau.Fa flags
79*f8ce3dfaSSepherosa Ziehauwhich control various options during allocation.
809bd82b5cSJoseph KoshyAllocated hash tables are contiguous arrays of
819bd82b5cSJoseph Koshy.Xr LIST_HEAD 3
829bd82b5cSJoseph Koshyentries, allocated using
839bd82b5cSJoseph Koshy.Xr malloc 9 ,
849bd82b5cSJoseph Koshyand initialized using
859bd82b5cSJoseph Koshy.Xr LIST_INIT 3 .
869bd82b5cSJoseph KoshyThe malloc arena to be used for allocation is pointed to by argument
879bd82b5cSJoseph Koshy.Fa type .
889bd82b5cSJoseph Koshy.Pp
899bd82b5cSJoseph KoshyThe
909bd82b5cSJoseph Koshy.Fn hashdestroy
919bd82b5cSJoseph Koshyfunction frees the space occupied by the hash table pointed to by argument
929bd82b5cSJoseph Koshy.Fa hashtbl .
939bd82b5cSJoseph KoshyArgument
949bd82b5cSJoseph Koshy.Fa type
959bd82b5cSJoseph Koshydetermines the malloc arena to use when freeing space.
969bd82b5cSJoseph KoshyThe argument
979bd82b5cSJoseph Koshy.Fa hashmask
989bd82b5cSJoseph Koshyshould be the bit mask returned by the call to
999bd82b5cSJoseph Koshy.Fn hashinit
1009bd82b5cSJoseph Koshythat allocated the hash table.
101b939bb36SRandall StewartThe argument
102b939bb36SRandall Stewart.Fa flags
103b939bb36SRandall Stewartmust be used with one of the following values.
104b939bb36SRandall Stewart.Pp
105b939bb36SRandall Stewart.Bl -tag -width ".Dv HASH_NOWAIT" -offset indent -compact
106b939bb36SRandall Stewart.It Dv HASH_NOWAIT
107b939bb36SRandall StewartAny malloc performed by the
108b939bb36SRandall Stewart.Fn hashinit_flags
109*f8ce3dfaSSepherosa Ziehauand
110*f8ce3dfaSSepherosa Ziehau.Fn phashinit_flags
111b939bb36SRandall Stewartfunction will not be allowed to wait, and therefore may fail.
112b939bb36SRandall Stewart.It Dv HASH_WAITOK
113aa0445b7SBenjamin KadukAny malloc performed by
114b939bb36SRandall Stewart.Fn hashinit_flags
115*f8ce3dfaSSepherosa Ziehauand
116*f8ce3dfaSSepherosa Ziehau.Fn phashinit_flags
117b939bb36SRandall Stewartfunction is allowed to wait for memory.
118aa0445b7SBenjamin KadukThis is also the behavior of
119*f8ce3dfaSSepherosa Ziehau.Fn hashinit
120*f8ce3dfaSSepherosa Ziehauand
121*f8ce3dfaSSepherosa Ziehau.Fn phashinit .
122b939bb36SRandall Stewart.El
1239cbda590SRuslan Ermilov.Sh IMPLEMENTATION NOTES
1249cbda590SRuslan ErmilovThe largest prime hash value chosen by
1259cbda590SRuslan Ermilov.Fn phashinit
1269cbda590SRuslan Ermilovis 32749.
1279bd82b5cSJoseph Koshy.Sh RETURN VALUES
1289bd82b5cSJoseph KoshyThe
1299bd82b5cSJoseph Koshy.Fn hashinit
1309bd82b5cSJoseph Koshyfunction returns a pointer to an allocated hash table and sets the
1319bd82b5cSJoseph Koshylocation pointed to by
1329bd82b5cSJoseph Koshy.Fa hashmask
1339bd82b5cSJoseph Koshyto the bit mask to be used for computing the correct slot in the
1349bd82b5cSJoseph Koshyhash table.
1359bd82b5cSJoseph Koshy.Pp
1369bd82b5cSJoseph KoshyThe
1379bd82b5cSJoseph Koshy.Fn phashinit
1389bd82b5cSJoseph Koshyfunction returns a pointer to an allocated hash table and sets the
1399bd82b5cSJoseph Koshylocation pointed to by
1409bd82b5cSJoseph Koshy.Fa nentries
1419bd82b5cSJoseph Koshyto the number of rows in the hash table.
1429bd82b5cSJoseph Koshy.Sh EXAMPLES
1439bd82b5cSJoseph KoshyA typical example is shown below:
1449bd82b5cSJoseph Koshy.Bd -literal -offset indent
1459bd82b5cSJoseph Koshy\&...
1469bd82b5cSJoseph Koshystatic LIST_HEAD(foo, foo) *footable;
1479bd82b5cSJoseph Koshystatic u_long foomask;
1489bd82b5cSJoseph Koshy\&...
1499bd82b5cSJoseph Koshyfootable = hashinit(32, M_FOO, &foomask);
1509bd82b5cSJoseph Koshy.Ed
1519bd82b5cSJoseph Koshy.Pp
1529bd82b5cSJoseph KoshyHere we allocate a hash table with 32 entries from the malloc arena
1539bd82b5cSJoseph Koshypointed to by
1549bd82b5cSJoseph Koshy.Dv M_FOO .
1559bd82b5cSJoseph KoshyThe mask for the allocated hash table is returned in
1569bd82b5cSJoseph Koshy.Va foomask .
1579bd82b5cSJoseph KoshyA subsequent call to
1589bd82b5cSJoseph Koshy.Fn hashdestroy
1599bd82b5cSJoseph Koshyuses the value in
1609bd82b5cSJoseph Koshy.Va foomask :
1619bd82b5cSJoseph Koshy.Bd -literal -offset indent
1629bd82b5cSJoseph Koshy\&...
1639bd82b5cSJoseph Koshyhashdestroy(footable, M_FOO, foomask);
1649bd82b5cSJoseph Koshy.Ed
1659bd82b5cSJoseph Koshy.Sh DIAGNOSTICS
1669bd82b5cSJoseph KoshyThe
1679bd82b5cSJoseph Koshy.Fn hashinit
1689bd82b5cSJoseph Koshyand
1699bd82b5cSJoseph Koshy.Fn phashinit
1709bd82b5cSJoseph Koshyfunctions will panic if argument
1719bd82b5cSJoseph Koshy.Fa nelements
1729bd82b5cSJoseph Koshyis less than or equal to zero.
1739bd82b5cSJoseph Koshy.Pp
1749bd82b5cSJoseph KoshyThe
1759bd82b5cSJoseph Koshy.Fn hashdestroy
1769bd82b5cSJoseph Koshyfunction will panic if the hash table
1779bd82b5cSJoseph Koshypointed to by
1789bd82b5cSJoseph Koshy.Fa hashtbl
1799bd82b5cSJoseph Koshyis not empty.
1809cbda590SRuslan Ermilov.Sh SEE ALSO
1819cbda590SRuslan Ermilov.Xr LIST_HEAD 3 ,
1829cbda590SRuslan Ermilov.Xr malloc 9
1839bd82b5cSJoseph Koshy.Sh BUGS
1849bd82b5cSJoseph KoshyThere is no
1859bd82b5cSJoseph Koshy.Fn phashdestroy
1869bd82b5cSJoseph Koshyfunction, and using
1879bd82b5cSJoseph Koshy.Fn hashdestroy
1889bd82b5cSJoseph Koshyto free a hash table allocated by
1899bd82b5cSJoseph Koshy.Fn phashinit
1909bd82b5cSJoseph Koshyusually has grave consequences.
191