| 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 = "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 cfg!(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 | panic!("add a value for MIN_ALIGN" ) |
| 47 | }; |
| 48 | |
| 49 | #[allow (dead_code)] |
| 50 | unsafe fn realloc_fallback( |
| 51 | alloc: &System, |
| 52 | ptr: *mut u8, |
| 53 | old_layout: Layout, |
| 54 | new_size: usize, |
| 55 | ) -> *mut u8 { |
| 56 | // SAFETY: Docs for GlobalAlloc::realloc require this to be valid |
| 57 | unsafe { |
| 58 | let new_layout: Layout = Layout::from_size_align_unchecked(new_size, old_layout.align()); |
| 59 | |
| 60 | let new_ptr: *mut u8 = GlobalAlloc::alloc(self:alloc, new_layout); |
| 61 | if !new_ptr.is_null() { |
| 62 | let size: usize = usize::min(self:old_layout.size(), other:new_size); |
| 63 | ptr::copy_nonoverlapping(src:ptr, dst:new_ptr, count:size); |
| 64 | GlobalAlloc::dealloc(self:alloc, ptr, old_layout); |
| 65 | } |
| 66 | |
| 67 | new_ptr |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | cfg_if::cfg_if! { |
| 72 | if #[cfg(any( |
| 73 | target_family = "unix" , |
| 74 | target_os = "wasi" , |
| 75 | target_os = "teeos" , |
| 76 | target_os = "trusty" , |
| 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 | } |
| 97 | |