From 4600a8c7742b4c01f71490de25cc50282dd65ff7 Mon Sep 17 00:00:00 2001 From: Yash Karandikar Date: Sun, 19 Sep 2021 13:59:28 -0500 Subject: [PATCH] Create dummy allocator --- .cargo/config.toml | 2 +- src/allocator.rs | 17 +++++++++++++++++ src/main.rs | 9 +++++++++ src/memory.rs | 2 +- 4 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 src/allocator.rs diff --git a/.cargo/config.toml b/.cargo/config.toml index ebcc395..add8ee1 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,6 +1,6 @@ [unstable] build-std-features = ["compiler-builtins-mem"] -build-std = ["core", "compiler_builtins"] +build-std = ["core", "compiler_builtins", "alloc"] [build] target = "x86_64-karxos.json" diff --git a/src/allocator.rs b/src/allocator.rs new file mode 100644 index 0000000..c2146b9 --- /dev/null +++ b/src/allocator.rs @@ -0,0 +1,17 @@ +use alloc::alloc::{GlobalAlloc, Layout}; +use core::ptr::null_mut; + +pub struct Dummy; + +unsafe impl GlobalAlloc for Dummy { + unsafe fn alloc(&self, _layout: Layout) -> *mut u8 { + null_mut() + } + + unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) { + panic!("dealloc should never be called"); + } +} + +#[global_allocator] +static ALLOCATOR: Dummy = Dummy; diff --git a/src/main.rs b/src/main.rs index 34148cf..b35cfe5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,22 +5,31 @@ #![test_runner(crate::test_runner)] #![reexport_test_harness_main = "test_main"] #![feature(abi_x86_interrupt)] +#![feature(alloc_error_handler)] mod gdt; mod interrupts; mod shell; mod vga_buffer; mod memory; +mod allocator; use core::panic::PanicInfo; use bootloader::BootInfo; +extern crate alloc; + #[panic_handler] fn panic(info: &PanicInfo) -> ! { println!("{}", info); loop {} } +#[alloc_error_handler] +fn alloc_error_handler(layout: alloc::alloc::Layout) -> ! { + panic!("allocation error: {:?}", layout) +} + fn init() { gdt::init_gdt(); interrupts::init(); diff --git a/src/memory.rs b/src/memory.rs index d173d37..b725b24 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -1,4 +1,4 @@ -use x86_64::{PhysAddr, VirtAddr, structures::paging::{FrameAllocator, Mapper, OffsetPageTable, Page, PageTable, PhysFrame, Size4KiB}}; +use x86_64::{PhysAddr, VirtAddr, structures::paging::{FrameAllocator, OffsetPageTable, PageTable, PhysFrame, Size4KiB}}; use bootloader::bootinfo::{MemoryMap, MemoryRegionType}; pub unsafe fn init(physical_memory_offset: VirtAddr) -> OffsetPageTable<'static> {