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.\" 2908126404SJohn-Mark Gurney.Dd June 30, 2015 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 , 4099e9de87SDag-Erling Smørgrav.Nm jenkins_hash , 4162208ca5SGleb Smirnoff.Nm jenkins_hash32 , 4299e9de87SDag-Erling Smørgrav.Nm murmur3_32_hash , 4399e9de87SDag-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" 6199e9de87SDag-Erling Smørgrav.Ft uint32_t 6299e9de87SDag-Erling Smørgrav.Fn murmur3_32_hash "const void *buf" "size_t len" "uint32_t hash" 6399e9de87SDag-Erling Smørgrav.Ft uint32_t 6499e9de87SDag-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 7508126404SJohn-Mark GurneyA 7608126404SJohn-Mark Gurney.Fa len 7708126404SJohn-Mark Gurneyargument is the length of the buffer in bytes. 7808126404SJohn-Mark GurneyA 7908126404SJohn-Mark Gurney.Fa count 8008126404SJohn-Mark Gurneyargument is the length of the buffer in 32-bit words. 8108126404SJohn-Mark Gurney.Pp 822433924cSAndre OppermannThe 832433924cSAndre Oppermann.Fn hash32_buf 842433924cSAndre Oppermannfunction is used as a general buffer hashing function. 852433924cSAndre OppermannThe argument 862433924cSAndre Oppermann.Fa buf 872433924cSAndre Oppermannis used to pass in the location, and 882433924cSAndre Oppermann.Fa len 8908126404SJohn-Mark Gurneyis the length of the buffer in bytes. 902433924cSAndre OppermannThe argument 912433924cSAndre Oppermann.Fa hash 922433924cSAndre Oppermannis used to extend an existing hash, or is passed the initial value 932433924cSAndre Oppermann.Dv HASHINIT 942433924cSAndre Oppermannto start a new hash. 952433924cSAndre Oppermann.Pp 962433924cSAndre OppermannThe 972433924cSAndre Oppermann.Fn hash32_str 982433924cSAndre Oppermannfunction is used to hash a 992433924cSAndre Oppermann.Dv NUL 1002433924cSAndre Oppermannterminated string passed in 1012433924cSAndre Oppermann.Fa buf 1022433924cSAndre Oppermannwith initial hash value given in 1032433924cSAndre Oppermann.Fa hash . 1042433924cSAndre Oppermann.Pp 1052433924cSAndre OppermannThe 1062433924cSAndre Oppermann.Fn hash32_strn 1072433924cSAndre Oppermannfunction is like the 1082433924cSAndre Oppermann.Fn hash32_str 1092433924cSAndre Oppermannfunction, except it also takes a 1102433924cSAndre Oppermann.Fa len 1112433924cSAndre Oppermannargument, which is the maximal length of the expected string. 1122433924cSAndre Oppermann.Pp 1132433924cSAndre OppermannThe 1142433924cSAndre Oppermann.Fn hash32_stre 1152433924cSAndre Oppermannand 1162433924cSAndre Oppermann.Fn hash32_strne 1172433924cSAndre Oppermannfunctions are helper functions used by the kernel to hash pathname 1182433924cSAndre Oppermanncomponents. 1192433924cSAndre OppermannThese functions have the additional termination condition 1202433924cSAndre Oppermannof terminating when they find a character given by 1212433924cSAndre Oppermann.Fa end 1222433924cSAndre Oppermannin the string to be hashed. 1232433924cSAndre OppermannIf the argument 1242433924cSAndre Oppermann.Fa ep 1252433924cSAndre Oppermannis not 1262433924cSAndre Oppermann.Dv NULL , 1272433924cSAndre Oppermannit is set to the point in the buffer at which the hash function 1282433924cSAndre Oppermannterminated hashing. 12962208ca5SGleb Smirnoff.Pp 13062208ca5SGleb SmirnoffThe 13162208ca5SGleb Smirnoff.Fn jenkins_hash 13262208ca5SGleb Smirnofffunction has same semantics as the 13362208ca5SGleb Smirnoff.Fn hash32_buf , 13462208ca5SGleb Smirnoffbut provides more advanced hashing algorithm with better distribution. 13562208ca5SGleb Smirnoff.Pp 13662208ca5SGleb SmirnoffThe 13762208ca5SGleb Smirnoff.Fn jenkins_hash32 13862208ca5SGleb Smirnoffuses same hashing algorithm as the 13962208ca5SGleb Smirnoff.Fn jenkins_hash 14062208ca5SGleb Smirnofffunction, but works only on 14162208ca5SGleb Smirnoff.Ft uint32_t 142*7c64ddd5SWarren Blocksized arrays, thus is simpler and faster. 14362208ca5SGleb SmirnoffIt accepts an array of 14462208ca5SGleb Smirnoff.Ft uint32_t 14562208ca5SGleb Smirnoffvalues in its first argument and size of this array in the second argument. 14699e9de87SDag-Erling Smørgrav.Pp 14799e9de87SDag-Erling SmørgravThe 14899e9de87SDag-Erling Smørgrav.Fn murmur3_32_hash 14999e9de87SDag-Erling Smørgravand 15099e9de87SDag-Erling Smørgrav.Fn murmur3_32_hash32 15199e9de87SDag-Erling Smørgravfunctions are similar to 15299e9de87SDag-Erling Smørgrav.Fn jenkins_hash 15399e9de87SDag-Erling Smørgravand 15499e9de87SDag-Erling Smørgrav.Fn jenkins_hash32 , 15599e9de87SDag-Erling Smørgravbut implement the 32-bit version of MurmurHash3. 1562433924cSAndre Oppermann.Sh RETURN VALUES 1572433924cSAndre OppermannThe 1582433924cSAndre Oppermann.Fn hash32 1592433924cSAndre Oppermannfunctions return a 32 bit hash value of the buffer or string. 1602433924cSAndre Oppermann.Sh EXAMPLES 1612433924cSAndre Oppermann.Bd -literal -offset indent 1622433924cSAndre OppermannLIST_HEAD(head, cache) *hashtbl = NULL; 1632433924cSAndre Oppermannu_long mask = 0; 1642433924cSAndre Oppermann 1652433924cSAndre Oppermannvoid 1662433924cSAndre Oppermannsample_init(void) 1672433924cSAndre Oppermann{ 168bd84dd2fSRuslan Ermilov 1692433924cSAndre Oppermann hashtbl = hashinit(numwanted, type, flags, &mask); 1702433924cSAndre Oppermann} 1712433924cSAndre Oppermann 1722433924cSAndre Oppermannvoid 1732433924cSAndre Oppermannsample_use(char *str, int len) 1742433924cSAndre Oppermann{ 1752433924cSAndre Oppermann uint32_t hash; 1762433924cSAndre Oppermann 1772433924cSAndre Oppermann hash = hash32_str(str, HASHINIT); 1782433924cSAndre Oppermann hash = hash32_buf(&len, sizeof(len), hash); 1792433924cSAndre Oppermann hashtbl[hash & mask] = len; 1802433924cSAndre Oppermann} 1812433924cSAndre Oppermann.Ed 1822433924cSAndre Oppermann.Sh SEE ALSO 1832433924cSAndre Oppermann.Xr free 9 , 1842433924cSAndre Oppermann.Xr hashinit 9 , 1852433924cSAndre Oppermann.Xr malloc 9 1862433924cSAndre Oppermann.Sh LIMITATIONS 1872433924cSAndre OppermannThe 1882433924cSAndre Oppermann.Fn hash32 1892433924cSAndre Oppermannfunctions are only 32 bit functions. 1902433924cSAndre OppermannThey will prove to give poor 64 bit performance, especially for the 1912433924cSAndre Oppermanntop 32 bits. 1922433924cSAndre OppermannAt the current time, this is not seen as a great limitation, as these 1932433924cSAndre Oppermannhash values are usually used to index into an array. 1942433924cSAndre OppermannShould these hash values be used for other means, this limitation should 1952433924cSAndre Oppermannbe revisited. 1962433924cSAndre Oppermann.Sh HISTORY 1972433924cSAndre OppermannThe 1982433924cSAndre Oppermann.Nm 19962208ca5SGleb Smirnofffunctions first appeared in 2002433924cSAndre Oppermann.Nx 1.6 . 20162208ca5SGleb SmirnoffThe current implementation of 20262208ca5SGleb Smirnoff.Nm hash32 20362208ca5SGleb Smirnofffunctions was first committed to 20462208ca5SGleb Smirnoff.Ox 3.2 , 20562208ca5SGleb Smirnoffand later imported to 20662208ca5SGleb Smirnoff.Fx 6.1 . 2072433924cSAndre OppermannThe 20862208ca5SGleb Smirnoff.Nm jenkins_hash 20962208ca5SGleb Smirnofffunctions were added in 21062208ca5SGleb Smirnoff.Fx 10.0 . 21199e9de87SDag-Erling SmørgravThe 21299e9de87SDag-Erling Smørgrav.Nm murmur3_32_hash 21399e9de87SDag-Erling Smørgravfunctions were added in 21499e9de87SDag-Erling Smørgrav.Fx 10.1 . 21562208ca5SGleb Smirnoff.Sh AUTHORS 21662208ca5SGleb SmirnoffThe 21762208ca5SGleb Smirnoff.Nm hash32 21862208ca5SGleb Smirnofffunctions were written by 21962208ca5SGleb Smirnoff.An Tobias Weingartner . 22062208ca5SGleb SmirnoffThe 22162208ca5SGleb Smirnoff.Nm jenkins_hash 22299e9de87SDag-Erling Smørgravfunctions were written by 22399e9de87SDag-Erling Smørgrav.An Bob Jenkins . 22499e9de87SDag-Erling SmørgravThe 22599e9de87SDag-Erling Smørgrav.Nm murmur3_32_hash 22699e9de87SDag-Erling Smørgravfunctions were written by 22799e9de87SDag-Erling Smørgrav.An Dag-Erling Sm\(/orgrav Aq Mt des@FreeBSD.org . 228