Commit c60a82fd authored by Stefan Lankes's avatar Stefan Lankes Committed by Stefan Lankes
Browse files

Replace `Memoryblock` with `NonNull<[u8]>`

parent 70ec83a4
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
#![feature(panic_info_message)] #![feature(panic_info_message)]
#![feature(specialization)] #![feature(specialization)]
#![feature(naked_functions)] #![feature(naked_functions)]
#![feature(nonnull_slice_from_raw_parts)]
#![feature(core_intrinsics)] #![feature(core_intrinsics)]
#![feature(alloc_error_handler)] #![feature(alloc_error_handler)]
#![allow(unused_macros)] #![allow(unused_macros)]
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
use crate::mm::hole::{Hole, HoleList}; use crate::mm::hole::{Hole, HoleList};
use crate::mm::kernel_end_address; use crate::mm::kernel_end_address;
use crate::synch::spinlock::*; use crate::synch::spinlock::*;
use core::alloc::{AllocErr, AllocInit, AllocRef, GlobalAlloc, Layout, MemoryBlock}; use core::alloc::{AllocErr, AllocRef, GlobalAlloc, Layout};
use core::ops::Deref; use core::ops::Deref;
use core::ptr::NonNull; use core::ptr::NonNull;
use core::{mem, ptr}; use core::{mem, ptr};
...@@ -158,18 +158,9 @@ impl Heap { ...@@ -158,18 +158,9 @@ impl Heap {
} }
unsafe impl AllocRef for Heap { unsafe impl AllocRef for Heap {
fn alloc(&mut self, layout: Layout, init: AllocInit) -> Result<MemoryBlock, AllocErr> { fn alloc(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
let (ptr, size) = self.allocate_first_fit(layout)?; let (ptr, size) = self.allocate_first_fit(layout)?;
let memory = MemoryBlock { ptr, size }; Ok(NonNull::slice_from_raw_parts(ptr, size))
match init {
AllocInit::Uninitialized => {}
AllocInit::Zeroed => unsafe {
memory.ptr.as_ptr().write_bytes(0, memory.size);
},
}
Ok(memory)
} }
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) { unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
...@@ -212,17 +203,17 @@ unsafe impl GlobalAlloc for LockedHeap { ...@@ -212,17 +203,17 @@ unsafe impl GlobalAlloc for LockedHeap {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 { unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
self.0 self.0
.lock() .lock()
.alloc(layout, AllocInit::Uninitialized) .alloc(layout)
.ok() .ok()
.map_or(ptr::null_mut() as *mut u8, |mem| mem.ptr.as_ptr()) .map_or(ptr::null_mut() as *mut u8, |mut mem| mem.as_mut().as_mut_ptr())
} }
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
self.0 let ptr = self.alloc(layout.clone());
.lock() if !ptr.is_null() {
.alloc(layout, AllocInit::Zeroed) ptr::write_bytes(ptr, 0, layout.size());
.ok() }
.map_or(ptr::null_mut() as *mut u8, |mem| mem.ptr.as_ptr()) ptr
} }
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment