From 4b9e4ce42a369ae040e6ff0abd3a95a18ef99acc Mon Sep 17 00:00:00 2001 From: Gwilym Kuiper Date: Mon, 16 Aug 2021 22:28:41 +0100 Subject: [PATCH] Move tests to agb_alloc mod.rs --- agb/src/agb_alloc/bump_allocator.rs | 37 +--------------------------- agb/src/agb_alloc/mod.rs | 38 +++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 36 deletions(-) diff --git a/agb/src/agb_alloc/bump_allocator.rs b/agb/src/agb_alloc/bump_allocator.rs index 65335f10..733e6667 100644 --- a/agb/src/agb_alloc/bump_allocator.rs +++ b/agb/src/agb_alloc/bump_allocator.rs @@ -3,9 +3,6 @@ use core::ptr::NonNull; use crate::interrupt::Mutex; -const EWRAM_START: usize = 0x0200_0000; -const EWRAM_END: usize = 0x0204_0000; - fn get_data_end() -> usize { extern "C" { static __ewram_data_end: usize; @@ -46,7 +43,7 @@ impl BumpAllocator { let resulting_ptr = ptr + amount_to_add; 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(); } @@ -63,35 +60,3 @@ unsafe impl GlobalAlloc for BumpAllocator { 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 - ); - } -} diff --git a/agb/src/agb_alloc/mod.rs b/agb/src/agb_alloc/mod.rs index 45220b1e..ec01fb04 100644 --- a/agb/src/agb_alloc/mod.rs +++ b/agb/src/agb_alloc/mod.rs @@ -1,7 +1,45 @@ +use core::alloc::Layout; + mod block_allocator; mod bump_allocator; use bump_allocator::BumpAllocator; +const EWRAM_END: usize = 0x0204_0000; + #[global_allocator] 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 + ); + } +}