Implement posix_memalign

This commit is contained in:
Peter Limkilde Svendsen
2019-05-30 18:20:22 +02:00
parent dab6530fb4
commit 02d1a7fe6f
2 changed files with 94 additions and 0 deletions
+21
View File
@@ -582,6 +582,27 @@ pub extern "C" fn nrand48(xsubi: [c_ushort; 3]) -> c_long {
unimplemented!();
}
#[no_mangle]
pub unsafe extern "C" fn posix_memalign(memptr: *mut *mut c_void, alignment: size_t, size: size_t) -> c_int {
const void_ptr_size: usize = mem::size_of::<*mut c_void>();
/* Check that alignment is:
* a) a multiple of sizeof(void *)
* b) a power-of-two multiple of sizeof(void *). Integer division as
* below gives the correct result once a) is ensured. */
if alignment % void_ptr_size == 0 && (alignment/void_ptr_size).is_power_of_two() {
let ptr = platform::alloc_align(size, alignment);
if !ptr.is_null() {
*memptr = ptr;
0
} else {
ENOMEM
}
} else {
EINVAL
}
}
// #[no_mangle]
pub extern "C" fn ptsname(fildes: c_int) -> *mut c_char {
unimplemented!();