@@ -84,6 +84,8 @@ unsafe impl Allocator for MyAllocator {
84
84
}
85
85
86
86
unsafe fn deallocate ( & self , ptr : NonNull < u8 > , _layout : Layout ) {
87
+ // Make sure accesses via `self` don't disturb anything.
88
+ let _val = self . bins [ 0 ] . top . get ( ) ;
87
89
// Since manually finding the corresponding bin of `ptr` is very expensive,
88
90
// doing pointer arithmetics is preferred.
89
91
// But this means we access `top` via `ptr` rather than `self`!
@@ -93,22 +95,30 @@ unsafe impl Allocator for MyAllocator {
93
95
if self . thread_id == thread_id {
94
96
unsafe { ( * their_bin) . push ( ptr) } ;
95
97
} else {
96
- todo ! ( "Deallocating from another thread" )
98
+ todo ! ( "Deallocating from another thread" ) ;
97
99
}
100
+ // Make sure we can also still access this via `self` after the rest is done.
101
+ let _val = self . bins [ 0 ] . top . get ( ) ;
98
102
}
99
103
}
100
104
101
105
// Make sure to involve `Box` in allocating these,
102
106
// as that's where `noalias` may come from.
103
- fn v < T , A : Allocator > ( t : T , a : A ) -> Vec < T , A > {
107
+ fn v1 < T , A : Allocator > ( t : T , a : A ) -> Vec < T , A > {
104
108
( Box :: new_in ( [ t] , a) as Box < [ T ] , A > ) . into_vec ( )
105
109
}
110
+ fn v2 < T , A : Allocator > ( t : T , a : A ) -> Vec < T , A > {
111
+ let v = v1 ( t, a) ;
112
+ // There was a bug in `into_boxed_slice` that caused aliasing issues,
113
+ // so round-trip through that as well.
114
+ v. into_boxed_slice ( ) . into_vec ( )
115
+ }
106
116
107
117
fn main ( ) {
108
118
assert ! ( mem:: size_of:: <MyBin >( ) <= 128 ) ; // if it grows bigger, the trick to access the "header" no longer works
109
119
let my_alloc = MyAllocator :: new ( ) ;
110
- let a = v ( 1usize , & my_alloc) ;
111
- let b = v ( 2usize , & my_alloc) ;
120
+ let a = v1 ( 1usize , & my_alloc) ;
121
+ let b = v2 ( 2usize , & my_alloc) ;
112
122
assert_eq ! ( a[ 0 ] + 1 , b[ 0 ] ) ;
113
123
assert_eq ! ( addr_of!( a[ 0 ] ) . wrapping_add( 1 ) , addr_of!( b[ 0 ] ) ) ;
114
124
drop ( ( a, b) ) ;
0 commit comments