From eccb99d67d85dafea41347133bb79458b6a73a79 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Mon, 15 Aug 2016 15:27:32 -0600 Subject: [PATCH] Use linked list allocator --- alloc/hole_list_allocator/Cargo.toml | 12 ++++++ alloc/hole_list_allocator/src/lib.rs | 59 ++++++++++++++++++++++++++++ arch/x86_64/Cargo.toml | 2 +- arch/x86_64/src/lib.rs | 3 +- arch/x86_64/src/main.rs | 2 +- arch/x86_64/src/memory/mod.rs | 8 ++-- 6 files changed, 79 insertions(+), 7 deletions(-) create mode 100644 alloc/hole_list_allocator/Cargo.toml create mode 100644 alloc/hole_list_allocator/src/lib.rs diff --git a/alloc/hole_list_allocator/Cargo.toml b/alloc/hole_list_allocator/Cargo.toml new file mode 100644 index 0000000..eba97fb --- /dev/null +++ b/alloc/hole_list_allocator/Cargo.toml @@ -0,0 +1,12 @@ +[package] +authors = ["Philipp Oppermann "] +name = "hole_list_allocator" +version = "0.1.0" + +[dependencies] +linked_list_allocator = "0.2.0" +spin = "0.3.5" + +[dependencies.lazy_static] +version = "0.2.1" +features = ["spin_no_std"] diff --git a/alloc/hole_list_allocator/src/lib.rs b/alloc/hole_list_allocator/src/lib.rs new file mode 100644 index 0000000..ddbd070 --- /dev/null +++ b/alloc/hole_list_allocator/src/lib.rs @@ -0,0 +1,59 @@ +#![feature(allocator)] +#![feature(const_fn)] + +#![allocator] +#![no_std] + +use spin::Mutex; +use linked_list_allocator::Heap; + +extern crate spin; +extern crate linked_list_allocator; +#[macro_use] +extern crate lazy_static; + +pub const HEAP_START: usize = 0x1_0000_0000; // Put at end of 4GB +pub const HEAP_SIZE: usize = 16 * 1024 * 1024; // 16 MB + +lazy_static! { + static ref HEAP: Mutex = Mutex::new(unsafe { + Heap::new(HEAP_START, HEAP_SIZE) + }); +} + +#[no_mangle] +pub extern fn __rust_allocate(size: usize, align: usize) -> *mut u8 { + HEAP.lock().allocate_first_fit(size, align).expect("out of memory") +} + +#[no_mangle] +pub extern fn __rust_deallocate(ptr: *mut u8, size: usize, align: usize) { + unsafe { HEAP.lock().deallocate(ptr, size, align) }; +} + +#[no_mangle] +pub extern fn __rust_usable_size(size: usize, _align: usize) -> usize { + size +} + +#[no_mangle] +pub extern fn __rust_reallocate_inplace(_ptr: *mut u8, size: usize, + _new_size: usize, _align: usize) -> usize +{ + size +} + +#[no_mangle] +pub extern fn __rust_reallocate(ptr: *mut u8, size: usize, new_size: usize, + align: usize) -> *mut u8 { + use core::{ptr, cmp}; + + // from: https://github.com/rust-lang/rust/blob/ + // c66d2380a810c9a2b3dbb4f93a830b101ee49cc2/ + // src/liballoc_system/lib.rs#L98-L101 + + let new_ptr = __rust_allocate(new_size, align); + unsafe { ptr::copy(ptr, new_ptr, cmp::min(size, new_size)) }; + __rust_deallocate(ptr, size, align); + new_ptr +} diff --git a/arch/x86_64/Cargo.toml b/arch/x86_64/Cargo.toml index 6d6fb27..250c559 100644 --- a/arch/x86_64/Cargo.toml +++ b/arch/x86_64/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" [dependencies] bitflags = "*" -bump_allocator = { path = "../../alloc/bump_allocator"} +hole_list_allocator = { path = "../../alloc/hole_list_allocator"} [dependencies.x86] default-features = false diff --git a/arch/x86_64/src/lib.rs b/arch/x86_64/src/lib.rs index faf6e9d..79128db 100644 --- a/arch/x86_64/src/lib.rs +++ b/arch/x86_64/src/lib.rs @@ -9,9 +9,10 @@ #![feature(unique)] #![no_std] +extern crate hole_list_allocator as allocator; + #[macro_use] extern crate bitflags; -extern crate bump_allocator; extern crate x86; /// Print to console diff --git a/arch/x86_64/src/main.rs b/arch/x86_64/src/main.rs index 60e86ce..99147b9 100644 --- a/arch/x86_64/src/main.rs +++ b/arch/x86_64/src/main.rs @@ -3,7 +3,7 @@ /// It must create the IDT with the correct entries, those entries are /// defined in other files inside of the `arch` module -use bump_allocator::{HEAP_START, HEAP_SIZE}; +use allocator::{HEAP_START, HEAP_SIZE}; use externs::memset; use gdt; use idt; diff --git a/arch/x86_64/src/memory/mod.rs b/arch/x86_64/src/memory/mod.rs index 7435413..ee79995 100644 --- a/arch/x86_64/src/memory/mod.rs +++ b/arch/x86_64/src/memory/mod.rs @@ -12,16 +12,16 @@ pub mod area_frame_alloc; static mut MEMORY_MAP: [MemoryArea; 512] = [MemoryArea { base_addr: 0, length: 0, _type: 0, acpi: 0 }; 512]; /// Memory does not exist -const MEMORY_AREA_NULL: u32 = 0; +pub const MEMORY_AREA_NULL: u32 = 0; /// Memory is free to use -const MEMORY_AREA_FREE: u32 = 1; +pub const MEMORY_AREA_FREE: u32 = 1; /// Memory is reserved -const MEMORY_AREA_RESERVED: u32 = 2; +pub const MEMORY_AREA_RESERVED: u32 = 2; /// Memory is used by ACPI, and can be reclaimed -const MEMORY_AREA_ACPI: u32 = 3; +pub const MEMORY_AREA_ACPI: u32 = 3; #[derive(Clone)] pub struct MemoryAreaIter {