@@ -8,13 +8,10 @@ use std::{
8
8
} ;
9
9
10
10
/// An allocator for `T` objects, with a block index of `N` bytes.
11
- ///
12
- /// If `N` is 1, then `blocks_capacity` will be `u8::MAX`.
13
- /// If `N` is 2, then `blocks_capacity` will be `u16::MAX`.
14
11
pub struct OrderedPoolAllocator < ' buf , T , const N : usize = { mem:: size_of:: < usize > ( ) } > {
12
+ physical_blocks_ptr : * mut MaybeUninit < T > ,
15
13
physical_block_indices_ptr : * mut [ u8 ; N ] ,
16
14
virtual_block_indices_ptr : * mut [ u8 ; N ] ,
17
- physical_blocks_ptr : * mut MaybeUninit < T > ,
18
15
blocks_len : usize ,
19
16
deallocated_blocks_len : usize ,
20
17
blocks_capacity : usize ,
@@ -25,18 +22,21 @@ impl<'buf, T, const N: usize> OrderedPoolAllocator<'buf, T, N> {
25
22
pub fn new_in ( buf : & ' buf mut [ u8 ] ) -> Self {
26
23
let blocks_capacity = usize:: MAX
27
24
. min ( 2usize . pow ( 8 ) . pow ( N as u32 ) - 1 )
28
- . min ( buf. len ( ) / ( 2 * mem:: size_of :: < [ u8 ; N ] > ( ) + mem:: size_of :: < MaybeUninit < T > > ( ) ) ) ;
25
+ . min ( buf. len ( ) / ( mem:: size_of :: < MaybeUninit < T > > ( ) + 2 * mem:: size_of :: < [ u8 ; N ] > ( ) ) ) ;
29
26
30
27
unsafe {
31
28
Self {
32
- physical_block_indices_ptr : buf. as_mut_ptr ( ) . cast ( ) ,
33
- virtual_block_indices_ptr : buf
29
+ physical_blocks_ptr : buf. as_mut_ptr ( ) . cast ( ) ,
30
+ physical_block_indices_ptr : buf
34
31
. as_mut_ptr ( )
35
- . add ( blocks_capacity * mem:: size_of :: < [ u8 ; N ] > ( ) )
32
+ . add ( blocks_capacity * mem:: size_of :: < MaybeUninit < T > > ( ) )
36
33
. cast ( ) ,
37
- physical_blocks_ptr : buf
34
+ virtual_block_indices_ptr : buf
38
35
. as_mut_ptr ( )
39
- . add ( 2 * blocks_capacity * mem:: size_of :: < [ u8 ; N ] > ( ) )
36
+ . add (
37
+ blocks_capacity
38
+ * ( mem:: size_of :: < MaybeUninit < T > > ( ) + mem:: size_of :: < [ u8 ; N ] > ( ) ) ,
39
+ )
40
40
. cast ( ) ,
41
41
blocks_len : 0 ,
42
42
deallocated_blocks_len : 0 ,
@@ -48,12 +48,12 @@ impl<'buf, T, const N: usize> OrderedPoolAllocator<'buf, T, N> {
48
48
49
49
/// Allocates a block and returns a pointer to the block.
50
50
///
51
- /// This method will always prioritize reallocating an existing deallocated block over allocating
52
- /// a new block.
51
+ /// This method will always prioritize reallocating an existing deallocated block over allocating a new block.
53
52
///
54
53
/// # Safety
55
54
///
56
- /// Behavior is undefined if the returned pointer points to an uninitialized instance of `T` when the allocator is dropped.
55
+ /// Behavior is undefined if the returned pointer points to an uninitialized instance of `T` when the allocator is
56
+ /// dropped.
57
57
pub unsafe fn allocate ( & mut self ) -> Result < NonNull < T > , AllocError > {
58
58
let physical_block_index = match self . deallocated_blocks_len {
59
59
0 if self . is_full ( ) => return Err ( AllocError ) ,
@@ -96,7 +96,6 @@ impl<'buf, T, const N: usize> OrderedPoolAllocator<'buf, T, N> {
96
96
/// Behavior is undefined if any of the following conditions are violated:
97
97
///
98
98
/// * `ptr` must point to an instance of `T` allocated by this allocator.
99
- ///
100
99
/// * `ptr` must not point to an instance of `T` that has already been dropped or deallocated by this allocator.
101
100
pub unsafe fn deallocate ( & mut self , ptr : NonNull < T > ) {
102
101
if self . is_empty ( ) {
@@ -206,10 +205,6 @@ impl<'buf, T, const N: usize> OrderedPoolAllocator<'buf, T, N> {
206
205
}
207
206
208
207
fn usize_to_bytes ( block_index : usize ) -> [ u8 ; N ] {
209
- if N == mem:: size_of :: < usize > ( ) {
210
- unsafe { return * block_index. to_le_bytes ( ) . as_ptr ( ) . cast ( ) }
211
- }
212
-
213
208
let mut buf = [ 0u8 ; N ] ;
214
209
215
210
unsafe {
@@ -327,7 +322,7 @@ mod tests {
327
322
}
328
323
329
324
unsafe fn deallocate ( sut : & mut OrderedPoolAllocator < SecureU32 , 1 > , rng : & mut ThreadRng ) {
330
- let i = rng. gen_range ( 0 ..sut. len ( ) ) ;
325
+ let i = rng. random_range ( 0 ..sut. len ( ) ) ;
331
326
let ptr = NonNull :: new_unchecked ( sut[ i] . as_mut_ptr ( ) ) ;
332
327
sut. deallocate ( ptr) ;
333
328
assert_eq ! ( 0 , ptr. as_ref( ) . 0 )
@@ -336,14 +331,14 @@ mod tests {
336
331
unsafe {
337
332
let mut buf = [ 0u8 ; 128 ] ;
338
333
let mut sut = OrderedPoolAllocator :: < SecureU32 , 1 > :: new_in ( & mut buf) ;
339
- let mut rng = rand:: thread_rng ( ) ;
334
+ let mut rng = rand:: rng ( ) ;
340
335
341
336
for _ in 0 ..20_000_000 {
342
337
if sut. is_empty ( ) {
343
338
allocate ( & mut sut, u32:: MAX )
344
339
} else if sut. is_full ( ) {
345
340
deallocate ( & mut sut, & mut rng)
346
- } else if rng. gen_bool ( 0.5 ) {
341
+ } else if rng. random_bool ( 0.5 ) {
347
342
allocate ( & mut sut, u32:: MAX )
348
343
} else {
349
344
deallocate ( & mut sut, & mut rng)
0 commit comments