Browse Source

+::alloca: wire allocator lib to a rust Alloc

master
3moon 8 years ago
parent
commit
f27ac4bf6e
2 changed files with 43 additions and 0 deletions
  1. +40
    -0
      src/alloca.rs
  2. +3
    -0
      src/lib.rs

+ 40
- 0
src/alloca.rs View File

@ -0,0 +1,40 @@
use spin::Mutex;
use sparkle_bump_alloc::Heap;
use alloc::allocator::{Alloc, Layout, AllocErr};
pub const HEAP_START: usize = 0o_000_001_000_000_0000;
pub const HEAP_SIZE: usize = 100 * 1024; // 100 KiB
static HEAP: Mutex<Option<Heap>> = Mutex::new(None);
pub fn heap_init(start: usize, size: usize) {
*HEAP.lock() = Some(Heap::new(start, size));
}
pub struct Allocator;
unsafe impl<'a> Alloc for &'a Allocator {
#[inline]
unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
if let Some(ref mut heap) = *HEAP.lock() {
heap.allocate(layout.size(), layout.align())
.ok_or(AllocErr::Exhausted { request: layout })
} else {
panic!("kheap: attempting alloc w/ uninitialized heap");
}
}
#[inline]
unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) {
if let Some(ref mut heap) = *HEAP.lock() {
heap.deallocate(ptr, layout.size(), layout.align());
} else {
panic!("kheap: attempting dealloc w/ uninitialized heap");
}
}
#[inline]
fn oom(&mut self, err: AllocErr) -> ! {
panic!("kheap OOM: {:?}", err);
}
}

+ 3
- 0
src/lib.rs View File

@ -12,6 +12,8 @@
extern crate log;
#[macro_use]
extern crate once;
#[macro_use]
extern crate alloc;
extern crate rlibc;
extern crate spin;
extern crate volatile;
@ -26,6 +28,7 @@ extern crate sparkle_bump_alloc;
#[macro_use]
pub mod macros;
mod alloca;
mod misc;
mod logger;
mod arch;


Loading…
Cancel
Save