From 6a45b295607d0e1dd79be6a337fce543df91da8e Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Tue, 12 Apr 2016 00:37:50 +0200 Subject: [PATCH] Create hole_list_allocator crate --- libs/hole_list_allocator/.gitignore | 2 + libs/hole_list_allocator/Cargo.toml | 9 +++++ libs/hole_list_allocator/src/lib.rs | 59 +++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 libs/hole_list_allocator/.gitignore create mode 100644 libs/hole_list_allocator/Cargo.toml create mode 100644 libs/hole_list_allocator/src/lib.rs diff --git a/libs/hole_list_allocator/.gitignore b/libs/hole_list_allocator/.gitignore new file mode 100644 index 00000000..49d4fed9 --- /dev/null +++ b/libs/hole_list_allocator/.gitignore @@ -0,0 +1,2 @@ +# Generated by Cargo +/target/ diff --git a/libs/hole_list_allocator/Cargo.toml b/libs/hole_list_allocator/Cargo.toml new file mode 100644 index 00000000..a055c0b9 --- /dev/null +++ b/libs/hole_list_allocator/Cargo.toml @@ -0,0 +1,9 @@ +[package] +authors = ["Philipp Oppermann "] +name = "hole_list_allocator" +version = "0.1.0" + +[dependencies] +linked_list_allocator = "0.2.0" +spin = "0.3.5" +once = "0.2.0" diff --git a/libs/hole_list_allocator/src/lib.rs b/libs/hole_list_allocator/src/lib.rs new file mode 100644 index 00000000..c6829eb2 --- /dev/null +++ b/libs/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 once; + +pub const HEAP_START: usize = 0o_000_001_000_000_0000; +pub const HEAP_SIZE: usize = 100 * 1024; // 100 KiB + +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 +}