Skip to content

Commit

Permalink
feat: implement allocator, kindof
Browse files Browse the repository at this point in the history
  • Loading branch information
marekvospel committed Nov 26, 2023
1 parent 1dc5fb2 commit f1dba3f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 22 deletions.
16 changes: 0 additions & 16 deletions src/memory/allocator.rs

This file was deleted.

46 changes: 46 additions & 0 deletions src/memory/allocator/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use core::{
alloc::{GlobalAlloc, Layout},
mem::transmute,
};

static mut NODES: Option<&mut LinkedAllocatorNode> = None;

#[global_allocator]
static ALLOCATOR: LinkedListAllocator = LinkedListAllocator {};

pub struct LinkedListAllocator {}

unsafe impl<'a> GlobalAlloc for LinkedListAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
if NODES.is_none() {
panic!("LinkedListAllocator has not been initialized yet");
}

let node = NODES.as_deref_mut().unwrap();

if layout.size() > node.size {
panic!("Out of heap memory");
}

node.size = node.size - layout.size();
let ptr = node as *mut _ as usize + node.size;

ptr as *mut u8
}

unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {
// unimplemented!("Deallocate")
}
}

pub unsafe fn init(node: &'static mut LinkedAllocatorNode) {
if NODES.is_some() {
panic!("LinkedListAllocator is already initialized");
}
NODES = Some(node);
}

pub struct LinkedAllocatorNode<'a> {
pub(crate) size: usize,
pub(crate) next: Option<&'a mut Self>,
}
33 changes: 27 additions & 6 deletions src/memory/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use multiboot2::{BootInformation, ElfSectionFlags};
use x86_64::registers::control::{Cr0, Cr0Flags};
use x86_64::registers::model_specific::{Efer, EferFlags, Msr};
use x86_64::registers::xcontrol::{XCr0, XCr0Flags};
use core::ops::RangeInclusive;

use crate::memory::allocator::LinkedAllocatorNode;
use crate::memory::frames::bump_alloc::BumpAllocator;
use crate::memory::frames::{FrameIter, PhysicalFrame, PAGE_SIZE};
use crate::memory::paging::entry::EntryFlags;
use crate::memory::paging::mapper::{self, ActivePageTable};
use crate::memory::paging::mapper::ActivePageTable;
use crate::memory::paging::Page;
use crate::println;
use core::ops::RangeInclusive;
use alloc::string::String;
use multiboot2::{BootInformation, ElfSectionFlags};
use x86_64::registers::control::{Cr0, Cr0Flags};
use x86_64::registers::model_specific::{Efer, EferFlags, Msr};
use x86_64::registers::xcontrol::{XCr0, XCr0Flags};

use self::frames::FrameAlloc;
use self::paging::inactive::InactivePageTable;
Expand Down Expand Up @@ -45,6 +47,25 @@ pub(super) fn init(boot_info: &BootInformation) -> () {
remap_kernel(&mut frame_allocator, &mut active_page, boot_info);

println!("[OK] Kernel remapped!");
println!("[INFO] Initializing linked list allocator...");

let page = Page::containing_address(0xffff_ffff_ff00_0000);
let frame = frame_allocator.allocate_frame().expect("Out of memory");
active_page.map_to(page, frame, EntryFlags::WRITABLE, &mut frame_allocator);
let node: &mut LinkedAllocatorNode<'static> = unsafe { &mut *(page.start_address() as *mut _) };

*node = LinkedAllocatorNode {
size: PAGE_SIZE as usize - 16,
next: None,
};

unsafe { allocator::init(node) };

println!("[OK] Linked list allocator initialized!");
let mut str = String::from("Hello");

str += " World!";
println!("String: {}", str);
}

fn remap_kernel<A: FrameAlloc>(
Expand Down

0 comments on commit f1dba3f

Please sign in to comment.