Hacky version of memalign

This commit is contained in:
Jeremy Soller
2018-04-03 19:45:36 -06:00
parent dabd8dc6a2
commit 742339ca9e
2 changed files with 27 additions and 1 deletions
+18
View File
@@ -323,6 +323,24 @@ pub unsafe extern "C" fn malloc(size: size_t) -> *mut c_void {
}
}
#[no_mangle]
pub unsafe extern "C" fn memalign(alignment: size_t, size: size_t) -> *mut c_void {
let mut align = 16;
while align <= alignment {
align *= 2;
}
let offset = align/2;
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]
pub extern "C" fn mblen(s: *const c_char, n: size_t) -> c_int {
unimplemented!();