Fix: stale dnode caching instance cause locked-up upon accessing (#52)
[lunaix-os.git] / lunaix-os / libs / hash.c
1 #include <klibc/hash.h>
2 #include <lunaix/compiler.h>
3
4 /**
5  * @brief Simple string hash function (sdbm)
6  *
7  * ref: http://www.cse.yorku.ca/~oz/hash.html
8  * 
9  * sdbm has lower standard deviation in bucket distribution
10  * than djb2 (previously used) for low bucket size (16, 32).
11  *
12  * @param str
13  * @return unsigned int
14  */
15 u32_t _weak
16 strhash_32(const char* str, u32_t truncate_to)
17 {
18     if (!str)
19         return 0;
20
21     u32_t hash = 0;
22     int c;
23
24     while ((c = *str++))
25         hash = (hash << 6) + (hash << 16) + c - hash;
26
27     return hash >> (HASH_SIZE_BITS - truncate_to);
28 }