Skip to content

Commit 2f27a7d

Browse files
author
Filip Chovanec
committed
Add root allocator
1 parent 9f5e5d6 commit 2f27a7d

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

kernel/src/memman/mall.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use core::alloc::GlobalAlloc;
2+
extern crate alloc;
3+
4+
type MainAllocator = RootAllocator;
5+
6+
#[global_allocator]
7+
static GLOBAL_ALLOC: MainAllocator = MainAllocator::new();
8+
9+
struct RootAllocator {}
10+
11+
impl RootAllocator {
12+
const fn new() -> Self {
13+
Self {}
14+
}
15+
}
16+
17+
unsafe impl GlobalAlloc for RootAllocator {
18+
unsafe fn alloc(&self, layout: core::alloc::Layout) -> *mut u8 {
19+
super::staticalloc::GLOBAL_STATIC_ALLOCATOR.alloc(layout)
20+
}
21+
unsafe fn dealloc(&self, ptr: *mut u8, layout: core::alloc::Layout) {
22+
super::staticalloc::GLOBAL_STATIC_ALLOCATOR.dealloc(ptr, layout);
23+
}
24+
}

kernel/src/memman/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
55
*/
66

7+
pub mod mall;
78
pub mod map;
89
pub mod staticalloc;

kernel/src/memman/staticalloc.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ extern crate alloc;
44

55
const MAX: usize = crate::config::STATIC_ALLOCATOR_SIZE_BYTES;
66

7-
#[global_allocator]
8-
static GLOBAL_STATIC_ALLOCATOR: StaticAllocator<MAX> = StaticAllocator::new();
7+
pub static GLOBAL_STATIC_ALLOCATOR: StaticAllocator<MAX> = StaticAllocator::new();
98

10-
struct StaticAllocator<const SIZE: usize> {
9+
pub struct StaticAllocator<const SIZE: usize> {
1110
buffer: [u8; SIZE],
1211
bump_addr: spin::Mutex<usize>,
1312
refcount: spin::Mutex<usize>,

0 commit comments

Comments
 (0)