Update to new allocation API (WIP)
This commit is contained in:
4
Cargo.lock
generated
4
Cargo.lock
generated
@@ -108,7 +108,7 @@ dependencies = [
|
||||
"linked_list_allocator 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"raw-cpuid 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"redox_syscall 0.1.37",
|
||||
"slab_allocator 0.3.0",
|
||||
"slab_allocator 0.3.1",
|
||||
"spin 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"x86 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
@@ -270,7 +270,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "slab_allocator"
|
||||
version = "0.3.0"
|
||||
version = "0.3.1"
|
||||
dependencies = [
|
||||
"linked_list_allocator 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"spin 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
||||
@@ -14,7 +14,7 @@ clippy = { version = "*", optional = true }
|
||||
linked_list_allocator = "0.6"
|
||||
raw-cpuid = "3.0"
|
||||
redox_syscall = { path = "syscall" }
|
||||
slab_allocator = { path = "slab_allocator" }
|
||||
slab_allocator = { path = "slab_allocator", optional = true }
|
||||
spin = "0.4"
|
||||
|
||||
[dependencies.goblin]
|
||||
@@ -27,11 +27,11 @@ version = "0.7"
|
||||
default-features = false
|
||||
|
||||
[features]
|
||||
default = ["acpi", "graphical_debug"]
|
||||
default = []
|
||||
acpi = []
|
||||
doc = []
|
||||
graphical_debug = []
|
||||
live = []
|
||||
multi_core = []
|
||||
pti = []
|
||||
slab = []
|
||||
slab = ["slab_allocator"]
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use alloc::heap::{Alloc, AllocErr, Layout};
|
||||
use alloc::heap::{AllocErr, GlobalAlloc, Layout, Opaque};
|
||||
use core::ptr::NonNull;
|
||||
use linked_list_allocator::Heap;
|
||||
use spin::Mutex;
|
||||
|
||||
@@ -14,8 +15,8 @@ impl Allocator {
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl<'a> Alloc for &'a Allocator {
|
||||
unsafe fn alloc(&mut self, mut layout: Layout) -> Result<*mut u8, AllocErr> {
|
||||
unsafe impl GlobalAlloc for Allocator {
|
||||
unsafe fn alloc(&self, layout: Layout) -> *mut Opaque {
|
||||
loop {
|
||||
let res = if let Some(ref mut heap) = *HEAP.lock() {
|
||||
heap.allocate_first_fit(layout)
|
||||
@@ -24,9 +25,7 @@ unsafe impl<'a> Alloc for &'a Allocator {
|
||||
};
|
||||
|
||||
match res {
|
||||
Err(AllocErr::Exhausted { request }) => {
|
||||
layout = request;
|
||||
|
||||
Err(AllocErr) => {
|
||||
let size = if let Some(ref heap) = *HEAP.lock() {
|
||||
heap.size()
|
||||
} else {
|
||||
@@ -41,28 +40,16 @@ unsafe impl<'a> Alloc for &'a Allocator {
|
||||
panic!("__rust_allocate: heap not initialized");
|
||||
}
|
||||
},
|
||||
other => return other,
|
||||
other => return other.ok().map_or(0 as *mut Opaque, |allocation| allocation.as_ptr()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) {
|
||||
unsafe fn dealloc(&self, ptr: *mut Opaque, layout: Layout) {
|
||||
if let Some(ref mut heap) = *HEAP.lock() {
|
||||
heap.deallocate(ptr, layout)
|
||||
heap.deallocate(NonNull::new_unchecked(ptr), layout)
|
||||
} else {
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,10 @@ pub use self::linked_list::Allocator;
|
||||
#[cfg(feature="slab")]
|
||||
pub use self::slab::Allocator;
|
||||
|
||||
#[cfg(not(feature="slab"))]
|
||||
mod linked_list;
|
||||
|
||||
#[cfg(feature="slab")]
|
||||
mod slab;
|
||||
|
||||
unsafe fn map_heap(active_table: &mut ActivePageTable, offset: usize, size: usize) {
|
||||
|
||||
@@ -67,7 +67,7 @@ impl ContextList {
|
||||
let context_lock = self.new_context()?;
|
||||
{
|
||||
let mut context = context_lock.write();
|
||||
let mut fx = unsafe { Box::from_raw(Heap.alloc(Layout::from_size_align_unchecked(512, 16)).unwrap() as *mut [u8; 512]) };
|
||||
let mut fx = unsafe { Box::from_raw(Heap.alloc(Layout::from_size_align_unchecked(512, 16)).unwrap().as_ptr() as *mut [u8; 512]) };
|
||||
for b in fx.iter_mut() {
|
||||
*b = 0;
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ pub fn init() {
|
||||
let mut contexts = contexts_mut();
|
||||
let context_lock = contexts.new_context().expect("could not initialize first context");
|
||||
let mut context = context_lock.write();
|
||||
let mut fx = unsafe { Box::from_raw(Heap.alloc(Layout::from_size_align_unchecked(512, 16)).unwrap() as *mut [u8; 512]) };
|
||||
let mut fx = unsafe { Box::from_raw(Heap.alloc(Layout::from_size_align_unchecked(512, 16)).unwrap().as_ptr() as *mut [u8; 512]) };
|
||||
for b in fx.iter_mut() {
|
||||
*b = 0;
|
||||
}
|
||||
|
||||
@@ -43,6 +43,7 @@ extern crate bitflags;
|
||||
extern crate goblin;
|
||||
extern crate linked_list_allocator;
|
||||
extern crate spin;
|
||||
#[cfg(feature = "slab")]
|
||||
extern crate slab_allocator;
|
||||
|
||||
use alloc::arc::Arc;
|
||||
|
||||
@@ -22,6 +22,11 @@ pub extern "C" fn rust_begin_unwind(fmt: ::core::fmt::Arguments, file: &str, lin
|
||||
}
|
||||
}
|
||||
|
||||
#[lang = "oom"]
|
||||
pub extern fn rust_oom() -> ! {
|
||||
panic!("kernel memory allocation failed");
|
||||
}
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
#[no_mangle]
|
||||
/// Required to handle panics
|
||||
|
||||
@@ -113,7 +113,7 @@ pub fn clone(flags: usize, stack_base: usize) -> Result<ContextId> {
|
||||
arch = context.arch.clone();
|
||||
|
||||
if let Some(ref fx) = context.kfx {
|
||||
let mut new_fx = unsafe { Box::from_raw(Heap.alloc(Layout::from_size_align_unchecked(512, 16)).unwrap() as *mut [u8; 512]) };
|
||||
let mut new_fx = unsafe { Box::from_raw(Heap.alloc(Layout::from_size_align_unchecked(512, 16)).unwrap().as_ptr() as *mut [u8; 512]) };
|
||||
for (new_b, b) in new_fx.iter_mut().zip(fx.iter()) {
|
||||
*new_b = *b;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user