mirror of
https://github.com/panpaul/tiny_os
synced 2024-09-20 09:45:19 +08:00
chore: introduce PAGE_LAYOUT
This commit is contained in:
parent
70ca917778
commit
13e331f4d2
@ -1,5 +1,6 @@
|
||||
use crate::utils::extern_addr::ExternSymbol;
|
||||
use crate::utils::size::KIB;
|
||||
use core::alloc::Layout;
|
||||
|
||||
extern "C" {
|
||||
pub static KERNEL_START: ExternSymbol;
|
||||
@ -30,6 +31,7 @@ extern "C" {
|
||||
}
|
||||
|
||||
pub const PAGE_SIZE: usize = 4 * KIB;
|
||||
pub const PAGE_LAYOUT: Layout = unsafe { Layout::from_size_align_unchecked(PAGE_SIZE, PAGE_SIZE) };
|
||||
|
||||
#[inline(always)]
|
||||
pub fn zero_bss() {
|
||||
|
@ -168,6 +168,7 @@ pub type BitmapAllocator32K = BitmapAllocator<Bitmap32K>;
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::arch::layout::PAGE_LAYOUT;
|
||||
|
||||
#[test_case]
|
||||
fn test_bitmap32() {
|
||||
@ -261,25 +262,20 @@ mod tests {
|
||||
|
||||
// alloc from empty
|
||||
for i in 0..32 {
|
||||
let ptr =
|
||||
unsafe { allocator.alloc(Layout::from_size_align(PAGE_SIZE, PAGE_SIZE).unwrap()) };
|
||||
let ptr = unsafe { allocator.alloc(PAGE_LAYOUT) };
|
||||
assert_eq!(PhysAddr::from(ptr).as_usize(), 0x42 + i * PAGE_SIZE);
|
||||
}
|
||||
|
||||
// dealloc
|
||||
for i in 0..16 {
|
||||
unsafe {
|
||||
allocator.dealloc(
|
||||
PhysAddr(0x42 + i * 2 * PAGE_SIZE).as_mut_ptr(),
|
||||
Layout::from_size_align(PAGE_SIZE, PAGE_SIZE).unwrap(),
|
||||
);
|
||||
allocator.dealloc(PhysAddr(0x42 + i * 2 * PAGE_SIZE).as_mut_ptr(), PAGE_LAYOUT);
|
||||
}
|
||||
}
|
||||
|
||||
// predictable alloc from dealloc pattern
|
||||
for i in 0..16 {
|
||||
let ptr =
|
||||
unsafe { allocator.alloc(Layout::from_size_align(PAGE_SIZE, PAGE_SIZE).unwrap()) };
|
||||
let ptr = unsafe { allocator.alloc(PAGE_LAYOUT) };
|
||||
assert_eq!(PhysAddr::from(ptr).as_usize(), 0x42 + i * 2 * PAGE_SIZE);
|
||||
}
|
||||
}
|
||||
|
@ -175,7 +175,7 @@ unsafe impl GlobalAlloc for FreeListAllocator {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::arch::layout::PAGE_SIZE;
|
||||
use crate::arch::layout::{PAGE_LAYOUT, PAGE_SIZE};
|
||||
|
||||
#[test_case]
|
||||
fn test_freelist() {
|
||||
@ -183,24 +183,19 @@ mod tests {
|
||||
|
||||
let allocator = FreeListAllocator::new(BASE, 32 * PAGE_SIZE);
|
||||
for i in 0..32 {
|
||||
let ptr =
|
||||
unsafe { allocator.alloc(Layout::from_size_align(PAGE_SIZE, PAGE_SIZE).unwrap()) };
|
||||
let ptr = unsafe { allocator.alloc(PAGE_LAYOUT) };
|
||||
assert_eq!(ptr as usize, (BASE + i * PAGE_SIZE).as_usize());
|
||||
}
|
||||
|
||||
let ptr =
|
||||
unsafe { allocator.alloc(Layout::from_size_align(PAGE_SIZE, PAGE_SIZE).unwrap()) };
|
||||
let ptr = unsafe { allocator.alloc(PAGE_LAYOUT) };
|
||||
assert_eq!(ptr, core::ptr::null_mut());
|
||||
|
||||
for i in (0..32).rev() {
|
||||
let ptr = (BASE + i * PAGE_SIZE).as_mut_ptr();
|
||||
unsafe {
|
||||
allocator.dealloc(ptr, Layout::from_size_align(PAGE_SIZE, PAGE_SIZE).unwrap())
|
||||
};
|
||||
unsafe { allocator.dealloc(ptr, PAGE_LAYOUT) };
|
||||
}
|
||||
|
||||
let ptr =
|
||||
unsafe { allocator.alloc(Layout::from_size_align(PAGE_SIZE, PAGE_SIZE).unwrap()) };
|
||||
let ptr = unsafe { allocator.alloc(PAGE_LAYOUT) };
|
||||
assert_eq!(ptr as usize, BASE.as_usize());
|
||||
}
|
||||
|
||||
@ -216,8 +211,7 @@ mod tests {
|
||||
|
||||
let mut cnt = 32 - 4;
|
||||
loop {
|
||||
let ptr =
|
||||
unsafe { allocator.alloc(Layout::from_size_align(PAGE_SIZE, PAGE_SIZE).unwrap()) };
|
||||
let ptr = unsafe { allocator.alloc(PAGE_LAYOUT) };
|
||||
if ptr.is_null() {
|
||||
assert_eq!(cnt, 0);
|
||||
break;
|
||||
|
Loading…
Reference in New Issue
Block a user