mirror of
https://github.com/italicsjenga/agb.git
synced 2024-12-25 09:11:34 +11:00
extend to also supporting the end
This commit is contained in:
parent
3c6c7efc79
commit
475edadcb9
|
@ -12,7 +12,7 @@ use core::ptr::NonNull;
|
||||||
use crate::interrupt::free;
|
use crate::interrupt::free;
|
||||||
use bare_metal::{CriticalSection, Mutex};
|
use bare_metal::{CriticalSection, Mutex};
|
||||||
|
|
||||||
use super::bump_allocator::BumpAllocator;
|
use super::bump_allocator::{BumpAllocator, StartEnd};
|
||||||
use super::SendNonNull;
|
use super::SendNonNull;
|
||||||
|
|
||||||
struct Block {
|
struct Block {
|
||||||
|
@ -49,7 +49,7 @@ pub(crate) struct BlockAllocator {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BlockAllocator {
|
impl BlockAllocator {
|
||||||
pub(super) const unsafe fn new(start: fn() -> usize) -> Self {
|
pub(super) const unsafe fn new(start: StartEnd) -> Self {
|
||||||
Self {
|
Self {
|
||||||
inner_allocator: BumpAllocator::new(start),
|
inner_allocator: BumpAllocator::new(start),
|
||||||
state: Mutex::new(RefCell::new(BlockAllocatorState {
|
state: Mutex::new(RefCell::new(BlockAllocatorState {
|
||||||
|
|
|
@ -6,16 +6,23 @@ use super::SendNonNull;
|
||||||
use crate::interrupt::free;
|
use crate::interrupt::free;
|
||||||
use bare_metal::{CriticalSection, Mutex};
|
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 {
|
pub(crate) struct BumpAllocator {
|
||||||
current_ptr: Mutex<RefCell<Option<SendNonNull<u8>>>>,
|
current_ptr: Mutex<RefCell<Option<SendNonNull<u8>>>>,
|
||||||
start: Mutex<fn() -> usize>,
|
start_end: Mutex<StartEnd>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BumpAllocator {
|
impl BumpAllocator {
|
||||||
pub const fn new(start: fn() -> usize) -> Self {
|
pub const fn new(start_end: StartEnd) -> Self {
|
||||||
Self {
|
Self {
|
||||||
current_ptr: Mutex::new(RefCell::new(None)),
|
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 {
|
let ptr = if let Some(c) = *current_ptr {
|
||||||
c.as_ptr() as usize
|
c.as_ptr() as usize
|
||||||
} else {
|
} else {
|
||||||
self.start.borrow(*cs)()
|
self.start_end.borrow(*cs).start.0()
|
||||||
};
|
};
|
||||||
|
|
||||||
let alignment_bitmask = layout.align() - 1;
|
let alignment_bitmask = layout.align() - 1;
|
||||||
|
@ -38,7 +45,7 @@ impl BumpAllocator {
|
||||||
let resulting_ptr = ptr + amount_to_add;
|
let resulting_ptr = ptr + amount_to_add;
|
||||||
let new_current_ptr = resulting_ptr + layout.size();
|
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();
|
return core::ptr::null_mut();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,8 @@ mod bump_allocator;
|
||||||
|
|
||||||
use block_allocator::BlockAllocator;
|
use block_allocator::BlockAllocator;
|
||||||
|
|
||||||
|
use self::bump_allocator::{AddrFn, StartEnd};
|
||||||
|
|
||||||
struct SendNonNull<T>(NonNull<T>);
|
struct SendNonNull<T>(NonNull<T>);
|
||||||
unsafe impl<T> Send for SendNonNull<T> {}
|
unsafe impl<T> Send for SendNonNull<T> {}
|
||||||
|
|
||||||
|
@ -33,7 +35,12 @@ impl<T> DerefMut for SendNonNull<T> {
|
||||||
const EWRAM_END: usize = 0x0204_0000;
|
const EWRAM_END: usize = 0x0204_0000;
|
||||||
|
|
||||||
#[global_allocator]
|
#[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)]
|
#[cfg(test)]
|
||||||
pub unsafe fn number_of_blocks() -> u32 {
|
pub unsafe fn number_of_blocks() -> u32 {
|
||||||
|
|
Loading…
Reference in a new issue