@@ -50,45 +50,6 @@ pub trait MemoryPool: Send + Sync + std::fmt::Debug {
5050fn  allocated ( & self )  -> usize ; 
5151} 
5252
53- /// A cooperative MemoryManager which tracks memory in a cooperative fashion. 
54- /// `ExecutionPlan` nodes such as `SortExec`, which require large amounts of memory 
55- /// register their memory requests with the MemoryManager which then tracks the total 
56- ///  memory that has been allocated across all such nodes. 
57- /// 
58- /// The associated [`MemoryPool`] determines how to respond to memory allocation 
59- /// requests, and any associated fairness control 
60- #[ derive( Debug ) ]  
61- pub  struct  MemoryManager  { 
62-     pool :  Arc < dyn  MemoryPool > , 
63- } 
64- 
65- impl  MemoryManager  { 
66-     /// Create new memory manager based on the configuration 
67- pub  fn  new ( pool :  Arc < dyn  MemoryPool > )  -> Self  { 
68-         Self  {  pool } 
69-     } 
70- 
71-     /// Returns the number of allocated bytes 
72- /// 
73- /// Note: this can exceed the pool size as a result of [`MemoryManager::allocate`] 
74- pub  fn  allocated ( & self )  -> usize  { 
75-         self . pool . allocated ( ) 
76-     } 
77- 
78-     /// Returns a new empty allocation identified by `name` 
79- pub  fn  new_allocation ( & self ,  name :  String )  -> TrackedAllocation  { 
80-         self . new_allocation_with_options ( AllocationOptions :: new ( name) ) 
81-     } 
82- 
83-     /// Returns a new empty allocation with the provided [`AllocationOptions`] 
84- pub  fn  new_allocation_with_options ( 
85-         & self , 
86-         options :  AllocationOptions , 
87-     )  -> TrackedAllocation  { 
88-         TrackedAllocation :: new_empty ( options,  Arc :: clone ( & self . pool ) ) 
89-     } 
90- } 
91- 
9253/// Options associated with a [`TrackedAllocation`] 
9354#[ derive( Debug ) ]  
9455pub  struct  AllocationOptions  { 
@@ -131,12 +92,21 @@ pub struct TrackedAllocation {
13192} 
13293
13394impl  TrackedAllocation  { 
134-     fn  new_empty ( options :  AllocationOptions ,  policy :  Arc < dyn  MemoryPool > )  -> Self  { 
135-         policy. allocate ( & options) ; 
95+     /// Create a new [`TrackedAllocation`] in the provided [`MemoryPool`] 
96+ pub  fn  new ( pool :  & Arc < dyn  MemoryPool > ,  name :  String )  -> Self  { 
97+         Self :: new_with_options ( pool,  AllocationOptions :: new ( name) ) 
98+     } 
99+ 
100+     /// Create a new [`TrackedAllocation`] in the provided [`MemoryPool`] 
101+ pub  fn  new_with_options ( 
102+         pool :  & Arc < dyn  MemoryPool > , 
103+         options :  AllocationOptions , 
104+     )  -> Self  { 
105+         pool. allocate ( & options) ; 
136106        Self  { 
137107            options, 
138108            size :  0 , 
139-             policy, 
109+             policy :   Arc :: clone ( pool ) , 
140110        } 
141111    } 
142112
@@ -231,31 +201,30 @@ mod tests {
231201
232202    #[ test]  
233203    fn  test_memory_manager_underflow ( )  { 
234-         let  policy = Arc :: new ( GreedyMemoryPool :: new ( 50 ) ) ; 
235-         let  manager = MemoryManager :: new ( policy) ; 
236-         let  mut  a1 = manager. new_allocation ( "a1" . to_string ( ) ) ; 
237-         assert_eq ! ( manager. allocated( ) ,  0 ) ; 
204+         let  pool = Arc :: new ( GreedyMemoryPool :: new ( 50 ) )  as  _ ; 
205+         let  mut  a1 = TrackedAllocation :: new ( & pool,  "a1" . to_string ( ) ) ; 
206+         assert_eq ! ( pool. allocated( ) ,  0 ) ; 
238207
239208        a1. grow ( 100 ) ; 
240-         assert_eq ! ( manager . allocated( ) ,  100 ) ; 
209+         assert_eq ! ( pool . allocated( ) ,  100 ) ; 
241210
242211        assert_eq ! ( a1. free( ) ,  100 ) ; 
243-         assert_eq ! ( manager . allocated( ) ,  0 ) ; 
212+         assert_eq ! ( pool . allocated( ) ,  0 ) ; 
244213
245214        a1. try_grow ( 100 ) . unwrap_err ( ) ; 
246-         assert_eq ! ( manager . allocated( ) ,  0 ) ; 
215+         assert_eq ! ( pool . allocated( ) ,  0 ) ; 
247216
248217        a1. try_grow ( 30 ) . unwrap ( ) ; 
249-         assert_eq ! ( manager . allocated( ) ,  30 ) ; 
218+         assert_eq ! ( pool . allocated( ) ,  30 ) ; 
250219
251-         let  mut  a2 = manager . new_allocation ( "a2" . to_string ( ) ) ; 
220+         let  mut  a2 = TrackedAllocation :: new ( & pool ,   "a2" . to_string ( ) ) ; 
252221        a2. try_grow ( 25 ) . unwrap_err ( ) ; 
253-         assert_eq ! ( manager . allocated( ) ,  30 ) ; 
222+         assert_eq ! ( pool . allocated( ) ,  30 ) ; 
254223
255224        drop ( a1) ; 
256-         assert_eq ! ( manager . allocated( ) ,  0 ) ; 
225+         assert_eq ! ( pool . allocated( ) ,  0 ) ; 
257226
258227        a2. try_grow ( 25 ) . unwrap ( ) ; 
259-         assert_eq ! ( manager . allocated( ) ,  25 ) ; 
228+         assert_eq ! ( pool . allocated( ) ,  25 ) ; 
260229    } 
261230} 
0 commit comments