hash.cc (569B)
1 #include "mutextable" 2 3 /* 4 * How to hash a pointer into an unsigned. Based on the famous hashpjw() 5 * function. 6 */ 7 8 // #define DEBUG 9 unsigned MutexTable::hash(void *obj) { 10 unsigned v = 0; 11 unsigned char *cp = (unsigned char *)obj; 12 for (size_t i = 0; i < sizeof(obj); i++) { 13 v <<= 4; 14 v |= *cp++; 15 if (unsigned g = v & 0xf0000000) { 16 v = v ^ ( g >> 24 ); 17 v = v ^ g; 18 } 19 } 20 v %= _size; 21 22 # ifdef DEBUG 23 debugmsg("Pointer " << obj << " hashed to bucket " << v << 24 " (given size " << _size << ")\n"); 25 # endif 26 27 return v; 28 }