1
1
use core:: alloc:: { GlobalAlloc , Layout } ;
2
- use core:: cell:: RefCell ;
2
+ use core:: cell:: { Cell , RefCell } ;
3
3
use core:: ptr:: { self , NonNull } ;
4
4
5
5
use const_default:: ConstDefault ;
@@ -11,6 +11,7 @@ type TlsfHeap = Tlsf<'static, usize, usize, { usize::BITS as usize }, { usize::B
11
11
/// A two-Level segregated fit heap.
12
12
pub struct Heap {
13
13
heap : Mutex < RefCell < TlsfHeap > > ,
14
+ once_flag : Mutex < Cell < bool > > ,
14
15
}
15
16
16
17
impl Heap {
@@ -21,6 +22,7 @@ impl Heap {
21
22
pub const fn empty ( ) -> Heap {
22
23
Heap {
23
24
heap : Mutex :: new ( RefCell :: new ( ConstDefault :: DEFAULT ) ) ,
25
+ once_flag : Mutex :: new ( Cell :: new ( false ) ) ,
24
26
}
25
27
}
26
28
@@ -49,7 +51,12 @@ impl Heap {
49
51
/// - This function must be called exactly ONCE.
50
52
/// - `size > 0`
51
53
pub unsafe fn init ( & self , start_addr : usize , size : usize ) {
54
+ assert ! ( size > 0 ) ;
52
55
critical_section:: with ( |cs| {
56
+ let once_flag = self . once_flag . borrow ( cs) ;
57
+ assert ! ( !once_flag. get( ) ) ;
58
+ once_flag. set ( true ) ;
59
+
53
60
let block: & [ u8 ] = core:: slice:: from_raw_parts ( start_addr as * const u8 , size) ;
54
61
self . heap
55
62
. borrow ( cs)
0 commit comments