3 ms·
fairly straightforward: #[global_allocator] static GLOBAL: StupidAlloc = StupidAlloc; #[no_mangle] pub unsafe extern "
by raggi 3y ago
fairly straightforward:
#[global_allocator]
static GLOBAL: StupidAlloc = StupidAlloc;
#[no_mangle]
pub unsafe extern "C" fn stupid_malloc(size: usize) -> *mut u8 {
let layout = Layout::from_size_align(size, 1).unwrap();
GLOBAL.alloc(layout)
}
#[no_mangle]
pub unsafe extern "C" fn stupid_free(ptr: *mut u8) {
let layout = Layout::from_size_align(1, 1).unwrap();
GLOBAL.dealloc(ptr, layout)
}
then build with --crate-type cdylib, giving you a .so you can link
- lights0123 3y agoAlthough this crate likely enforces a higher alignment (to the page boundary with mmap) regardless of what is requested, you should request a higher alignment because malloc is guaranteed to be aligned to the largest C type, which is usually either 8 or 16 bytes.
- raggi 3y agofixing the bugs is left as an exercise for the reader