1 | #![forbid (unsafe_op_in_unsafe_fn)] |
2 | |
3 | use crate::alloc::{GlobalAlloc, Layout, System}; |
4 | use 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)] |
9 | const MIN_ALIGN: usize = if cfg!(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 cfg!(any( |
16 | target_arch = "x86" , |
17 | target_arch = "arm" , |
18 | target_arch = "m68k" , |
19 | target_arch = "csky" , |
20 | target_arch = "mips" , |
21 | target_arch = "mips32r6" , |
22 | target_arch = "powerpc" , |
23 | target_arch = "powerpc64" , |
24 | target_arch = "sparc" , |
25 | target_arch = "wasm32" , |
26 | target_arch = "hexagon" , |
27 | target_arch = "riscv32" , |
28 | target_arch = "xtensa" , |
29 | )) { |
30 | 8 |
31 | } else if cfg!(any( |
32 | target_arch = "x86_64" , |
33 | target_arch = "aarch64" , |
34 | target_arch = "arm64ec" , |
35 | target_arch = "loongarch64" , |
36 | target_arch = "mips64" , |
37 | target_arch = "mips64r6" , |
38 | target_arch = "s390x" , |
39 | target_arch = "sparc64" , |
40 | target_arch = "riscv64" , |
41 | target_arch = "wasm64" , |
42 | )) { |
43 | 16 |
44 | } else { |
45 | panic!("add a value for MIN_ALIGN" ) |
46 | }; |
47 | |
48 | #[allow (dead_code)] |
49 | unsafe fn realloc_fallback( |
50 | alloc: &System, |
51 | ptr: *mut u8, |
52 | old_layout: Layout, |
53 | new_size: usize, |
54 | ) -> *mut u8 { |
55 | // SAFETY: Docs for GlobalAlloc::realloc require this to be valid |
56 | unsafe { |
57 | let new_layout: Layout = Layout::from_size_align_unchecked(new_size, old_layout.align()); |
58 | |
59 | let new_ptr: *mut u8 = GlobalAlloc::alloc(self:alloc, new_layout); |
60 | if !new_ptr.is_null() { |
61 | let size: usize = usize::min(self:old_layout.size(), other:new_size); |
62 | ptr::copy_nonoverlapping(src:ptr, dst:new_ptr, count:size); |
63 | GlobalAlloc::dealloc(self:alloc, ptr, old_layout); |
64 | } |
65 | |
66 | new_ptr |
67 | } |
68 | } |
69 | |
70 | cfg_if::cfg_if! { |
71 | if #[cfg(any( |
72 | target_family = "unix" , |
73 | target_os = "wasi" , |
74 | target_os = "teeos" , |
75 | target_os = "trusty" , |
76 | ))] { |
77 | mod unix; |
78 | } else if #[cfg(target_os = "windows" )] { |
79 | mod windows; |
80 | } else if #[cfg(target_os = "hermit" )] { |
81 | mod hermit; |
82 | } else if #[cfg(all(target_vendor = "fortanix" , target_env = "sgx" ))] { |
83 | mod sgx; |
84 | } else if #[cfg(target_os = "solid_asp3" )] { |
85 | mod solid; |
86 | } else if #[cfg(target_os = "uefi" )] { |
87 | mod uefi; |
88 | } else if #[cfg(target_family = "wasm" )] { |
89 | mod wasm; |
90 | } else if #[cfg(target_os = "xous" )] { |
91 | mod xous; |
92 | } else if #[cfg(target_os = "zkvm" )] { |
93 | mod zkvm; |
94 | } |
95 | } |
96 | |