Use malloc in mini_core::allocate

This commit is contained in:
bjorn3 2019-01-26 15:10:21 +01:00
parent 4fbff29a4a
commit 4bca86c236
2 changed files with 56 additions and 16 deletions

View File

@ -51,6 +51,7 @@ unsafe impl Copy for isize {}
unsafe impl Copy for char {}
unsafe impl<'a, T: ?Sized> Copy for &'a T {}
unsafe impl<T: ?Sized> Copy for *const T {}
unsafe impl<T: ?Sized> Copy for *mut T {}
#[lang = "sync"]
pub unsafe trait Sync {}
@ -118,6 +119,14 @@ impl Add for u8 {
}
}
impl Add for usize {
type Output = Self;
fn add(self, rhs: Self) -> Self {
self + rhs
}
}
#[lang = "sub"]
pub trait Sub<RHS = Self> {
type Output;
@ -172,6 +181,33 @@ impl PartialEq for u8 {
}
}
impl PartialEq for u16 {
fn eq(&self, other: &u16) -> bool {
(*self) == (*other)
}
fn ne(&self, other: &u16) -> bool {
(*self) != (*other)
}
}
impl PartialEq for u32 {
fn eq(&self, other: &u32) -> bool {
(*self) == (*other)
}
fn ne(&self, other: &u32) -> bool {
(*self) != (*other)
}
}
impl PartialEq for usize {
fn eq(&self, other: &usize) -> bool {
(*self) == (*other)
}
fn ne(&self, other: &usize) -> bool {
(*self) != (*other)
}
}
impl PartialEq for char {
fn eq(&self, other: &char) -> bool {
(*self) == (*other)
@ -230,7 +266,7 @@ pub trait FnMut<Args>: FnOnce<Args> {
}
#[lang = "panic"]
pub fn panic(_expr_file_line_col: &(&'static str, &'static str, u32, u32)) -> ! {
pub fn panic(&(_msg, _file, _line, _col): &(&'static str, &'static str, u32, u32)) -> ! {
unsafe {
intrinsics::abort();
}
@ -254,11 +290,12 @@ pub struct Box<T: ?Sized>(*mut T);
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Box<U>> for Box<T> {}
static mut MY_TINY_HEAP: [u8; 16] = [0xff; 16];
#[lang = "exchange_malloc"]
// Make it available to jited mini_core_hello_world
// FIXME remove next line when jit supports linking rlibs
#[inline(always)]
unsafe fn allocate(size: usize, _align: usize) -> *mut u8 {
&mut MY_TINY_HEAP as *mut [u8; 16] as *mut u8
libc::malloc(size)
}
#[lang = "drop"]
@ -281,6 +318,17 @@ pub mod intrinsics {
}
}
pub mod libc {
#[link(name = "c")]
extern "C" {
pub fn puts(s: *const u8);
pub fn malloc(size: usize) -> *mut u8;
pub fn memcpy(dst: *mut u8, src: *const u8, size: usize);
pub fn memmove(dst: *mut u8, src: *const u8, size: usize);
pub fn strncpy(dst: *mut u8, src: *const u8, size: usize);
}
}
#[lang = "index"]
pub trait Index<Idx: ?Sized> {
type Output: ?Sized;

View File

@ -7,11 +7,7 @@
extern crate mini_core;
use mini_core::*;
#[link(name = "c")]
extern "C" {
fn puts(s: *const u8);
}
use mini_core::libc::*;
unsafe extern "C" fn my_puts(s: *const u8) {
puts(s);
@ -132,13 +128,9 @@ fn main() {
let ptr: *const u8 = hello as *const [u8] as *const u8;
puts(ptr);
// TODO remove when jit supports linking rlibs
#[cfg(not(jit))]
{
let world: Box<&str> = box "World!\0";
puts(*world as *const str as *const u8);
world as Box<SomeTrait>;
}
let world: Box<&str> = box "World!\0";
puts(*world as *const str as *const u8);
world as Box<SomeTrait>;
assert_eq!(intrinsics::size_of_val(hello) as u8, 6);