xref: /freebsd/share/man/man9/hashinit.9 (revision 9cbda59000e0f366b04c83d8d852c7c34b467722)
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.\"
289bd82b5cSJoseph Koshy.Dd October 10, 2004
299bd82b5cSJoseph Koshy.Dt HASHINIT 9
309bd82b5cSJoseph Koshy.Os
319bd82b5cSJoseph Koshy.Sh NAME
329bd82b5cSJoseph Koshy.Nm hashinit , 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
419bd82b5cSJoseph Koshy.Fn hashdestroy "void *hashtbl" "struct malloc_type *type" "u_long hashmask"
429bd82b5cSJoseph Koshy.Ft "void *"
439bd82b5cSJoseph Koshy.Fn phashinit "int nelements" "struct malloc_type *type" "u_long *nentries"
449bd82b5cSJoseph Koshy.Sh DESCRIPTION
459bd82b5cSJoseph KoshyThe
469bd82b5cSJoseph Koshy.Fn hashinit
479bd82b5cSJoseph Koshyand
489bd82b5cSJoseph Koshy.Fn phashinit
499bd82b5cSJoseph Koshyfunctions allocate space for hash tables of size given by the argument
509bd82b5cSJoseph Koshy.Fa nelements .
519bd82b5cSJoseph Koshy.Pp
529bd82b5cSJoseph KoshyThe
539bd82b5cSJoseph Koshy.Fn hashinit
549bd82b5cSJoseph Koshyfunction allocates hash tables that are sized to largest power of two
559bd82b5cSJoseph Koshyless than or equal to argument
569bd82b5cSJoseph Koshy.Fa nelements .
579bd82b5cSJoseph KoshyThe
589bd82b5cSJoseph Koshy.Fn phashinit
599bd82b5cSJoseph Koshyfunction allocates hash tables that are sized to the largest prime
609bd82b5cSJoseph Koshynumber less than or equal to argument
619bd82b5cSJoseph Koshy.Fa nelements .
629bd82b5cSJoseph KoshyAllocated hash tables are contiguous arrays of
639bd82b5cSJoseph Koshy.Xr LIST_HEAD 3
649bd82b5cSJoseph Koshyentries, allocated using
659bd82b5cSJoseph Koshy.Xr malloc 9 ,
669bd82b5cSJoseph Koshyand initialized using
679bd82b5cSJoseph Koshy.Xr LIST_INIT 3 .
689bd82b5cSJoseph KoshyThe malloc arena to be used for allocation is pointed to by argument
699bd82b5cSJoseph Koshy.Fa type .
709bd82b5cSJoseph Koshy.Pp
719bd82b5cSJoseph KoshyThe
729bd82b5cSJoseph Koshy.Fn hashdestroy
739bd82b5cSJoseph Koshyfunction frees the space occupied by the hash table pointed to by argument
749bd82b5cSJoseph Koshy.Fa hashtbl .
759bd82b5cSJoseph KoshyArgument
769bd82b5cSJoseph Koshy.Fa type
779bd82b5cSJoseph Koshydetermines the malloc arena to use when freeing space.
789bd82b5cSJoseph KoshyThe argument
799bd82b5cSJoseph Koshy.Fa hashmask
809bd82b5cSJoseph Koshyshould be the bit mask returned by the call to
819bd82b5cSJoseph Koshy.Fn hashinit
829bd82b5cSJoseph Koshythat allocated the hash table.
839cbda590SRuslan Ermilov.Sh IMPLEMENTATION NOTES
849cbda590SRuslan ErmilovThe largest prime hash value chosen by
859cbda590SRuslan Ermilov.Fn phashinit
869cbda590SRuslan Ermilovis 32749.
879bd82b5cSJoseph Koshy.Sh RETURN VALUES
889bd82b5cSJoseph KoshyThe
899bd82b5cSJoseph Koshy.Fn hashinit
909bd82b5cSJoseph Koshyfunction returns a pointer to an allocated hash table and sets the
919bd82b5cSJoseph Koshylocation pointed to by
929bd82b5cSJoseph Koshy.Fa hashmask
939bd82b5cSJoseph Koshyto the bit mask to be used for computing the correct slot in the
949bd82b5cSJoseph Koshyhash table.
959bd82b5cSJoseph Koshy.Pp
969bd82b5cSJoseph KoshyThe
979bd82b5cSJoseph Koshy.Fn phashinit
989bd82b5cSJoseph Koshyfunction returns a pointer to an allocated hash table and sets the
999bd82b5cSJoseph Koshylocation pointed to by
1009bd82b5cSJoseph Koshy.Fa nentries
1019bd82b5cSJoseph Koshyto the number of rows in the hash table.
1029bd82b5cSJoseph Koshy.Sh EXAMPLES
1039bd82b5cSJoseph KoshyA typical example is shown below:
1049bd82b5cSJoseph Koshy.Bd -literal -offset indent
1059bd82b5cSJoseph Koshy\&...
1069bd82b5cSJoseph Koshystatic LIST_HEAD(foo, foo) *footable;
1079bd82b5cSJoseph Koshystatic u_long foomask;
1089bd82b5cSJoseph Koshy\&...
1099bd82b5cSJoseph Koshyfootable = hashinit(32, M_FOO, &foomask);
1109bd82b5cSJoseph Koshy.Ed
1119bd82b5cSJoseph Koshy.Pp
1129bd82b5cSJoseph KoshyHere we allocate a hash table with 32 entries from the malloc arena
1139bd82b5cSJoseph Koshypointed to by
1149bd82b5cSJoseph Koshy.Dv M_FOO .
1159bd82b5cSJoseph KoshyThe mask for the allocated hash table is returned in
1169bd82b5cSJoseph Koshy.Va foomask .
1179bd82b5cSJoseph KoshyA subsequent call to
1189bd82b5cSJoseph Koshy.Fn hashdestroy
1199bd82b5cSJoseph Koshyuses the value in
1209bd82b5cSJoseph Koshy.Va foomask :
1219bd82b5cSJoseph Koshy.Bd -literal -offset indent
1229bd82b5cSJoseph Koshy\&...
1239bd82b5cSJoseph Koshyhashdestroy(footable, M_FOO, foomask);
1249bd82b5cSJoseph Koshy.Ed
1259bd82b5cSJoseph Koshy.Sh DIAGNOSTICS
1269bd82b5cSJoseph KoshyThe
1279bd82b5cSJoseph Koshy.Fn hashinit
1289bd82b5cSJoseph Koshyand
1299bd82b5cSJoseph Koshy.Fn phashinit
1309bd82b5cSJoseph Koshyfunctions will panic if argument
1319bd82b5cSJoseph Koshy.Fa nelements
1329bd82b5cSJoseph Koshyis less than or equal to zero.
1339bd82b5cSJoseph Koshy.Pp
1349bd82b5cSJoseph KoshyThe
1359bd82b5cSJoseph Koshy.Fn hashdestroy
1369bd82b5cSJoseph Koshyfunction will panic if the hash table
1379bd82b5cSJoseph Koshypointed to by
1389bd82b5cSJoseph Koshy.Fa hashtbl
1399bd82b5cSJoseph Koshyis not empty.
1409cbda590SRuslan Ermilov.Sh SEE ALSO
1419cbda590SRuslan Ermilov.Xr LIST_HEAD 3 ,
1429cbda590SRuslan Ermilov.Xr malloc 9
1439bd82b5cSJoseph Koshy.Sh BUGS
1449bd82b5cSJoseph KoshyThere is no
1459bd82b5cSJoseph Koshy.Fn phashdestroy
1469bd82b5cSJoseph Koshyfunction, and using
1479bd82b5cSJoseph Koshy.Fn hashdestroy
1489bd82b5cSJoseph Koshyto free a hash table allocated by
1499bd82b5cSJoseph Koshy.Fn phashinit
1509bd82b5cSJoseph Koshyusually has grave consequences.
151