Browse Source

Merge branch 'simplify-posix_memalign' into 'master'

Minor fixes to posix_memalign()

See merge request redox-os/relibc!271
Jeremy Soller 4 years ago
parent
commit
39346f5c05
1 changed files with 7 additions and 10 deletions
  1. 7 10
      src/header/stdlib/mod.rs

+ 7 - 10
src/header/stdlib/mod.rs

@@ -643,21 +643,18 @@ pub unsafe extern "C" fn posix_memalign(
     alignment: size_t,
     size: size_t,
 ) -> c_int {
-    const void_ptr_size: usize = mem::size_of::<*mut c_void>();
+    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() {
+    if alignment % VOID_PTR_SIZE == 0 && alignment.is_power_of_two() {
         let ptr = platform::alloc_align(size, alignment);
-        if !ptr.is_null() {
-            *memptr = ptr;
-            0
-        } else {
+        *memptr = ptr;
+        if ptr.is_null() {
             ENOMEM
+        } else {
+            0
         }
     } else {
+        *memptr = ptr::null_mut();
         EINVAL
     }
 }