extend to also supporting the end

This commit is contained in:
Corwin 2022-02-07 21:43:17 +00:00
parent 3c6c7efc79
commit 475edadcb9
3 changed files with 22 additions and 8 deletions

View file

@ -12,7 +12,7 @@ use core::ptr::NonNull;
use crate::interrupt::free;
use bare_metal::{CriticalSection, Mutex};
use super::bump_allocator::BumpAllocator;
use super::bump_allocator::{BumpAllocator, StartEnd};
use super::SendNonNull;
struct Block {
@ -49,7 +49,7 @@ pub(crate) struct BlockAllocator {
}
impl BlockAllocator {
pub(super) const unsafe fn new(start: fn() -> usize) -> Self {
pub(super) const unsafe fn new(start: StartEnd) -> Self {
Self {
inner_allocator: BumpAllocator::new(start),
state: Mutex::new(RefCell::new(BlockAllocatorState {

View file

@ -6,16 +6,23 @@ use super::SendNonNull;
use crate::interrupt::free;
use bare_metal::{CriticalSection, Mutex};
pub(crate) struct AddrFn(pub fn() -> usize);
pub(crate) struct StartEnd {
pub start: AddrFn,
pub end: AddrFn,
}
pub(crate) struct BumpAllocator {
current_ptr: Mutex<RefCell<Option<SendNonNull<u8>>>>,
start: Mutex<fn() -> usize>,
start_end: Mutex<StartEnd>,
}
impl BumpAllocator {
pub const fn new(start: fn() -> usize) -> Self {
pub const fn new(start_end: StartEnd) -> Self {
Self {
current_ptr: Mutex::new(RefCell::new(None)),
start: Mutex::new(start),
start_end: Mutex::new(start_end),
}
}
}
@ -27,7 +34,7 @@ impl BumpAllocator {
let ptr = if let Some(c) = *current_ptr {
c.as_ptr() as usize
} else {
self.start.borrow(*cs)()
self.start_end.borrow(*cs).start.0()
};
let alignment_bitmask = layout.align() - 1;
@ -38,7 +45,7 @@ impl BumpAllocator {
let resulting_ptr = ptr + amount_to_add;
let new_current_ptr = resulting_ptr + layout.size();
if new_current_ptr as usize >= super::EWRAM_END {
if new_current_ptr as usize >= self.start_end.borrow(*cs).end.0() {
return core::ptr::null_mut();
}

View file

@ -7,6 +7,8 @@ mod bump_allocator;
use block_allocator::BlockAllocator;
use self::bump_allocator::{AddrFn, StartEnd};
struct SendNonNull<T>(NonNull<T>);
unsafe impl<T> Send for SendNonNull<T> {}
@ -33,7 +35,12 @@ impl<T> DerefMut for SendNonNull<T> {
const EWRAM_END: usize = 0x0204_0000;
#[global_allocator]
static GLOBAL_ALLOC: BlockAllocator = unsafe { BlockAllocator::new(get_data_end) };
static GLOBAL_ALLOC: BlockAllocator = unsafe {
BlockAllocator::new(StartEnd {
start: AddrFn(get_data_end),
end: AddrFn(|| EWRAM_END),
})
};
#[cfg(test)]
pub unsafe fn number_of_blocks() -> u32 {