1.\" Copyright (c) 2001 Tobias Weingartner 2.\" All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\" 2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 3. The name of the author may not be used to endorse or promote products 13.\" derived from this software without specific prior written permission. 14.\" 15.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25.\" 26.\" $OpenBSD: hash.9,v 1.5 2003/04/17 05:08:39 jmc Exp $ 27.\" 28.Dd June 30, 2015 29.Dt HASH 9 30.Os 31.Sh NAME 32.Nm hash , 33.Nm hash32 , 34.Nm hash32_buf , 35.Nm hash32_str , 36.Nm hash32_strn , 37.Nm hash32_stre , 38.Nm hash32_strne , 39.Nm jenkins_hash , 40.Nm jenkins_hash32 , 41.Nm murmur3_32_hash , 42.Nm murmur3_32_hash32 43.Nd general kernel hashing functions 44.Sh SYNOPSIS 45.In sys/hash.h 46.Ft uint32_t 47.Fn hash32_buf "const void *buf" "size_t len" "uint32_t hash" 48.Ft uint32_t 49.Fn hash32_str "const void *buf" "uint32_t hash" 50.Ft uint32_t 51.Fn hash32_strn "const void *buf" "size_t len" "uint32_t hash" 52.Ft uint32_t 53.Fn hash32_stre "const void *buf" "int end" "const char **ep" "uint32_t hash" 54.Ft uint32_t 55.Fn hash32_strne "const void *buf" "size_t len" "int end" "const char **ep" "uint32_t hash" 56.Ft uint32_t 57.Fn jenkins_hash "const void *buf" "size_t len" "uint32_t hash" 58.Ft uint32_t 59.Fn jenkins_hash32 "const uint32_t *buf" "size_t count" "uint32_t hash" 60.Ft uint32_t 61.Fn murmur3_32_hash "const void *buf" "size_t len" "uint32_t hash" 62.Ft uint32_t 63.Fn murmur3_32_hash32 "const uint32_t *buf" "size_t count" "uint32_t hash" 64.Sh DESCRIPTION 65The 66.Fn hash32 67functions are used to give a consistent and general interface to 68a decent hashing algorithm within the kernel. 69These functions can be used to hash 70.Tn ASCII 71.Dv NUL 72terminated strings, as well as blocks of memory. 73.Pp 74A 75.Fa len 76argument is the length of the buffer in bytes. 77A 78.Fa count 79argument is the length of the buffer in 32-bit words. 80.Pp 81The 82.Fn hash32_buf 83function is used as a general buffer hashing function. 84The argument 85.Fa buf 86is used to pass in the location, and 87.Fa len 88is the length of the buffer in bytes. 89The argument 90.Fa hash 91is used to extend an existing hash, or is passed the initial value 92.Dv HASHINIT 93to start a new hash. 94.Pp 95The 96.Fn hash32_str 97function is used to hash a 98.Dv NUL 99terminated string passed in 100.Fa buf 101with initial hash value given in 102.Fa hash . 103.Pp 104The 105.Fn hash32_strn 106function is like the 107.Fn hash32_str 108function, except it also takes a 109.Fa len 110argument, which is the maximal length of the expected string. 111.Pp 112The 113.Fn hash32_stre 114and 115.Fn hash32_strne 116functions are helper functions used by the kernel to hash pathname 117components. 118These functions have the additional termination condition 119of terminating when they find a character given by 120.Fa end 121in the string to be hashed. 122If the argument 123.Fa ep 124is not 125.Dv NULL , 126it is set to the point in the buffer at which the hash function 127terminated hashing. 128.Pp 129The 130.Fn jenkins_hash 131function has same semantics as the 132.Fn hash32_buf , 133but provides more advanced hashing algorithm with better distribution. 134.Pp 135The 136.Fn jenkins_hash32 137uses same hashing algorithm as the 138.Fn jenkins_hash 139function, but works only on 140.Ft uint32_t 141sized arrays, thus is simpler and faster. 142It accepts an array of 143.Ft uint32_t 144values in its first argument and size of this array in the second argument. 145.Pp 146The 147.Fn murmur3_32_hash 148and 149.Fn murmur3_32_hash32 150functions are similar to 151.Fn jenkins_hash 152and 153.Fn jenkins_hash32 , 154but implement the 32-bit version of MurmurHash3. 155.Sh RETURN VALUES 156The 157.Fn hash32 158functions return a 32 bit hash value of the buffer or string. 159.Sh EXAMPLES 160.Bd -literal -offset indent 161LIST_HEAD(head, cache) *hashtbl = NULL; 162u_long mask = 0; 163 164void 165sample_init(void) 166{ 167 168 hashtbl = hashinit(numwanted, type, flags, &mask); 169} 170 171void 172sample_use(char *str, int len) 173{ 174 uint32_t hash; 175 176 hash = hash32_str(str, HASHINIT); 177 hash = hash32_buf(&len, sizeof(len), hash); 178 hashtbl[hash & mask] = len; 179} 180.Ed 181.Sh SEE ALSO 182.Xr free 9 , 183.Xr hashinit 9 , 184.Xr malloc 9 185.Sh LIMITATIONS 186The 187.Fn hash32 188functions are only 32 bit functions. 189They will prove to give poor 64 bit performance, especially for the 190top 32 bits. 191At the current time, this is not seen as a great limitation, as these 192hash values are usually used to index into an array. 193Should these hash values be used for other means, this limitation should 194be revisited. 195.Sh HISTORY 196The 197.Nm 198functions first appeared in 199.Nx 1.6 . 200The current implementation of 201.Nm hash32 202functions was first committed to 203.Ox 3.2 , 204and later imported to 205.Fx 6.1 . 206The 207.Nm jenkins_hash 208functions were added in 209.Fx 10.0 . 210The 211.Nm murmur3_32_hash 212functions were added in 213.Fx 10.1 . 214.Sh AUTHORS 215The 216.Nm hash32 217functions were written by 218.An Tobias Weingartner . 219The 220.Nm jenkins_hash 221functions were written by 222.An Bob Jenkins . 223The 224.Nm murmur3_32_hash 225functions were written by 226.An Dag-Erling Sm\(/orgrav Aq Mt des@FreeBSD.org . 227