xref: /freebsd/share/man/man9/hash.9 (revision 99e9de871aee402a1166cdecb957fe6c8c99da13)
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.\"
29*99e9de87SDag-Erling Smørgrav.Dd October 18, 2014
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 ,
3962208ca5SGleb Smirnoff.Nm hash32_strne ,
40*99e9de87SDag-Erling Smørgrav.Nm jenkins_hash ,
4162208ca5SGleb Smirnoff.Nm jenkins_hash32 ,
42*99e9de87SDag-Erling Smørgrav.Nm murmur3_32_hash ,
43*99e9de87SDag-Erling Smørgrav.Nm murmur3_32_hash32
442433924cSAndre Oppermann.Nd general kernel hashing functions
452433924cSAndre Oppermann.Sh SYNOPSIS
46bd84dd2fSRuslan Ermilov.In sys/hash.h
472433924cSAndre Oppermann.Ft uint32_t
48a341ab71SRuslan Ermilov.Fn hash32_buf "const void *buf" "size_t len" "uint32_t hash"
492433924cSAndre Oppermann.Ft uint32_t
50a341ab71SRuslan Ermilov.Fn hash32_str "const void *buf" "uint32_t hash"
512433924cSAndre Oppermann.Ft uint32_t
52a341ab71SRuslan Ermilov.Fn hash32_strn "const void *buf" "size_t len" "uint32_t hash"
532433924cSAndre Oppermann.Ft uint32_t
546429a5cbSAndrew Thompson.Fn hash32_stre "const void *buf" "int end" "const char **ep" "uint32_t hash"
552433924cSAndre Oppermann.Ft uint32_t
566429a5cbSAndrew Thompson.Fn hash32_strne "const void *buf" "size_t len" "int end" "const char **ep" "uint32_t hash"
5762208ca5SGleb Smirnoff.Ft uint32_t
5862208ca5SGleb Smirnoff.Fn jenkins_hash "const void *buf" "size_t len" "uint32_t hash"
5962208ca5SGleb Smirnoff.Ft uint32_t
6062208ca5SGleb Smirnoff.Fn jenkins_hash32 "const uint32_t *buf" "size_t count" "uint32_t hash"
61*99e9de87SDag-Erling Smørgrav.Ft uint32_t
62*99e9de87SDag-Erling Smørgrav.Fn murmur3_32_hash "const void *buf" "size_t len" "uint32_t hash"
63*99e9de87SDag-Erling Smørgrav.Ft uint32_t
64*99e9de87SDag-Erling Smørgrav.Fn murmur3_32_hash32 "const uint32_t *buf" "size_t count" "uint32_t hash"
652433924cSAndre Oppermann.Sh DESCRIPTION
662433924cSAndre OppermannThe
672433924cSAndre Oppermann.Fn hash32
682433924cSAndre Oppermannfunctions are used to give a consistent and general interface to
692433924cSAndre Oppermanna decent hashing algorithm within the kernel.
70bd84dd2fSRuslan ErmilovThese functions can be used to hash
71bd84dd2fSRuslan Ermilov.Tn ASCII
722433924cSAndre Oppermann.Dv NUL
732433924cSAndre Oppermannterminated strings, as well as blocks of memory.
742433924cSAndre Oppermann.Pp
752433924cSAndre OppermannThe
762433924cSAndre Oppermann.Fn hash32_buf
772433924cSAndre Oppermannfunction is used as a general buffer hashing function.
782433924cSAndre OppermannThe argument
792433924cSAndre Oppermann.Fa buf
802433924cSAndre Oppermannis used to pass in the location, and
812433924cSAndre Oppermann.Fa len
822433924cSAndre Oppermannis the length of the buffer.
832433924cSAndre OppermannThe argument
842433924cSAndre Oppermann.Fa hash
852433924cSAndre Oppermannis used to extend an existing hash, or is passed the initial value
862433924cSAndre Oppermann.Dv HASHINIT
872433924cSAndre Oppermannto start a new hash.
882433924cSAndre Oppermann.Pp
892433924cSAndre OppermannThe
902433924cSAndre Oppermann.Fn hash32_str
912433924cSAndre Oppermannfunction is used to hash a
922433924cSAndre Oppermann.Dv NUL
932433924cSAndre Oppermannterminated string passed in
942433924cSAndre Oppermann.Fa buf
952433924cSAndre Oppermannwith initial hash value given in
962433924cSAndre Oppermann.Fa hash .
972433924cSAndre Oppermann.Pp
982433924cSAndre OppermannThe
992433924cSAndre Oppermann.Fn hash32_strn
1002433924cSAndre Oppermannfunction is like the
1012433924cSAndre Oppermann.Fn hash32_str
1022433924cSAndre Oppermannfunction, except it also takes a
1032433924cSAndre Oppermann.Fa len
1042433924cSAndre Oppermannargument, which is the maximal length of the expected string.
1052433924cSAndre Oppermann.Pp
1062433924cSAndre OppermannThe
1072433924cSAndre Oppermann.Fn hash32_stre
1082433924cSAndre Oppermannand
1092433924cSAndre Oppermann.Fn hash32_strne
1102433924cSAndre Oppermannfunctions are helper functions used by the kernel to hash pathname
1112433924cSAndre Oppermanncomponents.
1122433924cSAndre OppermannThese functions have the additional termination condition
1132433924cSAndre Oppermannof terminating when they find a character given by
1142433924cSAndre Oppermann.Fa end
1152433924cSAndre Oppermannin the string to be hashed.
1162433924cSAndre OppermannIf the argument
1172433924cSAndre Oppermann.Fa ep
1182433924cSAndre Oppermannis not
1192433924cSAndre Oppermann.Dv NULL ,
1202433924cSAndre Oppermannit is set to the point in the buffer at which the hash function
1212433924cSAndre Oppermannterminated hashing.
12262208ca5SGleb Smirnoff.Pp
12362208ca5SGleb SmirnoffThe
12462208ca5SGleb Smirnoff.Fn jenkins_hash
12562208ca5SGleb Smirnofffunction has same semantics as the
12662208ca5SGleb Smirnoff.Fn hash32_buf ,
12762208ca5SGleb Smirnoffbut provides more advanced hashing algorithm with better distribution.
12862208ca5SGleb Smirnoff.Pp
12962208ca5SGleb SmirnoffThe
13062208ca5SGleb Smirnoff.Fn jenkins_hash32
13162208ca5SGleb Smirnoffuses same hashing algorithm as the
13262208ca5SGleb Smirnoff.Fn jenkins_hash
13362208ca5SGleb Smirnofffunction, but works only on
13462208ca5SGleb Smirnoff.Ft uint32_t
13562208ca5SGleb Smirnoffsized arrays, thus is simplier and faster.
13662208ca5SGleb SmirnoffIt accepts an array of
13762208ca5SGleb Smirnoff.Ft uint32_t
13862208ca5SGleb Smirnoffvalues in its first argument and size of this array in the second argument.
139*99e9de87SDag-Erling Smørgrav.Pp
140*99e9de87SDag-Erling SmørgravThe
141*99e9de87SDag-Erling Smørgrav.Fn murmur3_32_hash
142*99e9de87SDag-Erling Smørgravand
143*99e9de87SDag-Erling Smørgrav.Fn murmur3_32_hash32
144*99e9de87SDag-Erling Smørgravfunctions are similar to
145*99e9de87SDag-Erling Smørgrav.Fn jenkins_hash
146*99e9de87SDag-Erling Smørgravand
147*99e9de87SDag-Erling Smørgrav.Fn jenkins_hash32 ,
148*99e9de87SDag-Erling Smørgravbut implement the 32-bit version of MurmurHash3.
1492433924cSAndre Oppermann.Sh RETURN VALUES
1502433924cSAndre OppermannThe
1512433924cSAndre Oppermann.Fn hash32
1522433924cSAndre Oppermannfunctions return a 32 bit hash value of the buffer or string.
1532433924cSAndre Oppermann.Sh EXAMPLES
1542433924cSAndre Oppermann.Bd -literal -offset indent
1552433924cSAndre OppermannLIST_HEAD(head, cache) *hashtbl = NULL;
1562433924cSAndre Oppermannu_long mask = 0;
1572433924cSAndre Oppermann
1582433924cSAndre Oppermannvoid
1592433924cSAndre Oppermannsample_init(void)
1602433924cSAndre Oppermann{
161bd84dd2fSRuslan Ermilov
1622433924cSAndre Oppermann        hashtbl = hashinit(numwanted, type, flags, &mask);
1632433924cSAndre Oppermann}
1642433924cSAndre Oppermann
1652433924cSAndre Oppermannvoid
1662433924cSAndre Oppermannsample_use(char *str, int len)
1672433924cSAndre Oppermann{
1682433924cSAndre Oppermann        uint32_t hash;
1692433924cSAndre Oppermann
1702433924cSAndre Oppermann        hash = hash32_str(str, HASHINIT);
1712433924cSAndre Oppermann        hash = hash32_buf(&len, sizeof(len), hash);
1722433924cSAndre Oppermann        hashtbl[hash & mask] = len;
1732433924cSAndre Oppermann}
1742433924cSAndre Oppermann.Ed
1752433924cSAndre Oppermann.Sh SEE ALSO
1762433924cSAndre Oppermann.Xr free 9 ,
1772433924cSAndre Oppermann.Xr hashinit 9 ,
1782433924cSAndre Oppermann.Xr malloc 9
1792433924cSAndre Oppermann.Sh LIMITATIONS
1802433924cSAndre OppermannThe
1812433924cSAndre Oppermann.Fn hash32
1822433924cSAndre Oppermannfunctions are only 32 bit functions.
1832433924cSAndre OppermannThey will prove to give poor 64 bit performance, especially for the
1842433924cSAndre Oppermanntop 32 bits.
1852433924cSAndre OppermannAt the current time, this is not seen as a great limitation, as these
1862433924cSAndre Oppermannhash values are usually used to index into an array.
1872433924cSAndre OppermannShould these hash values be used for other means, this limitation should
1882433924cSAndre Oppermannbe revisited.
1892433924cSAndre Oppermann.Sh HISTORY
1902433924cSAndre OppermannThe
1912433924cSAndre Oppermann.Nm
19262208ca5SGleb Smirnofffunctions first appeared in
1932433924cSAndre Oppermann.Nx 1.6 .
19462208ca5SGleb SmirnoffThe current implementation of
19562208ca5SGleb Smirnoff.Nm hash32
19662208ca5SGleb Smirnofffunctions was first committed to
19762208ca5SGleb Smirnoff.Ox 3.2 ,
19862208ca5SGleb Smirnoffand later imported to
19962208ca5SGleb Smirnoff.Fx 6.1 .
2002433924cSAndre OppermannThe
20162208ca5SGleb Smirnoff.Nm jenkins_hash
20262208ca5SGleb Smirnofffunctions were added in
20362208ca5SGleb Smirnoff.Fx 10.0 .
204*99e9de87SDag-Erling SmørgravThe
205*99e9de87SDag-Erling Smørgrav.Nm murmur3_32_hash
206*99e9de87SDag-Erling Smørgravfunctions were added in
207*99e9de87SDag-Erling Smørgrav.Fx 10.1 .
20862208ca5SGleb Smirnoff.Sh AUTHORS
20962208ca5SGleb SmirnoffThe
21062208ca5SGleb Smirnoff.Nm hash32
21162208ca5SGleb Smirnofffunctions were written by
21262208ca5SGleb Smirnoff.An Tobias Weingartner .
21362208ca5SGleb SmirnoffThe
21462208ca5SGleb Smirnoff.Nm jenkins_hash
215*99e9de87SDag-Erling Smørgravfunctions were written by
216*99e9de87SDag-Erling Smørgrav.An Bob Jenkins .
217*99e9de87SDag-Erling SmørgravThe
218*99e9de87SDag-Erling Smørgrav.Nm murmur3_32_hash
219*99e9de87SDag-Erling Smørgravfunctions were written by
220*99e9de87SDag-Erling Smørgrav.An Dag-Erling Sm\(/orgrav Aq Mt des@FreeBSD.org .
221