Skip to content

Improve liballoc_jemalloc {re}alloc_excess #45482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 33 additions & 9 deletions src/liballoc_jemalloc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ mod contents {
target_os = "dragonfly", target_os = "windows", target_env = "musl"),
link_name = "je_nallocx")]
fn nallocx(size: size_t, flags: c_int) -> size_t;
#[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios",
target_os = "dragonfly", target_os = "windows", target_env = "musl"),
link_name = "je_sallocx")]
fn sallocx(size: size_t, flags: c_int) -> size_t;
}

const MALLOCX_ZERO: c_int = 0x40;
Expand Down Expand Up @@ -201,27 +205,47 @@ mod contents {
align: usize,
excess: *mut usize,
err: *mut u8) -> *mut u8 {
let p = __rde_alloc(size, align, err);
if !p.is_null() {
*excess = size;
let flags = align_to_flags(align);
let ptr = mallocx(size as size_t, flags) as *mut u8;
if ptr.is_null() {
let layout = Layout::from_size_align_unchecked(size, align);
ptr::write(err as *mut AllocErr,
AllocErr::Exhausted { request: layout });
} else {
*excess = sallocx(size as size_t, flags) as usize;
}
return p
ptr
}

#[no_mangle]
#[linkage = "external"]
pub unsafe extern fn __rde_realloc_excess(ptr: *mut u8,
old_size: usize,
_old_size: usize,
old_align: usize,
new_size: usize,
new_align: usize,
excess: *mut usize,
err: *mut u8) -> *mut u8 {
let p = __rde_realloc(ptr, old_size, old_align, new_size, new_align, err);
if !p.is_null() {
*excess = new_size;
if new_align != old_align {
ptr::write(
err as *mut AllocErr,
AllocErr::Unsupported { details: "can't change alignments" },
);
return 0 as *mut u8
}
return p

let flags = align_to_flags(new_align);
let ptr = rallocx(ptr as *mut c_void, new_size as size_t, flags) as *mut u8;
if ptr.is_null() {
let layout = Layout::from_size_align_unchecked(new_size, new_align);
ptr::write(
err as *mut AllocErr,
AllocErr::Exhausted { request: layout },
);
} else {
*excess = sallocx(new_size as size_t, flags) as usize;
}
ptr
}

#[no_mangle]
Expand Down