Skip to content
Snippets Groups Projects
Commit 62fbff93 authored by Jeremy Soller's avatar Jeremy Soller
Browse files

Clean up malloc

parent 742339ca
No related branches found
No related tags found
No related merge requests found
...@@ -310,35 +310,33 @@ pub extern "C" fn lrand48() -> c_long { ...@@ -310,35 +310,33 @@ pub extern "C" fn lrand48() -> c_long {
unimplemented!(); unimplemented!();
} }
#[no_mangle] unsafe fn malloc_inner(size: usize, offset: usize, align: usize) -> *mut c_void {
pub unsafe extern "C" fn malloc(size: size_t) -> *mut c_void { assert!(offset >= 16);
let align = 8; assert!(align >= 8);
let ptr = ralloc::alloc(size + 16, align);
let ptr = ralloc::alloc(size + offset, align);
if !ptr.is_null() { if !ptr.is_null() {
*(ptr as *mut u64) = (size + 16) as u64; *(ptr as *mut u64) = (size + offset) as u64;
*(ptr as *mut u64).offset(1) = align as u64; *(ptr as *mut u64).offset(1) = align as u64;
ptr.offset(16) as *mut c_void ptr.offset(offset as isize) as *mut c_void
} else { } else {
ptr as *mut c_void ptr as *mut c_void
} }
} }
#[no_mangle]
pub unsafe extern "C" fn malloc(size: size_t) -> *mut c_void {
malloc_inner(size, 16, 8)
}
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn memalign(alignment: size_t, size: size_t) -> *mut c_void { pub unsafe extern "C" fn memalign(alignment: size_t, size: size_t) -> *mut c_void {
let mut align = 16; let mut align = 16;
while align <= alignment { while align <= alignment as usize {
align *= 2; align *= 2;
} }
let offset = align/2; malloc_inner(size, align/2, align)
let ptr = ralloc::alloc(size + offset, align);
if !ptr.is_null() {
*(ptr as *mut u64) = (size + offset) as u64;
*(ptr as *mut u64).offset(1) = align as u64;
ptr.offset(offset as isize) as *mut c_void
} else {
ptr as *mut c_void
}
} }
#[no_mangle] #[no_mangle]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment