mirror of
https://github.com/italicsjenga/agb.git
synced 2024-12-23 08:11:33 +11:00
commit
f91a464a89
|
@ -71,12 +71,6 @@ impl BlockAllocator {
|
||||||
f(inner)
|
f(inner)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
|
||||||
#[cfg(any(test, feature = "testing"))]
|
|
||||||
pub unsafe fn number_of_blocks(&self) -> u32 {
|
|
||||||
self.with_inner(|inner| inner.number_of_blocks())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub unsafe fn alloc(&self, layout: Layout) -> Option<NonNull<u8>> {
|
pub unsafe fn alloc(&self, layout: Layout) -> Option<NonNull<u8>> {
|
||||||
self.with_inner(|inner| inner.alloc(layout))
|
self.with_inner(|inner| inner.alloc(layout))
|
||||||
}
|
}
|
||||||
|
@ -105,20 +99,6 @@ impl BlockAllocatorInner {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
|
||||||
#[cfg(any(test, feature = "testing"))]
|
|
||||||
pub unsafe fn number_of_blocks(&mut self) -> u32 {
|
|
||||||
let mut count = 0;
|
|
||||||
|
|
||||||
let mut list_ptr = &mut self.state.first_free_block;
|
|
||||||
while let Some(mut current) = list_ptr {
|
|
||||||
count += 1;
|
|
||||||
list_ptr = &mut current.as_mut().next;
|
|
||||||
}
|
|
||||||
|
|
||||||
count
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Requests a brand new block from the inner bump allocator
|
/// Requests a brand new block from the inner bump allocator
|
||||||
fn new_block(&mut self, layout: Layout) -> Option<NonNull<u8>> {
|
fn new_block(&mut self, layout: Layout) -> Option<NonNull<u8>> {
|
||||||
let overall_layout = Block::either_layout(layout);
|
let overall_layout = Block::either_layout(layout);
|
||||||
|
|
|
@ -1,10 +1,7 @@
|
||||||
use core::alloc::{GlobalAlloc, Layout};
|
use core::alloc::Layout;
|
||||||
use core::cell::RefCell;
|
|
||||||
use core::ptr::NonNull;
|
use core::ptr::NonNull;
|
||||||
|
|
||||||
use super::SendNonNull;
|
use super::SendNonNull;
|
||||||
use crate::interrupt::free;
|
|
||||||
use bare_metal::Mutex;
|
|
||||||
|
|
||||||
pub(crate) struct StartEnd {
|
pub(crate) struct StartEnd {
|
||||||
pub start: fn() -> usize,
|
pub start: fn() -> usize,
|
||||||
|
@ -16,10 +13,6 @@ pub(crate) struct BumpAllocatorInner {
|
||||||
start_end: StartEnd,
|
start_end: StartEnd,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct BumpAllocator {
|
|
||||||
inner: Mutex<RefCell<BumpAllocatorInner>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl BumpAllocatorInner {
|
impl BumpAllocatorInner {
|
||||||
pub const fn new(start_end: StartEnd) -> Self {
|
pub const fn new(start_end: StartEnd) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
@ -58,20 +51,3 @@ impl BumpAllocatorInner {
|
||||||
NonNull::new(resulting_ptr as *mut _)
|
NonNull::new(resulting_ptr as *mut _)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BumpAllocator {
|
|
||||||
fn alloc_safe(&self, layout: Layout) -> Option<NonNull<u8>> {
|
|
||||||
free(|key| self.inner.borrow(key).borrow_mut().alloc(layout))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
unsafe impl GlobalAlloc for BumpAllocator {
|
|
||||||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
|
||||||
match self.alloc_safe(layout) {
|
|
||||||
None => core::ptr::null_mut(),
|
|
||||||
Some(p) => p.as_ptr(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {}
|
|
||||||
}
|
|
||||||
|
|
|
@ -119,11 +119,6 @@ static __IWRAM_ALLOC: BlockAllocator = unsafe {
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(any(test, feature = "testing"))]
|
|
||||||
pub(crate) unsafe fn number_of_blocks() -> u32 {
|
|
||||||
GLOBAL_ALLOC.number_of_blocks()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn iwram_data_end() -> usize {
|
fn iwram_data_end() -> usize {
|
||||||
extern "C" {
|
extern "C" {
|
||||||
static __iwram_end: u8;
|
static __iwram_end: u8;
|
||||||
|
|
|
@ -306,12 +306,6 @@ pub mod test_runner {
|
||||||
self(gba);
|
self(gba);
|
||||||
mgba::test_runner_measure_cycles();
|
mgba::test_runner_measure_cycles();
|
||||||
|
|
||||||
assert!(
|
|
||||||
unsafe { agb_alloc::number_of_blocks() } < 2,
|
|
||||||
"memory is being leaked, there are {} blocks",
|
|
||||||
unsafe { agb_alloc::number_of_blocks() }
|
|
||||||
);
|
|
||||||
|
|
||||||
mgba.print(format_args!("[ok]"), mgba::DebugLevel::Info)
|
mgba.print(format_args!("[ok]"), mgba::DebugLevel::Info)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue