@@ -329,27 +329,41 @@ pub trait Machine<'tcx>: Sized {
329329 ptr : Pointer < Self :: Provenance > ,
330330 ) -> Option < ( AllocId , Size , Self :: ProvenanceExtra ) > ;
331331
332- /// Called to adjust allocations to the Provenance and AllocExtra of this machine.
332+ /// Called to adjust global allocations to the Provenance and AllocExtra of this machine.
333333 ///
334334 /// If `alloc` contains pointers, then they are all pointing to globals.
335335 ///
336- /// The way we construct allocations is to always first construct it without extra and then add
337- /// the extra. This keeps uniform code paths for handling both allocations created by CTFE for
338- /// globals, and allocations created by Miri during evaluation.
339- ///
340- /// `kind` is the kind of the allocation being adjusted; it can be `None` when
341- /// it's a global and `GLOBAL_KIND` is `None`.
342- ///
343336 /// This should avoid copying if no work has to be done! If this returns an owned
344337 /// allocation (because a copy had to be done to adjust things), machine memory will
345338 /// cache the result. (This relies on `AllocMap::get_or` being able to add the
346339 /// owned allocation to the map even when the map is shared.)
347- fn adjust_allocation < ' b > (
340+ fn adjust_global_allocation < ' b > (
348341 ecx : & InterpCx < ' tcx , Self > ,
349342 id : AllocId ,
350- alloc : Cow < ' b , Allocation > ,
351- kind : Option < MemoryKind < Self :: MemoryKind > > ,
352- ) -> InterpResult < ' tcx , Cow < ' b , Allocation < Self :: Provenance , Self :: AllocExtra , Self :: Bytes > > > ;
343+ alloc : & ' b Allocation ,
344+ ) -> InterpResult < ' tcx , Cow < ' b , Allocation < Self :: Provenance , Self :: AllocExtra , Self :: Bytes > > >
345+ {
346+ // The default implementation does a copy; CTFE machines have a more efficient implementation
347+ // based on their particular choice for `Provenance`, `AllocExtra`, and `Bytes`.
348+ let kind = Self :: GLOBAL_KIND
349+ . expect ( "if GLOBAL_KIND is None, adjust_global_allocation must be overwritten" ) ;
350+ let alloc = alloc. adjust_from_tcx ( & ecx. tcx , |ptr| ecx. global_root_pointer ( ptr) ) ?;
351+ let extra =
352+ Self :: init_alloc_extra ( ecx, id, MemoryKind :: Machine ( kind) , alloc. size ( ) , alloc. align ) ?;
353+ Ok ( Cow :: Owned ( alloc. with_extra ( extra) ) )
354+ }
355+
356+ /// Initialize the extra state of an allocation.
357+ ///
358+ /// This is guaranteed to be called exactly once on all allocations that are accessed by the
359+ /// program.
360+ fn init_alloc_extra (
361+ ecx : & InterpCx < ' tcx , Self > ,
362+ id : AllocId ,
363+ kind : MemoryKind < Self :: MemoryKind > ,
364+ size : Size ,
365+ align : Align ,
366+ ) -> InterpResult < ' tcx , Self :: AllocExtra > ;
353367
354368 /// Return a "root" pointer for the given allocation: the one that is used for direct
355369 /// accesses to this static/const/fn allocation, or the one returned from the heap allocator.
@@ -473,7 +487,7 @@ pub trait Machine<'tcx>: Sized {
473487 }
474488
475489 /// Called immediately before a new stack frame gets pushed.
476- fn init_frame_extra (
490+ fn init_frame (
477491 ecx : & mut InterpCx < ' tcx , Self > ,
478492 frame : Frame < ' tcx , Self :: Provenance > ,
479493 ) -> InterpResult < ' tcx , Frame < ' tcx , Self :: Provenance , Self :: FrameExtra > > ;
@@ -590,13 +604,23 @@ pub macro compile_time_machine(<$tcx: lifetime>) {
590604 }
591605
592606 #[ inline( always) ]
593- fn adjust_allocation < ' b > (
607+ fn adjust_global_allocation < ' b > (
594608 _ecx : & InterpCx < $tcx, Self > ,
595609 _id : AllocId ,
596- alloc : Cow < ' b , Allocation > ,
597- _kind : Option < MemoryKind < Self :: MemoryKind > > ,
610+ alloc : & ' b Allocation ,
598611 ) -> InterpResult < $tcx, Cow < ' b , Allocation < Self :: Provenance > > > {
599- Ok ( alloc)
612+ // Overwrite default implementation: no need to adjust anything.
613+ Ok ( Cow :: Borrowed ( alloc) )
614+ }
615+
616+ fn init_alloc_extra (
617+ _ecx : & InterpCx < $tcx, Self > ,
618+ _id : AllocId ,
619+ _kind : MemoryKind < Self :: MemoryKind > ,
620+ _size : Size ,
621+ _align : Align ,
622+ ) -> InterpResult < $tcx, Self :: AllocExtra > {
623+ Ok ( ( ) )
600624 }
601625
602626 fn extern_static_pointer (
0 commit comments