diff --git a/Cargo.toml b/Cargo.toml index 4e8cef2..5622e81 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ arch_test = { path = "arch/test" } [target.'cfg(target_arch = "x86_64")'.dependencies] arch_x86_64 = { path = "arch/x86_64" } +bump_allocator = { path = "alloc/bump_allocator"} [profile.dev] panic = "abort" diff --git a/alloc/bump_allocator/Cargo.toml b/alloc/bump_allocator/Cargo.toml new file mode 100644 index 0000000..0c55184 --- /dev/null +++ b/alloc/bump_allocator/Cargo.toml @@ -0,0 +1,3 @@ +[package] +name = "bump_allocator" +version = "0.1.0" diff --git a/alloc/bump_allocator/src/lib.rs b/alloc/bump_allocator/src/lib.rs new file mode 100644 index 0000000..937a6b4 --- /dev/null +++ b/alloc/bump_allocator/src/lib.rs @@ -0,0 +1,47 @@ +//! A simple allocator that never frees, for testing + +#![feature(allocator)] +#![allocator] +#![no_std] + +pub static mut HEAP: usize = 10*1024*1024; + +#[no_mangle] +pub extern fn __rust_allocate(size: usize, align: usize) -> *mut u8 { + unsafe { + let mut ret = HEAP; + if align.is_power_of_two() { + ret += (align - 1); + ret &= !(align - 1); + } else { + assert_eq!(align, 0); + } + HEAP = ret + size; + ret as *mut u8 + } +} + +#[no_mangle] +pub extern fn __rust_usable_size(size: usize, align: usize) -> usize { + size +} + +#[no_mangle] +pub extern fn __rust_deallocate(ptr: *mut u8, size: usize, align: usize) { + +} + +#[no_mangle] +pub extern fn __rust_reallocate(ptr: *mut u8, size: usize, new_size: usize, align: usize) -> *mut u8 { + use core::{ptr, cmp}; + + 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 +} + +#[no_mangle] +pub extern fn __rust_reallocate_inplace(ptr: *mut u8, size: usize, new_size: usize, align: usize) -> usize { + size +} diff --git a/kernel/lib.rs b/kernel/lib.rs index 826fe95..7dd9a80 100644 --- a/kernel/lib.rs +++ b/kernel/lib.rs @@ -64,6 +64,8 @@ //! An error will be returned, `ENOBUFS`, if the buffer is not long enough for the name. //! In this case, it is recommended to add one page, 4096 bytes, to the buffer and retry. +#![feature(alloc)] +#![feature(collections)] #![feature(const_fn)] #![feature(lang_items)] #![feature(question_mark)] @@ -84,6 +86,14 @@ extern crate arch_test as arch; #[macro_use] extern crate arch_x86_64 as arch; +/// Bump allocator +#[cfg(all(not(test), target_arch = "x86_64"))] +extern crate bump_allocator; + +extern crate alloc; +#[macro_use] +extern crate collections; + /// Context management pub mod context;