1*6c9c2933SSascha Wildner.\" Copyright (c) 2001 Tobias Weingartner 2*6c9c2933SSascha Wildner.\" All rights reserved. 3*6c9c2933SSascha Wildner.\" 4*6c9c2933SSascha Wildner.\" Redistribution and use in source and binary forms, with or without 5*6c9c2933SSascha Wildner.\" modification, are permitted provided that the following conditions 6*6c9c2933SSascha Wildner.\" are met: 7*6c9c2933SSascha Wildner.\" 1. Redistributions of source code must retain the above copyright 8*6c9c2933SSascha Wildner.\" notice, this list of conditions and the following disclaimer. 9*6c9c2933SSascha Wildner.\" 2. Redistributions in binary form must reproduce the above copyright 10*6c9c2933SSascha Wildner.\" notice, this list of conditions and the following disclaimer in the 11*6c9c2933SSascha Wildner.\" documentation and/or other materials provided with the distribution. 12*6c9c2933SSascha Wildner.\" 3. The name of the author may not be used to endorse or promote products 13*6c9c2933SSascha Wildner.\" derived from this software without specific prior written permission. 14*6c9c2933SSascha Wildner.\" 15*6c9c2933SSascha Wildner.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16*6c9c2933SSascha Wildner.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17*6c9c2933SSascha Wildner.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18*6c9c2933SSascha Wildner.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19*6c9c2933SSascha Wildner.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20*6c9c2933SSascha Wildner.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21*6c9c2933SSascha Wildner.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22*6c9c2933SSascha Wildner.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23*6c9c2933SSascha Wildner.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24*6c9c2933SSascha Wildner.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25*6c9c2933SSascha Wildner.\" 26*6c9c2933SSascha Wildner.\" $OpenBSD: hash.9,v 1.5 2003/04/17 05:08:39 jmc Exp $ 27*6c9c2933SSascha Wildner.\" $FreeBSD: src/share/man/man9/hash.9,v 1.4 2007/04/09 22:55:14 thompsa Exp $ 28*6c9c2933SSascha Wildner.\" 29*6c9c2933SSascha Wildner.Dd June 6, 2012 30*6c9c2933SSascha Wildner.Dt HASH 9 31*6c9c2933SSascha Wildner.Os 32*6c9c2933SSascha Wildner.Sh NAME 33*6c9c2933SSascha Wildner.Nm hash , 34*6c9c2933SSascha Wildner.Nm hash32 , 35*6c9c2933SSascha Wildner.Nm hash32_buf , 36*6c9c2933SSascha Wildner.Nm hash32_str , 37*6c9c2933SSascha Wildner.Nm hash32_strn , 38*6c9c2933SSascha Wildner.Nm hash32_stre , 39*6c9c2933SSascha Wildner.Nm hash32_strne 40*6c9c2933SSascha Wildner.Nd general kernel hashing functions 41*6c9c2933SSascha Wildner.Sh SYNOPSIS 42*6c9c2933SSascha Wildner.In sys/hash.h 43*6c9c2933SSascha Wildner.Ft uint32_t 44*6c9c2933SSascha Wildner.Fn hash32_buf "const void *buf" "size_t len" "uint32_t hash" 45*6c9c2933SSascha Wildner.Ft uint32_t 46*6c9c2933SSascha Wildner.Fn hash32_str "const void *buf" "uint32_t hash" 47*6c9c2933SSascha Wildner.Ft uint32_t 48*6c9c2933SSascha Wildner.Fn hash32_strn "const void *buf" "size_t len" "uint32_t hash" 49*6c9c2933SSascha Wildner.Ft uint32_t 50*6c9c2933SSascha Wildner.Fn hash32_stre "const void *buf" "int end" "const char **ep" "uint32_t hash" 51*6c9c2933SSascha Wildner.Ft uint32_t 52*6c9c2933SSascha Wildner.Fn hash32_strne "const void *buf" "size_t len" "int end" "const char **ep" "uint32_t hash" 53*6c9c2933SSascha Wildner.Sh DESCRIPTION 54*6c9c2933SSascha WildnerThe 55*6c9c2933SSascha Wildner.Fn hash32 56*6c9c2933SSascha Wildnerfunctions are used to give a consistent and general interface to 57*6c9c2933SSascha Wildnera decent hashing algorithm within the kernel. 58*6c9c2933SSascha WildnerThese functions can be used to hash 59*6c9c2933SSascha Wildner.Tn ASCII 60*6c9c2933SSascha Wildner.Dv NUL 61*6c9c2933SSascha Wildnerterminated strings, as well as blocks of memory. 62*6c9c2933SSascha Wildner.Pp 63*6c9c2933SSascha WildnerThe 64*6c9c2933SSascha Wildner.Fn hash32_buf 65*6c9c2933SSascha Wildnerfunction is used as a general buffer hashing function. 66*6c9c2933SSascha WildnerThe argument 67*6c9c2933SSascha Wildner.Fa buf 68*6c9c2933SSascha Wildneris used to pass in the location, and 69*6c9c2933SSascha Wildner.Fa len 70*6c9c2933SSascha Wildneris the length of the buffer. 71*6c9c2933SSascha WildnerThe argument 72*6c9c2933SSascha Wildner.Fa hash 73*6c9c2933SSascha Wildneris used to extend an existing hash, or is passed the initial value 74*6c9c2933SSascha Wildner.Dv HASHINIT 75*6c9c2933SSascha Wildnerto start a new hash. 76*6c9c2933SSascha Wildner.Pp 77*6c9c2933SSascha WildnerThe 78*6c9c2933SSascha Wildner.Fn hash32_str 79*6c9c2933SSascha Wildnerfunction is used to hash a 80*6c9c2933SSascha Wildner.Dv NUL 81*6c9c2933SSascha Wildnerterminated string passed in 82*6c9c2933SSascha Wildner.Fa buf 83*6c9c2933SSascha Wildnerwith initial hash value given in 84*6c9c2933SSascha Wildner.Fa hash . 85*6c9c2933SSascha Wildner.Pp 86*6c9c2933SSascha WildnerThe 87*6c9c2933SSascha Wildner.Fn hash32_strn 88*6c9c2933SSascha Wildnerfunction is like the 89*6c9c2933SSascha Wildner.Fn hash32_str 90*6c9c2933SSascha Wildnerfunction, except it also takes a 91*6c9c2933SSascha Wildner.Fa len 92*6c9c2933SSascha Wildnerargument, which is the maximal length of the expected string. 93*6c9c2933SSascha Wildner.Pp 94*6c9c2933SSascha WildnerThe 95*6c9c2933SSascha Wildner.Fn hash32_stre 96*6c9c2933SSascha Wildnerand 97*6c9c2933SSascha Wildner.Fn hash32_strne 98*6c9c2933SSascha Wildnerfunctions are helper functions used by the kernel to hash pathname 99*6c9c2933SSascha Wildnercomponents. 100*6c9c2933SSascha WildnerThese functions have the additional termination condition 101*6c9c2933SSascha Wildnerof terminating when they find a character given by 102*6c9c2933SSascha Wildner.Fa end 103*6c9c2933SSascha Wildnerin the string to be hashed. 104*6c9c2933SSascha WildnerIf the argument 105*6c9c2933SSascha Wildner.Fa ep 106*6c9c2933SSascha Wildneris not 107*6c9c2933SSascha Wildner.Dv NULL , 108*6c9c2933SSascha Wildnerit is set to the point in the buffer at which the hash function 109*6c9c2933SSascha Wildnerterminated hashing. 110*6c9c2933SSascha Wildner.Sh RETURN VALUES 111*6c9c2933SSascha WildnerThe 112*6c9c2933SSascha Wildner.Fn hash32 113*6c9c2933SSascha Wildnerfunctions return a 32 bit hash value of the buffer or string. 114*6c9c2933SSascha Wildner.Sh EXAMPLES 115*6c9c2933SSascha Wildner.Bd -literal -offset indent 116*6c9c2933SSascha WildnerLIST_HEAD(head, cache) *hashtbl = NULL; 117*6c9c2933SSascha Wildneru_long mask = 0; 118*6c9c2933SSascha Wildner 119*6c9c2933SSascha Wildnervoid 120*6c9c2933SSascha Wildnersample_init(void) 121*6c9c2933SSascha Wildner{ 122*6c9c2933SSascha Wildner 123*6c9c2933SSascha Wildner hashtbl = hashinit(numwanted, type, flags, &mask); 124*6c9c2933SSascha Wildner} 125*6c9c2933SSascha Wildner 126*6c9c2933SSascha Wildnervoid 127*6c9c2933SSascha Wildnersample_use(char *str, int len) 128*6c9c2933SSascha Wildner{ 129*6c9c2933SSascha Wildner uint32_t hash; 130*6c9c2933SSascha Wildner 131*6c9c2933SSascha Wildner hash = hash32_str(str, HASHINIT); 132*6c9c2933SSascha Wildner hash = hash32_buf(&len, sizeof(len), hash); 133*6c9c2933SSascha Wildner hashtbl[hash & mask] = len; 134*6c9c2933SSascha Wildner} 135*6c9c2933SSascha Wildner.Ed 136*6c9c2933SSascha Wildner.Sh SEE ALSO 137*6c9c2933SSascha Wildner.Xr hashinit 9 , 138*6c9c2933SSascha Wildner.Xr kfree 9 , 139*6c9c2933SSascha Wildner.Xr kmalloc 9 140*6c9c2933SSascha Wildner.Sh LIMITATIONS 141*6c9c2933SSascha WildnerThe 142*6c9c2933SSascha Wildner.Fn hash32 143*6c9c2933SSascha Wildnerfunctions are only 32 bit functions. 144*6c9c2933SSascha WildnerThey will prove to give poor 64 bit performance, especially for the 145*6c9c2933SSascha Wildnertop 32 bits. 146*6c9c2933SSascha WildnerAt the current time, this is not seen as a great limitation, as these 147*6c9c2933SSascha Wildnerhash values are usually used to index into an array. 148*6c9c2933SSascha WildnerShould these hash values be used for other means, this limitation should 149*6c9c2933SSascha Wildnerbe revisited. 150*6c9c2933SSascha Wildner.Sh HISTORY 151*6c9c2933SSascha WildnerThe 152*6c9c2933SSascha Wildner.Nm 153*6c9c2933SSascha Wildnerfunctions were first committed to 154*6c9c2933SSascha Wildner.Nx 1.6 . 155*6c9c2933SSascha WildnerThe 156*6c9c2933SSascha Wildner.Ox 157*6c9c2933SSascha Wildnerversions were written and massaged for 158*6c9c2933SSascha Wildner.Ox 2.3 159*6c9c2933SSascha Wildnerby Tobias Weingartner, 160*6c9c2933SSascha Wildnerand finally committed for 161*6c9c2933SSascha Wildner.Ox 3.2 . 162