Skip to content

Commit 027a29e

Browse files
committed
Added dummy allocator
1 parent 34cdf64 commit 027a29e

File tree

5 files changed

+27
-7
lines changed

5 files changed

+27
-7
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ features = ["spin_no_std"]
1818
[package.metadata.bootimage]
1919
build-command = [
2020
"build",
21-
"-Z", "build-std=core,compiler_builtins",
21+
"-Z", "build-std=core,compiler_builtins,alloc",
2222
"-Z", "build-std-features=compiler-builtins-mem",
2323
"--target", "x86_64-my_os.json"
2424
]

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ ARCH = x86_64
55

66
build:
77
cargo build \
8-
-Z build-std=core,compiler_builtins \
8+
-Z build-std=core,compiler_builtins,alloc \
99
-Z build-std-features=compiler-builtins-mem \
1010
--target ${ARCH}-${PROJECT}.json
1111

src/allocator.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use alloc::alloc::{GlobalAlloc, Layout};
2+
use core::ptr::null_mut;
3+
4+
#[global_allocator]
5+
static ALLOCATOR: Dummy = Dummy;
6+
7+
pub struct Dummy;
8+
9+
unsafe impl GlobalAlloc for Dummy {
10+
unsafe fn alloc(&self, _layout: Layout) -> *mut u8 {
11+
null_mut()
12+
}
13+
14+
unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {
15+
panic!("dealloc should be never called")
16+
}
17+
}

src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
#![no_std]
22
#![feature(abi_x86_interrupt)]
33

4+
pub mod allocator;
45
pub mod gdt;
56
pub mod interrupts;
67
pub mod memory;
78
pub mod vga_buffer;
89

10+
extern crate alloc;
11+
912
pub fn init() {
1013
gdt::init();
1114
interrupts::init_idt();

src/main.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ use bootloader::{entry_point, BootInfo};
55
use core::panic::PanicInfo;
66
use my_os::println;
77

8+
extern crate alloc;
9+
10+
use alloc::boxed::Box;
11+
812
entry_point!(kernel_main);
913

1014
fn kernel_main(boot_info: &'static BootInfo) -> ! {
@@ -19,11 +23,7 @@ fn kernel_main(boot_info: &'static BootInfo) -> ! {
1923
let mut mapper = unsafe { memory::init(phys_mem_offset) };
2024
let mut frame_allocator = unsafe { BootInfoFrameAllocator::init(&boot_info.memory_map) };
2125

22-
let page = Page::containing_address(VirtAddr::new(0));
23-
memory::create_example_mapping(page, &mut mapper, &mut frame_allocator);
24-
25-
let page_ptr: *mut u64 = page.start_address().as_mut_ptr();
26-
unsafe { page_ptr.offset(400).write_volatile(0x_f021_f077_f065_f04e) };
26+
let x = Box::new(41);
2727

2828
println!("It did not crash!");
2929
my_os::hlt_loop();

0 commit comments

Comments
 (0)