@@ -73,6 +73,9 @@ pub struct Memory<'a, 'mir, 'tcx: 'a + 'mir, M: Machine<'a, 'mir, 'tcx>> {
7373 /// that do not exist any more.
7474 dead_alloc_map : FxHashMap < AllocId , ( Size , Align ) > ,
7575
76+ /// Extra data added by the machine.
77+ pub extra : M :: MemoryExtra ,
78+
7679 /// Lets us implement `HasDataLayout`, which is awfully convenient.
7780 pub ( super ) tcx : TyCtxtAt < ' a , ' tcx , ' tcx > ,
7881}
@@ -88,13 +91,19 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> HasDataLayout
8891
8992// FIXME: Really we shouldn't clone memory, ever. Snapshot machinery should instead
9093// carefully copy only the reachable parts.
91- impl < ' a , ' mir , ' tcx : ' a + ' mir , M : Machine < ' a , ' mir , ' tcx > >
92- Clone for Memory < ' a , ' mir , ' tcx , M >
94+ impl < ' a , ' mir , ' tcx , M >
95+ Clone
96+ for
97+ Memory < ' a , ' mir , ' tcx , M >
98+ where
99+ M : Machine < ' a , ' mir , ' tcx , PointerTag =( ) , AllocExtra =( ) , MemoryExtra =( ) > ,
100+ M :: MemoryMap : AllocMap < AllocId , ( MemoryKind < M :: MemoryKinds > , Allocation ) > ,
93101{
94102 fn clone ( & self ) -> Self {
95103 Memory {
96104 alloc_map : self . alloc_map . clone ( ) ,
97105 dead_alloc_map : self . dead_alloc_map . clone ( ) ,
106+ extra : ( ) ,
98107 tcx : self . tcx ,
99108 }
100109 }
@@ -103,8 +112,9 @@ impl<'a, 'mir, 'tcx: 'a + 'mir, M: Machine<'a, 'mir, 'tcx>>
103112impl < ' a , ' mir , ' tcx , M : Machine < ' a , ' mir , ' tcx > > Memory < ' a , ' mir , ' tcx , M > {
104113 pub fn new ( tcx : TyCtxtAt < ' a , ' tcx , ' tcx > ) -> Self {
105114 Memory {
106- alloc_map : Default :: default ( ) ,
115+ alloc_map : M :: MemoryMap :: default ( ) ,
107116 dead_alloc_map : FxHashMap :: default ( ) ,
117+ extra : M :: MemoryExtra :: default ( ) ,
108118 tcx,
109119 }
110120 }
@@ -133,7 +143,8 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
133143 align : Align ,
134144 kind : MemoryKind < M :: MemoryKinds > ,
135145 ) -> EvalResult < ' tcx , Pointer > {
136- Ok ( Pointer :: from ( self . allocate_with ( Allocation :: undef ( size, align) , kind) ?) )
146+ let extra = AllocationExtra :: memory_allocated ( size, & self . extra ) ;
147+ Ok ( Pointer :: from ( self . allocate_with ( Allocation :: undef ( size, align, extra) , kind) ?) )
137148 }
138149
139150 pub fn reallocate (
@@ -309,15 +320,16 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
309320 /// this machine use the same pointer tag, so it is indirected through
310321 /// `M::static_with_default_tag`.
311322 fn get_static_alloc (
312- tcx : TyCtxtAt < ' a , ' tcx , ' tcx > ,
313323 id : AllocId ,
324+ tcx : TyCtxtAt < ' a , ' tcx , ' tcx > ,
325+ memory_extra : & M :: MemoryExtra ,
314326 ) -> EvalResult < ' tcx , Cow < ' tcx , Allocation < M :: PointerTag , M :: AllocExtra > > > {
315327 let alloc = tcx. alloc_map . lock ( ) . get ( id) ;
316328 let def_id = match alloc {
317329 Some ( AllocType :: Memory ( mem) ) => {
318330 // We got tcx memory. Let the machine figure out whether and how to
319331 // turn that into memory with the right pointer tag.
320- return Ok ( M :: adjust_static_allocation ( mem) )
332+ return Ok ( M :: adjust_static_allocation ( mem, memory_extra ) )
321333 }
322334 Some ( AllocType :: Function ( ..) ) => {
323335 return err ! ( DerefFunctionPointer )
@@ -331,7 +343,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
331343 // We got a "lazy" static that has not been computed yet, do some work
332344 trace ! ( "static_alloc: Need to compute {:?}" , def_id) ;
333345 if tcx. is_foreign_item ( def_id) {
334- return M :: find_foreign_static ( tcx, def_id ) ;
346+ return M :: find_foreign_static ( def_id , tcx, memory_extra ) ;
335347 }
336348 let instance = Instance :: mono ( tcx. tcx , def_id) ;
337349 let gid = GlobalId {
@@ -351,7 +363,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
351363 let allocation = tcx. alloc_map . lock ( ) . unwrap_memory ( raw_const. alloc_id ) ;
352364 // We got tcx memory. Let the machine figure out whether and how to
353365 // turn that into memory with the right pointer tag.
354- M :: adjust_static_allocation ( allocation)
366+ M :: adjust_static_allocation ( allocation, memory_extra )
355367 } )
356368 }
357369
@@ -361,7 +373,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
361373 // `get_static_alloc` that we can actually use directly without inserting anything anywhere.
362374 // So the error type is `EvalResult<'tcx, &Allocation<M::PointerTag>>`.
363375 let a = self . alloc_map . get_or ( id, || {
364- let alloc = Self :: get_static_alloc ( self . tcx , id ) . map_err ( Err ) ?;
376+ let alloc = Self :: get_static_alloc ( id , self . tcx , & self . extra ) . map_err ( Err ) ?;
365377 match alloc {
366378 Cow :: Borrowed ( alloc) => {
367379 // We got a ref, cheaply return that as an "error" so that the
@@ -390,10 +402,11 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
390402 id : AllocId ,
391403 ) -> EvalResult < ' tcx , & mut Allocation < M :: PointerTag , M :: AllocExtra > > {
392404 let tcx = self . tcx ;
405+ let memory_extra = & self . extra ;
393406 let a = self . alloc_map . get_mut_or ( id, || {
394407 // Need to make a copy, even if `get_static_alloc` is able
395408 // to give us a cheap reference.
396- let alloc = Self :: get_static_alloc ( tcx, id ) ?;
409+ let alloc = Self :: get_static_alloc ( id , tcx, memory_extra ) ?;
397410 if alloc. mutability == Mutability :: Immutable {
398411 return err ! ( ModifiedConstantMemory ) ;
399412 }
@@ -601,7 +614,8 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
601614/// Interning (for CTFE)
602615impl < ' a , ' mir , ' tcx , M > Memory < ' a , ' mir , ' tcx , M >
603616where
604- M : Machine < ' a , ' mir , ' tcx , PointerTag =( ) , AllocExtra =( ) > ,
617+ M : Machine < ' a , ' mir , ' tcx , PointerTag =( ) , AllocExtra =( ) , MemoryExtra =( ) > ,
618+ // FIXME: Working around https://github.com/rust-lang/rust/issues/24159
605619 M :: MemoryMap : AllocMap < AllocId , ( MemoryKind < M :: MemoryKinds > , Allocation ) > ,
606620{
607621 /// mark an allocation as static and initialized, either mutable or not
0 commit comments