mu_lib 0.2.2

XCENA mu Library
Documentation
#![doc(hidden)]

#[repr(C)]
pub struct KernelInfo {
    pub name: *const core::ffi::c_char,
    pub func: extern "C" fn(mode: u32, ndarray_value_bit: u32, argv: *mut core::ffi::c_char),
}

unsafe impl Sync for KernelInfo {}

mod libc {
    use core::ffi::{c_int, c_void};

    #[link(name = "c")]
    #[allow(dead_code)]
    extern "C" {
        pub fn malloc(size: usize) -> *mut c_void;
        pub fn posix_memalign(memptr: *mut *mut c_void, alignment: usize, size: usize) -> c_int;
        //pub fn realloc(ptr: *mut c_void, size: usize) -> *mut c_void;
        //pub fn calloc(nmemb: usize, size: usize) -> *mut c_void;
        pub fn free(ptr: *mut u8);
    }
}

#[allow(dead_code)]
pub struct LibcAllocator;
unsafe impl core::alloc::GlobalAlloc for LibcAllocator {
    #[inline]
    unsafe fn alloc(&self, layout: core::alloc::Layout) -> *mut u8 {
        use core::ffi::c_void;
        let mut memptr: *mut c_void = core::ptr::null_mut();
        let alignment = layout.align();
        let size = layout.size();

        if size == 0 || size > 4 * 1024 * 1024 {
            return core::ptr::null_mut() as *mut u8;
        }

        if alignment >= core::mem::size_of::<*const ()>() {
            let result = libc::posix_memalign(&mut memptr as *mut *mut c_void, alignment, size);
            if result != 0 {
                return core::ptr::null_mut() as *mut u8;
            }
        } else {
            memptr = libc::malloc(size);
            if memptr.is_null() {
                return core::ptr::null_mut() as *mut u8;
            }
        }

        memptr as *mut u8
    }

    #[inline]
    unsafe fn dealloc(&self, ptr: *mut u8, _layout: core::alloc::Layout) {
        libc::free(ptr as *mut _);
    }
}

#[cfg(feature = "default_global_allocator")]
#[global_allocator]
static ALLOCATOR: LibcAllocator = LibcAllocator;