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
@@ -44,12 +46,16 @@ impl Heap {
44
46
///
45
47
/// # Safety
46
48
///
47
- /// Obey these or Bad Stuff will happen.
49
+ /// This function will panic if either of the following are true:
48
50
///
49
- /// - This function must be called exactly ONCE.
50
- /// - `size > 0`
51
+ /// - this function is called more than ONCE.
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
+ assert ! ( !self . once_flag. borrow( cs) . get( ) ) ;
57
+ self . once_flag . borrow ( cs) . set ( true ) ;
58
+
53
59
let block: & [ u8 ] = core:: slice:: from_raw_parts ( start_addr as * const u8 , size) ;
54
60
self . heap
55
61
. borrow ( cs)
0 commit comments