From 4bca86c236c7ebb35811491d1a1c64e9050b27c0 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sat, 26 Jan 2019 15:10:21 +0100 Subject: [PATCH] Use malloc in mini_core::allocate --- example/mini_core.rs | 56 +++++++++++++++++++++++++++++--- example/mini_core_hello_world.rs | 16 +++------ 2 files changed, 56 insertions(+), 16 deletions(-) diff --git a/example/mini_core.rs b/example/mini_core.rs index a3ab7623287..8b21d62ea54 100644 --- a/example/mini_core.rs +++ b/example/mini_core.rs @@ -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 Copy for *const T {} +unsafe impl 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 { 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: FnOnce { } #[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(*mut T); impl, U: ?Sized> CoerceUnsized> for Box {} -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 { type Output: ?Sized; diff --git a/example/mini_core_hello_world.rs b/example/mini_core_hello_world.rs index 638db7d24ef..1ddd203e028 100644 --- a/example/mini_core_hello_world.rs +++ b/example/mini_core_hello_world.rs @@ -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; - } + let world: Box<&str> = box "World!\0"; + puts(*world as *const str as *const u8); + world as Box; assert_eq!(intrinsics::size_of_val(hello) as u8, 6);