use global allocator in rust user programs

master
WangRunji 6 years ago
parent fbf9409db2
commit 68f73bcd1a

19
user/Cargo.lock generated

@ -1,6 +1,17 @@
[[package]]
name = "linked_list_allocator"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"spin 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "rcore-ulib"
version = "0.1.0"
dependencies = [
"linked_list_allocator 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "rcore-user-programs"
@ -9,3 +20,11 @@ dependencies = [
"rcore-ulib 0.1.0",
]
[[package]]
name = "spin"
version = "0.4.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum linked_list_allocator 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "655d57c71827fe0891ce72231b6aa5e14033dae3f604609e6a6f807267c1678d"
"checksum spin 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "ceac490aa12c567115b40b7b7fceca03a6c9d53d5defea066123debc83c5dc1f"

@ -5,3 +5,4 @@ authors = ["WangRunji <wangrunji0408@163.com>"]
edition = "2018"
[dependencies]
linked_list_allocator = "0.6"

@ -1,5 +1,7 @@
use crate::syscall::{sys_close, sys_dup, sys_exit, sys_open};
use crate::io::{O_RDONLY, O_WRONLY, STDIN, STDOUT};
use crate::ALLOCATOR;
use core::alloc::Layout;
use core::panic::PanicInfo;
@ -31,6 +33,12 @@ fn initfd(fd2: usize, path: &str, open_flags: usize) -> i32 {
return ret;
}
fn init_heap() {
const HEAP_SIZE: usize = 0x1000;
static mut HEAP: [u8; HEAP_SIZE] = [0; HEAP_SIZE];
unsafe { ALLOCATOR.lock().init(HEAP.as_ptr() as usize, HEAP_SIZE); }
}
#[no_mangle]
pub extern "C" fn _start(_argc: isize, _argv: *const *const u8) -> ! {
let fd = initfd(STDIN, "stdin:", O_RDONLY);
@ -42,6 +50,7 @@ pub extern "C" fn _start(_argc: isize, _argv: *const *const u8) -> ! {
panic!("open <stdout> failed: {}.", fd);
}
init_heap();
main();
sys_exit(0)
}
@ -66,18 +75,3 @@ fn oom(_: Layout) -> ! {
pub extern "C" fn abort() -> ! {
sys_exit(2)
}
#[no_mangle]
pub extern "C" fn __mulsi3(mut a: u32, mut b: u32) -> u32 {
let mut r: u32 = 0;
while a > 0 {
if a & 1 > 0 {
r += b;
}
a >>= 1;
b <<= 1;
}
r
}

@ -9,3 +9,8 @@
pub mod io;
pub mod syscall;
pub mod lang_items;
use linked_list_allocator::LockedHeap;
#[global_allocator]
static ALLOCATOR: LockedHeap = LockedHeap::empty();

@ -7,7 +7,7 @@
"os": "none",
"arch": "riscv32",
"cpu": "generic-rv32",
"features": "",
"features": "+m,+a,+c",
"max-atomic-width": "32",
"linker": "rust-lld",
"linker-flavor": "ld.lld",

Loading…
Cancel
Save