Skip to content

Commit

Permalink
fix: fix the error for talc allocator
Browse files Browse the repository at this point in the history
  • Loading branch information
Godones committed May 9, 2024
1 parent 68aea4c commit 6fe9e7b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
9 changes: 3 additions & 6 deletions subsystems/mem/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ mod frame;
#[cfg(feature = "buddy")]
mod heap;
mod manager;
#[cfg(feature = "talloc")]
mod talc_wrapper;
mod vmm;

pub use frame::{alloc_frame_trackers, alloc_frames, free_frames, FrameTracker, VmmPageAllocator};
Expand All @@ -23,12 +25,7 @@ static HEAP_ALLOCATOR: heap::HeapAllocator = heap::HeapAllocator::new();

#[cfg(feature = "talc")]
#[global_allocator]
static HEAP_ALLOCATOR: talc::Talck<ksync::Mutex<()>, talc::ClaimOnOom> = talc::Talc::new(unsafe {
talc::ClaimOnOom::new(talc::Span::from_const_array(core::ptr::addr_of!(
KERNEL_HEAP
)))
})
.lock();
static HEAP_ALLOCATOR: talc_wrapper::TalcAllocator = talc_wrapper::TalcAllocator;

#[cfg(any(feature = "talloc", feature = "buddy"))]
static mut KERNEL_HEAP: [u8; HEAP_SIZE] = [0; HEAP_SIZE];
Expand Down
37 changes: 37 additions & 0 deletions subsystems/mem/src/talc_wrapper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use core::alloc::GlobalAlloc;

use config::FRAME_SIZE;
use log::trace;
use talc::{ClaimOnOom, Talc, Talck};

use crate::{alloc_frames, free_frames, KERNEL_HEAP};

static HEAP_ALLOCATOR: Talck<ksync::Mutex<()>, ClaimOnOom> = Talc::new(unsafe {
ClaimOnOom::new(talc::Span::from_const_array(core::ptr::addr_of!(
KERNEL_HEAP
)))
})
.lock();

pub struct TalcAllocator;

unsafe impl GlobalAlloc for TalcAllocator {
unsafe fn alloc(&self, layout: core::alloc::Layout) -> *mut u8 {
if layout.size() >= 5 * 1024 * 1024 {
let need_page = (layout.size() + FRAME_SIZE - 1) / FRAME_SIZE;
trace!("alloc big page: {:#x}", layout.size());
alloc_frames(need_page)
} else {
HEAP_ALLOCATOR.alloc(layout)
}
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: core::alloc::Layout) {
if layout.size() >= 5 * 1024 * 1024 {
let need_page = (layout.size() + FRAME_SIZE - 1) / FRAME_SIZE;
trace!("free big page: {:#x}", layout.size());
free_frames(ptr, need_page);
} else {
HEAP_ALLOCATOR.dealloc(ptr, layout);
}
}
}

0 comments on commit 6fe9e7b

Please sign in to comment.