Re-organize code of frame allocator's pools

This commit is contained in:
Zhang Junyang
2025-03-19 15:35:12 +08:00
committed by Tate, Hongliang Tian
parent 77c4feffd6
commit ba0dc8c122
4 changed files with 146 additions and 136 deletions

View File

@ -69,14 +69,14 @@ impl<const NR_CONT_FRAMES: usize, const COUNT: usize> CacheArray<NR_CONT_FRAMES,
///
/// It may deallocate directly to this cache. If the cache is full, it will
/// deallocate to the global pool.
fn add_free_memory(&mut self, guard: &DisabledLocalIrqGuard, addr: Paddr) {
fn dealloc(&mut self, guard: &DisabledLocalIrqGuard, addr: Paddr) {
if self.push_front(addr).is_none() {
super::pools::add_free_memory(guard, addr, Self::segment_size());
super::pools::dealloc(guard, addr, Self::segment_size());
let nr_to_dealloc = COUNT * 2 / 3;
for _ in 0..nr_to_dealloc {
let frame = self.pop_front().unwrap();
super::pools::add_free_memory(guard, frame, Self::segment_size());
super::pools::dealloc(guard, frame, Self::segment_size());
}
};
}
@ -131,10 +131,10 @@ pub(super) fn alloc(guard: &DisabledLocalIrqGuard, layout: Layout) -> Option<Pad
}
}
pub(super) fn add_free_memory(guard: &DisabledLocalIrqGuard, addr: Paddr, size: usize) {
pub(super) fn dealloc(guard: &DisabledLocalIrqGuard, addr: Paddr, size: usize) {
let nr_frames = size / PAGE_SIZE;
if nr_frames > 4 {
super::pools::add_free_memory(guard, addr, size);
super::pools::dealloc(guard, addr, size);
return;
}
@ -142,10 +142,10 @@ pub(super) fn add_free_memory(guard: &DisabledLocalIrqGuard, addr: Paddr, size:
let mut cache = cache_cell.borrow_mut();
match nr_frames {
1 => cache.cache1.add_free_memory(guard, addr),
2 => cache.cache2.add_free_memory(guard, addr),
3 => cache.cache3.add_free_memory(guard, addr),
4 => cache.cache4.add_free_memory(guard, addr),
_ => super::pools::add_free_memory(guard, addr, size),
1 => cache.cache1.dealloc(guard, addr),
2 => cache.cache2.dealloc(guard, addr),
3 => cache.cache3.dealloc(guard, addr),
4 => cache.cache4.dealloc(guard, addr),
_ => super::pools::dealloc(guard, addr, size),
}
}