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.\" 2808126404SJohn-Mark Gurney.Dd June 30, 2015 292433924cSAndre Oppermann.Dt HASH 9 302433924cSAndre Oppermann.Os 312433924cSAndre Oppermann.Sh NAME 32bd84dd2fSRuslan Ermilov.Nm hash , 33bd84dd2fSRuslan Ermilov.Nm hash32 , 34bd84dd2fSRuslan Ermilov.Nm hash32_buf , 35bd84dd2fSRuslan Ermilov.Nm hash32_str , 36bd84dd2fSRuslan Ermilov.Nm hash32_strn , 37bd84dd2fSRuslan Ermilov.Nm hash32_stre , 3862208ca5SGleb Smirnoff.Nm hash32_strne , 3999e9de87SDag-Erling Smørgrav.Nm jenkins_hash , 4062208ca5SGleb Smirnoff.Nm jenkins_hash32 , 4199e9de87SDag-Erling Smørgrav.Nm murmur3_32_hash , 4299e9de87SDag-Erling Smørgrav.Nm murmur3_32_hash32 432433924cSAndre Oppermann.Nd general kernel hashing functions 442433924cSAndre Oppermann.Sh SYNOPSIS 45bd84dd2fSRuslan Ermilov.In sys/hash.h 462433924cSAndre Oppermann.Ft uint32_t 47a341ab71SRuslan Ermilov.Fn hash32_buf "const void *buf" "size_t len" "uint32_t hash" 482433924cSAndre Oppermann.Ft uint32_t 49a341ab71SRuslan Ermilov.Fn hash32_str "const void *buf" "uint32_t hash" 502433924cSAndre Oppermann.Ft uint32_t 51a341ab71SRuslan Ermilov.Fn hash32_strn "const void *buf" "size_t len" "uint32_t hash" 522433924cSAndre Oppermann.Ft uint32_t 536429a5cbSAndrew Thompson.Fn hash32_stre "const void *buf" "int end" "const char **ep" "uint32_t hash" 542433924cSAndre Oppermann.Ft uint32_t 556429a5cbSAndrew Thompson.Fn hash32_strne "const void *buf" "size_t len" "int end" "const char **ep" "uint32_t hash" 5662208ca5SGleb Smirnoff.Ft uint32_t 5762208ca5SGleb Smirnoff.Fn jenkins_hash "const void *buf" "size_t len" "uint32_t hash" 5862208ca5SGleb Smirnoff.Ft uint32_t 5962208ca5SGleb Smirnoff.Fn jenkins_hash32 "const uint32_t *buf" "size_t count" "uint32_t hash" 6099e9de87SDag-Erling Smørgrav.Ft uint32_t 6199e9de87SDag-Erling Smørgrav.Fn murmur3_32_hash "const void *buf" "size_t len" "uint32_t hash" 6299e9de87SDag-Erling Smørgrav.Ft uint32_t 6399e9de87SDag-Erling Smørgrav.Fn murmur3_32_hash32 "const uint32_t *buf" "size_t count" "uint32_t hash" 642433924cSAndre Oppermann.Sh DESCRIPTION 652433924cSAndre OppermannThe 662433924cSAndre Oppermann.Fn hash32 672433924cSAndre Oppermannfunctions are used to give a consistent and general interface to 682433924cSAndre Oppermanna decent hashing algorithm within the kernel. 69bd84dd2fSRuslan ErmilovThese functions can be used to hash 70bd84dd2fSRuslan Ermilov.Tn ASCII 712433924cSAndre Oppermann.Dv NUL 722433924cSAndre Oppermannterminated strings, as well as blocks of memory. 732433924cSAndre Oppermann.Pp 7408126404SJohn-Mark GurneyA 7508126404SJohn-Mark Gurney.Fa len 7608126404SJohn-Mark Gurneyargument is the length of the buffer in bytes. 7708126404SJohn-Mark GurneyA 7808126404SJohn-Mark Gurney.Fa count 7908126404SJohn-Mark Gurneyargument is the length of the buffer in 32-bit words. 8008126404SJohn-Mark Gurney.Pp 812433924cSAndre OppermannThe 822433924cSAndre Oppermann.Fn hash32_buf 832433924cSAndre Oppermannfunction is used as a general buffer hashing function. 842433924cSAndre OppermannThe argument 852433924cSAndre Oppermann.Fa buf 862433924cSAndre Oppermannis used to pass in the location, and 872433924cSAndre Oppermann.Fa len 8808126404SJohn-Mark Gurneyis the length of the buffer in bytes. 892433924cSAndre OppermannThe argument 902433924cSAndre Oppermann.Fa hash 912433924cSAndre Oppermannis used to extend an existing hash, or is passed the initial value 922433924cSAndre Oppermann.Dv HASHINIT 932433924cSAndre Oppermannto start a new hash. 942433924cSAndre Oppermann.Pp 952433924cSAndre OppermannThe 962433924cSAndre Oppermann.Fn hash32_str 972433924cSAndre Oppermannfunction is used to hash a 982433924cSAndre Oppermann.Dv NUL 992433924cSAndre Oppermannterminated string passed in 1002433924cSAndre Oppermann.Fa buf 1012433924cSAndre Oppermannwith initial hash value given in 1022433924cSAndre Oppermann.Fa hash . 1032433924cSAndre Oppermann.Pp 1042433924cSAndre OppermannThe 1052433924cSAndre Oppermann.Fn hash32_strn 1062433924cSAndre Oppermannfunction is like the 1072433924cSAndre Oppermann.Fn hash32_str 1082433924cSAndre Oppermannfunction, except it also takes a 1092433924cSAndre Oppermann.Fa len 1102433924cSAndre Oppermannargument, which is the maximal length of the expected string. 1112433924cSAndre Oppermann.Pp 1122433924cSAndre OppermannThe 1132433924cSAndre Oppermann.Fn hash32_stre 1142433924cSAndre Oppermannand 1152433924cSAndre Oppermann.Fn hash32_strne 1162433924cSAndre Oppermannfunctions are helper functions used by the kernel to hash pathname 1172433924cSAndre Oppermanncomponents. 1182433924cSAndre OppermannThese functions have the additional termination condition 1192433924cSAndre Oppermannof terminating when they find a character given by 1202433924cSAndre Oppermann.Fa end 1212433924cSAndre Oppermannin the string to be hashed. 1222433924cSAndre OppermannIf the argument 1232433924cSAndre Oppermann.Fa ep 1242433924cSAndre Oppermannis not 1252433924cSAndre Oppermann.Dv NULL , 1262433924cSAndre Oppermannit is set to the point in the buffer at which the hash function 1272433924cSAndre Oppermannterminated hashing. 12862208ca5SGleb Smirnoff.Pp 12962208ca5SGleb SmirnoffThe 13062208ca5SGleb Smirnoff.Fn jenkins_hash 13162208ca5SGleb Smirnofffunction has same semantics as the 13262208ca5SGleb Smirnoff.Fn hash32_buf , 13362208ca5SGleb Smirnoffbut provides more advanced hashing algorithm with better distribution. 13462208ca5SGleb Smirnoff.Pp 13562208ca5SGleb SmirnoffThe 13662208ca5SGleb Smirnoff.Fn jenkins_hash32 13762208ca5SGleb Smirnoffuses same hashing algorithm as the 13862208ca5SGleb Smirnoff.Fn jenkins_hash 13962208ca5SGleb Smirnofffunction, but works only on 14062208ca5SGleb Smirnoff.Ft uint32_t 141*7c64ddd5SWarren Blocksized arrays, thus is simpler and faster. 14262208ca5SGleb SmirnoffIt accepts an array of 14362208ca5SGleb Smirnoff.Ft uint32_t 14462208ca5SGleb Smirnoffvalues in its first argument and size of this array in the second argument. 14599e9de87SDag-Erling Smørgrav.Pp 14699e9de87SDag-Erling SmørgravThe 14799e9de87SDag-Erling Smørgrav.Fn murmur3_32_hash 14899e9de87SDag-Erling Smørgravand 14999e9de87SDag-Erling Smørgrav.Fn murmur3_32_hash32 15099e9de87SDag-Erling Smørgravfunctions are similar to 15199e9de87SDag-Erling Smørgrav.Fn jenkins_hash 15299e9de87SDag-Erling Smørgravand 15399e9de87SDag-Erling Smørgrav.Fn jenkins_hash32 , 15499e9de87SDag-Erling Smørgravbut implement the 32-bit version of MurmurHash3. 1552433924cSAndre Oppermann.Sh RETURN VALUES 1562433924cSAndre OppermannThe 1572433924cSAndre Oppermann.Fn hash32 1582433924cSAndre Oppermannfunctions return a 32 bit hash value of the buffer or string. 1592433924cSAndre Oppermann.Sh EXAMPLES 1602433924cSAndre Oppermann.Bd -literal -offset indent 1612433924cSAndre OppermannLIST_HEAD(head, cache) *hashtbl = NULL; 1622433924cSAndre Oppermannu_long mask = 0; 1632433924cSAndre Oppermann 1642433924cSAndre Oppermannvoid 1652433924cSAndre Oppermannsample_init(void) 1662433924cSAndre Oppermann{ 167bd84dd2fSRuslan Ermilov 1682433924cSAndre Oppermann hashtbl = hashinit(numwanted, type, flags, &mask); 1692433924cSAndre Oppermann} 1702433924cSAndre Oppermann 1712433924cSAndre Oppermannvoid 1722433924cSAndre Oppermannsample_use(char *str, int len) 1732433924cSAndre Oppermann{ 1742433924cSAndre Oppermann uint32_t hash; 1752433924cSAndre Oppermann 1762433924cSAndre Oppermann hash = hash32_str(str, HASHINIT); 1772433924cSAndre Oppermann hash = hash32_buf(&len, sizeof(len), hash); 1782433924cSAndre Oppermann hashtbl[hash & mask] = len; 1792433924cSAndre Oppermann} 1802433924cSAndre Oppermann.Ed 1812433924cSAndre Oppermann.Sh SEE ALSO 1822433924cSAndre Oppermann.Xr free 9 , 1832433924cSAndre Oppermann.Xr hashinit 9 , 1842433924cSAndre Oppermann.Xr malloc 9 1852433924cSAndre Oppermann.Sh LIMITATIONS 1862433924cSAndre OppermannThe 1872433924cSAndre Oppermann.Fn hash32 1882433924cSAndre Oppermannfunctions are only 32 bit functions. 1892433924cSAndre OppermannThey will prove to give poor 64 bit performance, especially for the 1902433924cSAndre Oppermanntop 32 bits. 1912433924cSAndre OppermannAt the current time, this is not seen as a great limitation, as these 1922433924cSAndre Oppermannhash values are usually used to index into an array. 1932433924cSAndre OppermannShould these hash values be used for other means, this limitation should 1942433924cSAndre Oppermannbe revisited. 1952433924cSAndre Oppermann.Sh HISTORY 1962433924cSAndre OppermannThe 1972433924cSAndre Oppermann.Nm 19862208ca5SGleb Smirnofffunctions first appeared in 1992433924cSAndre Oppermann.Nx 1.6 . 20062208ca5SGleb SmirnoffThe current implementation of 20162208ca5SGleb Smirnoff.Nm hash32 20262208ca5SGleb Smirnofffunctions was first committed to 20362208ca5SGleb Smirnoff.Ox 3.2 , 20462208ca5SGleb Smirnoffand later imported to 20562208ca5SGleb Smirnoff.Fx 6.1 . 2062433924cSAndre OppermannThe 20762208ca5SGleb Smirnoff.Nm jenkins_hash 20862208ca5SGleb Smirnofffunctions were added in 20962208ca5SGleb Smirnoff.Fx 10.0 . 21099e9de87SDag-Erling SmørgravThe 21199e9de87SDag-Erling Smørgrav.Nm murmur3_32_hash 21299e9de87SDag-Erling Smørgravfunctions were added in 21399e9de87SDag-Erling Smørgrav.Fx 10.1 . 21462208ca5SGleb Smirnoff.Sh AUTHORS 21562208ca5SGleb SmirnoffThe 21662208ca5SGleb Smirnoff.Nm hash32 21762208ca5SGleb Smirnofffunctions were written by 21862208ca5SGleb Smirnoff.An Tobias Weingartner . 21962208ca5SGleb SmirnoffThe 22062208ca5SGleb Smirnoff.Nm jenkins_hash 22199e9de87SDag-Erling Smørgravfunctions were written by 22299e9de87SDag-Erling Smørgrav.An Bob Jenkins . 22399e9de87SDag-Erling SmørgravThe 22499e9de87SDag-Erling Smørgrav.Nm murmur3_32_hash 22599e9de87SDag-Erling Smørgravfunctions were written by 22699e9de87SDag-Erling Smørgrav.An Dag-Erling Sm\(/orgrav Aq Mt des@FreeBSD.org . 227