Skip to content

Commit 4b59c82

Browse files
committed
Auto merge of #45514 - gnzlbg:jemalloc_realloc2, r=<try>
[jemalloc] set correct excess in realloc_excess
2 parents f764eaf + f39594d commit 4b59c82

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

src/liballoc_jemalloc/lib.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ mod contents {
6767
target_os = "dragonfly", target_os = "windows", target_env = "musl"),
6868
link_name = "je_nallocx")]
6969
fn nallocx(size: size_t, flags: c_int) -> size_t;
70+
#[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios",
71+
target_os = "dragonfly", target_os = "windows", target_env = "musl"),
72+
link_name = "je_sallocx")]
73+
fn sallocx(ptr: *mut c_void, flags: c_int) -> size_t;
7074
}
7175

7276
const MALLOCX_ZERO: c_int = 0x40;
@@ -211,17 +215,29 @@ mod contents {
211215
#[no_mangle]
212216
#[linkage = "external"]
213217
pub unsafe extern fn __rde_realloc_excess(ptr: *mut u8,
214-
old_size: usize,
218+
_old_size: usize,
215219
old_align: usize,
216220
new_size: usize,
217221
new_align: usize,
218222
excess: *mut usize,
219223
err: *mut u8) -> *mut u8 {
220-
let p = __rde_realloc(ptr, old_size, old_align, new_size, new_align, err);
221-
if !p.is_null() {
222-
*excess = new_size;
224+
if new_align != old_align {
225+
ptr::write(err as *mut AllocErr,
226+
AllocErr::Unsupported { details: "can't change alignments" });
227+
return 0 as *mut u8
223228
}
224-
return p
229+
230+
let flags = align_to_flags(new_align);
231+
let ptr = rallocx(ptr as *mut c_void, new_size, flags) as *mut u8;
232+
if ptr.is_null() {
233+
let layout = Layout::from_size_align_unchecked(new_size, new_align);
234+
ptr::write(err as *mut AllocErr,
235+
AllocErr::Exhausted { request: layout });
236+
} else {
237+
let alloc_size = sallocx(ptr as *mut c_void, flags);
238+
*excess = alloc_size;
239+
}
240+
ptr
225241
}
226242

227243
#[no_mangle]

0 commit comments

Comments
 (0)