diff options
author | Bernard Normier <bernard@zeroc.com> | 2017-03-18 14:33:42 -0400 |
---|---|---|
committer | Bernard Normier <bernard@zeroc.com> | 2017-03-18 14:33:42 -0400 |
commit | e91630e6cd758212d79b220ec28d9c91e42b42a5 (patch) | |
tree | 2eafbe10754257f828bc852abbb777a17aa34fda /python/modules | |
parent | Add connection hashing in Python (diff) | |
download | ice-e91630e6cd758212d79b220ec28d9c91e42b42a5.tar.bz2 ice-e91630e6cd758212d79b220ec28d9c91e42b42a5.tar.xz ice-e91630e6cd758212d79b220ec28d9c91e42b42a5.zip |
Updated pointer hash
Diffstat (limited to 'python/modules')
-rw-r--r-- | python/modules/IcePy/Connection.cpp | 60 |
1 files changed, 46 insertions, 14 deletions
diff --git a/python/modules/IcePy/Connection.cpp b/python/modules/IcePy/Connection.cpp index 34d296307f2..7375ff71106 100644 --- a/python/modules/IcePy/Connection.cpp +++ b/python/modules/IcePy/Connection.cpp @@ -25,6 +25,51 @@ using namespace std; using namespace IcePy; +namespace +{ + +// P = sizeof(void*), L = sizeof(long) +template<int P, int L> struct Hasher; + +template<> +struct Hasher<4, 4> +{ + long operator()(void* ptr) const + { + return reinterpret_cast<long>(ptr); + } +}; + +template<> +struct Hasher<8, 8> +{ + long operator()(void* ptr) const + { + return reinterpret_cast<long>(ptr); + } +}; + +template<> +struct Hasher<8, 4> +{ + long operator()(void* ptr) const + { + intptr_t v = reinterpret_cast<intptr_t>(ptr); + + // Eliminate lower 4 bits as objecs are usually aligned on 16 bytes boundaries, + // then eliminate upper bits + return (v >> 4) & 0xFFFFFFFF; + } +}; + +long +hashPointer(void* ptr) +{ + return Hasher<sizeof(void*), sizeof(long)>()(ptr); +} + +} + namespace IcePy { @@ -297,20 +342,7 @@ extern "C" static long connectionHash(ConnectionObject* self) { -#if defined(_MSC_VER) && defined(_WIN64) - // - // Hash a 64-bit pointer into a 32-bit long. - // - unsigned long long addr = reinterpret_cast<long>((*self->connection).get()); - unsigned long low = static_cast<unsigned long>(addr); - unsigned long hi = static_cast<unsigned long>(addr >> 32); - unsigned long hash = 5381; - hash = ((hash << 5) + hash) ^ (2654435761u * low); - hash = ((hash << 5) + hash) ^ (2654435761u * hi); - return static_cast<long>(hash); -#else - return reinterpret_cast<long>((*self->connection).get()); -#endif + return hashPointer((*self->connection).get()); } #ifdef WIN32 |