1#![forbid(unsafe_op_in_unsafe_fn)]
2
3use crate::alloc::Layout;
4use crate::ptr;
5
6#[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 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 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(target_family = "unix", target_os = "wasi", target_os = "teeos", target_os = "trusty") => {
68 mod unix;
69 use unix as imp;
70 }
71 target_os = "windows" => {
72 mod windows;
73 use windows as imp;
74 }
75 target_os = "hermit" => {
76 mod hermit;
77 use hermit as imp;
78 }
79 target_os = "motor" => {
80 mod motor;
81 use motor as imp;
82 }
83 all(target_vendor = "fortanix", target_env = "sgx") => {
84 mod sgx;
85 use sgx as imp;
86 }
87 target_os = "solid_asp3" => {
88 mod solid;
89 use solid as imp;
90 }
91 target_os = "uefi" => {
92 mod uefi;
93 use uefi as imp;
94 }
95 target_os = "vexos" => {
96 mod vexos;
97 use vexos as imp;
98 }
99 target_family = "wasm" => {
100 mod wasm;
101 use wasm as imp;
102 }
103 target_os = "xous" => {
104 mod xous;
105 use xous as imp;
106 }
107 target_os = "zkvm" => {
108 mod zkvm;
109 use zkvm as imp;
110 }
111}
112
113pub use imp::{alloc, dealloc, realloc};
114
115cfg_select! {
116 any(target_os = "hermit", target_os = "solid_asp3", target_os = "uefi", target_os = "zkvm") => {
117 #[inline]
118 pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
119 let ptr = unsafe { alloc(layout) };
120 if !ptr.is_null() {
121 unsafe { ptr.write_bytes(0, layout.size()) };
122 }
123 ptr
124 }
125 }
126 _ => {
127 pub use imp::alloc_zeroed;
128 }
129}