mirror of
https://github.com/italicsjenga/agb.git
synced 2025-01-26 09:06:33 +11:00
Move tests to agb_alloc mod.rs
This commit is contained in:
parent
7425e9973d
commit
4b9e4ce42a
2 changed files with 39 additions and 36 deletions
|
@ -3,9 +3,6 @@ use core::ptr::NonNull;
|
||||||
|
|
||||||
use crate::interrupt::Mutex;
|
use crate::interrupt::Mutex;
|
||||||
|
|
||||||
const EWRAM_START: usize = 0x0200_0000;
|
|
||||||
const EWRAM_END: usize = 0x0204_0000;
|
|
||||||
|
|
||||||
fn get_data_end() -> usize {
|
fn get_data_end() -> usize {
|
||||||
extern "C" {
|
extern "C" {
|
||||||
static __ewram_data_end: usize;
|
static __ewram_data_end: usize;
|
||||||
|
@ -46,7 +43,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 >= EWRAM_END {
|
if new_current_ptr as usize >= super::EWRAM_END {
|
||||||
return core::ptr::null_mut();
|
return core::ptr::null_mut();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,35 +60,3 @@ unsafe impl GlobalAlloc for BumpAllocator {
|
||||||
|
|
||||||
unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {}
|
unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[alloc_error_handler]
|
|
||||||
fn alloc_error(layout: Layout) -> ! {
|
|
||||||
panic!(
|
|
||||||
"Failed to allocate size {} with alignment {}",
|
|
||||||
layout.size(),
|
|
||||||
layout.align()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod test {
|
|
||||||
use super::*;
|
|
||||||
use alloc::boxed::Box;
|
|
||||||
|
|
||||||
#[test_case]
|
|
||||||
fn test_box(_gba: &mut crate::Gba) {
|
|
||||||
let first_box = Box::new(1);
|
|
||||||
let second_box = Box::new(2);
|
|
||||||
|
|
||||||
assert!(&*first_box as *const _ < &*second_box as *const _);
|
|
||||||
assert_eq!(*first_box, 1);
|
|
||||||
assert_eq!(*second_box, 2);
|
|
||||||
|
|
||||||
let address = &*first_box as *const _ as usize;
|
|
||||||
assert!(
|
|
||||||
address >= EWRAM_START && address < EWRAM_END,
|
|
||||||
"ewram is located between 0x0200_0000 and 0x0204_0000, address was actually found to be {:#010X}",
|
|
||||||
address
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,7 +1,45 @@
|
||||||
|
use core::alloc::Layout;
|
||||||
|
|
||||||
mod block_allocator;
|
mod block_allocator;
|
||||||
mod bump_allocator;
|
mod bump_allocator;
|
||||||
|
|
||||||
use bump_allocator::BumpAllocator;
|
use bump_allocator::BumpAllocator;
|
||||||
|
|
||||||
|
const EWRAM_END: usize = 0x0204_0000;
|
||||||
|
|
||||||
#[global_allocator]
|
#[global_allocator]
|
||||||
static GLOBAL_ALLOC: BumpAllocator = BumpAllocator::new();
|
static GLOBAL_ALLOC: BumpAllocator = BumpAllocator::new();
|
||||||
|
|
||||||
|
#[alloc_error_handler]
|
||||||
|
fn alloc_error(layout: Layout) -> ! {
|
||||||
|
panic!(
|
||||||
|
"Failed to allocate size {} with alignment {}",
|
||||||
|
layout.size(),
|
||||||
|
layout.align()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
const EWRAM_START: usize = 0x0200_0000;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
use alloc::boxed::Box;
|
||||||
|
|
||||||
|
#[test_case]
|
||||||
|
fn test_box(_gba: &mut crate::Gba) {
|
||||||
|
let first_box = Box::new(1);
|
||||||
|
let second_box = Box::new(2);
|
||||||
|
|
||||||
|
assert!(&*first_box as *const _ < &*second_box as *const _);
|
||||||
|
assert_eq!(*first_box, 1);
|
||||||
|
assert_eq!(*second_box, 2);
|
||||||
|
|
||||||
|
let address = &*first_box as *const _ as usize;
|
||||||
|
assert!(
|
||||||
|
address >= EWRAM_START && address < EWRAM_END,
|
||||||
|
"ewram is located between 0x0200_0000 and 0x0204_0000, address was actually found to be {:#010X}",
|
||||||
|
address
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue