1
0
Fork 0
mirror of https://github.com/jugeeya/UltimateTrainingModpack.git synced 2025-03-14 02:16:10 +00:00

Add println to skyline-rs

This commit is contained in:
jam1garner 2020-04-08 01:13:06 -04:00
parent 4e05ea1e95
commit 1cf373d338
4 changed files with 52 additions and 28 deletions

View file

@ -1,35 +1,27 @@
use core::alloc::{GlobalAlloc, Layout};
use core::ffi::c_void;
extern "C" {
fn malloc(size: usize) -> *const c_void;
fn free(ptr: *const c_void);
fn calloc(num: usize, size: usize) -> *const c_void;
fn realloc(ptr: *const c_void, size: usize) -> *const c_void;
// fn aligned_alloc(align: usize, size: usize) -> *const c_void;
}
use crate::libc;
pub struct Allocator;
unsafe impl GlobalAlloc for Allocator {
#[inline]
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
malloc(layout.size()) as *mut u8
libc::malloc(layout.size()) as *mut u8
}
#[inline]
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
calloc(layout.size(), 1) as *mut u8
libc::calloc(layout.size(), 1) as *mut u8
}
#[inline]
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
free(ptr as *mut c_void)
libc::free(ptr as *mut libc::c_void)
}
#[inline]
unsafe fn realloc(&self, ptr: *mut u8, _layout: Layout, new_size: usize) -> *mut u8 {
realloc(ptr as *mut c_void, new_size) as *mut u8
libc::realloc(ptr as *mut libc::c_void, new_size) as *mut u8
}
}

View file

@ -2,10 +2,12 @@
#![allow(incomplete_features)]
#![feature(alloc_error_handler, lang_items, start, global_asm, const_generics, impl_trait_in_bindings)]
pub mod alloc;
pub use alloc::Allocator;
pub extern crate alloc;
pub mod libc;
pub mod build;
pub mod extern_alloc;
pub use extern_alloc::Allocator;
extern "C" {
fn skyline_tcp_send_raw(bytes: *const u8, usize: u64);
@ -17,3 +19,28 @@ pub fn log(message: &str) {
}
}
#[macro_export] macro_rules! setup {
() => {
#[global_allocator]
pub static ALLOCATOR: $crate::Allocator = $crate::Allocator;
};
}
#[macro_export] macro_rules! println {
() => {
$crate::log();
};
($($arg:tt)*) => {
{
use $crate::alloc::format;
$crate::log(&format!(
$($arg)*
));
}
};
}
pub mod prelude {
pub use crate::Allocator;
pub use crate::println;
}

13
skyline/src/libc.rs Normal file
View file

@ -0,0 +1,13 @@
//! Public exports of libc functions
pub use core::ffi::c_void;
#[allow(non_camel_case_types)]
type size_t = usize;
extern "C" {
pub fn malloc(size: size_t) -> *const c_void;
pub fn free(ptr: *const c_void);
pub fn calloc(num: size_t, size: size_t) -> *const c_void;
pub fn realloc(ptr: *const c_void, size: size_t) -> *const c_void;
// fn aligned_alloc(align: usize, size: usize) -> *const c_void;
}

View file

@ -1,20 +1,12 @@
#![no_std]
use skyline::{Allocator, log};
#[global_allocator]
pub static ALLOCATOR: Allocator = Allocator;
extern crate alloc;
use alloc::vec;
use skyline::prelude::*;
skyline::setup!();
#[no_mangle]
pub fn main() {
let x = vec![1, 2, 3];
log("Hello from Skyline Rust Plugin!\n");
println!("Hello from Skyline Rust Plugin!\n");
if x[1] == 2 {
log("x[1] == 2\n");
} else {
log("x[1] != 2\n");
for i in 0..3 {
println!("{}", i);
}
}