@@ -73,6 +73,9 @@ pub struct Memory<'a, 'mir, 'tcx: 'a + 'mir, M: Machine<'a, 'mir, 'tcx>> {
73
73
/// that do not exist any more.
74
74
dead_alloc_map : FxHashMap < AllocId , ( Size , Align ) > ,
75
75
76
+ /// Extra data added by the machine.
77
+ pub extra : M :: MemoryExtra ,
78
+
76
79
/// Lets us implement `HasDataLayout`, which is awfully convenient.
77
80
pub ( super ) tcx : TyCtxtAt < ' a , ' tcx , ' tcx > ,
78
81
}
@@ -88,13 +91,19 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> HasDataLayout
88
91
89
92
// FIXME: Really we shouldn't clone memory, ever. Snapshot machinery should instead
90
93
// 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 ) > ,
93
101
{
94
102
fn clone ( & self ) -> Self {
95
103
Memory {
96
104
alloc_map : self . alloc_map . clone ( ) ,
97
105
dead_alloc_map : self . dead_alloc_map . clone ( ) ,
106
+ extra : ( ) ,
98
107
tcx : self . tcx ,
99
108
}
100
109
}
@@ -103,8 +112,9 @@ impl<'a, 'mir, 'tcx: 'a + 'mir, M: Machine<'a, 'mir, 'tcx>>
103
112
impl < ' a , ' mir , ' tcx , M : Machine < ' a , ' mir , ' tcx > > Memory < ' a , ' mir , ' tcx , M > {
104
113
pub fn new ( tcx : TyCtxtAt < ' a , ' tcx , ' tcx > ) -> Self {
105
114
Memory {
106
- alloc_map : Default :: default ( ) ,
115
+ alloc_map : M :: MemoryMap :: default ( ) ,
107
116
dead_alloc_map : FxHashMap :: default ( ) ,
117
+ extra : M :: MemoryExtra :: default ( ) ,
108
118
tcx,
109
119
}
110
120
}
@@ -133,7 +143,8 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
133
143
align : Align ,
134
144
kind : MemoryKind < M :: MemoryKinds > ,
135
145
) -> 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) ?) )
137
148
}
138
149
139
150
pub fn reallocate (
@@ -309,15 +320,16 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
309
320
/// this machine use the same pointer tag, so it is indirected through
310
321
/// `M::static_with_default_tag`.
311
322
fn get_static_alloc (
312
- tcx : TyCtxtAt < ' a , ' tcx , ' tcx > ,
313
323
id : AllocId ,
324
+ tcx : TyCtxtAt < ' a , ' tcx , ' tcx > ,
325
+ memory_extra : & M :: MemoryExtra ,
314
326
) -> EvalResult < ' tcx , Cow < ' tcx , Allocation < M :: PointerTag , M :: AllocExtra > > > {
315
327
let alloc = tcx. alloc_map . lock ( ) . get ( id) ;
316
328
let def_id = match alloc {
317
329
Some ( AllocType :: Memory ( mem) ) => {
318
330
// We got tcx memory. Let the machine figure out whether and how to
319
331
// 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 ) )
321
333
}
322
334
Some ( AllocType :: Function ( ..) ) => {
323
335
return err ! ( DerefFunctionPointer )
@@ -331,7 +343,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
331
343
// We got a "lazy" static that has not been computed yet, do some work
332
344
trace ! ( "static_alloc: Need to compute {:?}" , def_id) ;
333
345
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 ) ;
335
347
}
336
348
let instance = Instance :: mono ( tcx. tcx , def_id) ;
337
349
let gid = GlobalId {
@@ -351,7 +363,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
351
363
let allocation = tcx. alloc_map . lock ( ) . unwrap_memory ( raw_const. alloc_id ) ;
352
364
// We got tcx memory. Let the machine figure out whether and how to
353
365
// turn that into memory with the right pointer tag.
354
- M :: adjust_static_allocation ( allocation)
366
+ M :: adjust_static_allocation ( allocation, memory_extra )
355
367
} )
356
368
}
357
369
@@ -361,7 +373,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
361
373
// `get_static_alloc` that we can actually use directly without inserting anything anywhere.
362
374
// So the error type is `EvalResult<'tcx, &Allocation<M::PointerTag>>`.
363
375
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 ) ?;
365
377
match alloc {
366
378
Cow :: Borrowed ( alloc) => {
367
379
// 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> {
390
402
id : AllocId ,
391
403
) -> EvalResult < ' tcx , & mut Allocation < M :: PointerTag , M :: AllocExtra > > {
392
404
let tcx = self . tcx ;
405
+ let memory_extra = & self . extra ;
393
406
let a = self . alloc_map . get_mut_or ( id, || {
394
407
// Need to make a copy, even if `get_static_alloc` is able
395
408
// 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 ) ?;
397
410
if alloc. mutability == Mutability :: Immutable {
398
411
return err ! ( ModifiedConstantMemory ) ;
399
412
}
@@ -601,7 +614,8 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
601
614
/// Interning (for CTFE)
602
615
impl < ' a , ' mir , ' tcx , M > Memory < ' a , ' mir , ' tcx , M >
603
616
where
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
605
619
M :: MemoryMap : AllocMap < AllocId , ( MemoryKind < M :: MemoryKinds > , Allocation ) > ,
606
620
{
607
621
/// mark an allocation as static and initialized, either mutable or not
0 commit comments