From: Ben Pfaff Date: Wed, 4 Nov 2009 23:00:28 +0000 (-0800) Subject: hash: Implement hash function for pointer values. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0064467516248fb3b28bdbd7d0f4e54b861627cf;p=openvswitch hash: Implement hash function for pointer values. This will be used by an upcoming commit, and it's generally useful to have around. --- diff --git a/lib/hash.h b/lib/hash.h index 5485f8ce..4ad14aff 100644 --- a/lib/hash.h +++ b/lib/hash.h @@ -68,4 +68,17 @@ static inline uint32_t hash_int(uint32_t x, uint32_t basis) return x + basis; } +static inline uint32_t hash_pointer(const void *p, uint32_t basis) +{ + /* Often pointers are hashed simply by casting to integer type, but that + * has pitfalls since the lower bits of a pointer are often all 0 for + * alignment reasons. It's hard to guess where the entropy really is, so + * we give up here and just use a high-quality hash function. + * + * The double cast suppresses a warning on 64-bit systems about casting to + * an integer to different size. That's OK in this case, since most of the + * entropy in the pointer is almost certainly in the lower 32 bits. */ + return hash_int((uint32_t) (uintptr_t) p, basis); +} + #endif /* hash.h */