xref: /freebsd/share/man/man9/hash.9 (revision a341ab7170320e85e4085d50c91795ed3f32d8aa)
12433924cSAndre Oppermann.\" Copyright (c) 2001 Tobias Weingartner
22433924cSAndre Oppermann.\" All rights reserved.
32433924cSAndre Oppermann.\"
42433924cSAndre Oppermann.\" Redistribution and use in source and binary forms, with or without
52433924cSAndre Oppermann.\" modification, are permitted provided that the following conditions
62433924cSAndre Oppermann.\" are met:
72433924cSAndre Oppermann.\" 1. Redistributions of source code must retain the above copyright
82433924cSAndre Oppermann.\"    notice, this list of conditions and the following disclaimer.
92433924cSAndre Oppermann.\" 2. Redistributions in binary form must reproduce the above copyright
102433924cSAndre Oppermann.\"    notice, this list of conditions and the following disclaimer in the
112433924cSAndre Oppermann.\"    documentation and/or other materials provided with the distribution.
122433924cSAndre Oppermann.\" 3. The name of the author may not be used to endorse or promote products
132433924cSAndre Oppermann.\"    derived from this software without specific prior written permission.
142433924cSAndre Oppermann.\"
152433924cSAndre Oppermann.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
162433924cSAndre Oppermann.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
172433924cSAndre Oppermann.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
182433924cSAndre Oppermann.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
192433924cSAndre Oppermann.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
202433924cSAndre Oppermann.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
212433924cSAndre Oppermann.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
222433924cSAndre Oppermann.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
232433924cSAndre Oppermann.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
242433924cSAndre Oppermann.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
252433924cSAndre Oppermann.\"
262433924cSAndre Oppermann.\"     $OpenBSD: hash.9,v 1.5 2003/04/17 05:08:39 jmc Exp $
272433924cSAndre Oppermann.\" $FreeBSD$
282433924cSAndre Oppermann.\"
29a341ab71SRuslan Ermilov.Dd October 19, 2006
302433924cSAndre Oppermann.Dt HASH 9
312433924cSAndre Oppermann.Os
322433924cSAndre Oppermann.Sh NAME
33bd84dd2fSRuslan Ermilov.Nm hash ,
34bd84dd2fSRuslan Ermilov.Nm hash32 ,
35bd84dd2fSRuslan Ermilov.Nm hash32_buf ,
36bd84dd2fSRuslan Ermilov.Nm hash32_str ,
37bd84dd2fSRuslan Ermilov.Nm hash32_strn ,
38bd84dd2fSRuslan Ermilov.Nm hash32_stre ,
39bd84dd2fSRuslan Ermilov.Nm hash32_strne
402433924cSAndre Oppermann.Nd general kernel hashing functions
412433924cSAndre Oppermann.Sh SYNOPSIS
42bd84dd2fSRuslan Ermilov.In sys/hash.h
432433924cSAndre Oppermann.Ft uint32_t
44a341ab71SRuslan Ermilov.Fn hash32_buf "const void *buf" "size_t len" "uint32_t hash"
452433924cSAndre Oppermann.Ft uint32_t
46a341ab71SRuslan Ermilov.Fn hash32_str "const void *buf" "uint32_t hash"
472433924cSAndre Oppermann.Ft uint32_t
48a341ab71SRuslan Ermilov.Fn hash32_strn "const void *buf" "size_t len" "uint32_t hash"
492433924cSAndre Oppermann.Ft uint32_t
50a341ab71SRuslan Ermilov.Fn hash32_stre "const void *buf" "int end" "char **ep" "uint32_t hash"
512433924cSAndre Oppermann.Ft uint32_t
52a341ab71SRuslan Ermilov.Fn hash32_strne "const void *buf" "size_t len" "int end" "char **ep" "uint32_t hash"
532433924cSAndre Oppermann.Sh DESCRIPTION
542433924cSAndre OppermannThe
552433924cSAndre Oppermann.Fn hash32
562433924cSAndre Oppermannfunctions are used to give a consistent and general interface to
572433924cSAndre Oppermanna decent hashing algorithm within the kernel.
58bd84dd2fSRuslan ErmilovThese functions can be used to hash
59bd84dd2fSRuslan Ermilov.Tn ASCII
602433924cSAndre Oppermann.Dv NUL
612433924cSAndre Oppermannterminated strings, as well as blocks of memory.
622433924cSAndre Oppermann.Pp
632433924cSAndre OppermannThe
642433924cSAndre Oppermann.Fn hash32_buf
652433924cSAndre Oppermannfunction is used as a general buffer hashing function.
662433924cSAndre OppermannThe argument
672433924cSAndre Oppermann.Fa buf
682433924cSAndre Oppermannis used to pass in the location, and
692433924cSAndre Oppermann.Fa len
702433924cSAndre Oppermannis the length of the buffer.
712433924cSAndre OppermannThe argument
722433924cSAndre Oppermann.Fa hash
732433924cSAndre Oppermannis used to extend an existing hash, or is passed the initial value
742433924cSAndre Oppermann.Dv HASHINIT
752433924cSAndre Oppermannto start a new hash.
762433924cSAndre Oppermann.Pp
772433924cSAndre OppermannThe
782433924cSAndre Oppermann.Fn hash32_str
792433924cSAndre Oppermannfunction is used to hash a
802433924cSAndre Oppermann.Dv NUL
812433924cSAndre Oppermannterminated string passed in
822433924cSAndre Oppermann.Fa buf
832433924cSAndre Oppermannwith initial hash value given in
842433924cSAndre Oppermann.Fa hash .
852433924cSAndre Oppermann.Pp
862433924cSAndre OppermannThe
872433924cSAndre Oppermann.Fn hash32_strn
882433924cSAndre Oppermannfunction is like the
892433924cSAndre Oppermann.Fn hash32_str
902433924cSAndre Oppermannfunction, except it also takes a
912433924cSAndre Oppermann.Fa len
922433924cSAndre Oppermannargument, which is the maximal length of the expected string.
932433924cSAndre Oppermann.Pp
942433924cSAndre OppermannThe
952433924cSAndre Oppermann.Fn hash32_stre
962433924cSAndre Oppermannand
972433924cSAndre Oppermann.Fn hash32_strne
982433924cSAndre Oppermannfunctions are helper functions used by the kernel to hash pathname
992433924cSAndre Oppermanncomponents.
1002433924cSAndre OppermannThese functions have the additional termination condition
1012433924cSAndre Oppermannof terminating when they find a character given by
1022433924cSAndre Oppermann.Fa end
1032433924cSAndre Oppermannin the string to be hashed.
1042433924cSAndre OppermannIf the argument
1052433924cSAndre Oppermann.Fa ep
1062433924cSAndre Oppermannis not
1072433924cSAndre Oppermann.Dv NULL ,
1082433924cSAndre Oppermannit is set to the point in the buffer at which the hash function
1092433924cSAndre Oppermannterminated hashing.
1102433924cSAndre Oppermann.Sh RETURN VALUES
1112433924cSAndre OppermannThe
1122433924cSAndre Oppermann.Fn hash32
1132433924cSAndre Oppermannfunctions return a 32 bit hash value of the buffer or string.
1142433924cSAndre Oppermann.Sh EXAMPLES
1152433924cSAndre Oppermann.Bd -literal -offset indent
1162433924cSAndre OppermannLIST_HEAD(head, cache) *hashtbl = NULL;
1172433924cSAndre Oppermannu_long mask = 0;
1182433924cSAndre Oppermann
1192433924cSAndre Oppermannvoid
1202433924cSAndre Oppermannsample_init(void)
1212433924cSAndre Oppermann{
122bd84dd2fSRuslan Ermilov
1232433924cSAndre Oppermann        hashtbl = hashinit(numwanted, type, flags, &mask);
1242433924cSAndre Oppermann}
1252433924cSAndre Oppermann
1262433924cSAndre Oppermannvoid
1272433924cSAndre Oppermannsample_use(char *str, int len)
1282433924cSAndre Oppermann{
1292433924cSAndre Oppermann        uint32_t hash;
1302433924cSAndre Oppermann
1312433924cSAndre Oppermann        hash = hash32_str(str, HASHINIT);
1322433924cSAndre Oppermann        hash = hash32_buf(&len, sizeof(len), hash);
1332433924cSAndre Oppermann        hashtbl[hash & mask] = len;
1342433924cSAndre Oppermann}
1352433924cSAndre Oppermann.Ed
1362433924cSAndre Oppermann.Sh SEE ALSO
1372433924cSAndre Oppermann.Xr free 9 ,
1382433924cSAndre Oppermann.Xr hashinit 9 ,
1392433924cSAndre Oppermann.Xr malloc 9
1402433924cSAndre Oppermann.Sh LIMITATIONS
1412433924cSAndre OppermannThe
1422433924cSAndre Oppermann.Fn hash32
1432433924cSAndre Oppermannfunctions are only 32 bit functions.
1442433924cSAndre OppermannThey will prove to give poor 64 bit performance, especially for the
1452433924cSAndre Oppermanntop 32 bits.
1462433924cSAndre OppermannAt the current time, this is not seen as a great limitation, as these
1472433924cSAndre Oppermannhash values are usually used to index into an array.
1482433924cSAndre OppermannShould these hash values be used for other means, this limitation should
1492433924cSAndre Oppermannbe revisited.
1502433924cSAndre Oppermann.Sh HISTORY
1512433924cSAndre OppermannThe
1522433924cSAndre Oppermann.Nm
1532433924cSAndre Oppermannfunctions were first committed to
1542433924cSAndre Oppermann.Nx 1.6 .
1552433924cSAndre OppermannThe
1562433924cSAndre Oppermann.Ox
1572433924cSAndre Oppermannversions were written and massaged for
1582433924cSAndre Oppermann.Ox 2.3
1592433924cSAndre Oppermannby Tobias Weingartner,
1602433924cSAndre Oppermannand finally committed for
1612433924cSAndre Oppermann.Ox 3.2 .
162