page_alloc: make test fixture public

Having the ability to use the `FrameAlloctor`, simulating via the heap
is a very useful pattern when testing libkernel code. Make the test
fixture public for other modules to use.
This commit is contained in:
Matthew Leach
2025-12-17 22:06:46 +00:00
committed by Ashwin Naren
parent 54f2d683b8
commit a867493fa7

View File

@@ -427,7 +427,7 @@ pub trait PageAllocGetter<C: CpuOps>: 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<MockCpuOps>,
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<MockCpuOps> {
let this = ManuallyDrop::new(self);
unsafe { ptr::read(&this.allocator) }
}
}
impl Drop for TestFixture {