xref: /netbsd-src/share/man/man9/hashinit.9 (revision 4119c8fbd91b45252db7899e3f1b1b4225141e8f)
1*4119c8fbSwiz.\"     $NetBSD: hashinit.9,v 1.9 2013/09/17 19:58:03 wiz Exp $
28af3733eSchap.\"
38af3733eSchap.\" Copyright (c) 2006 The NetBSD Foundation, Inc.
48af3733eSchap.\" All rights reserved.
58af3733eSchap.\"
68af3733eSchap.\" This code is derived from software contributed to The NetBSD Foundation
78af3733eSchap.\" by Chapman Flack.
88af3733eSchap.\"
98af3733eSchap.\" Redistribution and use in source and binary forms, with or without
108af3733eSchap.\" modification, are permitted provided that the following conditions
118af3733eSchap.\" are met:
128af3733eSchap.\" 1. Redistributions of source code must retain the above copyright
138af3733eSchap.\"    notice, this list of conditions and the following disclaimer.
148af3733eSchap.\" 2. Redistributions in binary form must reproduce the above copyright
158af3733eSchap.\"    notice, this list of conditions and the following disclaimer in the
168af3733eSchap.\"    documentation and/or other materials provided with the distribution.
178af3733eSchap.\"
188af3733eSchap.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
198af3733eSchap.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
208af3733eSchap.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
218af3733eSchap.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
228af3733eSchap.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
238af3733eSchap.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
248af3733eSchap.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
258af3733eSchap.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
268af3733eSchap.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
278af3733eSchap.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
288af3733eSchap.\" POSSIBILITY OF SUCH DAMAGE.
298af3733eSchap.\"
30d4aaae92Spooka.Dd July 1, 2008
318af3733eSchap.Dt HASHINIT 9
328af3733eSchap.Os
338af3733eSchap.Sh NAME
348af3733eSchap.Nm hashinit ,
358af3733eSchap.Nm hashdone
368af3733eSchap.Nd kernel hash table construction and destruction
378af3733eSchap.Sh SYNOPSIS
388af3733eSchap.In sys/systm.h
398af3733eSchap.Ft "void *"
408af3733eSchap.Fo hashinit
418af3733eSchap.Fa "u_int chains"
428af3733eSchap.Fa "enum hashtype htype"
43fc066b20Syamt.Fa "bool waitok"
448af3733eSchap.Fa "u_long *hashmask"
458af3733eSchap.Fc
468af3733eSchap.Ft void
47fc066b20Syamt.Fn hashdone "void *hashtbl" "enum hashtype htype" "u_long hashmask"
488af3733eSchap.Sh DESCRIPTION
498af3733eSchapThe
508af3733eSchap.Fn hashinit
518af3733eSchapfunction allocates and initializes space for a simple chaining hash table.
528af3733eSchapThe number of slots will be the least power of two not smaller than
5342f4810aSchap.Fa chains .
5442f4810aSchapThe customary choice for
558af3733eSchap.Fa chains
5642f4810aSchapis the maximum number of elements you intend to store divided by
5742f4810aSchapyour intended load factor.
588af3733eSchapThe
598af3733eSchap.Dv LIST...
608af3733eSchapor
618af3733eSchap.Dv TAILQ...
628af3733eSchapmacros of
638af3733eSchap.Xr queue 3
648af3733eSchapcan be used to manipulate the chains; pass
658af3733eSchap.Dv HASH_LIST
668af3733eSchapor
678af3733eSchap.Dv HASH_TAILQ
688af3733eSchapas
698af3733eSchap.Fa htype
70bd723a14Swizto indicate which.
71bd723a14SwizEach slot will be initialized as the head of an empty chain of the
72bd723a14Swizproper type.
73bd723a14SwizBecause different data structures from
7442f4810aSchap.Xr queue 3
7542f4810aSchapcan define head structures of different sizes, the total size of the
7642f4810aSchapallocated table can vary with the choice of
7742f4810aSchap.Fa htype .
788af3733eSchap.Pp
79fc066b20SyamtIf
80fc066b20Syamt.Fa waitok
81fc066b20Syamtis true,
82fc066b20Syamt.Fa hashinit
83fc066b20Syamtcan wait until enough memory is available.
84fc066b20SyamtOtherwise, it immediately fails if there is not enough memory is available.
858af3733eSchap.Pp
868af3733eSchapA value will be stored into
878af3733eSchap.Fa *hashmask
888af3733eSchapsuitable for masking any computed hash, to obtain the index of a chain
898af3733eSchaphead in the allocated table.
908af3733eSchap.Pp
918af3733eSchapThe
928af3733eSchap.Fn hashdone
938af3733eSchapfunction deallocates the storage allocated by
948af3733eSchap.Fn hashinit
958af3733eSchapand pointed to by
968af3733eSchap.Fa hashtbl ,
9742f4810aSchapgiven the same
98fc066b20Syamt.Fa htype
99fc066b20Syamtand
100fc066b20Syamt.Fa hashmask
101fc066b20Syamtthat were passed to and returned from
10242f4810aSchap.Fn hashinit .
10342f4810aSchapIf the table contains any nonempty chain when
10442f4810aSchap.Fn hashdone
10542f4810aSchapis called, the result is undefined.
1068af3733eSchap.Sh RETURN VALUES
1078af3733eSchapThe value returned by
1088af3733eSchap.Fn hashinit
1098af3733eSchapshould be cast as pointer to an array of
1108af3733eSchap.Dv LIST_HEAD
1118af3733eSchapor
1128af3733eSchap.Dv TAILQ_HEAD
1138af3733eSchapas appropriate.
114fc066b20Syamt.Fn hashinit
115fc066b20Syamtreturns
1168af3733eSchap.Dv NULL
117fc066b20Syamton failure.
118*4119c8fbSwiz.Sh CODE REFERENCES
119*4119c8fbSwizThese functions are implemented in
120*4119c8fbSwiz.Pa sys/kern/subr_hash.c .
1218af3733eSchap.Sh SEE ALSO
1228af3733eSchap.Xr queue 3 ,
1238af3733eSchap.Xr hash 9 ,
1248af3733eSchap.Xr malloc 9
1258af3733eSchap.Sh HISTORY
1268af3733eSchapA
1278af3733eSchap.Fn hashinit
1288af3733eSchapfunction was present, without the
1298af3733eSchap.Fa htype
1308af3733eSchapor
1318af3733eSchap.Fa mflags
1328af3733eSchaparguments, in
1338af3733eSchap.Bx 4.4 alpha .
1348af3733eSchapIt was independent of
1358af3733eSchap.Xr queue 3
1368af3733eSchapand simply allocated and nulled a table of pointer-sized slots.
1378af3733eSchapIt sized the table to the
1388af3733eSchap.Em largest
1398af3733eSchappower of two
1408af3733eSchap.Em not greater than
1418af3733eSchap.Fa chains ;
14242f4810aSchapthat is, it built in a load factor between 1 and 2.
1438af3733eSchap.Pp
1448af3733eSchap.Nx 1.0
1458af3733eSchapwas the first
1468af3733eSchap.Nx
1478af3733eSchaprelease to have a
1488af3733eSchap.Fn hashinit
1498af3733eSchapfunction.
1508af3733eSchapIt resembled that from
1518af3733eSchap.Bx 4.4
1528af3733eSchapbut made each slot a
1538af3733eSchap.Dv LIST_HEAD
1548af3733eSchapfrom
1558af3733eSchap.Xr queue 3 .
1568af3733eSchapFor
1578af3733eSchap.Nx 1.3.3
1588af3733eSchapit had been changed to size the table to the least power of two
1598af3733eSchapnot less than
1608af3733eSchap.Em or equal to
1618af3733eSchap.Fa chains .
1628af3733eSchapBy
1638af3733eSchap.Nx 1.4
1648af3733eSchapit had the
1658af3733eSchap.Fa mflags
1668af3733eSchapargument and the current sizing rule.
1678af3733eSchap.Pp
1688af3733eSchap.Nx 1.5
1698af3733eSchaphad the
1708af3733eSchap.Fn hashdone
1718af3733eSchapfunction.
1728af3733eSchapBy
1738af3733eSchap.Nx 1.6
1748af3733eSchap.Fn hashinit
1758af3733eSchapsupported
1768af3733eSchap.Dv LIST
1778af3733eSchapor
1788af3733eSchap.Dv TAILQ
1798af3733eSchapchains selected with
1808af3733eSchap.Fa htype .
1818af3733eSchap.Pp
1828af3733eSchap.Fx
1838af3733eSchaphas a
1848af3733eSchap.Fn hashinit
1858af3733eSchapwith behavior equivalent (as of
1868af3733eSchap.Fx 6.1 )
1878af3733eSchapto that in
1888af3733eSchap.Nx 1.0 ,
1898af3733eSchapand a
1908af3733eSchap.Fn hashdestroy
1918af3733eSchapthat behaves as
1928af3733eSchap.Fn hashdone
1938af3733eSchapbut checks that all chains are empty first.
1948af3733eSchap.Ox
1958af3733eSchaphas a
1968af3733eSchap.Fn hashinit
1978af3733eSchapcomparable (as of
1988af3733eSchap.Ox 3.9 )
1998af3733eSchapto that of
2008af3733eSchap.Nx 1.4 .
2018af3733eSchapThis manual page was added for
2028af3733eSchap.Nx 4.0 .
2038af3733eSchap.Sh BUGS
2048af3733eSchapThe only part of the work of implementing a hash table that these functions
2058af3733eSchaprelieve is the part that isn't much work.
206