Skip to main content

std/sys/alloc/
mod.rs

1#![forbid(unsafe_op_in_unsafe_fn)]
2
3use crate::alloc::Layout;
4use crate::ptr;
5
6// The minimum alignment guaranteed by the architecture. This value is used to
7// add fast paths for low alignment values.
8#[allow(dead_code)]
9const MIN_ALIGN: usize = if falsecfg!(any(
10    all(target_arch = "riscv32", any(target_os = "espidf", target_os = "zkvm")),
11    all(target_arch = "xtensa", target_os = "espidf"),
12)) {
13    // The allocator on the esp-idf and zkvm platforms guarantees 4 byte alignment.
14    4
15} else if falsecfg!(any(
16    target_arch = "x86",
17    target_arch = "arm",
18    target_arch = "m68k",
19    target_arch = "csky",
20    target_arch = "loongarch32",
21    target_arch = "mips",
22    target_arch = "mips32r6",
23    target_arch = "powerpc",
24    target_arch = "powerpc64",
25    target_arch = "sparc",
26    target_arch = "wasm32",
27    target_arch = "hexagon",
28    target_arch = "riscv32",
29    target_arch = "xtensa",
30)) {
31    8
32} else if truecfg!(any(
33    target_arch = "x86_64",
34    target_arch = "aarch64",
35    target_arch = "arm64ec",
36    target_arch = "loongarch64",
37    target_arch = "mips64",
38    target_arch = "mips64r6",
39    target_arch = "s390x",
40    target_arch = "sparc64",
41    target_arch = "riscv64",
42    target_arch = "wasm64",
43)) {
44    16
45} else {
46    { ::core::panicking::panic_fmt(format_args!("add a value for MIN_ALIGN")); }panic!("add a value for MIN_ALIGN")
47};
48
49#[allow(dead_code)]
50unsafe fn realloc_fallback(ptr: *mut u8, old_layout: Layout, new_size: usize) -> *mut u8 {
51    // SAFETY: Docs for GlobalAlloc::realloc require this to be valid
52    unsafe {
53        let new_layout = Layout::from_size_align_unchecked(new_size, old_layout.align());
54
55        let new_ptr = alloc(new_layout);
56        if !new_ptr.is_null() {
57            let size = usize::min(old_layout.size(), new_size);
58            ptr::copy_nonoverlapping(ptr, new_ptr, size);
59            dealloc(ptr, old_layout);
60        }
61
62        new_ptr
63    }
64}
65
66cfg_select! {
67    any(
68        target_family = "unix",
69        target_os = "wasi",
70        target_os = "teeos",
71        target_os = "trusty",
72    ) => {
73        mod unix;
74        use unix as imp;
75    }
76    target_os = "windows" => {
77        mod windows;
78        use windows as imp;
79    }
80    target_os = "hermit" => {
81        mod hermit;
82        use hermit as imp;
83    }
84    target_os = "motor" => {
85        mod motor;
86        use motor as imp;
87    }
88    all(target_vendor = "fortanix", target_env = "sgx") => {
89        mod sgx;
90        use sgx as imp;
91    }
92    target_os = "solid_asp3" => {
93        mod solid;
94        use solid as imp;
95    }
96    target_os = "uefi" => {
97        mod uefi;
98        use uefi as imp;
99    }
100    target_os = "vexos" => {
101        mod vexos;
102        use vexos as imp;
103    }
104    target_family = "wasm" => {
105        mod wasm;
106        use wasm as imp;
107    }
108    target_os = "xous" => {
109        mod xous;
110        use xous as imp;
111    }
112    target_os = "zkvm" => {
113        mod zkvm;
114        use zkvm as imp;
115    }
116}
117
118pub use imp::{alloc, dealloc, realloc};
119
120cfg_select! {
121    any(
122        target_os = "hermit",
123        target_os = "solid_asp3",
124        target_os = "uefi",
125        target_os = "zkvm",
126    ) => {
127        #[inline]
128        pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
129            let ptr = unsafe { alloc(layout) };
130            if !ptr.is_null() {
131                unsafe { ptr.write_bytes(0, layout.size()) };
132            }
133            ptr
134        }
135    }
136    _ => {
137        pub use imp::alloc_zeroed;
138    }
139}