diff --git a/Cargo.toml b/Cargo.toml index b26c81a..f28c35a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ edition = "2018" libc = { version = "0.2", default-features = false } libc-print = "0.1.23" errno = { version = "*", default-features = false } +static-alloc = "0.2.5" [profile.dev] # This isn't required for development builds, but makes development diff --git a/Dockerfile b/Dockerfile index 2c19b7d..c4b5058 100644 --- a/Dockerfile +++ b/Dockerfile @@ -25,15 +25,11 @@ RUN set -x && \ objcopy -R .eh_frame -R .got.plt target/release/init-wrapper target/release/init-wrapper && \ ls -alh target/release/init-wrapper && \ readelf -W -S ./target/release/init-wrapper -# && \ -# ./target/release/init-wrapper - # ldd target/release/init-wrapper && \ - # && \ - # ldd target/release/init-wrapper - RUN --mount=type=bind,rw,source=.,target=/host \ - cp -avf target/release/init-wrapper /host/init-wrapper && \ - ./target/release/init-wrapper +RUN --mount=type=bind,rw,source=.,target=/host \ + cp -avf target/release/init-wrapper /host/init-wrapper +# cp -avf target/release/init-wrapper /host/init-wrapper && \ +# ./target/release/init-wrapper FROM scratch diff --git a/src/mem.rs b/src/mem.rs index 757c167..4dde7e2 100644 --- a/src/mem.rs +++ b/src/mem.rs @@ -1,96 +1,3 @@ -use core::alloc::{GlobalAlloc, Layout}; -use core::{cmp, mem, ptr}; -use libc::{c_void, calloc, free, malloc, posix_memalign, realloc}; - -const MIN_ALIGN: usize = 16; - -/// The global allocator type. -#[derive(Default)] -pub struct Allocator; - -impl Allocator {} -unsafe impl GlobalAlloc for Allocator { - #[inline] - unsafe fn alloc(&self, layout: Layout) -> *mut u8 { - // jemalloc provides alignment less than MIN_ALIGN for small allocations. - // So only rely on MIN_ALIGN if size >= align. - // Also see and - // . - if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() { - malloc(layout.size()) as *mut u8 - } else { - #[cfg(target_os = "macos")] - { - if layout.align() > (1 << 31) { - return ptr::null_mut(); - } - } - aligned_malloc(&layout) - } - } - - #[inline] - unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { - // See the comment above in `alloc` for why this check looks the way it does. - if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() { - calloc(layout.size(), 1) as *mut u8 - } else { - let ptr = self.alloc(layout); - if !ptr.is_null() { - ptr::write_bytes(ptr, 0, layout.size()); - } - ptr - } - } - - #[inline] - unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) { - free(ptr as *mut c_void) - } - - #[inline] - unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 { - if layout.align() <= MIN_ALIGN && layout.align() <= new_size { - realloc(ptr as *mut c_void, new_size) as *mut u8 - } else { - realloc_fallback(self, ptr, layout, new_size) - } - } -} - -/// The static global allocator. +use static_alloc::Bump; #[global_allocator] -static GLOBAL_ALLOCATOR: Allocator = Allocator; - -#[inline] -unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 { - let mut out = ptr::null_mut(); - // posix_memalign requires that the alignment be a multiple of `sizeof(void*)`. - // Since these are all powers of 2, we can just use max. - let align = layout.align().max(mem::size_of::()); - let ret = posix_memalign(&mut out, align, layout.size()); - if ret != 0 { - ptr::null_mut() - } else { - out as *mut u8 - } -} - -#[inline] -unsafe fn realloc_fallback( - allocator: &Allocator, - 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 = allocator.alloc(new_layout); - if !new_ptr.is_null() { - let size = cmp::min(old_layout.size(), new_size); - ptr::copy_nonoverlapping(ptr, new_ptr, size); - allocator.dealloc(ptr, old_layout); - } - new_ptr -} +static GLOBAL_ALLOCATOR: Bump<[u8; 1 << 16]> = Bump::uninit();