![]() |
|
|||
0001 /* SPDX-License-Identifier: GPL-2.0 */ 0002 #ifndef __LINUX_STRINGHASH_H 0003 #define __LINUX_STRINGHASH_H 0004 0005 #include <linux/compiler.h> /* For __pure */ 0006 #include <linux/types.h> /* For u32, u64 */ 0007 #include <linux/hash.h> 0008 0009 /* 0010 * Routines for hashing strings of bytes to a 32-bit hash value. 0011 * 0012 * These hash functions are NOT GUARANTEED STABLE between kernel 0013 * versions, architectures, or even repeated boots of the same kernel. 0014 * (E.g. they may depend on boot-time hardware detection or be 0015 * deliberately randomized.) 0016 * 0017 * They are also not intended to be secure against collisions caused by 0018 * malicious inputs; much slower hash functions are required for that. 0019 * 0020 * They are optimized for pathname components, meaning short strings. 0021 * Even if a majority of files have longer names, the dynamic profile of 0022 * pathname components skews short due to short directory names. 0023 * (E.g. /usr/lib/libsesquipedalianism.so.3.141.) 0024 */ 0025 0026 /* 0027 * Version 1: one byte at a time. Example of use: 0028 * 0029 * unsigned long hash = init_name_hash; 0030 * while (*p) 0031 * hash = partial_name_hash(tolower(*p++), hash); 0032 * hash = end_name_hash(hash); 0033 * 0034 * Although this is designed for bytes, fs/hfsplus/unicode.c 0035 * abuses it to hash 16-bit values. 0036 */ 0037 0038 /* Hash courtesy of the R5 hash in reiserfs modulo sign bits */ 0039 #define init_name_hash(salt) (unsigned long)(salt) 0040 0041 /* partial hash update function. Assume roughly 4 bits per character */ 0042 static inline unsigned long 0043 partial_name_hash(unsigned long c, unsigned long prevhash) 0044 { 0045 return (prevhash + (c << 4) + (c >> 4)) * 11; 0046 } 0047 0048 /* 0049 * Finally: cut down the number of bits to a int value (and try to avoid 0050 * losing bits). This also has the property (wanted by the dcache) 0051 * that the msbits make a good hash table index. 0052 */ 0053 static inline unsigned int end_name_hash(unsigned long hash) 0054 { 0055 return hash_long(hash, 32); 0056 } 0057 0058 /* 0059 * Version 2: One word (32 or 64 bits) at a time. 0060 * If CONFIG_DCACHE_WORD_ACCESS is defined (meaning <asm/word-at-a-time.h> 0061 * exists, which describes major Linux platforms like x86 and ARM), then 0062 * this computes a different hash function much faster. 0063 * 0064 * If not set, this falls back to a wrapper around the preceding. 0065 */ 0066 extern unsigned int __pure full_name_hash(const void *salt, const char *, unsigned int); 0067 0068 /* 0069 * A hash_len is a u64 with the hash of a string in the low 0070 * half and the length in the high half. 0071 */ 0072 #define hashlen_hash(hashlen) ((u32)(hashlen)) 0073 #define hashlen_len(hashlen) ((u32)((hashlen) >> 32)) 0074 #define hashlen_create(hash, len) ((u64)(len)<<32 | (u32)(hash)) 0075 0076 /* Return the "hash_len" (hash and length) of a null-terminated string */ 0077 extern u64 __pure hashlen_string(const void *salt, const char *name); 0078 0079 #endif /* __LINUX_STRINGHASH_H */
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.1.0 LXR engine. The LXR team |
![]() ![]() |