Skip to content

Commit bee2d37

Browse files
committed
Move relocation range copies into allocation
1 parent 6fe31fe commit bee2d37

File tree

2 files changed

+53
-27
lines changed

2 files changed

+53
-27
lines changed

src/librustc/mir/interpret/allocation.rs

+50
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,56 @@ impl<Tag> DerefMut for Relocations<Tag> {
693693
}
694694
}
695695

696+
/// A partial, owned list of relocations to transfer into another allocation.
697+
pub struct AllocationRelocations<Tag> {
698+
relative_relocations: Vec<(Size, (Tag, AllocId))>,
699+
}
700+
701+
impl<Tag: Copy, Extra> Allocation<Tag, Extra> {
702+
pub fn prepare_relocation_copy(
703+
&self,
704+
cx: &impl HasDataLayout,
705+
src: Pointer<Tag>,
706+
size: Size,
707+
dest: Pointer<Tag>,
708+
length: u64,
709+
) -> AllocationRelocations<Tag> {
710+
let relocations = self.get_relocations(cx, src, size);
711+
if relocations.is_empty() {
712+
return AllocationRelocations { relative_relocations: Vec::new() };
713+
}
714+
715+
let mut new_relocations = Vec::with_capacity(relocations.len() * (length as usize));
716+
717+
for i in 0..length {
718+
new_relocations.extend(
719+
relocations
720+
.iter()
721+
.map(|&(offset, reloc)| {
722+
// compute offset for current repetition
723+
let dest_offset = dest.offset + (i * size);
724+
(
725+
// shift offsets from source allocation to destination allocation
726+
offset + dest_offset - src.offset,
727+
reloc,
728+
)
729+
})
730+
);
731+
}
732+
733+
AllocationRelocations {
734+
relative_relocations: new_relocations,
735+
}
736+
}
737+
738+
pub fn mark_relocation_range(
739+
&mut self,
740+
relocations: AllocationRelocations<Tag>,
741+
) {
742+
self.relocations.insert_presorted(relocations.relative_relocations);
743+
}
744+
}
745+
696746
////////////////////////////////////////////////////////////////////////////////
697747
// Undefined byte tracking
698748
////////////////////////////////////////////////////////////////////////////////

src/librustc_mir/interpret/memory.rs

+3-27
Original file line numberDiff line numberDiff line change
@@ -808,32 +808,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
808808
// since we don't want to keep any relocations at the target.
809809
// (`get_bytes_with_undef_and_ptr` below checks that there are no
810810
// relocations overlapping the edges; those would not be handled correctly).
811-
let relocations = {
812-
let relocations = self.get(src.alloc_id)?.get_relocations(self, src, size);
813-
if relocations.is_empty() {
814-
// nothing to copy, ignore even the `length` loop
815-
Vec::new()
816-
} else {
817-
let mut new_relocations = Vec::with_capacity(relocations.len() * (length as usize));
818-
for i in 0..length {
819-
new_relocations.extend(
820-
relocations
821-
.iter()
822-
.map(|&(offset, reloc)| {
823-
// compute offset for current repetition
824-
let dest_offset = dest.offset + (i * size);
825-
(
826-
// shift offsets from source allocation to destination allocation
827-
offset + dest_offset - src.offset,
828-
reloc,
829-
)
830-
})
831-
);
832-
}
833-
834-
new_relocations
835-
}
836-
};
811+
let relocations = self.get(src.alloc_id)?
812+
.prepare_relocation_copy(self, src, size, dest, length);
837813

838814
let tcx = self.tcx.tcx;
839815

@@ -880,7 +856,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
880856
// copy definedness to the destination
881857
self.copy_undef_mask(src, dest, size, length)?;
882858
// copy the relocations to the destination
883-
self.get_mut(dest.alloc_id)?.relocations.insert_presorted(relocations);
859+
self.get_mut(dest.alloc_id)?.mark_relocation_range(relocations);
884860

885861
Ok(())
886862
}

0 commit comments

Comments
 (0)