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
#![feature(allocator_api)] #![no_std] extern crate core; #[repr(C)] pub struct mi_heap_t { __x: u8 } #[link(name="mimalloc")] extern "C" { pub fn mi_stats_print(_: *mut libc::FILE); pub fn mi_stats_reset(); pub fn mi_malloc(_: usize) -> *mut u8; pub fn mi_free(_: *mut u8); pub fn mi_heap_malloc(_: *mut mi_heap_t,_: usize ) -> *mut u8; pub fn mi_heap_destroy(_: *mut mi_heap_t); pub fn mi_realloc(_: *mut u8,_: usize) -> *mut u8; pub fn mi_expand(_: *mut u8,_: usize) -> *mut u8; pub fn mi_zalloc(_: usize) -> *mut u8; pub fn mi_heap_get_default() -> *mut mi_heap_t; pub fn mi_calloc(_: usize,_: usize) -> *mut u8; } use core::alloc::{GlobalAlloc,Layout}; pub struct MiMalloc; unsafe impl GlobalAlloc for MiMalloc { unsafe fn alloc(&self,layout: Layout) -> *mut u8 { let ptr = mi_malloc(layout.size()); assert!(!ptr.is_null()); ptr } unsafe fn dealloc(&self,ptr: *mut u8,_:Layout) { assert!(!ptr.is_null()); mi_free(ptr); } unsafe fn alloc_zeroed(&self,layout: Layout) -> *mut u8 { mi_calloc(1,layout.size()) } unsafe fn realloc(&self,ptr: *mut u8,layout: Layout,new_size: usize) -> *mut u8 { if ptr.is_null() { self.alloc(layout) } else { mi_realloc(ptr, new_size) } } }