Skip to content

Commit 1fa8522

Browse files
author
Filip Chovanec
committed
Add rust allocation with staticalloc mod
1 parent 72f0a79 commit 1fa8522

File tree

8 files changed

+74
-23
lines changed

8 files changed

+74
-23
lines changed

config/profiles/default/rkernel.rs

+3
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ pub const MESSAGE_FIRST: &str = "Hello World!\n";
66
/// If it overflows , a kernel panic is triggered
77
pub const LOG_STATIC_CAPACITY: usize = 204_800;
88

9+
/// The static allocator allocator is used by rust prior to any other being setup. It takes space in the kernel binary itself. Using this we can size its size.
10+
///
11+
pub const STATIC_ALLOCATOR_SIZE_BYTES: usize = 100_000;

kernel/.cargo/config.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[unstable]
2-
build-std = ["core", "compiler_builtins"]
2+
build-std = ["core", "compiler_builtins", "alloc"]

kernel/limine.conf

-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@ timeout = 0
22

33
/ REZ_OS
44
protocol: limine
5-
textmode: yes
65
path: boot():/kernel.x86_64.bin

kernel/src/driver/lfb.rs

-20
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,8 @@ use tinybmp::Bmp;
55

66
pub fn init() {
77
let fb = crate::limine::framebuffer0();
8-
let circle = Circle::new(Point::new(22, 22), 140).into_styled(PrimitiveStyle::with_stroke(
9-
crate::limine::LColor {
10-
r: 250,
11-
g: 250,
12-
b: 250,
13-
},
14-
1,
15-
));
16-
circle.draw(fb).unwrap();
178

189
let bmp_data = include_bytes!("../../../cat.bmp");
1910
let bmp = Bmp::from_slice(bmp_data).unwrap();
2011
Image::new(&bmp, Point::new(200, 200)).draw(fb).unwrap();
21-
22-
/*
23-
//draw_pixel(fb, 600, 600, (250, 250, 250)).unwrap();
24-
let mut a = 0;
25-
loop {
26-
for x in 1..1200 {
27-
for y in 1..800 {
28-
draw_pixel(fb, x, y, (a, 250 - a, a)).unwrap();
29-
}
30-
}
31-
}*/
3212
}

kernel/src/main.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
*/
2525

2626
use core::panic::{self, PanicInfo};
27-
// Do not remove these imports, they may useless but they prevent link errors
27+
extern crate alloc;
28+
// Do not remove these imports, they may look useless but they prevent link errors
2829
#[allow(unused_imports)]
2930
use rlibc;
3031
use rlibcex;
@@ -144,6 +145,7 @@ pub extern "C" fn kmain() {
144145
log!("HHDM: 0x{:016X}\n", hhdm);
145146

146147
log!("Framebuffer 0: {:?}\n", limine::framebuffer0());
148+
alloc::boxed::Box::new(4);
147149
driver::lfb::init();
148150

149151
log!("Nothing to do!\n");

kernel/src/memman/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
*/
66

77
pub mod map;
8+
pub mod staticalloc;

kernel/src/memman/staticalloc.rs

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use crate::log;
2+
use core::alloc::GlobalAlloc;
3+
extern crate alloc;
4+
5+
const MAX: usize = crate::config::STATIC_ALLOCATOR_SIZE_BYTES;
6+
7+
#[global_allocator]
8+
static GLOBAL_STATIC_ALLOCATOR: StaticAllocator<MAX> = StaticAllocator::new();
9+
10+
struct StaticAllocator<const SIZE: usize> {
11+
buffer: [u8; SIZE],
12+
bump_addr: spin::Mutex<usize>,
13+
}
14+
15+
impl<const SIZE: usize> StaticAllocator<SIZE> {
16+
const fn new() -> Self {
17+
Self {
18+
// just a data holder, not actually referenced but still needed
19+
buffer: [0; SIZE],
20+
// get properly inited in its `alloc` definition
21+
bump_addr: spin::Mutex::new(0),
22+
}
23+
}
24+
}
25+
26+
unsafe impl<const SIZE: usize> GlobalAlloc for StaticAllocator<SIZE> {
27+
unsafe fn alloc(&self, layout: core::alloc::Layout) -> *mut u8 {
28+
// expect by rust docs
29+
if layout.size() == 0 {
30+
log!("ZERO!");
31+
return core::ptr::null_mut();
32+
}
33+
34+
let mut bumplock = self.bump_addr.lock();
35+
36+
// kinda bad design, but its late and works so idc
37+
if *bumplock == 0 {
38+
*bumplock = self.buffer.as_ptr() as usize;
39+
}
40+
41+
// we take the first free chunk, or a bit more for it to be aligned
42+
let first_start = *bumplock;
43+
let aligned_start = if first_start % layout.align() > 0 {
44+
first_start + layout.align() - (first_start % layout.align())
45+
} else {
46+
first_start
47+
};
48+
// if the returned chunk is greater than last address of buffer, we ran out of memory :(
49+
if aligned_start + layout.size() >= self.buffer.as_ptr() as usize + self.buffer.len() {
50+
log!("NO SPACE :(!");
51+
return core::ptr::null_mut();
52+
}
53+
// upadte that ting
54+
*bumplock = aligned_start + layout.size();
55+
56+
aligned_start as *mut u8
57+
}
58+
// we dont free this lmao
59+
unsafe fn dealloc(&self, ptr: *mut u8, layout: core::alloc::Layout) {}
60+
}

smeltfile.py

+6
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,10 @@ def isoroot():
8989

9090
return File(output)
9191

92+
@task("docs")
93+
def doc_kernel():
94+
use(file_tree("kernel/src/"))
95+
shell("cd kernel && cargo doc --document-private-items")
96+
return File("kernel/target/doc/kernel/index.html")
97+
9298
smelt3.cli()

0 commit comments

Comments
 (0)