@@ -10,6 +10,15 @@ use std::{
1010 sync:: Arc ,
1111} ;
1212
13+ use alloc:: {
14+ borrow:: ToOwned ,
15+ string:: { String , ToString } ,
16+ vec:: Vec ,
17+ } ;
18+
19+ #[ cfg( feature = "hashbrown" ) ]
20+ use hashbrown:: { HashMap , HashSet } ;
21+
1322use log:: { log, Level } ;
1423
1524use super :: { AllocationReport , AllocationType , SubAllocator , SubAllocatorBase } ;
@@ -27,25 +36,25 @@ fn align_up(val: u64, alignment: u64) -> u64 {
2736
2837#[ derive( Debug ) ]
2938pub ( crate ) struct MemoryChunk {
30- pub ( crate ) chunk_id : std :: num:: NonZeroU64 ,
39+ pub ( crate ) chunk_id : core :: num:: NonZeroU64 ,
3140 pub ( crate ) size : u64 ,
3241 pub ( crate ) offset : u64 ,
3342 pub ( crate ) allocation_type : AllocationType ,
3443 pub ( crate ) name : Option < String > ,
3544 /// Only used if [`crate::AllocatorDebugSettings::store_stack_traces`] is [`true`]
3645 #[ cfg( feature = "std" ) ]
3746 pub ( crate ) backtrace : Arc < Backtrace > ,
38- next : Option < std :: num:: NonZeroU64 > ,
39- prev : Option < std :: num:: NonZeroU64 > ,
47+ next : Option < core :: num:: NonZeroU64 > ,
48+ prev : Option < core :: num:: NonZeroU64 > ,
4049}
4150
4251#[ derive( Debug ) ]
4352pub ( crate ) struct FreeListAllocator {
4453 size : u64 ,
4554 allocated : u64 ,
4655 pub ( crate ) chunk_id_counter : u64 ,
47- pub ( crate ) chunks : HashMap < std :: num:: NonZeroU64 , MemoryChunk > ,
48- free_chunks : HashSet < std :: num:: NonZeroU64 > ,
56+ pub ( crate ) chunks : HashMap < core :: num:: NonZeroU64 , MemoryChunk > ,
57+ free_chunks : HashSet < core :: num:: NonZeroU64 > ,
4958}
5059
5160/// Test if two suballocations will overlap the same page.
@@ -70,7 +79,7 @@ fn has_granularity_conflict(type0: AllocationType, type1: AllocationType) -> boo
7079impl FreeListAllocator {
7180 pub ( crate ) fn new ( size : u64 ) -> Self {
7281 #[ allow( clippy:: unwrap_used) ]
73- let initial_chunk_id = std :: num:: NonZeroU64 :: new ( 1 ) . unwrap ( ) ;
82+ let initial_chunk_id = core :: num:: NonZeroU64 :: new ( 1 ) . unwrap ( ) ;
7483
7584 let mut chunks = HashMap :: default ( ) ;
7685 chunks. insert (
@@ -103,27 +112,27 @@ impl FreeListAllocator {
103112 }
104113
105114 /// Generates a new unique chunk ID
106- fn get_new_chunk_id ( & mut self ) -> Result < std :: num:: NonZeroU64 > {
115+ fn get_new_chunk_id ( & mut self ) -> Result < core :: num:: NonZeroU64 > {
107116 if self . chunk_id_counter == u64:: MAX {
108117 // End of chunk id counter reached, no more allocations are possible.
109118 return Err ( AllocationError :: OutOfMemory ) ;
110119 }
111120
112121 let id = self . chunk_id_counter ;
113122 self . chunk_id_counter += 1 ;
114- std :: num:: NonZeroU64 :: new ( id) . ok_or_else ( || {
123+ core :: num:: NonZeroU64 :: new ( id) . ok_or_else ( || {
115124 AllocationError :: Internal ( "New chunk id was 0, which is not allowed." . into ( ) )
116125 } )
117126 }
118127 /// Finds the specified `chunk_id` in the list of free chunks and removes if from the list
119- fn remove_id_from_free_list ( & mut self , chunk_id : std :: num:: NonZeroU64 ) {
128+ fn remove_id_from_free_list ( & mut self , chunk_id : core :: num:: NonZeroU64 ) {
120129 self . free_chunks . remove ( & chunk_id) ;
121130 }
122131 /// Merges two adjacent chunks. Right chunk will be merged into the left chunk
123132 fn merge_free_chunks (
124133 & mut self ,
125- chunk_left : std :: num:: NonZeroU64 ,
126- chunk_right : std :: num:: NonZeroU64 ,
134+ chunk_left : core :: num:: NonZeroU64 ,
135+ chunk_right : core :: num:: NonZeroU64 ,
127136 ) -> Result < ( ) > {
128137 // Gather data from right chunk and remove it
129138 let ( right_size, right_next) = {
@@ -172,7 +181,7 @@ impl SubAllocator for FreeListAllocator {
172181 return Err ( AllocationError :: OutOfMemory ) ;
173182 }
174183
175- let mut best_fit_id: Option < std :: num:: NonZeroU64 > = None ;
184+ let mut best_fit_id: Option < core :: num:: NonZeroU64 > = None ;
176185 let mut best_offset = 0u64 ;
177186 let mut best_aligned_size = 0u64 ;
178187 let mut best_chunk_size = 0u64 ;
@@ -297,7 +306,7 @@ impl SubAllocator for FreeListAllocator {
297306 Ok ( ( best_offset, chunk_id) )
298307 }
299308
300- fn free ( & mut self , chunk_id : Option < std :: num:: NonZeroU64 > ) -> Result < ( ) > {
309+ fn free ( & mut self , chunk_id : Option < core :: num:: NonZeroU64 > ) -> Result < ( ) > {
301310 let chunk_id = chunk_id
302311 . ok_or_else ( || AllocationError :: Internal ( "Chunk ID must be a valid value." . into ( ) ) ) ?;
303312
@@ -337,7 +346,7 @@ impl SubAllocator for FreeListAllocator {
337346
338347 fn rename_allocation (
339348 & mut self ,
340- chunk_id : Option < std :: num:: NonZeroU64 > ,
349+ chunk_id : Option < core :: num:: NonZeroU64 > ,
341350 name : & str ,
342351 ) -> Result < ( ) > {
343352 let chunk_id = chunk_id
0 commit comments