4
4
use crate :: alloc:: { self , Layout , LayoutError } ;
5
5
use core:: error:: Error ;
6
6
use core:: fmt:: { self , Debug , Display , Formatter } ;
7
+ #[ cfg( not( no_global_oom_handling) ) ]
8
+ use core:: intrinsics:: const_allocate;
7
9
use core:: marker:: PhantomData ;
8
10
#[ cfg( not( no_global_oom_handling) ) ]
9
11
use core:: marker:: Unsize ;
10
- use core:: mem:: { self , SizedTypeProperties } ;
12
+ use core:: mem;
13
+ #[ cfg( not( no_global_oom_handling) ) ]
14
+ use core:: mem:: SizedTypeProperties ;
11
15
use core:: ops:: { Deref , DerefMut } ;
12
16
use core:: ptr:: Pointee ;
13
17
use core:: ptr:: { self , NonNull } ;
@@ -91,6 +95,65 @@ impl<T> ThinBox<T> {
91
95
92
96
#[ unstable( feature = "thin_box" , issue = "92791" ) ]
93
97
impl < Dyn : ?Sized > ThinBox < Dyn > {
98
+ #[ cfg( not( no_global_oom_handling) ) ]
99
+ fn new_unsize_zst < T > ( value : T ) -> Self
100
+ where
101
+ T : Unsize < Dyn > ,
102
+ {
103
+ assert ! ( mem:: size_of:: <T >( ) == 0 ) ;
104
+
105
+ const fn max ( a : usize , b : usize ) -> usize {
106
+ if a > b { a } else { b }
107
+ }
108
+
109
+ // We are using `[T; 0]` here instead of `T` because
110
+ // compiler instantiates this code even for non-ZST,
111
+ // and reports a pointer going beyond the allocation.
112
+ let alloc: & [ T ; 0 ] = const {
113
+ // ZST T pointer must be aligned to both the value and the metadata.
114
+ // Metadata must be at the address `&T - size_of::<Metadata>`.
115
+ // This is how we need to allocate the memory:
116
+ // [ padding | metadata | ZST T value ]
117
+
118
+ // FIXME: just call `WithHeader::alloc_layout` with size reset to 0.
119
+ // Currently that's blocked on `Layout::extend` not being `const fn`.
120
+
121
+ let alloc_align =
122
+ max ( mem:: align_of :: < T > ( ) , mem:: align_of :: < <Dyn as Pointee >:: Metadata > ( ) ) ;
123
+
124
+ let alloc_size =
125
+ max ( mem:: align_of :: < T > ( ) , mem:: size_of :: < <Dyn as Pointee >:: Metadata > ( ) ) ;
126
+
127
+ unsafe {
128
+ // SAFETY: align is power of two because it is the maximum of two alignments.
129
+ let alloc: * mut u8 = const_allocate ( alloc_size, alloc_align) ;
130
+
131
+ // Zerofill to be safe.
132
+ // SAFETY: `alloc_size` is the size of the allocation.
133
+ ptr:: write_bytes ( alloc, 0 , alloc_size) ;
134
+
135
+ // SAFETY: adding offset within the allocation.
136
+ let metadata_ptr: * mut u8 = alloc. add (
137
+ alloc_size. checked_sub ( mem:: size_of :: < <Dyn as Pointee >:: Metadata > ( ) ) . unwrap ( ) ,
138
+ ) ;
139
+ // SAFETY: `*metadata_ptr` is within the allocation.
140
+ ptr:: write (
141
+ metadata_ptr as * mut <Dyn as Pointee >:: Metadata ,
142
+ ptr:: metadata :: < Dyn > ( ptr:: dangling :: < T > ( ) as * const Dyn ) ,
143
+ ) ;
144
+
145
+ // SAFETY: `alloc_size` is the size of the allocation
146
+ // and the pointer we create is zero-sized.
147
+ & * ( alloc. add ( alloc_size) as * const [ T ; 0 ] )
148
+ }
149
+ } ;
150
+
151
+ let ptr = WithOpaqueHeader ( NonNull :: new ( alloc as * const _ as * mut u8 ) . unwrap ( ) ) ;
152
+ // Forget the value to avoid double drop.
153
+ mem:: forget ( value) ;
154
+ ThinBox :: < Dyn > { ptr, _marker : PhantomData }
155
+ }
156
+
94
157
/// Moves a type to the heap with its [`Metadata`] stored in the heap allocation instead of on
95
158
/// the stack.
96
159
///
@@ -109,9 +172,13 @@ impl<Dyn: ?Sized> ThinBox<Dyn> {
109
172
where
110
173
T : Unsize < Dyn > ,
111
174
{
112
- let meta = ptr:: metadata ( & value as & Dyn ) ;
113
- let ptr = WithOpaqueHeader :: new ( meta, value) ;
114
- ThinBox { ptr, _marker : PhantomData }
175
+ if mem:: size_of :: < T > ( ) == 0 {
176
+ Self :: new_unsize_zst ( value)
177
+ } else {
178
+ let meta = ptr:: metadata ( & value as & Dyn ) ;
179
+ let ptr = WithOpaqueHeader :: new ( meta, value) ;
180
+ ThinBox { ptr, _marker : PhantomData }
181
+ }
115
182
}
116
183
}
117
184
@@ -300,20 +367,19 @@ impl<H> WithHeader<H> {
300
367
301
368
impl < H > Drop for DropGuard < H > {
302
369
fn drop ( & mut self ) {
370
+ // All ZST are allocated statically.
371
+ if self . value_layout . size ( ) == 0 {
372
+ return ;
373
+ }
374
+
303
375
unsafe {
304
376
// SAFETY: Layout must have been computable if we're in drop
305
377
let ( layout, value_offset) =
306
378
WithHeader :: < H > :: alloc_layout ( self . value_layout ) . unwrap_unchecked ( ) ;
307
379
308
- // Note: Don't deallocate if the layout size is zero, because the pointer
309
- // didn't come from the allocator.
310
- if layout. size ( ) != 0 {
311
- alloc:: dealloc ( self . ptr . as_ptr ( ) . sub ( value_offset) , layout) ;
312
- } else {
313
- debug_assert ! (
314
- value_offset == 0 && H :: IS_ZST && self . value_layout. size( ) == 0
315
- ) ;
316
- }
380
+ // Since we only allocate for non-ZSTs, the layout size cannot be zero.
381
+ debug_assert ! ( layout. size( ) != 0 ) ;
382
+ alloc:: dealloc ( self . ptr . as_ptr ( ) . sub ( value_offset) , layout) ;
317
383
}
318
384
}
319
385
}
0 commit comments