@@ -7,13 +7,13 @@ use crate::Allocator;
77/// Internally uses a `Vec` protected by a `Mutex` to store available allocators.
88#[ derive( Default ) ]
99pub struct AllocatorPool {
10- allocators : Mutex < Vec < Allocator > > ,
10+ allocators : Mutex < Vec < AllocatorWrapper > > ,
1111}
1212
1313impl AllocatorPool {
14- /// Creates a new [`AllocatorPool`] pre-filled with the given number of default [`Allocator`] instances.
14+ /// Creates a new [`AllocatorPool`] pre-filled with the given number of default `AllocatorWrapper` instances.
1515 pub fn new ( size : usize ) -> AllocatorPool {
16- let allocators = std :: iter :: repeat_with ( Allocator :: default ) . take ( size) . collect ( ) ;
16+ let allocators = AllocatorWrapper :: new_vec ( size) ;
1717 AllocatorPool { allocators : Mutex :: new ( allocators) }
1818 }
1919
@@ -29,19 +29,19 @@ impl AllocatorPool {
2929 let mut allocators = self . allocators . lock ( ) . unwrap ( ) ;
3030 allocators. pop ( )
3131 } ;
32- let allocator = allocator. unwrap_or_default ( ) ;
32+ let allocator = allocator. unwrap_or_else ( AllocatorWrapper :: new ) ;
3333
3434 AllocatorGuard { allocator : ManuallyDrop :: new ( allocator) , pool : self }
3535 }
3636
37- /// Add an [`Allocator `] to the pool.
37+ /// Add an [`AllocatorWrapper `] to the pool.
3838 ///
3939 /// The `Allocator` should be empty, ready to be re-used.
4040 ///
4141 /// # Panics
4242 ///
4343 /// Panics if the underlying mutex is poisoned.
44- fn add ( & self , allocator : Allocator ) {
44+ fn add ( & self , allocator : AllocatorWrapper ) {
4545 let mut allocators = self . allocators . lock ( ) . unwrap ( ) ;
4646 allocators. push ( allocator) ;
4747 }
@@ -51,24 +51,58 @@ impl AllocatorPool {
5151///
5252/// On drop, the `Allocator` is reset and returned to the pool.
5353pub struct AllocatorGuard < ' alloc_pool > {
54- allocator : ManuallyDrop < Allocator > ,
54+ allocator : ManuallyDrop < AllocatorWrapper > ,
5555 pool : & ' alloc_pool AllocatorPool ,
5656}
5757
5858impl Deref for AllocatorGuard < ' _ > {
5959 type Target = Allocator ;
6060
6161 fn deref ( & self ) -> & Self :: Target {
62- & self . allocator
62+ self . allocator . get ( )
6363 }
6464}
6565
6666impl Drop for AllocatorGuard < ' _ > {
6767 /// Return [`Allocator`] back to the pool.
6868 fn drop ( & mut self ) {
69- // SAFETY: After taking ownership of the `Allocator `, we do not touch the `ManuallyDrop` again
69+ // SAFETY: After taking ownership of the `AllocatorWrapper `, we do not touch the `ManuallyDrop` again
7070 let mut allocator = unsafe { ManuallyDrop :: take ( & mut self . allocator ) } ;
7171 allocator. reset ( ) ;
7272 self . pool . add ( allocator) ;
7373 }
7474}
75+
76+ mod wrapper {
77+ use crate :: Allocator ;
78+
79+ /// Structure which wraps an [`Allocator`].
80+ ///
81+ /// This implementation adds no value to `Allocator`, but we can add support for fixed-size allocators
82+ /// by providing a different implementation of `AllocatorWrapper` behind a feature flag.
83+ pub struct AllocatorWrapper ( Allocator ) ;
84+
85+ impl AllocatorWrapper {
86+ /// Create a new [`AllocatorWrapper`].
87+ pub fn new ( ) -> Self {
88+ Self ( Allocator :: default ( ) )
89+ }
90+
91+ /// Get reference to underlying [`Allocator`].
92+ pub fn get ( & self ) -> & Allocator {
93+ & self . 0
94+ }
95+
96+ /// Reset the [`Allocator`] in this [`AllocatorWrapper`].
97+ pub fn reset ( & mut self ) {
98+ self . 0 . reset ( ) ;
99+ }
100+
101+ /// Create a `Vec` of [`AllocatorWrapper`]s.
102+ pub fn new_vec ( size : usize ) -> Vec < Self > {
103+ std:: iter:: repeat_with ( Self :: new) . take ( size) . collect ( )
104+ }
105+ }
106+ }
107+
108+ use wrapper:: AllocatorWrapper ;
0 commit comments