Commit 4eb81046 by Abseil Team Committed by Copybara-Service

Static cast instead of reinterpret cast raw hash set slots as casting from void*…

Static cast instead of reinterpret cast raw hash set slots as casting from void* to T* is well defined

PiperOrigin-RevId: 648352837
Change-Id: I082cd0c007706ae8baa8f26cdc85d51b69bffd54
parent a7c5f985
...@@ -878,7 +878,7 @@ struct GroupPortableImpl { ...@@ -878,7 +878,7 @@ struct GroupPortableImpl {
// Note: this includes: kEmpty, kDeleted, kSentinel. // Note: this includes: kEmpty, kDeleted, kSentinel.
// It is useful in contexts when kSentinel is not present. // It is useful in contexts when kSentinel is not present.
auto MaskNonFull() const { auto MaskNonFull() const {
return BitMask<uint64_t, kWidth, 3>(ctrl & kMsbs8Bytes); return BitMask<uint64_t, kWidth, 3>(ctrl & kMsbs8Bytes);
} }
NonIterableBitMask<uint64_t, kWidth, 3> MaskEmptyOrDeleted() const { NonIterableBitMask<uint64_t, kWidth, 3> MaskEmptyOrDeleted() const {
...@@ -1134,9 +1134,7 @@ class GrowthInfo { ...@@ -1134,9 +1134,7 @@ class GrowthInfo {
// Returns true if the table satisfies two properties: // Returns true if the table satisfies two properties:
// 1. Guaranteed to have no kDeleted slots. // 1. Guaranteed to have no kDeleted slots.
// 2. There is no growth left. // 2. There is no growth left.
bool HasNoGrowthLeftAndNoDeleted() const { bool HasNoGrowthLeftAndNoDeleted() const { return growth_left_info_ == 0; }
return growth_left_info_ == 0;
}
// Returns true if table guaranteed to have no k // Returns true if table guaranteed to have no k
bool HasNoDeleted() const { bool HasNoDeleted() const {
...@@ -1144,9 +1142,7 @@ class GrowthInfo { ...@@ -1144,9 +1142,7 @@ class GrowthInfo {
} }
// Returns the number of elements left to grow. // Returns the number of elements left to grow.
size_t GetGrowthLeft() const { size_t GetGrowthLeft() const { return growth_left_info_ & kGrowthLeftMask; }
return growth_left_info_ & kGrowthLeftMask;
}
private: private:
static constexpr size_t kGrowthLeftMask = ((~size_t{}) >> 1); static constexpr size_t kGrowthLeftMask = ((~size_t{}) >> 1);
...@@ -1421,8 +1417,8 @@ class CommonFields : public CommonFieldsGenerationInfo { ...@@ -1421,8 +1417,8 @@ class CommonFields : public CommonFieldsGenerationInfo {
should_rehash_for_bug_detection_on_insert(control(), capacity()); should_rehash_for_bug_detection_on_insert(control(), capacity());
} }
bool should_rehash_for_bug_detection_on_move() const { bool should_rehash_for_bug_detection_on_move() const {
return CommonFieldsGenerationInfo:: return CommonFieldsGenerationInfo::should_rehash_for_bug_detection_on_move(
should_rehash_for_bug_detection_on_move(control(), capacity()); control(), capacity());
} }
void reset_reserved_growth(size_t reservation) { void reset_reserved_growth(size_t reservation) {
CommonFieldsGenerationInfo::reset_reserved_growth(reservation, size()); CommonFieldsGenerationInfo::reset_reserved_growth(reservation, size());
...@@ -1855,8 +1851,8 @@ constexpr size_t BackingArrayAlignment(size_t align_of_slot) { ...@@ -1855,8 +1851,8 @@ constexpr size_t BackingArrayAlignment(size_t align_of_slot) {
// Returns the address of the ith slot in slots where each slot occupies // Returns the address of the ith slot in slots where each slot occupies
// slot_size. // slot_size.
inline void* SlotAddress(void* slot_array, size_t slot, size_t slot_size) { inline void* SlotAddress(void* slot_array, size_t slot, size_t slot_size) {
return reinterpret_cast<void*>(reinterpret_cast<char*>(slot_array) + return static_cast<void*>(static_cast<char*>(slot_array) +
(slot * slot_size)); (slot * slot_size));
} }
// Iterates over all full slots and calls `cb(const ctrl_t*, SlotType*)`. // Iterates over all full slots and calls `cb(const ctrl_t*, SlotType*)`.
...@@ -2100,8 +2096,8 @@ class HashSetResizeHelper { ...@@ -2100,8 +2096,8 @@ class HashSetResizeHelper {
using slot_type = typename PolicyTraits::slot_type; using slot_type = typename PolicyTraits::slot_type;
assert(is_single_group(c.capacity())); assert(is_single_group(c.capacity()));
auto* new_slots = reinterpret_cast<slot_type*>(c.slot_array()); auto* new_slots = static_cast<slot_type*>(c.slot_array());
auto* old_slots_ptr = reinterpret_cast<slot_type*>(old_slots()); auto* old_slots_ptr = static_cast<slot_type*>(old_slots());
size_t shuffle_bit = old_capacity_ / 2 + 1; size_t shuffle_bit = old_capacity_ / 2 + 1;
for (size_t i = 0; i < old_capacity_; ++i) { for (size_t i = 0; i < old_capacity_; ++i) {
...@@ -3675,8 +3671,7 @@ class raw_hash_set { ...@@ -3675,8 +3671,7 @@ class raw_hash_set {
insert_slot(to_slot(resize_helper.old_soo_data())); insert_slot(to_slot(resize_helper.old_soo_data()));
return; return;
} else { } else {
auto* old_slots = auto* old_slots = static_cast<slot_type*>(resize_helper.old_slots());
reinterpret_cast<slot_type*>(resize_helper.old_slots());
size_t total_probe_length = 0; size_t total_probe_length = 0;
for (size_t i = 0; i != resize_helper.old_capacity(); ++i) { for (size_t i = 0; i != resize_helper.old_capacity(); ++i) {
if (IsFull(resize_helper.old_ctrl()[i])) { if (IsFull(resize_helper.old_ctrl()[i])) {
...@@ -3692,9 +3687,7 @@ class raw_hash_set { ...@@ -3692,9 +3687,7 @@ class raw_hash_set {
// Casting directly from e.g. char* to slot_type* can cause compilation errors // Casting directly from e.g. char* to slot_type* can cause compilation errors
// on objective-C. This function converts to void* first, avoiding the issue. // on objective-C. This function converts to void* first, avoiding the issue.
static slot_type* to_slot(void* buf) { static slot_type* to_slot(void* buf) { return static_cast<slot_type*>(buf); }
return reinterpret_cast<slot_type*>(buf);
}
// Requires that lhs does not have a full SOO slot. // Requires that lhs does not have a full SOO slot.
static void move_common(bool that_is_full_soo, allocator_type& rhs_alloc, static void move_common(bool that_is_full_soo, allocator_type& rhs_alloc,
...@@ -3735,7 +3728,7 @@ class raw_hash_set { ...@@ -3735,7 +3728,7 @@ class raw_hash_set {
} }
} }
template<bool propagate_alloc> template <bool propagate_alloc>
raw_hash_set& assign_impl(raw_hash_set&& that) { raw_hash_set& assign_impl(raw_hash_set&& that) {
// We don't bother checking for this/that aliasing. We just need to avoid // We don't bother checking for this/that aliasing. We just need to avoid
// breaking the invariants in that case. // breaking the invariants in that case.
...@@ -3880,8 +3873,7 @@ class raw_hash_set { ...@@ -3880,8 +3873,7 @@ class raw_hash_set {
} }
// We only do validation for small tables so that it's constant time. // We only do validation for small tables so that it's constant time.
if (capacity() > 16) return; if (capacity() > 16) return;
IterateOverFullSlots( IterateOverFullSlots(common(), slot_array(), assert_consistent);
common(), slot_array(), assert_consistent);
#endif #endif
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment