only use critical section once

This commit is contained in:
Corwin Kuiper 2022-01-17 21:46:14 +00:00
parent 17de9a42bd
commit 03e9517215
2 changed files with 31 additions and 30 deletions

View file

@ -4,7 +4,7 @@ use core::cell::RefCell;
use core::ptr::NonNull;
use crate::interrupt::free;
use bare_metal::Mutex;
use bare_metal::{CriticalSection, Mutex};
use super::bump_allocator::BumpAllocator;
use super::SendNonNull;
@ -47,9 +47,9 @@ impl BlockAllocator {
}
}
fn new_block(&self, layout: Layout) -> *mut u8 {
fn new_block(&self, layout: Layout, cs: &CriticalSection) -> *mut u8 {
let overall_layout = Block::either_layout(layout);
self.inner_allocator.alloc_safe(overall_layout)
self.inner_allocator.alloc_critical(overall_layout, cs)
}
}
@ -91,7 +91,7 @@ unsafe impl GlobalAlloc for BlockAllocator {
list_ptr = &mut curr_block.next;
}
self.new_block(layout)
self.new_block(layout, key)
})
}
}

View file

@ -4,7 +4,7 @@ use core::ptr::NonNull;
use super::SendNonNull;
use crate::interrupt::free;
use bare_metal::Mutex;
use bare_metal::{CriticalSection, Mutex};
pub(crate) struct BumpAllocator {
current_ptr: Mutex<RefCell<Option<SendNonNull<u8>>>>,
@ -19,32 +19,33 @@ impl BumpAllocator {
}
impl BumpAllocator {
pub fn alloc_critical(&self, layout: Layout, cs: &CriticalSection) -> *mut u8 {
let mut current_ptr = self.current_ptr.borrow(*cs).borrow_mut();
let ptr = if let Some(c) = *current_ptr {
c.as_ptr() as usize
} else {
get_data_end()
};
let alignment_bitmask = layout.align() - 1;
let fixup = ptr & alignment_bitmask;
let amount_to_add = layout.align() - fixup;
let resulting_ptr = ptr + amount_to_add;
let new_current_ptr = resulting_ptr + layout.size();
if new_current_ptr as usize >= super::EWRAM_END {
return core::ptr::null_mut();
}
*current_ptr = NonNull::new(new_current_ptr as *mut _).map(SendNonNull);
resulting_ptr as *mut _
}
pub fn alloc_safe(&self, layout: Layout) -> *mut u8 {
free(|key| {
let mut current_ptr = self.current_ptr.borrow(*key).borrow_mut();
let ptr = if let Some(c) = *current_ptr {
c.as_ptr() as usize
} else {
get_data_end()
};
let alignment_bitmask = layout.align() - 1;
let fixup = ptr & alignment_bitmask;
let amount_to_add = layout.align() - fixup;
let resulting_ptr = ptr + amount_to_add;
let new_current_ptr = resulting_ptr + layout.size();
if new_current_ptr as usize >= super::EWRAM_END {
return core::ptr::null_mut();
}
*current_ptr = NonNull::new(new_current_ptr as *mut _).map(SendNonNull);
resulting_ptr as *mut _
})
free(|key| self.alloc_critical(layout, key))
}
}