diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/common/hash.h | 35 |
1 files changed, 0 insertions, 35 deletions
diff --git a/src/common/hash.h b/src/common/hash.h index ebd4125e2..b2538f3ea 100644 --- a/src/common/hash.h +++ b/src/common/hash.h | |||
| @@ -35,41 +35,6 @@ static inline u64 ComputeStructHash64(const T& data) { | |||
| 35 | return ComputeHash64(&data, sizeof(data)); | 35 | return ComputeHash64(&data, sizeof(data)); |
| 36 | } | 36 | } |
| 37 | 37 | ||
| 38 | /// A helper template that ensures the padding in a struct is initialized by memsetting to 0. | ||
| 39 | template <typename T> | ||
| 40 | struct HashableStruct { | ||
| 41 | // In addition to being trivially copyable, T must also have a trivial default constructor, | ||
| 42 | // because any member initialization would be overridden by memset | ||
| 43 | static_assert(std::is_trivial_v<T>, "Type passed to HashableStruct must be trivial"); | ||
| 44 | /* | ||
| 45 | * We use a union because "implicitly-defined copy/move constructor for a union X copies the | ||
| 46 | * object representation of X." and "implicitly-defined copy assignment operator for a union X | ||
| 47 | * copies the object representation (3.9) of X." = Bytewise copy instead of memberwise copy. | ||
| 48 | * This is important because the padding bytes are included in the hash and comparison between | ||
| 49 | * objects. | ||
| 50 | */ | ||
| 51 | union { | ||
| 52 | T state; | ||
| 53 | }; | ||
| 54 | |||
| 55 | HashableStruct() { | ||
| 56 | // Memset structure to zero padding bits, so that they will be deterministic when hashing | ||
| 57 | std::memset(&state, 0, sizeof(T)); | ||
| 58 | } | ||
| 59 | |||
| 60 | bool operator==(const HashableStruct<T>& o) const { | ||
| 61 | return std::memcmp(&state, &o.state, sizeof(T)) == 0; | ||
| 62 | }; | ||
| 63 | |||
| 64 | bool operator!=(const HashableStruct<T>& o) const { | ||
| 65 | return !(*this == o); | ||
| 66 | }; | ||
| 67 | |||
| 68 | std::size_t Hash() const { | ||
| 69 | return Common::ComputeStructHash64(state); | ||
| 70 | } | ||
| 71 | }; | ||
| 72 | |||
| 73 | struct PairHash { | 38 | struct PairHash { |
| 74 | template <class T1, class T2> | 39 | template <class T1, class T2> |
| 75 | std::size_t operator()(const std::pair<T1, T2>& pair) const noexcept { | 40 | std::size_t operator()(const std::pair<T1, T2>& pair) const noexcept { |