Skip to content

Commit bb9e850

Browse files
committed
std: Get rid of hash_offet in RawTable
This offset is always zero, and we don't consistently take it into account. This is okay, because it's zero, but if it ever changes we're going to have bugs (e.g. in the `dealloc` call, where we don't take it into account). It's better to remove this for now; if we ever have a need for a nonzero offset we can add it back, and handle it properly when we do so.
1 parent d21c023 commit bb9e850

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

src/libstd/collections/hash/table.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -717,26 +717,25 @@ fn calculate_offsets(hashes_size: usize,
717717
(pairs_offset, end_of_pairs, oflo)
718718
}
719719

720-
// Returns a tuple of (minimum required malloc alignment, hash_offset,
720+
// Returns a tuple of (minimum required malloc alignment,
721721
// array_size), from the start of a mallocated array.
722722
fn calculate_allocation(hash_size: usize,
723723
hash_align: usize,
724724
pairs_size: usize,
725725
pairs_align: usize)
726-
-> (usize, usize, usize, bool) {
727-
let hash_offset = 0;
726+
-> (usize, usize, bool) {
728727
let (_, end_of_pairs, oflo) = calculate_offsets(hash_size, pairs_size, pairs_align);
729728

730729
let align = cmp::max(hash_align, pairs_align);
731730

732-
(align, hash_offset, end_of_pairs, oflo)
731+
(align, end_of_pairs, oflo)
733732
}
734733

735734
#[test]
736735
fn test_offset_calculation() {
737-
assert_eq!(calculate_allocation(128, 8, 16, 8), (8, 0, 144, false));
738-
assert_eq!(calculate_allocation(3, 1, 2, 1), (1, 0, 5, false));
739-
assert_eq!(calculate_allocation(6, 2, 12, 4), (4, 0, 20, false));
736+
assert_eq!(calculate_allocation(128, 8, 16, 8), (8, 144, false));
737+
assert_eq!(calculate_allocation(3, 1, 2, 1), (1, 5, false));
738+
assert_eq!(calculate_allocation(6, 2, 12, 4), (4, 20, false));
740739
assert_eq!(calculate_offsets(128, 15, 4), (128, 143, false));
741740
assert_eq!(calculate_offsets(3, 2, 4), (4, 6, false));
742741
assert_eq!(calculate_offsets(6, 12, 4), (8, 20, false));
@@ -768,10 +767,10 @@ impl<K, V> RawTable<K, V> {
768767
// This is great in theory, but in practice getting the alignment
769768
// right is a little subtle. Therefore, calculating offsets has been
770769
// factored out into a different function.
771-
let (alignment, hash_offset, size, oflo) = calculate_allocation(hashes_size,
772-
align_of::<HashUint>(),
773-
pairs_size,
774-
align_of::<(K, V)>());
770+
let (alignment, size, oflo) = calculate_allocation(hashes_size,
771+
align_of::<HashUint>(),
772+
pairs_size,
773+
align_of::<(K, V)>());
775774
assert!(!oflo, "capacity overflow");
776775

777776
// One check for overflow that covers calculation and rounding of size.
@@ -784,7 +783,7 @@ impl<K, V> RawTable<K, V> {
784783
let buffer = Heap.alloc(Layout::from_size_align(size, alignment).unwrap())
785784
.unwrap_or_else(|e| Heap.oom(e));
786785

787-
let hashes = buffer.offset(hash_offset as isize) as *mut HashUint;
786+
let hashes = buffer as *mut HashUint;
788787

789788
RawTable {
790789
capacity_mask: capacity.wrapping_sub(1),
@@ -1183,10 +1182,10 @@ unsafe impl<#[may_dangle] K, #[may_dangle] V> Drop for RawTable<K, V> {
11831182

11841183
let hashes_size = self.capacity() * size_of::<HashUint>();
11851184
let pairs_size = self.capacity() * size_of::<(K, V)>();
1186-
let (align, _, size, oflo) = calculate_allocation(hashes_size,
1187-
align_of::<HashUint>(),
1188-
pairs_size,
1189-
align_of::<(K, V)>());
1185+
let (align, size, oflo) = calculate_allocation(hashes_size,
1186+
align_of::<HashUint>(),
1187+
pairs_size,
1188+
align_of::<(K, V)>());
11901189

11911190
debug_assert!(!oflo, "should be impossible");
11921191

0 commit comments

Comments
 (0)