@@ -12,18 +12,9 @@ macro_rules! const_try_result {
12
12
} ;
13
13
}
14
14
15
- // FIXME: Once `const_deallocate` is added, we won't have to sub-allocate
16
- // `const` heap allocations, and we won't need `rlsf`
17
-
18
15
/// Compile-time allocator.
19
16
///
20
- /// It's implemented on top of [`core::intrinsics::const_allocate`][]. Although
21
- /// the deallocation counterpart of the intrinsic function `const_deallocate`
22
- /// doesn't exist yet, `ConstAllocator` is capable of reusing deallocated
23
- /// regions as long as they are created from the same instance of
24
- /// `ConstAllocator`. This is accomplished by, instead of making a call to
25
- /// `const_allocate` for each allocation request, slicing out each allocated
26
- /// region from larger blocks using a dynamic storage allocation algorithm.
17
+ /// This is implemented on top of [`core::intrinsics::const_allocate`][].
27
18
///
28
19
/// # Stability
29
20
///
@@ -37,17 +28,8 @@ pub struct ConstAllocator {
37
28
/// - Live allocations created through `ConstAllocator as Allocator`.
38
29
///
39
30
ref_count : * mut usize ,
40
- tlsf : * mut TheFlexTlsf ,
41
31
}
42
32
43
- type TheFlexTlsf = rlsf:: FlexTlsf <
44
- ConstFlexSource ,
45
- usize ,
46
- usize ,
47
- { usize:: BITS as usize } ,
48
- { usize:: BITS as usize } ,
49
- > ;
50
-
51
33
impl ConstAllocator {
52
34
/// Call the specified closure, passing a reference to a `Self` constructed
53
35
/// on the stack.
@@ -173,18 +155,7 @@ impl ConstAllocator {
173
155
let mut ref_count = RefCountGuard ( 1 ) ;
174
156
let ref_count = ( & mut ref_count. 0 ) as * mut _ ;
175
157
176
- struct TlsfGuard ( Option < TheFlexTlsf > ) ;
177
- impl const Drop for TlsfGuard {
178
- fn drop ( & mut self ) {
179
- self . 0 . take ( ) . unwrap ( ) . destroy ( ) ;
180
- }
181
- }
182
-
183
- let mut tlsf = TlsfGuard ( Some ( TheFlexTlsf :: new ( ConstFlexSource ) ) ) ;
184
- let this = Self {
185
- ref_count,
186
- tlsf : tlsf. 0 . as_mut ( ) . unwrap ( ) ,
187
- } ;
158
+ let this = Self { ref_count } ;
188
159
189
160
f. call ( & this)
190
161
}
@@ -231,7 +202,6 @@ impl const Clone for ConstAllocator {
231
202
unsafe { * self . ref_count += 1 } ;
232
203
Self {
233
204
ref_count : self . ref_count ,
234
- tlsf : self . tlsf ,
235
205
}
236
206
}
237
207
@@ -426,52 +396,17 @@ where
426
396
427
397
unsafe impl const Allocator for ConstAllocator {
428
398
fn allocate ( & self , layout : Layout ) -> Result < NonNull < [ u8 ] > , AllocError > {
429
- let tlsf = unsafe { & mut * self . tlsf } ;
430
- if let Some ( x ) = tlsf . allocate ( layout ) {
399
+ let ptr = unsafe { core :: intrinsics :: const_allocate ( layout . size ( ) , layout . align ( ) ) } ;
400
+ if let Some ( ptr ) = NonNull :: new ( ptr ) {
431
401
unsafe { * self . ref_count += 1 } ;
432
- Ok ( rlsf:: nonnull_slice_from_raw_parts ( x , layout. size ( ) ) )
402
+ Ok ( rlsf:: nonnull_slice_from_raw_parts ( ptr , layout. size ( ) ) )
433
403
} else {
434
404
Err ( AllocError )
435
405
}
436
406
}
437
407
438
408
unsafe fn deallocate ( & self , ptr : NonNull < u8 > , layout : Layout ) {
439
- let tlsf = unsafe { & mut * self . tlsf } ;
440
- unsafe { tlsf. deallocate ( ptr, layout. align ( ) ) } ;
409
+ unsafe { core:: intrinsics:: const_deallocate ( ptr. as_ptr ( ) , layout. size ( ) , layout. align ( ) ) } ;
441
410
unsafe { * self . ref_count -= 1 } ;
442
411
}
443
412
}
444
-
445
- /// An implementation of `FlexSource` based on the CTFE heap.
446
- struct ConstFlexSource ;
447
-
448
- /// Theoretically could be one, but a larger value is chosen to lessen the
449
- /// fragmentation
450
- const BLOCK_SIZE : usize = 1 << 16 ;
451
-
452
- const _: ( ) = assert ! ( BLOCK_SIZE >= rlsf:: ALIGN ) ;
453
-
454
- unsafe impl const rlsf:: FlexSource for ConstFlexSource {
455
- unsafe fn alloc ( & mut self , min_size : usize ) -> Option < core:: ptr:: NonNull < [ u8 ] > > {
456
- // FIXME: Work-around for `?` being unsupported in `const fn`
457
- let size = if let Some ( size) = min_size. checked_add ( BLOCK_SIZE - 1 ) {
458
- size & !( BLOCK_SIZE - 1 )
459
- } else {
460
- return None ;
461
- } ;
462
-
463
- assert ! ( min_size != 0 ) ;
464
-
465
- let ptr = unsafe { core:: intrinsics:: const_allocate ( size, BLOCK_SIZE ) } ;
466
-
467
- // FIXME: `NonNull::new` is not `const fn` yet
468
- assert ! ( !ptr. guaranteed_eq( core:: ptr:: null_mut( ) ) ) ;
469
- let ptr = unsafe { NonNull :: new_unchecked ( ptr) } ;
470
-
471
- Some ( rlsf:: nonnull_slice_from_raw_parts ( ptr, size) )
472
- }
473
-
474
- fn min_align ( & self ) -> usize {
475
- BLOCK_SIZE
476
- }
477
- }
0 commit comments