@@ -17,6 +17,7 @@ use core::alloc::GlobalAlloc;
17
17
use core:: alloc:: Layout ;
18
18
#[ cfg( feature = "alloc_ref" ) ]
19
19
use core:: alloc:: { AllocError , Allocator } ;
20
+ use core:: mem:: MaybeUninit ;
20
21
#[ cfg( feature = "use_spin" ) ]
21
22
use core:: ops:: Deref ;
22
23
use core:: ptr:: NonNull ;
@@ -89,9 +90,9 @@ impl Heap {
89
90
/// This method panics if the heap is already initialized.
90
91
pub fn init_from_slice ( & mut self , mem : & ' static mut [ MaybeUninit < u8 > ] ) {
91
92
assert ! ( self . bottom == 0 , "The heap has already been initialized." ) ;
92
- let size = mem. size ( ) ;
93
+ let size = mem. len ( ) ;
93
94
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
95
96
// must not be 0. Initially the address is 0. The assertion above ensures that no
96
97
// initialization had been called before.
97
98
// The given address and size is valid according to the safety invariants of the mutable
@@ -116,6 +117,15 @@ impl Heap {
116
117
}
117
118
}
118
119
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
+
119
129
/// Allocates a chunk of the given size with the given alignment. Returns a pointer to the
120
130
/// beginning of that chunk if it was successful. Else it returns `None`.
121
131
/// This function scans the list of free memory blocks and uses the first block that is big
0 commit comments