diff --git a/Cargo.toml b/Cargo.toml index 5ac75deda3310c0b557488e9f7c0d3605c0eb09a..a1d8cfbd84126a9b5bcf46636770c45245a92fbd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,9 +9,9 @@ path = "src/lib.rs" crate-type = ["staticlib"] [dependencies] -alloc_kernel = { path = "alloc_kernel" } bitflags = "1" clippy = { version = "*", optional = true } +slab_allocator = "0.3.0" spin = "0.4" raw-cpuid = "3.0" redox_syscall = { path = "syscall" } diff --git a/alloc_kernel/Cargo.toml b/alloc_kernel/Cargo.toml deleted file mode 100644 index bbd1212c1d1cf90f81b9e0cd7b4bc12221b5d9ba..0000000000000000000000000000000000000000 --- a/alloc_kernel/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -authors = ["Philipp Oppermann <dev@phil-opp.com>"] -name = "alloc_kernel" -version = "0.1.0" - -[dependencies] -linked_list_allocator = { git = "https://github.com/redox-os/linked-list-allocator.git" } -spin = "*" diff --git a/src/arch/x86_64/start.rs b/src/arch/x86_64/start.rs index ddc7b61de95b3445da574d0b2acf723afe62bc56..41991c77f1f8abc7ada7eee8ab7baf59165d86e8 100644 --- a/src/arch/x86_64/start.rs +++ b/src/arch/x86_64/start.rs @@ -7,13 +7,13 @@ use core::slice; use core::sync::atomic::{AtomicBool, ATOMIC_BOOL_INIT, AtomicUsize, ATOMIC_USIZE_INIT, Ordering}; use acpi; -use allocator; use arch::x86_64::pti; use device; use gdt; use idt; use interrupt; use memory; +use memory::slab as allocator; use paging::{self, Page, VirtualAddress}; use paging::entry::EntryFlags; use paging::mapper::MapperFlushAll; @@ -113,7 +113,7 @@ pub unsafe extern fn kstart(args_ptr: *const KernelArgs) -> ! { flush_all.flush(&mut active_table); // Init the allocator - allocator::init(::KERNEL_HEAP_OFFSET, ::KERNEL_HEAP_SIZE); + allocator::init_heap(::KERNEL_HEAP_OFFSET, ::KERNEL_HEAP_SIZE); } // Initialize devices diff --git a/src/lib.rs b/src/lib.rs index 69727a62673677da6d48bef7e15c770ca7754366..34629d5b73fb8f529e6e8fa71230cccc4be8df03 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,7 +32,6 @@ #![feature(const_size_of)] #![no_std] -extern crate alloc_kernel as allocator; pub extern crate x86; #[macro_use] @@ -42,6 +41,7 @@ extern crate alloc; extern crate bitflags; extern crate goblin; extern crate spin; +extern crate slab_allocator; use alloc::arc::Arc; use core::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering}; @@ -103,7 +103,7 @@ pub mod time; pub mod tests; #[global_allocator] -static ALLOCATOR: allocator::Allocator = allocator::Allocator; +static ALLOCATOR: memory::slab::Allocator = memory::slab::Allocator; /// A unique number that identifies the current CPU - used for scheduling #[thread_local] diff --git a/src/memory/mod.rs b/src/memory/mod.rs index 6ab6f0fb07928b9a0799d3dbdba2aef5d90e2718..39cdf5db1604897fc2369eec8f84857449e71ad8 100644 --- a/src/memory/mod.rs +++ b/src/memory/mod.rs @@ -10,6 +10,7 @@ use spin::Mutex; pub mod bump; pub mod recycle; +pub mod slab; /// The current memory map. It's size is maxed out to 512 entries, due to it being /// from 0x500 to 0x5000 (800 is the absolute total) diff --git a/alloc_kernel/src/lib.rs b/src/memory/slab.rs similarity index 60% rename from alloc_kernel/src/lib.rs rename to src/memory/slab.rs index 9caeb286ee56cf929618b7e9b446e6a0d367ab72..286e146de57c310f09498d954bf1ddc0392af1a0 100644 --- a/alloc_kernel/src/lib.rs +++ b/src/memory/slab.rs @@ -1,20 +1,10 @@ -#![deny(warnings)] -#![feature(alloc)] -#![feature(allocator_api)] -#![feature(const_fn)] -#![no_std] - -extern crate alloc; -extern crate spin; -extern crate linked_list_allocator; - use alloc::heap::{Alloc, AllocErr, Layout}; use spin::Mutex; -use linked_list_allocator::Heap; +use slab_allocator::Heap; static HEAP: Mutex<Option<Heap>> = Mutex::new(None); -pub unsafe fn init(offset: usize, size: usize) { +pub unsafe fn init_heap(offset: usize, size: usize) { *HEAP.lock() = Some(Heap::new(offset, size)); } @@ -23,7 +13,7 @@ pub struct Allocator; unsafe impl<'a> Alloc for &'a Allocator { unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> { if let Some(ref mut heap) = *HEAP.lock() { - heap.allocate_first_fit(layout) + heap.allocate(layout) } else { panic!("__rust_allocate: heap not initialized"); } @@ -36,4 +26,16 @@ unsafe impl<'a> Alloc for &'a Allocator { panic!("__rust_deallocate: heap not initialized"); } } -} + + fn oom(&mut self, error: AllocErr) -> ! { + panic!("Out of memory: {:?}", error); + } + + fn usable_size(&self, layout: &Layout) -> (usize, usize) { + if let Some(ref mut heap) = *HEAP.lock() { + heap.usable_size(layout) + } else { + panic!("__rust_usable_size: heap not initialized"); + } + } +} \ No newline at end of file