1 #include <string.h> 2 #include "hash.h" 3 hash_mem(const void * data,unsigned int len)4unsigned int hash_mem(const void *data, unsigned int len) 5 { 6 unsigned int h; 7 unsigned int i; 8 9 h = len; 10 11 for (i = 0; i < len; ++i) 12 h = ((h >> 27) ^ (h << 5)) ^ ((const unsigned char *)data)[i]; 13 14 return h; 15 } 16 hash_string(const char * value)17unsigned int hash_string(const char *value) 18 { 19 return hash_mem(value, strlen(value)); 20 } 21 hash_u32(uint32_t value)22unsigned int hash_u32(uint32_t value) 23 { 24 return value * 2654435761UL; 25 } 26 hash_u64(uint64_t value)27unsigned int hash_u64(uint64_t value) 28 { 29 return hash_u32(value & 0xffffffff) ^ hash_u32(value >> 32); 30 } 31 hash_pointer(void * value)32unsigned int hash_pointer(void *value) 33 { 34 if (sizeof(value) == sizeof(uint64_t)) 35 return hash_u64((long)value); 36 return hash_u32((long)value); 37 } 38