From d16c140b7cd014f03064f6f66f35baaa0b9cb7ce Mon Sep 17 00:00:00 2001 From: gnzlbg Date: Wed, 25 Oct 2017 10:22:24 +0200 Subject: [PATCH 1/5] [jemalloc] set correct excess in realloc_excess --- src/liballoc_jemalloc/lib.rs | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/liballoc_jemalloc/lib.rs b/src/liballoc_jemalloc/lib.rs index d153f19c462..a1401e65f38 100644 --- a/src/liballoc_jemalloc/lib.rs +++ b/src/liballoc_jemalloc/lib.rs @@ -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(ptr: *mut c_void, flags: c_int) -> size_t; } const MALLOCX_ZERO: c_int = 0x40; @@ -211,17 +215,28 @@ mod contents { #[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, flags) as usize; + let alloc_size = sallocx(ptr as *mut c_void, flags); + 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 }); + } + *excess = alloc_size; + ptr } #[no_mangle] From e1a71e7b8a15026b1a8067135fccf3d18be0203d Mon Sep 17 00:00:00 2001 From: gnzlbg Date: Wed, 25 Oct 2017 14:28:38 +0200 Subject: [PATCH 2/5] [jemalloc] set correct excess in realloc_excess --- src/liballoc_jemalloc/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/liballoc_jemalloc/lib.rs b/src/liballoc_jemalloc/lib.rs index a1401e65f38..c10af35a94b 100644 --- a/src/liballoc_jemalloc/lib.rs +++ b/src/liballoc_jemalloc/lib.rs @@ -228,7 +228,7 @@ mod contents { } let flags = align_to_flags(new_align); - let ptr = rallocx(ptr as *mut c_void, new_size, flags) as usize; + let ptr = rallocx(ptr as *mut c_void, new_size, flags) as *mut u8; let alloc_size = sallocx(ptr as *mut c_void, flags); if ptr.is_null() { let layout = Layout::from_size_align_unchecked(new_size, new_align); From f39594d4bb719b9af7fcb56b7e4cd5a8868a8c50 Mon Sep 17 00:00:00 2001 From: gnzlbg Date: Wed, 25 Oct 2017 14:46:02 +0200 Subject: [PATCH 3/5] move sallocx and excess into not null branch --- src/liballoc_jemalloc/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/liballoc_jemalloc/lib.rs b/src/liballoc_jemalloc/lib.rs index c10af35a94b..09666bd0e62 100644 --- a/src/liballoc_jemalloc/lib.rs +++ b/src/liballoc_jemalloc/lib.rs @@ -229,13 +229,14 @@ mod contents { let flags = align_to_flags(new_align); let ptr = rallocx(ptr as *mut c_void, new_size, flags) as *mut u8; - let alloc_size = sallocx(ptr as *mut c_void, flags); 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 { + let alloc_size = sallocx(ptr as *mut c_void, flags); + *excess = alloc_size; } - *excess = alloc_size; ptr } From 45ef01269936c6b0c02fc5c212a5cea632884b69 Mon Sep 17 00:00:00 2001 From: gnzlbg Date: Wed, 25 Oct 2017 20:22:08 +0200 Subject: [PATCH 4/5] use nallocx instead of sallocx --- src/liballoc_jemalloc/lib.rs | 27 ++++++--------------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/src/liballoc_jemalloc/lib.rs b/src/liballoc_jemalloc/lib.rs index 09666bd0e62..6c787198d36 100644 --- a/src/liballoc_jemalloc/lib.rs +++ b/src/liballoc_jemalloc/lib.rs @@ -67,10 +67,6 @@ 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(ptr: *mut c_void, flags: c_int) -> size_t; } const MALLOCX_ZERO: c_int = 0x40; @@ -215,29 +211,18 @@ mod contents { #[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 { - if new_align != old_align { - ptr::write(err as *mut AllocErr, - AllocErr::Unsupported { details: "can't change alignments" }); - return 0 as *mut u8 + let p = __rde_realloc(ptr, old_size, old_align, new_size, new_align, err); + if !p.is_null() { + let flags = align_to_flags(new_align); + *excess = nallocx(new_size, flags) as usize; } - - let flags = align_to_flags(new_align); - let ptr = rallocx(ptr as *mut c_void, new_size, 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 { - let alloc_size = sallocx(ptr as *mut c_void, flags); - *excess = alloc_size; - } - ptr + p } #[no_mangle] From 549ab77e2375f07f3c425eb4c95ba31547288176 Mon Sep 17 00:00:00 2001 From: gnzlbg Date: Fri, 3 Nov 2017 17:44:58 +0100 Subject: [PATCH 5/5] [jemalloc] set correct excess in alloc_excess --- src/liballoc_jemalloc/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/liballoc_jemalloc/lib.rs b/src/liballoc_jemalloc/lib.rs index 6c787198d36..bc2ed1f9c45 100644 --- a/src/liballoc_jemalloc/lib.rs +++ b/src/liballoc_jemalloc/lib.rs @@ -203,7 +203,8 @@ mod contents { err: *mut u8) -> *mut u8 { let p = __rde_alloc(size, align, err); if !p.is_null() { - *excess = size; + let flags = align_to_flags(align); + *excess = nallocx(size, flags) as usize; } return p }