Skip to content

Commit 7962b5a

Browse files
committed
remove size field from Allocation
1 parent 44ec846 commit 7962b5a

File tree

3 files changed

+18
-19
lines changed

3 files changed

+18
-19
lines changed

compiler/rustc_middle/src/mir/interpret/allocation.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ pub struct Allocation<Tag = (), Extra = ()> {
2727
relocations: Relocations<Tag>,
2828
/// Denotes which part of this allocation is initialized.
2929
init_mask: InitMask,
30-
/// The size of the allocation. Currently, must always equal `bytes.len()`.
31-
pub size: Size,
3230
/// The alignment of the allocation to detect unaligned reads.
3331
/// (`Align` guarantees that this is a power of two.)
3432
pub align: Align,
@@ -94,7 +92,6 @@ impl<Tag> Allocation<Tag> {
9492
bytes,
9593
relocations: Relocations::new(),
9694
init_mask: InitMask::new(size, true),
97-
size,
9895
align,
9996
mutability: Mutability::Not,
10097
extra: (),
@@ -110,7 +107,6 @@ impl<Tag> Allocation<Tag> {
110107
bytes: vec![0; size.bytes_usize()],
111108
relocations: Relocations::new(),
112109
init_mask: InitMask::new(size, false),
113-
size,
114110
align,
115111
mutability: Mutability::Mut,
116112
extra: (),
@@ -127,7 +123,6 @@ impl Allocation<(), ()> {
127123
) -> Allocation<T, E> {
128124
Allocation {
129125
bytes: self.bytes,
130-
size: self.size,
131126
relocations: Relocations::from_presorted(
132127
self.relocations
133128
.iter()
@@ -150,7 +145,11 @@ impl Allocation<(), ()> {
150145
/// Raw accessors. Provide access to otherwise private bytes.
151146
impl<Tag, Extra> Allocation<Tag, Extra> {
152147
pub fn len(&self) -> usize {
153-
self.size.bytes_usize()
148+
self.bytes.len()
149+
}
150+
151+
pub fn size(&self) -> Size {
152+
Size::from_bytes(self.len())
154153
}
155154

156155
/// Looks at a slice which may describe uninitialized bytes or describe a relocation. This differs

compiler/rustc_mir/src/interpret/memory.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
244244
let new_ptr = self.allocate(new_size, new_align, kind);
245245
let old_size = match old_size_and_align {
246246
Some((size, _align)) => size,
247-
None => self.get_raw(ptr.alloc_id)?.size,
247+
None => self.get_raw(ptr.alloc_id)?.size(),
248248
};
249249
self.copy(ptr, new_ptr, old_size.min(new_size), /*nonoverlapping*/ true)?;
250250
self.deallocate(ptr, old_size_and_align, kind)?;
@@ -306,11 +306,11 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
306306
);
307307
}
308308
if let Some((size, align)) = old_size_and_align {
309-
if size != alloc.size || align != alloc.align {
309+
if size != alloc.size() || align != alloc.align {
310310
throw_ub_format!(
311311
"incorrect layout on deallocation: {} has size {} and alignment {}, but gave size {} and alignment {}",
312312
ptr.alloc_id,
313-
alloc.size.bytes(),
313+
alloc.size().bytes(),
314314
alloc.align.bytes(),
315315
size.bytes(),
316316
align.bytes(),
@@ -319,11 +319,11 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
319319
}
320320

321321
// Let the machine take some extra action
322-
let size = alloc.size;
322+
let size = alloc.size();
323323
AllocationExtra::memory_deallocated(&mut alloc, ptr, size)?;
324324

325325
// Don't forget to remember size and align of this now-dead allocation
326-
let old = self.dead_alloc_map.insert(ptr.alloc_id, (alloc.size, alloc.align));
326+
let old = self.dead_alloc_map.insert(ptr.alloc_id, (alloc.size(), alloc.align));
327327
if old.is_some() {
328328
bug!("Nothing can be deallocated twice");
329329
}
@@ -586,7 +586,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
586586
// a) cause cycles in case `id` refers to a static
587587
// b) duplicate a global's allocation in miri
588588
if let Some((_, alloc)) = self.alloc_map.get(id) {
589-
return Ok((alloc.size, alloc.align));
589+
return Ok((alloc.size(), alloc.align));
590590
}
591591

592592
// # Function pointers
@@ -614,7 +614,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
614614
Some(GlobalAlloc::Memory(alloc)) => {
615615
// Need to duplicate the logic here, because the global allocations have
616616
// different associated types than the interpreter-local ones.
617-
Ok((alloc.size, alloc.align))
617+
Ok((alloc.size(), alloc.align))
618618
}
619619
Some(GlobalAlloc::Function(_)) => bug!("We already checked function pointers above"),
620620
// The rest must be dead.

compiler/rustc_mir/src/util/pretty.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -776,8 +776,8 @@ pub struct RenderAllocation<'a, 'tcx, Tag, Extra> {
776776
impl<Tag: Copy + Debug, Extra> std::fmt::Display for RenderAllocation<'a, 'tcx, Tag, Extra> {
777777
fn fmt(&self, w: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
778778
let RenderAllocation { tcx, alloc } = *self;
779-
write!(w, "size: {}, align: {})", alloc.size.bytes(), alloc.align.bytes())?;
780-
if alloc.size == Size::ZERO {
779+
write!(w, "size: {}, align: {})", alloc.size().bytes(), alloc.align.bytes())?;
780+
if alloc.size() == Size::ZERO {
781781
// We are done.
782782
return write!(w, " {{}}");
783783
}
@@ -822,9 +822,9 @@ fn write_allocation_bytes<Tag: Copy + Debug, Extra>(
822822
w: &mut dyn std::fmt::Write,
823823
prefix: &str,
824824
) -> std::fmt::Result {
825-
let num_lines = alloc.size.bytes_usize().saturating_sub(BYTES_PER_LINE);
825+
let num_lines = alloc.size().bytes_usize().saturating_sub(BYTES_PER_LINE);
826826
// Number of chars needed to represent all line numbers.
827-
let pos_width = format!("{:x}", alloc.size.bytes()).len();
827+
let pos_width = format!("{:x}", alloc.size().bytes()).len();
828828

829829
if num_lines > 0 {
830830
write!(w, "{}0x{:02$x} │ ", prefix, 0, pos_width)?;
@@ -845,7 +845,7 @@ fn write_allocation_bytes<Tag: Copy + Debug, Extra>(
845845
}
846846
};
847847

848-
while i < alloc.size {
848+
while i < alloc.size() {
849849
// The line start already has a space. While we could remove that space from the line start
850850
// printing and unconditionally print a space here, that would cause the single-line case
851851
// to have a single space before it, which looks weird.
@@ -929,7 +929,7 @@ fn write_allocation_bytes<Tag: Copy + Debug, Extra>(
929929
i += Size::from_bytes(1);
930930
}
931931
// Print a new line header if the next line still has some bytes to print.
932-
if i == line_start + Size::from_bytes(BYTES_PER_LINE) && i != alloc.size {
932+
if i == line_start + Size::from_bytes(BYTES_PER_LINE) && i != alloc.size() {
933933
line_start = write_allocation_newline(w, line_start, &ascii, pos_width, prefix)?;
934934
ascii.clear();
935935
}

0 commit comments

Comments
 (0)