11use core:: alloc:: { GlobalAlloc , Layout } ;
2- use core:: cell:: RefCell ;
2+ use core:: cell:: { Cell , RefCell } ;
33use core:: ptr:: { self , NonNull } ;
44
55use const_default:: ConstDefault ;
@@ -11,6 +11,7 @@ type TlsfHeap = Tlsf<'static, usize, usize, { usize::BITS as usize }, { usize::B
1111/// A two-Level segregated fit heap.
1212pub struct Heap {
1313 heap : Mutex < RefCell < TlsfHeap > > ,
14+ once_flag : Mutex < Cell < bool > > ,
1415}
1516
1617impl Heap {
@@ -21,6 +22,7 @@ impl Heap {
2122 pub const fn empty ( ) -> Heap {
2223 Heap {
2324 heap : Mutex :: new ( RefCell :: new ( ConstDefault :: DEFAULT ) ) ,
25+ once_flag : Mutex :: new ( Cell :: new ( false ) ) ,
2426 }
2527 }
2628
@@ -49,7 +51,12 @@ impl Heap {
4951 /// - This function must be called exactly ONCE.
5052 /// - `size > 0`
5153 pub unsafe fn init ( & self , start_addr : usize , size : usize ) {
54+ assert ! ( size > 0 ) ;
5255 critical_section:: with ( |cs| {
56+ let once_flag = self . once_flag . borrow ( cs) ;
57+ assert ! ( !once_flag. get( ) ) ;
58+ once_flag. set ( true ) ;
59+
5360 let block: & [ u8 ] = core:: slice:: from_raw_parts ( start_addr as * const u8 , size) ;
5461 self . heap
5562 . borrow ( cs)
0 commit comments