1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#![no_std]
#![feature(lang_items)]
#![feature(alloc_error_handler)]
#![feature(panic_info_message)]
extern crate alloc;
use alloc::string::ToString;
use core::alloc::{GlobalAlloc, Layout};
use alloc::format;
use cty::c_void;
#[cfg(feature = "eh-personality")]
#[lang = "eh_personality"]
extern "C" fn eh_personality() {}
#[cfg(feature = "oom-handler")]
#[alloc_error_handler]
fn on_oom(_layout: core::alloc::Layout) -> ! {
unsafe {
ndless_sys::abort();
}
}
#[cfg(feature = "panic-handler")]
#[panic_handler]
fn panic(info: &core::panic::PanicInfo) -> ! {
{
let msg = match info.message() {
Some(err) => format!("An error occured: {}", err),
None => "An error occured!".to_string(),
};
let location = match info.location() {
Some(loc) => format!(
"In file {} at line {} column {}",
loc.file(),
loc.line(),
loc.column()
),
None => "".to_string(),
};
ndless::msg::msg("Error", &format!("{}\n{}", msg, location));
}
ndless::process::abort();
}
struct CAllocator;
unsafe impl GlobalAlloc for CAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
ndless_sys::calloc(1, layout.size()) as *mut u8
}
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
ndless_sys::free(ptr as *mut c_void)
}
}
#[cfg(feature = "allocator")]
#[global_allocator]
static A: CAllocator = CAllocator;
#[cfg(feature = "ctype-ptr")]
#[no_mangle]
pub static __ctype_ptr__: [u8; 128 + 256] = [0; 128 + 256];