From 0064467516248fb3b28bdbd7d0f4e54b861627cf Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Wed, 4 Nov 2009 15:00:28 -0800 Subject: [PATCH] hash: Implement hash function for pointer values. This will be used by an upcoming commit, and it's generally useful to have around. --- lib/hash.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) 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 */ -- 2.30.2