Specialize Vec::from_elem<u8> to use calloc or memset

Fixes #38723.
This commit is contained in:
Matt Brubeck
2017-03-09 17:53:01 -08:00
parent 5637ed7566
commit 675475c4d3
7 changed files with 144 additions and 7 deletions

View File

@@ -38,6 +38,10 @@ mod imp {
target_os = "dragonfly", target_os = "windows", target_env = "musl"),
link_name = "je_mallocx")]
fn mallocx(size: size_t, flags: c_int) -> *mut c_void;
#[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios",
target_os = "dragonfly", target_os = "windows", target_env = "musl"),
link_name = "je_calloc")]
fn calloc(size: size_t, flags: c_int) -> *mut c_void;
#[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios",
target_os = "dragonfly", target_os = "windows", target_env = "musl"),
link_name = "je_rallocx")]
@@ -56,6 +60,8 @@ mod imp {
fn nallocx(size: size_t, flags: c_int) -> size_t;
}
const MALLOCX_ZERO: c_int = 0x40;
// The minimum alignment guaranteed by the architecture. This value is used to
// add fast paths for low alignment values. In practice, the alignment is a
// constant at the call site and the branch will be optimized out.
@@ -91,6 +97,16 @@ mod imp {
unsafe { mallocx(size as size_t, flags) as *mut u8 }
}
#[no_mangle]
pub extern "C" fn __rust_allocate_zeroed(size: usize, align: usize) -> *mut u8 {
if align <= MIN_ALIGN {
unsafe { calloc(size as size_t, 1) as *mut u8 }
} else {
let flags = align_to_flags(align) | MALLOCX_ZERO;
unsafe { mallocx(size as size_t, flags) as *mut u8 }
}
}
#[no_mangle]
pub extern "C" fn __rust_reallocate(ptr: *mut u8,
_old_size: usize,
@@ -135,6 +151,11 @@ mod imp {
bogus()
}
#[no_mangle]
pub extern "C" fn __rust_allocate_zeroed(_size: usize, _align: usize) -> *mut u8 {
bogus()
}
#[no_mangle]
pub extern "C" fn __rust_reallocate(_ptr: *mut u8,
_old_size: usize,