Nightly stuff
This commit is contained in:
parent
5b449f4e1e
commit
fd2384b7fa
9 changed files with 73 additions and 232 deletions
17
src/main.rs
17
src/main.rs
|
@ -1,8 +1,12 @@
|
|||
#![no_main]
|
||||
#![no_std]
|
||||
#![feature(alloc_error_handler)]
|
||||
|
||||
use safe_libc as libc;
|
||||
mod system_alloc;
|
||||
|
||||
extern crate alloc;
|
||||
use libc::cstr;
|
||||
use safe_libc as libc;
|
||||
|
||||
use core::fmt::Write;
|
||||
|
||||
|
@ -19,17 +23,20 @@ pub extern "C" fn main(_nargs: libc::c_int, _args: *const *const libc::c_char) -
|
|||
let x = libc::string::CString::from("foo");
|
||||
let l = x.len();
|
||||
let y = x.into_raw();
|
||||
let z = unsafe {
|
||||
libc::boxed::CBox::slice_from_raw_parts(y, l)
|
||||
};
|
||||
let z = unsafe { alloc::boxed::Box::from_raw(core::ptr::slice_from_raw_parts_mut(y, l)) };
|
||||
let _ = writeln!(stdout, "Foo: {} {} {}", z[0], z[1], z[2]);
|
||||
}
|
||||
|
||||
{
|
||||
let b = libc::boxed::CBox::new(42);
|
||||
let b = alloc::boxed::Box::new(42);
|
||||
let _ = writeln!(stdout, "Bar: {}", b);
|
||||
}
|
||||
|
||||
{
|
||||
let x = alloc::boxed::Box::new(());
|
||||
let _ = writeln!(stdout, "Box: {:?}", x);
|
||||
}
|
||||
|
||||
0
|
||||
}
|
||||
|
||||
|
|
21
src/system_alloc.rs
Normal file
21
src/system_alloc.rs
Normal file
|
@ -0,0 +1,21 @@
|
|||
use safe_libc as libc;
|
||||
|
||||
struct System;
|
||||
|
||||
unsafe impl alloc::alloc::GlobalAlloc for System {
|
||||
unsafe fn alloc(&self, layout: alloc::alloc::Layout) -> *mut u8 {
|
||||
libc::memalign(layout.align(), layout.size()).cast()
|
||||
}
|
||||
|
||||
unsafe fn dealloc(&self, ptr: *mut u8, _layout: alloc::alloc::Layout) {
|
||||
libc::free(ptr.cast());
|
||||
}
|
||||
}
|
||||
|
||||
#[global_allocator]
|
||||
static SYSTEM_ALLOC: System = System;
|
||||
|
||||
#[alloc_error_handler]
|
||||
fn alloc_error(_: core::alloc::Layout) -> ! {
|
||||
panic!("allocation failure");
|
||||
}
|
Reference in a new issue