~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/include/linux/stringhash.h

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /* SPDX-License-Identifier: GPL-2.0 */
  2 #ifndef __LINUX_STRINGHASH_H
  3 #define __LINUX_STRINGHASH_H
  4 
  5 #include <linux/compiler.h>     /* For __pure */
  6 #include <linux/types.h>        /* For u32, u64 */
  7 #include <linux/hash.h>
  8 
  9 /*
 10  * Routines for hashing strings of bytes to a 32-bit hash value.
 11  *
 12  * These hash functions are NOT GUARANTEED STABLE between kernel
 13  * versions, architectures, or even repeated boots of the same kernel.
 14  * (E.g. they may depend on boot-time hardware detection or be
 15  * deliberately randomized.)
 16  *
 17  * They are also not intended to be secure against collisions caused by
 18  * malicious inputs; much slower hash functions are required for that.
 19  *
 20  * They are optimized for pathname components, meaning short strings.
 21  * Even if a majority of files have longer names, the dynamic profile of
 22  * pathname components skews short due to short directory names.
 23  * (E.g. /usr/lib/libsesquipedalianism.so.3.141.)
 24  */
 25 
 26 /*
 27  * Version 1: one byte at a time.  Example of use:
 28  *
 29  * unsigned long hash = init_name_hash;
 30  * while (*p)
 31  *      hash = partial_name_hash(tolower(*p++), hash);
 32  * hash = end_name_hash(hash);
 33  *
 34  * Although this is designed for bytes, fs/hfsplus/unicode.c
 35  * abuses it to hash 16-bit values.
 36  */
 37 
 38 /* Hash courtesy of the R5 hash in reiserfs modulo sign bits */
 39 #define init_name_hash(salt)            (unsigned long)(salt)
 40 
 41 /* partial hash update function. Assume roughly 4 bits per character */
 42 static inline unsigned long
 43 partial_name_hash(unsigned long c, unsigned long prevhash)
 44 {
 45         return (prevhash + (c << 4) + (c >> 4)) * 11;
 46 }
 47 
 48 /*
 49  * Finally: cut down the number of bits to a int value (and try to avoid
 50  * losing bits).  This also has the property (wanted by the dcache)
 51  * that the msbits make a good hash table index.
 52  */
 53 static inline unsigned int end_name_hash(unsigned long hash)
 54 {
 55         return hash_long(hash, 32);
 56 }
 57 
 58 /*
 59  * Version 2: One word (32 or 64 bits) at a time.
 60  * If CONFIG_DCACHE_WORD_ACCESS is defined (meaning <asm/word-at-a-time.h>
 61  * exists, which describes major Linux platforms like x86 and ARM), then
 62  * this computes a different hash function much faster.
 63  *
 64  * If not set, this falls back to a wrapper around the preceding.
 65  */
 66 extern unsigned int __pure full_name_hash(const void *salt, const char *, unsigned int);
 67 
 68 /*
 69  * A hash_len is a u64 with the hash of a string in the low
 70  * half and the length in the high half.
 71  */
 72 #define hashlen_hash(hashlen) ((u32)(hashlen))
 73 #define hashlen_len(hashlen)  ((u32)((hashlen) >> 32))
 74 #define hashlen_create(hash, len) ((u64)(len)<<32 | (u32)(hash))
 75 
 76 /* Return the "hash_len" (hash and length) of a null-terminated string */
 77 extern u64 __pure hashlen_string(const void *salt, const char *name);
 78 
 79 #endif  /* __LINUX_STRINGHASH_H */
 80 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php