@@ -7,7 +7,7 @@ use linked_list_allocator::Heap as LLHeap;
77
88/// A linked list first fit heap.
99pub struct Heap {
10- heap : Mutex < RefCell < LLHeap > > ,
10+ heap : Mutex < RefCell < ( LLHeap , bool ) > > ,
1111}
1212
1313impl Heap {
@@ -17,7 +17,7 @@ impl Heap {
1717 /// [`init`](Self::init) method before using the allocator.
1818 pub const fn empty ( ) -> Heap {
1919 Heap {
20- heap : Mutex :: new ( RefCell :: new ( LLHeap :: empty ( ) ) ) ,
20+ heap : Mutex :: new ( RefCell :: new ( ( LLHeap :: empty ( ) , false ) ) ) ,
2121 }
2222 }
2323
@@ -41,34 +41,38 @@ impl Heap {
4141 ///
4242 /// # Safety
4343 ///
44- /// Obey these or Bad Stuff will happen.
44+ /// This function will panic if either of the following are true:
4545 ///
46- /// - This function must be called exactly ONCE.
47- /// - `size > 0`
46+ /// - this function is called more than ONCE.
47+ /// - `size == 0`.
4848 pub unsafe fn init ( & self , start_addr : usize , size : usize ) {
49+ assert ! ( size > 0 ) ;
4950 critical_section:: with ( |cs| {
51+ assert ! ( !self . heap. borrow_ref( cs) . 1 ) ;
52+ self . heap . borrow_ref_mut ( cs) . 1 = true ;
53+
5054 self . heap
51- . borrow ( cs)
52- . borrow_mut ( )
55+ . borrow_ref_mut ( cs)
56+ . 0
5357 . init ( start_addr as * mut u8 , size) ;
5458 } ) ;
5559 }
5660
5761 /// Returns an estimate of the amount of bytes in use.
5862 pub fn used ( & self ) -> usize {
59- critical_section:: with ( |cs| self . heap . borrow ( cs) . borrow_mut ( ) . used ( ) )
63+ critical_section:: with ( |cs| self . heap . borrow_ref_mut ( cs) . 0 . used ( ) )
6064 }
6165
6266 /// Returns an estimate of the amount of bytes available.
6367 pub fn free ( & self ) -> usize {
64- critical_section:: with ( |cs| self . heap . borrow ( cs) . borrow_mut ( ) . free ( ) )
68+ critical_section:: with ( |cs| self . heap . borrow_ref_mut ( cs) . 0 . free ( ) )
6569 }
6670
6771 fn alloc ( & self , layout : Layout ) -> Option < NonNull < u8 > > {
6872 critical_section:: with ( |cs| {
6973 self . heap
70- . borrow ( cs)
71- . borrow_mut ( )
74+ . borrow_ref_mut ( cs)
75+ . 0
7276 . allocate_first_fit ( layout)
7377 . ok ( )
7478 } )
@@ -77,8 +81,8 @@ impl Heap {
7781 unsafe fn dealloc ( & self , ptr : * mut u8 , layout : Layout ) {
7882 critical_section:: with ( |cs| {
7983 self . heap
80- . borrow ( cs)
81- . borrow_mut ( )
84+ . borrow_ref_mut ( cs)
85+ . 0
8286 . deallocate ( NonNull :: new_unchecked ( ptr) , layout)
8387 } ) ;
8488 }
0 commit comments