Skip to content

Commit b1926e7

Browse files
committed
add Allocation::mem_pos
1 parent 7996f37 commit b1926e7

File tree

5 files changed

+32
-34
lines changed

5 files changed

+32
-34
lines changed

src/librustc/mir/interpret/allocation.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,9 @@ pub struct Allocation<Tag = (),Extra = ()> {
3939
relocations: Relocations<Tag>,
4040
/// Denotes which part of this allocation is initialized.
4141
undef_mask: UndefMask,
42-
/// The size of the allocation. Currently, must always equal `bytes.len()`.
43-
pub size: Size,
44-
/// The alignment of the allocation to detect unaligned reads.
45-
pub align: Align,
42+
/// The position of the allocation.
43+
/// Currently, the size must always equal `bytes.len()`.
44+
pub mem_pos: MemoryPosition,
4645
/// `true` if the allocation is mutable.
4746
/// Also used by codegen to determine if a static should be put into mutable memory,
4847
/// which happens for `static mut` and `static` with interior mutability.
@@ -101,12 +100,12 @@ impl<Tag> Allocation<Tag> {
101100
pub fn from_bytes<'a>(slice: impl Into<Cow<'a, [u8]>>, align: Align) -> Self {
102101
let bytes = slice.into().into_owned();
103102
let size = Size::from_bytes(bytes.len() as u64);
103+
let mem_pos = MemoryPosition::new(size, align);
104104
Self {
105105
bytes,
106106
relocations: Relocations::new(),
107107
undef_mask: UndefMask::new(size, true),
108-
size,
109-
align,
108+
mem_pos,
110109
mutability: Mutability::Immutable,
111110
extra: (),
112111
}
@@ -122,8 +121,7 @@ impl<Tag> Allocation<Tag> {
122121
bytes: vec![0; mem_pos.size.bytes() as usize],
123122
relocations: Relocations::new(),
124123
undef_mask: UndefMask::new(mem_pos.size, false),
125-
size: mem_pos.size,
126-
align: mem_pos.align,
124+
mem_pos: mem_pos,
127125
mutability: Mutability::Mutable,
128126
extra: (),
129127
}
@@ -139,7 +137,6 @@ impl Allocation<(), ()> {
139137
) -> Allocation<T, E> {
140138
Allocation {
141139
bytes: self.bytes,
142-
size: self.size,
143140
relocations: Relocations::from_presorted(
144141
self.relocations.iter()
145142
// The allocations in the relocations (pointers stored *inside* this allocation)
@@ -151,7 +148,7 @@ impl Allocation<(), ()> {
151148
.collect()
152149
),
153150
undef_mask: self.undef_mask,
154-
align: self.align,
151+
mem_pos: self.mem_pos,
155152
mutability: self.mutability,
156153
extra,
157154
}
@@ -161,7 +158,7 @@ impl Allocation<(), ()> {
161158
/// Raw accessors. Provide access to otherwise private bytes.
162159
impl<Tag, Extra> Allocation<Tag, Extra> {
163160
pub fn len(&self) -> usize {
164-
self.size.bytes() as usize
161+
self.mem_pos.size.bytes() as usize
165162
}
166163

167164
/// Looks at a slice which may describe undefined bytes or describe a relocation. This differs

src/librustc_codegen_llvm/common.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,9 @@ impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
280280
Some(GlobalAlloc::Memory(alloc)) => {
281281
let init = const_alloc_to_llvm(self, alloc);
282282
if alloc.mutability == Mutability::Mutable {
283-
self.static_addr_of_mut(init, alloc.align, None)
283+
self.static_addr_of_mut(init, alloc.mem_pos.align, None)
284284
} else {
285-
self.static_addr_of(init, alloc.align, None)
285+
self.static_addr_of(init, alloc.mem_pos.align, None)
286286
}
287287
}
288288
Some(GlobalAlloc::Function(fn_instance)) => {
@@ -314,14 +314,14 @@ impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
314314
alloc: &Allocation,
315315
offset: Size,
316316
) -> PlaceRef<'tcx, &'ll Value> {
317-
assert_eq!(alloc.align, layout.pref_pos.align.abi);
317+
assert_eq!(alloc.mem_pos.align, layout.pref_pos.align.abi);
318318
let llty = self.type_ptr_to(layout.llvm_type(self));
319319
let llval = if layout.pref_pos.size == Size::ZERO {
320-
let llval = self.const_usize(alloc.align.bytes());
320+
let llval = self.const_usize(alloc.mem_pos.align.bytes());
321321
unsafe { llvm::LLVMConstIntToPtr(llval, llty) }
322322
} else {
323323
let init = const_alloc_to_llvm(self, alloc);
324-
let base_addr = self.static_addr_of(init, alloc.align, None);
324+
let base_addr = self.static_addr_of(init, alloc.mem_pos.align, None);
325325

326326
let llval = unsafe { llvm::LLVMConstInBoundsGEP(
327327
self.const_bitcast(base_addr, self.type_i8p()),

src/librustc_mir/interpret/memory.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
208208
let new_ptr = self.allocate(new_mem_pos, kind);
209209
let old_size = match old_mem_pos {
210210
Some(old_mem_pos) => old_mem_pos.size,
211-
None => self.get_raw(ptr.alloc_id)?.size,
211+
None => self.get_raw(ptr.alloc_id)?.mem_pos.size,
212212
};
213213
self.copy(
214214
ptr,
@@ -268,21 +268,23 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
268268
format!("{:?}", kind),
269269
))
270270
}
271+
272+
let bytes_mem_pos = alloc.mem_pos;
273+
271274
if let Some(mem_pos) = old_mem_pos {
272-
if mem_pos != MemoryPosition::new(alloc.size, alloc.align) {
273-
let got_mem_pos = MemoryPosition::new(alloc.size, alloc.align);
274-
throw_unsup!(IncorrectAllocationInformation(mem_pos, got_mem_pos))
275+
if mem_pos != bytes_mem_pos {
276+
throw_unsup!(IncorrectAllocationInformation(mem_pos, bytes_mem_pos))
275277
}
276278
}
277279

278280
// Let the machine take some extra action
279-
let size = alloc.size;
281+
let size = alloc.mem_pos.size;
280282
AllocationExtra::memory_deallocated(&mut alloc, ptr, size)?;
281283

282284
// Don't forget to remember size and align of this now-dead allocation
283285
let old = self.dead_alloc_map.insert(
284286
ptr.alloc_id,
285-
(alloc.size, alloc.align)
287+
(alloc.mem_pos.size, alloc.mem_pos.align)
286288
);
287289
if old.is_some() {
288290
bug!("Nothing can be deallocated twice");
@@ -561,7 +563,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
561563
// a) cause cycles in case `id` refers to a static
562564
// b) duplicate a static's allocation in miri
563565
if let Some((_, alloc)) = self.alloc_map.get(id) {
564-
return Ok((alloc.size, alloc.align));
566+
return Ok((alloc.mem_pos.size, alloc.mem_pos.align));
565567
}
566568

567569
// # Function pointers
@@ -589,7 +591,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
589591
Some(GlobalAlloc::Memory(alloc)) =>
590592
// Need to duplicate the logic here, because the global allocations have
591593
// different associated types than the interpreter-local ones.
592-
Ok((alloc.size, alloc.align)),
594+
Ok((alloc.mem_pos.size, alloc.mem_pos.align)),
593595
Some(GlobalAlloc::Function(_)) =>
594596
bug!("We already checked function pointers above"),
595597
// The rest must be dead.
@@ -651,7 +653,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
651653
let prefix_len = msg.len();
652654
let mut relocations = vec![];
653655

654-
for i in 0..alloc.size.bytes() {
656+
for i in 0..alloc.mem_pos.size.bytes() {
655657
let i = Size::from_bytes(i);
656658
if let Some(&(_, target_id)) = alloc.relocations().get(&i) {
657659
if allocs_seen.insert(target_id) {
@@ -675,8 +677,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
675677
trace!(
676678
"{}({} bytes, alignment {}){}",
677679
msg,
678-
alloc.size.bytes(),
679-
alloc.align.bytes(),
680+
alloc.mem_pos.size.bytes(),
681+
alloc.mem_pos.align.bytes(),
680682
extra
681683
);
682684

src/librustc_mir/interpret/snapshot.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc::mir::interpret::{
1515
};
1616

1717
use rustc::ty::{self, TyCtxt};
18-
use rustc::ty::layout::{Align, Size};
18+
use rustc::ty::layout::MemoryPosition;
1919
use rustc_data_structures::fx::FxHashSet;
2020
use rustc_index::vec::IndexVec;
2121
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
@@ -275,8 +275,7 @@ struct AllocationSnapshot<'a> {
275275
bytes: &'a [u8],
276276
relocations: Relocations<(), AllocIdSnapshot<'a>>,
277277
undef_mask: &'a UndefMask,
278-
align: &'a Align,
279-
size: &'a Size,
278+
mem_pos: &'a MemoryPosition,
280279
mutability: &'a Mutability,
281280
}
282281

@@ -287,8 +286,7 @@ impl<'a, Ctx> Snapshot<'a, Ctx> for &'a Allocation
287286

288287
fn snapshot(&self, ctx: &'a Ctx) -> Self::Item {
289288
let Allocation {
290-
size,
291-
align,
289+
mem_pos,
292290
mutability,
293291
extra: (),
294292
..
@@ -306,8 +304,7 @@ impl<'a, Ctx> Snapshot<'a, Ctx> for &'a Allocation
306304
AllocationSnapshot {
307305
bytes,
308306
undef_mask,
309-
align,
310-
size,
307+
mem_pos,
311308
mutability,
312309
relocations: relocations.snapshot(ctx),
313310
}

src/librustc_target/abi/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,8 @@ impl AddAssign for LayoutPositionPref {
601601
/// An aligned size.
602602
/// Better name appreciated.
603603
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
604+
// I am opposed to adding these, therefore they are in a separate section, and this note.
605+
#[derive(PartialOrd, Ord)]
604606
pub struct MemoryPosition {
605607
/// A size not rounded up to alignment
606608
pub size: Size,

0 commit comments

Comments
 (0)