diff --git a/libkernel/src/memory/page_alloc.rs b/libkernel/src/memory/page_alloc.rs index 0f8a067..f8ef7a2 100644 --- a/libkernel/src/memory/page_alloc.rs +++ b/libkernel/src/memory/page_alloc.rs @@ -427,7 +427,7 @@ pub trait PageAllocGetter: Send + Sync + 'static { } #[cfg(test)] -mod tests { +pub mod tests { use super::*; use crate::{ memory::{ @@ -438,13 +438,13 @@ mod tests { test::MockCpuOps, }; use core::{alloc::Layout, mem::MaybeUninit}; - use std::vec::Vec; // For collecting results in tests + use std::{mem::ManuallyDrop, ptr, vec::Vec}; // For collecting results in tests const KIB: usize = 1024; const MIB: usize = 1024 * KIB; const PAGE_SIZE: usize = 4096; - struct TestFixture { + pub struct TestFixture { allocator: FrameAllocator, base_ptr: *mut u8, layout: Layout, @@ -456,7 +456,7 @@ mod tests { /// - `mem_regions`: A slice of `(start, size)` tuples defining available memory regions. /// The `start` is relative to the beginning of the allocated memory block. /// - `res_regions`: A slice of `(start, size)` tuples for reserved regions (e.g., kernel). - fn new(mem_regions: &[(usize, usize)], res_regions: &[(usize, usize)]) -> Self { + pub fn new(mem_regions: &[(usize, usize)], res_regions: &[(usize, usize)]) -> Self { // Determine the total memory size required for the test environment. let total_size = mem_regions .iter() @@ -534,6 +534,12 @@ mod tests { fn free_pages(&self) -> usize { self.allocator.inner.lock_save_irq().free_pages } + + pub fn leak_allocator(self) -> FrameAllocator { + let this = ManuallyDrop::new(self); + + unsafe { ptr::read(&this.allocator) } + } } impl Drop for TestFixture {