할당자 교체
This commit is contained in:
parent
2e653268c8
commit
99c1df2a99
|
@ -9,6 +9,7 @@ edition = "2018"
|
||||||
libc = { version = "0.2", default-features = false }
|
libc = { version = "0.2", default-features = false }
|
||||||
libc-print = "0.1.23"
|
libc-print = "0.1.23"
|
||||||
errno = { version = "*", default-features = false }
|
errno = { version = "*", default-features = false }
|
||||||
|
static-alloc = "0.2.5"
|
||||||
|
|
||||||
[profile.dev]
|
[profile.dev]
|
||||||
# This isn't required for development builds, but makes development
|
# This isn't required for development builds, but makes development
|
||||||
|
|
12
Dockerfile
12
Dockerfile
|
@ -25,15 +25,11 @@ RUN set -x && \
|
||||||
objcopy -R .eh_frame -R .got.plt target/release/init-wrapper target/release/init-wrapper && \
|
objcopy -R .eh_frame -R .got.plt target/release/init-wrapper target/release/init-wrapper && \
|
||||||
ls -alh target/release/init-wrapper && \
|
ls -alh target/release/init-wrapper && \
|
||||||
readelf -W -S ./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 \
|
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
|
# cp -avf target/release/init-wrapper /host/init-wrapper && \
|
||||||
|
# ./target/release/init-wrapper
|
||||||
|
|
||||||
|
|
||||||
FROM scratch
|
FROM scratch
|
||||||
|
|
97
src/mem.rs
97
src/mem.rs
|
@ -1,96 +1,3 @@
|
||||||
use core::alloc::{GlobalAlloc, Layout};
|
use static_alloc::Bump;
|
||||||
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 <https://github.com/rust-lang/rust/issues/45955> and
|
|
||||||
// <https://github.com/rust-lang/rust/issues/62251#issuecomment-507580914>.
|
|
||||||
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.
|
|
||||||
#[global_allocator]
|
#[global_allocator]
|
||||||
static GLOBAL_ALLOCATOR: Allocator = Allocator;
|
static GLOBAL_ALLOCATOR: Bump<[u8; 1 << 16]> = Bump::uninit();
|
||||||
|
|
||||||
#[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::<usize>());
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue