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
@@ -44,12 +46,16 @@ impl Heap {
4446 ///
4547 /// # Safety
4648 ///
47- /// Obey these or Bad Stuff will happen.
49+ /// This function will panic if either of the following are true:
4850 ///
49- /// - This function must be called exactly ONCE.
50- /// - `size > 0`
51+ /// - this function is called more than ONCE.
52+ /// - `size == 0`.
5153 pub unsafe fn init ( & self , start_addr : usize , size : usize ) {
54+ assert ! ( size > 0 ) ;
5255 critical_section:: with ( |cs| {
56+ assert ! ( !self . once_flag. borrow( cs) . get( ) ) ;
57+ self . once_flag . borrow ( cs) . set ( true ) ;
58+
5359 let block: & [ u8 ] = core:: slice:: from_raw_parts ( start_addr as * const u8 , size) ;
5460 self . heap
5561 . borrow ( cs)
0 commit comments