diff options
author | Ben Pfaff <blp@nicira.com> | 2009-11-04 15:00:28 -0800 |
---|---|---|
committer | Ben Pfaff <blp@nicira.com> | 2009-11-04 15:00:28 -0800 |
commit | 0064467516248fb3b28bdbd7d0f4e54b861627cf (patch) | |
tree | b8419228d601bc6b043111ecef70c394f16b747d /lib/hash.h | |
parent | ec6fde61c85ff6a57b05d422a9d8b5ef679ad928 (diff) |
hash: Implement hash function for pointer values.
This will be used by an upcoming commit, and it's generally useful to
have around.
Diffstat (limited to 'lib/hash.h')
-rw-r--r-- | lib/hash.h | 13 |
1 files changed, 13 insertions, 0 deletions
@@ -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 */ |