about summary refs log tree commit diff
path: root/library/std/src/sys/common/alloc.rs
diff options
context:
space:
mode:
authorChristiaan Dirkx <christiaan@dirkx.email>2021-02-24 19:16:24 +0100
committerChristiaan Dirkx <christiaan@dirkx.email>2021-04-14 13:24:10 +0200
commit905d23b65c07a1da4452f9a20b1891fc46533fb7 (patch)
tree0d69242543d3852ec3f569172d587209e0851b32 /library/std/src/sys/common/alloc.rs
parent3f8added7003120582953d4f3f43991fb3bb2798 (diff)
downloadrust-905d23b65c07a1da4452f9a20b1891fc46533fb7.tar.gz
rust-905d23b65c07a1da4452f9a20b1891fc46533fb7.zip
Move `std::sys_common::alloc` to `std::sys::common`
Diffstat (limited to 'library/std/src/sys/common/alloc.rs')
-rw-r--r--library/std/src/sys/common/alloc.rs48
1 files changed, 48 insertions, 0 deletions
diff --git a/library/std/src/sys/common/alloc.rs b/library/std/src/sys/common/alloc.rs
new file mode 100644
index 00000000000..6c1bc0d839a
--- /dev/null
+++ b/library/std/src/sys/common/alloc.rs
@@ -0,0 +1,48 @@
+#![allow(dead_code)]
+
+use crate::alloc::{GlobalAlloc, Layout, System};
+use crate::cmp;
+use crate::ptr;
+
+// The minimum alignment guaranteed by the architecture. This value is used to
+// add fast paths for low alignment values.
+#[cfg(all(any(
+    target_arch = "x86",
+    target_arch = "arm",
+    target_arch = "mips",
+    target_arch = "powerpc",
+    target_arch = "powerpc64",
+    target_arch = "sparc",
+    target_arch = "asmjs",
+    target_arch = "wasm32",
+    target_arch = "hexagon",
+    target_arch = "riscv32"
+)))]
+pub const MIN_ALIGN: usize = 8;
+#[cfg(all(any(
+    target_arch = "x86_64",
+    target_arch = "aarch64",
+    target_arch = "mips64",
+    target_arch = "s390x",
+    target_arch = "sparc64",
+    target_arch = "riscv64"
+)))]
+pub const MIN_ALIGN: usize = 16;
+
+pub unsafe fn realloc_fallback(
+    alloc: &System,
+    ptr: *mut u8,
+    old_layout: Layout,
+    new_size: usize,
+) -> *mut u8 {
+    // Docs for GlobalAlloc::realloc require this to be valid:
+    let new_layout = Layout::from_size_align_unchecked(new_size, old_layout.align());
+
+    let new_ptr = GlobalAlloc::alloc(alloc, new_layout);
+    if !new_ptr.is_null() {
+        let size = cmp::min(old_layout.size(), new_size);
+        ptr::copy_nonoverlapping(ptr, new_ptr, size);
+        GlobalAlloc::dealloc(alloc, ptr, old_layout);
+    }
+    new_ptr
+}