Browse Source

alloca: hack in phil-opp's linked_list_allocator

master
3moon 8 years ago
parent
commit
80ac44f4fe
5 changed files with 31 additions and 16 deletions
  1. +16
    -5
      Cargo.lock
  2. +1
    -3
      Cargo.toml
  3. +7
    -5
      src/alloca.rs
  4. +3
    -1
      src/arch/x86_64/memory/mod.rs
  5. +4
    -2
      src/lib.rs

+ 16
- 5
Cargo.lock View File

@ -3,11 +3,11 @@ name = "sparkle_os"
version = "0.1.0"
dependencies = [
"bitflags 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)",
"linked_list_allocator 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
"multiboot2 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
"once 0.1.0",
"rlibc 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"sparkle_bump_alloc 0.1.0",
"spin 0.4.5 (git+https://github.com/messense/spin-rs?rev=020f1b3)",
"volatile 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"x86 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
@ -48,6 +48,14 @@ version = "0.2.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "linked_list_allocator"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"spin 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "log"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
@ -213,15 +221,16 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "sparkle_bump_alloc"
version = "0.1.0"
[[package]]
name = "spin"
version = "0.4.5"
source = "git+https://github.com/messense/spin-rs?rev=020f1b3#020f1b3f160a1facf54355278af14cbf6a1613fd"
[[package]]
name = "spin"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "volatile"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
@ -246,6 +255,7 @@ dependencies = [
"checksum byteorder 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0fc10e8cc6b2580fda3f36eb6dc5316657f812a3df879a44a66fc9f0fdbc4855"
"checksum csv 0.14.7 (registry+https://github.com/rust-lang/crates.io-index)" = "266c1815d7ca63a5bd86284043faf91e8c95e943e55ce05dc0ae08e952de18bc"
"checksum libc 0.2.23 (registry+https://github.com/rust-lang/crates.io-index)" = "e7eb6b826bfc1fdea7935d46556250d1799b7fe2d9f7951071f4291710665e3e"
"checksum linked_list_allocator 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1d9b8a68289fdacb7242a519f3cf181a3394ebed3bd092ee07ffa2e5c2088afd"
"checksum log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "880f77541efa6e5cc74e76910c9884d9859683118839d6a1dc3b11e63512565b"
"checksum multiboot2 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dcea5906310f2607ea39a42b8af0745dc487d7583db9d981142a63b538647f69"
"checksum num 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)" = "98b15ba84e910ea7a1973bccd3df7b31ae282bf9d8bd2897779950c9b8303d40"
@ -267,5 +277,6 @@ dependencies = [
"checksum serde_json 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b5aaee47e038bf9552d30380d3973fff2593ee0a76d81ad4c581f267cdcadf36"
"checksum siphasher 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0df90a788073e8d0235a67e50441d47db7c8ad9debd91cbf43736a2a92d36537"
"checksum spin 0.4.5 (git+https://github.com/messense/spin-rs?rev=020f1b3)" = "<none>"
"checksum spin 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7e4deb3c2455c73779e6d3eebceae9599fc70957e54c69fe88f93aa48e62f432"
"checksum volatile 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6633c5250f16598f92b48272265ce9f8179447f702f0ba0cb640c5be6537b0c0"
"checksum x86 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ada1976e1004a0b5999beb7403541af96637e8f439b97d86c8c9abef2e0e7d19"

+ 1
- 3
Cargo.toml View File

@ -12,6 +12,7 @@ volatile = "0.2"
x86 = "0.8"
multiboot2 = "0.3.2"
bitflags = "0.9"
linked_list_allocator = "0.4.2"
[dependencies.spin]
git = "https://github.com/messense/spin-rs"
@ -23,6 +24,3 @@ default-features = false
[dependencies.once]
path = "lib/once"
[dependencies.sparkle_bump_alloc]
path = "lib/alloc/bump"

+ 7
- 5
src/alloca.rs View File

@ -1,5 +1,5 @@
use spin::Mutex;
use sparkle_bump_alloc::Heap;
use linked_list_allocator::Heap;
use alloc::allocator::{Alloc, Layout, AllocErr};
pub const HEAP_START: usize = 0o_000_001_000_000_0000;
@ -7,7 +7,7 @@ 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) {
pub unsafe fn heap_init(start: usize, size: usize) {
*HEAP.lock() = Some(Heap::new(start, size));
}
@ -17,8 +17,9 @@ 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 })
// heap.allocate(layout.size(), layout.align())
// .ok_or(AllocErr::Exhausted { request: layout })
heap.allocate_first_fit(layout)
} else {
panic!("kheap: attempting alloc w/ uninitialized heap");
}
@ -27,7 +28,8 @@ unsafe impl<'a> Alloc for &'a Allocator {
#[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());
heap.deallocate(ptr, layout)
// heap.deallocate(ptr, layout.size(), layout.align());
} else {
panic!("kheap: attempting dealloc w/ uninitialized heap");
}


+ 3
- 1
src/arch/x86_64/memory/mod.rs View File

@ -124,6 +124,8 @@ pub fn init(boot_info: &BootInformation) {
active_table.map(page, paging::WRITABLE, &mut frame_allocator);
}
alloca::heap_init(HEAP_START, HEAP_SIZE);
unsafe {
alloca::heap_init(HEAP_START, HEAP_SIZE);
}
info!("kheap: initialized");
}

+ 4
- 2
src/lib.rs View File

@ -21,10 +21,9 @@ extern crate volatile;
extern crate bitflags;
extern crate x86;
extern crate multiboot2;
extern crate linked_list_allocator;
// sparkle-* libs
extern crate sparkle_bump_alloc;
#[macro_use]
pub mod macros;
@ -38,6 +37,7 @@ use arch::x86_64::device::vga_console;
use arch::x86_64::memory;
use arch::x86_64::memory::FrameAllocator;
use alloca::Allocator;
use alloc::boxed::Box;
#[global_allocator]
static GLOBAL_ALLOC: Allocator = Allocator;
@ -54,6 +54,8 @@ pub extern fn kernel_main(multiboot_info_pointer: usize) {
memory::init(boot_info);
info!("* memory::init(): success! *");
info!("kheap: smoke test (boxing): {:?}", Box::new("hello world"));
loop {}
}


Loading…
Cancel
Save