@@ -17,6 +17,7 @@ use core::alloc::GlobalAlloc;
1717use core:: alloc:: Layout ;
1818#[ cfg( feature = "alloc_ref" ) ]
1919use core:: alloc:: { AllocError , Allocator } ;
20+ use core:: mem:: MaybeUninit ;
2021#[ cfg( feature = "use_spin" ) ]
2122use core:: ops:: Deref ;
2223use core:: ptr:: NonNull ;
@@ -89,9 +90,9 @@ impl Heap {
8990 /// This method panics if the heap is already initialized.
9091 pub fn init_from_slice ( & mut self , mem : & ' static mut [ MaybeUninit < u8 > ] ) {
9192 assert ! ( self . bottom == 0 , "The heap has already been initialized." ) ;
92- let size = mem. size ( ) ;
93+ let size = mem. len ( ) ;
9394 let address = mem. as_ptr ( ) as usize ;
94- // Safety : All initialization requires the bottom address to be valid, which implies it
95+ // SAFETY : All initialization requires the bottom address to be valid, which implies it
9596 // must not be 0. Initially the address is 0. The assertion above ensures that no
9697 // initialization had been called before.
9798 // The given address and size is valid according to the safety invariants of the mutable
@@ -116,6 +117,15 @@ impl Heap {
116117 }
117118 }
118119
120+ /// Crates a new heap from a slice of raw memory.
121+ pub fn with_memory ( mem : & ' static mut [ MaybeUninit < u8 > ] ) -> Heap {
122+ let size = mem. len ( ) ;
123+ let address = mem. as_ptr ( ) as usize ;
124+ // SAFETY: The given address and size is valid according to the safety invariants of the
125+ // mutable reference handed to us by the caller.
126+ unsafe { Self :: new ( address, size) }
127+ }
128+
119129 /// Allocates a chunk of the given size with the given alignment. Returns a pointer to the
120130 /// beginning of that chunk if it was successful. Else it returns `None`.
121131 /// This function scans the list of free memory blocks and uses the first block that is big
0 commit comments