Skip to content

Commit cfb955d

Browse files
committed
Consolidate wasi alloc with unix alloc.
1 parent 5a4098e commit cfb955d

File tree

3 files changed

+45
-111
lines changed

3 files changed

+45
-111
lines changed

library/std/src/sys/unix/alloc.rs

+44-42
Original file line numberDiff line numberDiff line change
@@ -52,46 +52,48 @@ unsafe impl GlobalAlloc for System {
5252
}
5353
}
5454

55-
#[cfg(any(
56-
target_os = "android",
57-
target_os = "illumos",
58-
target_os = "redox",
59-
target_os = "solaris"
60-
))]
61-
#[inline]
62-
unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {
63-
// On android we currently target API level 9 which unfortunately
64-
// doesn't have the `posix_memalign` API used below. Instead we use
65-
// `memalign`, but this unfortunately has the property on some systems
66-
// where the memory returned cannot be deallocated by `free`!
67-
//
68-
// Upon closer inspection, however, this appears to work just fine with
69-
// Android, so for this platform we should be fine to call `memalign`
70-
// (which is present in API level 9). Some helpful references could
71-
// possibly be chromium using memalign [1], attempts at documenting that
72-
// memalign + free is ok [2] [3], or the current source of chromium
73-
// which still uses memalign on android [4].
74-
//
75-
// [1]: https://codereview.chromium.org/10796020/
76-
// [2]: https://code.google.com/p/android/issues/detail?id=35391
77-
// [3]: https://bugs.chromium.org/p/chromium/issues/detail?id=138579
78-
// [4]: https://chromium.googlesource.com/chromium/src/base/+/master/
79-
// /memory/aligned_memory.cc
80-
libc::memalign(layout.align(), layout.size()) as *mut u8
81-
}
82-
83-
#[cfg(not(any(
84-
target_os = "android",
85-
target_os = "illumos",
86-
target_os = "redox",
87-
target_os = "solaris"
88-
)))]
89-
#[inline]
90-
unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {
91-
let mut out = ptr::null_mut();
92-
// posix_memalign requires that the alignment be a multiple of `sizeof(void*)`.
93-
// Since these are all powers of 2, we can just use max.
94-
let align = layout.align().max(crate::mem::size_of::<usize>());
95-
let ret = libc::posix_memalign(&mut out, align, layout.size());
96-
if ret != 0 { ptr::null_mut() } else { out as *mut u8 }
55+
cfg_if::cfg_if! {
56+
if #[cfg(any(
57+
target_os = "android",
58+
target_os = "illumos",
59+
target_os = "redox",
60+
target_os = "solaris"
61+
))] {
62+
#[inline]
63+
unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {
64+
// On android we currently target API level 9 which unfortunately
65+
// doesn't have the `posix_memalign` API used below. Instead we use
66+
// `memalign`, but this unfortunately has the property on some systems
67+
// where the memory returned cannot be deallocated by `free`!
68+
//
69+
// Upon closer inspection, however, this appears to work just fine with
70+
// Android, so for this platform we should be fine to call `memalign`
71+
// (which is present in API level 9). Some helpful references could
72+
// possibly be chromium using memalign [1], attempts at documenting that
73+
// memalign + free is ok [2] [3], or the current source of chromium
74+
// which still uses memalign on android [4].
75+
//
76+
// [1]: https://codereview.chromium.org/10796020/
77+
// [2]: https://code.google.com/p/android/issues/detail?id=35391
78+
// [3]: https://bugs.chromium.org/p/chromium/issues/detail?id=138579
79+
// [4]: https://chromium.googlesource.com/chromium/src/base/+/master/
80+
// /memory/aligned_memory.cc
81+
libc::memalign(layout.align(), layout.size()) as *mut u8
82+
}
83+
} else if #[cfg(target_os = "wasi")] {
84+
#[inline]
85+
unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {
86+
libc::aligned_alloc(layout.align(), layout.size()) as *mut u8
87+
}
88+
} else {
89+
#[inline]
90+
unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {
91+
let mut out = ptr::null_mut();
92+
// posix_memalign requires that the alignment be a multiple of `sizeof(void*)`.
93+
// Since these are all powers of 2, we can just use max.
94+
let align = layout.align().max(crate::mem::size_of::<usize>());
95+
let ret = libc::posix_memalign(&mut out, align, layout.size());
96+
if ret != 0 { ptr::null_mut() } else { out as *mut u8 }
97+
}
98+
}
9799
}

library/std/src/sys/wasi/alloc.rs

-69
This file was deleted.

library/std/src/sys/wasi/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use crate::io as std_io;
1818
use crate::mem;
1919

20+
#[path = "../unix/alloc.rs"]
2021
pub mod alloc;
2122
pub mod args;
2223
#[path = "../unsupported/cmath.rs"]

0 commit comments

Comments
 (0)