-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix the error for talc allocator
- Loading branch information
Showing
2 changed files
with
40 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |