Skip to content

Commit 4de6297

Browse files
committed
std: move allocators to sys
1 parent 08a9ca7 commit 4de6297

File tree

29 files changed

+88
-153
lines changed

29 files changed

+88
-153
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
1-
use super::hermit_abi;
21
use crate::alloc::{GlobalAlloc, Layout, System};
32
use crate::ptr;
43

54
#[stable(feature = "alloc_system_type", since = "1.28.0")]
65
unsafe impl GlobalAlloc for System {
76
#[inline]
87
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
9-
hermit_abi::malloc(layout.size(), layout.align())
8+
unsafe { hermit_abi::malloc(layout.size(), layout.align()) }
109
}
1110

1211
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
13-
let addr = hermit_abi::malloc(layout.size(), layout.align());
12+
let addr = unsafe { hermit_abi::malloc(layout.size(), layout.align()) };
1413

1514
if !addr.is_null() {
16-
ptr::write_bytes(addr, 0x00, layout.size());
15+
unsafe {
16+
ptr::write_bytes(addr, 0x00, layout.size());
17+
}
1718
}
1819

1920
addr
2021
}
2122

2223
#[inline]
2324
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
24-
hermit_abi::free(ptr, layout.size(), layout.align())
25+
unsafe { hermit_abi::free(ptr, layout.size(), layout.align()) }
2526
}
2627

2728
#[inline]
2829
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
29-
hermit_abi::realloc(ptr, layout.size(), layout.align(), new_size)
30+
unsafe { hermit_abi::realloc(ptr, layout.size(), layout.align(), new_size) }
3031
}
3132
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
#![forbid(unsafe_op_in_unsafe_fn)]
2-
use crate::alloc::{GlobalAlloc, Layout, System};
3-
use crate::cmp;
2+
3+
use crate::alloc::GlobalAlloc;
4+
use crate::alloc::Layout;
5+
use crate::alloc::System;
46
use crate::ptr;
57

68
// The minimum alignment guaranteed by the architecture. This value is used to
79
// add fast paths for low alignment values.
8-
#[cfg(any(
10+
#[allow(dead_code)]
11+
const MIN_ALIGN: usize = if cfg!(any(
12+
all(target_arch = "riscv32", any(target_os = "espidf", target_os = "zkvm")),
13+
all(target_arch = "xtensa", target_os = "espidf"),
14+
)) {
15+
// The allocator on the esp-idf and zkvm platforms guarantees 4 byte alignment.
16+
4
17+
} else if cfg!(any(
918
target_arch = "x86",
1019
target_arch = "arm",
1120
target_arch = "m68k",
@@ -17,11 +26,11 @@ use crate::ptr;
1726
target_arch = "sparc",
1827
target_arch = "wasm32",
1928
target_arch = "hexagon",
20-
all(target_arch = "riscv32", not(any(target_os = "espidf", target_os = "zkvm"))),
21-
all(target_arch = "xtensa", not(target_os = "espidf")),
22-
))]
23-
pub const MIN_ALIGN: usize = 8;
24-
#[cfg(any(
29+
target_arch = "riscv32",
30+
target_arch = "xtensa",
31+
)) {
32+
8
33+
} else if cfg!(any(
2534
target_arch = "x86_64",
2635
target_arch = "aarch64",
2736
target_arch = "arm64ec",
@@ -32,16 +41,14 @@ pub const MIN_ALIGN: usize = 8;
3241
target_arch = "sparc64",
3342
target_arch = "riscv64",
3443
target_arch = "wasm64",
35-
))]
36-
pub const MIN_ALIGN: usize = 16;
37-
// The allocator on the esp-idf and zkvm platforms guarantee 4 byte alignment.
38-
#[cfg(all(any(
39-
all(target_arch = "riscv32", any(target_os = "espidf", target_os = "zkvm")),
40-
all(target_arch = "xtensa", target_os = "espidf"),
41-
)))]
42-
pub const MIN_ALIGN: usize = 4;
44+
)) {
45+
16
46+
} else {
47+
panic!("add a value for MIN_ALIGN")
48+
};
4349

44-
pub unsafe fn realloc_fallback(
50+
#[allow(dead_code)]
51+
unsafe fn realloc_fallback(
4552
alloc: &System,
4653
ptr: *mut u8,
4754
old_layout: Layout,
@@ -53,10 +60,37 @@ pub unsafe fn realloc_fallback(
5360

5461
let new_ptr = GlobalAlloc::alloc(alloc, new_layout);
5562
if !new_ptr.is_null() {
56-
let size = cmp::min(old_layout.size(), new_size);
63+
let size = usize::min(old_layout.size(), new_size);
5764
ptr::copy_nonoverlapping(ptr, new_ptr, size);
5865
GlobalAlloc::dealloc(alloc, ptr, old_layout);
5966
}
67+
6068
new_ptr
6169
}
6270
}
71+
72+
cfg_if::cfg_if! {
73+
if #[cfg(any(
74+
target_family = "unix",
75+
target_os = "wasi",
76+
target_os = "teeos",
77+
))] {
78+
mod unix;
79+
} else if #[cfg(target_os = "windows")] {
80+
mod windows;
81+
} else if #[cfg(target_os = "hermit")] {
82+
mod hermit;
83+
} else if #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] {
84+
mod sgx;
85+
} else if #[cfg(target_os = "solid_asp3")] {
86+
mod solid;
87+
} else if #[cfg(target_os = "uefi")] {
88+
mod uefi;
89+
} else if #[cfg(target_family = "wasm")] {
90+
mod wasm;
91+
} else if #[cfg(target_os = "xous")] {
92+
mod xous;
93+
} else if #[cfg(target_os = "zkvm")] {
94+
mod zkvm;
95+
}
96+
}

library/std/src/sys/pal/sgx/alloc.rs renamed to library/std/src/sys/alloc/sgx.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use crate::alloc::{GlobalAlloc, Layout, System};
22
use crate::ptr;
33
use core::sync::atomic::{AtomicBool, Ordering};
44

5-
use super::abi::mem as sgx_mem;
6-
use super::waitqueue::SpinMutex;
5+
use crate::sys::pal::abi::mem as sgx_mem;
6+
use crate::sys::pal::waitqueue::SpinMutex;
77

88
// Using a SpinMutex because we never want to exit the enclave waiting for the
99
// allocator.

library/std/src/sys/pal/solid/alloc.rs renamed to library/std/src/sys/alloc/solid.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
use crate::{
2-
alloc::{GlobalAlloc, Layout, System},
3-
sys::common::alloc::{realloc_fallback, MIN_ALIGN},
4-
};
1+
use super::{realloc_fallback, MIN_ALIGN};
2+
use crate::alloc::{GlobalAlloc, Layout, System};
53

64
#[stable(feature = "alloc_system_type", since = "1.28.0")]
75
unsafe impl GlobalAlloc for System {

library/std/src/sys/pal/uefi/alloc.rs renamed to library/std/src/sys/alloc/uefi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
44
use r_efi::protocols::loaded_image;
55

6-
use super::helpers;
76
use crate::alloc::{GlobalAlloc, Layout, System};
87
use crate::sync::OnceLock;
8+
use crate::sys::pal::helpers;
99

1010
#[stable(feature = "alloc_system_type", since = "1.28.0")]
1111
unsafe impl GlobalAlloc for System {

library/std/src/sys/pal/unix/alloc.rs renamed to library/std/src/sys/alloc/unix.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
use super::{realloc_fallback, MIN_ALIGN};
12
use crate::alloc::{GlobalAlloc, Layout, System};
23
use crate::ptr;
3-
use crate::sys::common::alloc::{realloc_fallback, MIN_ALIGN};
44

55
#[stable(feature = "alloc_system_type", since = "1.28.0")]
66
unsafe impl GlobalAlloc for System {
@@ -11,7 +11,7 @@ unsafe impl GlobalAlloc for System {
1111
// Also see <https://github.com/rust-lang/rust/issues/45955> and
1212
// <https://github.com/rust-lang/rust/issues/62251#issuecomment-507580914>.
1313
if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
14-
libc::malloc(layout.size()) as *mut u8
14+
unsafe { libc::malloc(layout.size()) as *mut u8 }
1515
} else {
1616
// `posix_memalign` returns a non-aligned value if supplied a very
1717
// large alignment on older versions of Apple's platforms (unknown
@@ -25,35 +25,35 @@ unsafe impl GlobalAlloc for System {
2525
return ptr::null_mut();
2626
}
2727
}
28-
aligned_malloc(&layout)
28+
unsafe { aligned_malloc(&layout) }
2929
}
3030
}
3131

3232
#[inline]
3333
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
3434
// See the comment above in `alloc` for why this check looks the way it does.
3535
if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
36-
libc::calloc(layout.size(), 1) as *mut u8
36+
unsafe { libc::calloc(layout.size(), 1) as *mut u8 }
3737
} else {
38-
let ptr = self.alloc(layout);
38+
let ptr = unsafe { self.alloc(layout) };
3939
if !ptr.is_null() {
40-
ptr::write_bytes(ptr, 0, layout.size());
40+
unsafe { ptr::write_bytes(ptr, 0, layout.size()) };
4141
}
4242
ptr
4343
}
4444
}
4545

4646
#[inline]
4747
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
48-
libc::free(ptr as *mut libc::c_void)
48+
unsafe { libc::free(ptr as *mut libc::c_void) }
4949
}
5050

5151
#[inline]
5252
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
5353
if layout.align() <= MIN_ALIGN && layout.align() <= new_size {
54-
libc::realloc(ptr as *mut libc::c_void, new_size) as *mut u8
54+
unsafe { libc::realloc(ptr as *mut libc::c_void, new_size) as *mut u8 }
5555
} else {
56-
realloc_fallback(self, ptr, layout, new_size)
56+
unsafe { realloc_fallback(self, ptr, layout, new_size) }
5757
}
5858
}
5959
}
@@ -81,7 +81,7 @@ cfg_if::cfg_if! {
8181
// posix_memalign only has one, clear requirement: that the alignment be a multiple of
8282
// `sizeof(void*)`. Since these are all powers of 2, we can just use max.
8383
let align = layout.align().max(crate::mem::size_of::<usize>());
84-
let ret = libc::posix_memalign(&mut out, align, layout.size());
84+
let ret = unsafe { libc::posix_memalign(&mut out, align, layout.size()) };
8585
if ret != 0 { ptr::null_mut() } else { out as *mut u8 }
8686
}
8787
}

library/std/src/sys/pal/windows/alloc.rs renamed to library/std/src/sys/alloc/windows.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
use super::{realloc_fallback, MIN_ALIGN};
12
use crate::alloc::{GlobalAlloc, Layout, System};
23
use crate::ffi::c_void;
34
use crate::ptr;
45
use crate::sync::atomic::{AtomicPtr, Ordering};
5-
use crate::sys::c::{self, windows_targets};
6-
use crate::sys::common::alloc::{realloc_fallback, MIN_ALIGN};
6+
use crate::sys::pal::c::{self, windows_targets};
77
use core::mem::MaybeUninit;
88

99
#[cfg(test)]
@@ -112,28 +112,28 @@ fn init_or_get_process_heap() -> c::HANDLE {
112112
extern "C" fn process_heap_init_and_alloc(
113113
_heap: MaybeUninit<c::HANDLE>, // We pass this argument to match the ABI of `HeapAlloc`
114114
flags: u32,
115-
dwBytes: usize,
115+
bytes: usize,
116116
) -> *mut c_void {
117117
let heap = init_or_get_process_heap();
118118
if core::intrinsics::unlikely(heap.is_null()) {
119119
return ptr::null_mut();
120120
}
121121
// SAFETY: `heap` is a non-null handle returned by `GetProcessHeap`.
122-
unsafe { HeapAlloc(heap, flags, dwBytes) }
122+
unsafe { HeapAlloc(heap, flags, bytes) }
123123
}
124124

125125
#[inline(never)]
126126
fn process_heap_alloc(
127127
_heap: MaybeUninit<c::HANDLE>, // We pass this argument to match the ABI of `HeapAlloc`,
128128
flags: u32,
129-
dwBytes: usize,
129+
bytes: usize,
130130
) -> *mut c_void {
131131
let heap = HEAP.load(Ordering::Relaxed);
132132
if core::intrinsics::likely(!heap.is_null()) {
133133
// SAFETY: `heap` is a non-null handle returned by `GetProcessHeap`.
134-
unsafe { HeapAlloc(heap, flags, dwBytes) }
134+
unsafe { HeapAlloc(heap, flags, bytes) }
135135
} else {
136-
process_heap_init_and_alloc(MaybeUninit::uninit(), flags, dwBytes)
136+
process_heap_init_and_alloc(MaybeUninit::uninit(), flags, bytes)
137137
}
138138
}
139139

library/std/src/sys/pal/zkvm/alloc.rs renamed to library/std/src/sys/alloc/zkvm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use super::abi;
21
use crate::alloc::{GlobalAlloc, Layout, System};
2+
use crate::sys::pal::abi;
33

44
#[stable(feature = "alloc_system_type", since = "1.28.0")]
55
unsafe impl GlobalAlloc for System {

library/std/src/sys/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
/// descriptors.
66
mod pal;
77

8+
mod alloc;
89
mod personality;
910

1011
#[unstable(feature = "anonymous_pipe", issue = "127154")]

library/std/src/sys/pal/common/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
#![allow(dead_code)]
1212

13-
pub mod alloc;
1413
pub mod small_c_string;
1514

1615
#[cfg(test)]

library/std/src/sys/pal/hermit/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
use crate::os::raw::c_char;
1919

20-
pub mod alloc;
2120
pub mod args;
2221
pub mod env;
2322
pub mod fd;

library/std/src/sys/pal/sgx/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use crate::io::ErrorKind;
99
use crate::sync::atomic::{AtomicBool, Ordering};
1010

1111
pub mod abi;
12-
pub mod alloc;
1312
pub mod args;
1413
pub mod env;
1514
pub mod fd;

library/std/src/sys/pal/solid/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ pub mod itron {
1616
use super::unsupported;
1717
}
1818

19-
pub mod alloc;
2019
#[path = "../unsupported/args.rs"]
2120
pub mod args;
2221
pub mod env;

library/std/src/sys/pal/teeos/alloc.rs

-57
This file was deleted.

0 commit comments

Comments
 (0)