mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-26 19:03:27 +00:00
Add a fixed-size cache of frame allocation
This commit is contained in:
committed by
Tate, Hongliang Tian
parent
5f05963ee5
commit
28e7c0ff1f
@ -22,11 +22,51 @@
|
||||
//! [`GlobalFrameAllocator`]: ostd::mm::GlobalFrameAllocator
|
||||
//! [`global_frame_allocator`]: ostd::global_frame_allocator
|
||||
|
||||
mod allocator;
|
||||
use core::alloc::Layout;
|
||||
|
||||
use ostd::{
|
||||
mm::{frame::GlobalFrameAllocator, Paddr},
|
||||
trap,
|
||||
};
|
||||
|
||||
mod cache;
|
||||
mod chunk;
|
||||
mod per_cpu_counter;
|
||||
mod pools;
|
||||
mod set;
|
||||
|
||||
#[cfg(ktest)]
|
||||
mod test;
|
||||
|
||||
pub use allocator::{load_total_free_size, FrameAllocator};
|
||||
/// Loads the total size (in bytes) of free memory in the allocator.
|
||||
pub fn load_total_free_size() -> usize {
|
||||
per_cpu_counter::read_total_free_size()
|
||||
}
|
||||
|
||||
/// The global frame allocator provided by OSDK.
|
||||
///
|
||||
/// It is a singleton that provides frame allocation for the kernel. If
|
||||
/// multiple instances of this struct are created, all the member functions
|
||||
/// will eventually access the same allocator.
|
||||
pub struct FrameAllocator;
|
||||
|
||||
impl GlobalFrameAllocator for FrameAllocator {
|
||||
fn alloc(&self, layout: Layout) -> Option<Paddr> {
|
||||
let guard = trap::disable_local();
|
||||
let res = cache::alloc(&guard, layout);
|
||||
if res.is_some() {
|
||||
per_cpu_counter::sub_free_size(&guard, layout.size());
|
||||
}
|
||||
res
|
||||
}
|
||||
|
||||
fn dealloc(&self, addr: Paddr, size: usize) {
|
||||
self.add_free_memory(addr, size);
|
||||
}
|
||||
|
||||
fn add_free_memory(&self, addr: Paddr, size: usize) {
|
||||
let guard = trap::disable_local();
|
||||
per_cpu_counter::add_free_size(&guard, size);
|
||||
cache::add_free_memory(&guard, addr, size);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user