@@ -7,7 +7,7 @@ use linked_list_allocator::Heap as LLHeap;
7
7
8
8
/// A linked list first fit heap.
9
9
pub struct Heap {
10
- heap : Mutex < RefCell < LLHeap > > ,
10
+ heap : Mutex < RefCell < ( LLHeap , bool ) > > ,
11
11
}
12
12
13
13
impl Heap {
@@ -17,7 +17,7 @@ impl Heap {
17
17
/// [`init`](Self::init) method before using the allocator.
18
18
pub const fn empty ( ) -> Heap {
19
19
Heap {
20
- heap : Mutex :: new ( RefCell :: new ( LLHeap :: empty ( ) ) ) ,
20
+ heap : Mutex :: new ( RefCell :: new ( ( LLHeap :: empty ( ) , false ) ) ) ,
21
21
}
22
22
}
23
23
@@ -41,34 +41,39 @@ impl Heap {
41
41
///
42
42
/// # Safety
43
43
///
44
- /// Obey these or Bad Stuff will happen .
44
+ /// Please ensure that `start_addr`` points to valid memory and that `size`` is correct .
45
45
///
46
- /// - This function must be called exactly ONCE.
47
- /// - `size > 0`
46
+ /// # Panics
47
+ ///
48
+ /// This function will panic if either of the following are true:
49
+ ///
50
+ /// - this function is called more than ONCE.
51
+ /// - `size == 0`.
48
52
pub unsafe fn init ( & self , start_addr : usize , size : usize ) {
53
+ assert ! ( size > 0 ) ;
49
54
critical_section:: with ( |cs| {
50
- self . heap
51
- . borrow ( cs )
52
- . borrow_mut ( )
53
- . init ( start_addr as * mut u8 , size) ;
55
+ let mut heap = self . heap . borrow_ref_mut ( cs ) ;
56
+ assert ! ( !heap . 1 ) ;
57
+ heap . 1 = true ;
58
+ heap . 0 . init ( start_addr as * mut u8 , size) ;
54
59
} ) ;
55
60
}
56
61
57
62
/// Returns an estimate of the amount of bytes in use.
58
63
pub fn used ( & self ) -> usize {
59
- critical_section:: with ( |cs| self . heap . borrow ( cs) . borrow_mut ( ) . used ( ) )
64
+ critical_section:: with ( |cs| self . heap . borrow_ref_mut ( cs) . 0 . used ( ) )
60
65
}
61
66
62
67
/// Returns an estimate of the amount of bytes available.
63
68
pub fn free ( & self ) -> usize {
64
- critical_section:: with ( |cs| self . heap . borrow ( cs) . borrow_mut ( ) . free ( ) )
69
+ critical_section:: with ( |cs| self . heap . borrow_ref_mut ( cs) . 0 . free ( ) )
65
70
}
66
71
67
72
fn alloc ( & self , layout : Layout ) -> Option < NonNull < u8 > > {
68
73
critical_section:: with ( |cs| {
69
74
self . heap
70
- . borrow ( cs)
71
- . borrow_mut ( )
75
+ . borrow_ref_mut ( cs)
76
+ . 0
72
77
. allocate_first_fit ( layout)
73
78
. ok ( )
74
79
} )
@@ -77,8 +82,8 @@ impl Heap {
77
82
unsafe fn dealloc ( & self , ptr : * mut u8 , layout : Layout ) {
78
83
critical_section:: with ( |cs| {
79
84
self . heap
80
- . borrow ( cs)
81
- . borrow_mut ( )
85
+ . borrow_ref_mut ( cs)
86
+ . 0
82
87
. deallocate ( NonNull :: new_unchecked ( ptr) , layout)
83
88
} ) ;
84
89
}
0 commit comments