From b41cc6f84b9aae4b208f86e0631332245485d7a0 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Sun, 15 Apr 2018 19:17:00 +0200 Subject: [PATCH] Fork the jemallocator crate, fix for nightly-2018-04-15 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CC https://github.com/alexcrichton/jemallocator/pull/40, https://github.com/rust-lang/rust/pull/49669 The new version of jemallocator requires a more recent jemalloc https://github.com/alexcrichton/jemallocator/pull/34 which doesn’t build on our current Android toolchain https://github.com/jemalloc/jemalloc/issues/1175. To avoid blocking on figuring that out, duplicate ~70 lines from jemallocator and use the older jemalloc-sys directly. --- Cargo.lock | 12 +---- components/allocator/Cargo.toml | 4 +- components/allocator/lib.rs | 84 ++++++++++++++++++++++++++++++--- rust-toolchain | 2 +- 4 files changed, 81 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4174c3634721d..0c73f24e5396b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1358,15 +1358,6 @@ dependencies = [ "libc 0.2.39 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "jemallocator" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "jemalloc-sys 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.39 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "jpeg-decoder" version = "0.1.14" @@ -2817,7 +2808,7 @@ dependencies = [ name = "servo_allocator" version = "0.0.1" dependencies = [ - "jemallocator 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", + "jemalloc-sys 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.39 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -3906,7 +3897,6 @@ dependencies = [ "checksum itertools 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)" = "b07332223953b5051bceb67e8c4700aa65291535568e1f12408c43c4a42c0394" "checksum itoa 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "eb2f404fbc66fd9aac13e998248505e7ecb2ad8e44ab6388684c5fb11c6c251c" "checksum jemalloc-sys 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "479294d130502fada93c7a957e8d059b632b03d6204aca37af557dee947f30a9" -"checksum jemallocator 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "28b211ca65c440322b6d4d9b5b850b01e8e298393b7ebcb8205b7cbb14ea6329" "checksum jpeg-decoder 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "0dfe27a6c0dabd772d0f9b9f8701c4ca12c4d1eebcadf2be1f6f70396f6a1434" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" "checksum khronos_api 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d9ef23fcc4059260c5936f638c9805ebfc87cb172fa6661d130cba7f97d58f55" diff --git a/components/allocator/Cargo.toml b/components/allocator/Cargo.toml index 645815ba1f13f..c3c27357cbfd3 100644 --- a/components/allocator/Cargo.toml +++ b/components/allocator/Cargo.toml @@ -9,13 +9,13 @@ publish = false path = "lib.rs" [features] -unstable = ["kernel32-sys", "jemallocator"] +unstable = ["kernel32-sys", "jemalloc-sys"] [dependencies] libc = "0.2" # Only used when 'unstable' is disabled, but looks like Cargo cannot express that. [target.'cfg(not(windows))'.dependencies] -jemallocator = { version = "0.1.4", optional = true } +jemalloc-sys = { version = "0.1.4", optional = true } [target.'cfg(windows)'.dependencies] kernel32-sys = { version = "0.2.1", optional = true } diff --git a/components/allocator/lib.rs b/components/allocator/lib.rs index 6c248f08055da..3d933599688a2 100644 --- a/components/allocator/lib.rs +++ b/components/allocator/lib.rs @@ -4,8 +4,7 @@ //! Selecting the default global allocator for Servo -#![cfg_attr(all(feature = "unstable", windows), feature(alloc_system, allocator_api))] -#![cfg_attr(feature = "unstable", feature(global_allocator))] +#![cfg_attr(feature = "unstable", feature(global_allocator, allocator_api, alloc_system))] #[cfg(feature = "unstable")] #[global_allocator] @@ -16,19 +15,90 @@ pub use platform::*; #[cfg(all(feature = "unstable", not(windows)))] mod platform { - extern crate jemallocator; + extern crate jemalloc_sys as ffi; - pub use self::jemallocator::Jemalloc as Allocator; - use std::os::raw::c_void; + use std::alloc::{GlobalAlloc, Layout, Opaque, System}; + use std::os::raw::{c_int, c_void}; /// Get the size of a heap block. pub unsafe extern "C" fn usable_size(ptr: *const c_void) -> usize { - jemallocator::usable_size(ptr) + ffi::malloc_usable_size(ptr as *const _) } /// Memory allocation APIs compatible with libc pub mod libc_compat { - pub use super::jemallocator::ffi::{malloc, realloc, free}; + pub use super::ffi::{malloc, realloc, free}; + } + + pub struct Allocator; + + // The minimum alignment guaranteed by the architecture. This value is used to + // add fast paths for low alignment values. + #[cfg(all(any(target_arch = "arm", + target_arch = "mips", + target_arch = "mipsel", + target_arch = "powerpc")))] + const MIN_ALIGN: usize = 8; + #[cfg(all(any(target_arch = "x86", + target_arch = "x86_64", + target_arch = "aarch64", + target_arch = "powerpc64", + target_arch = "powerpc64le", + target_arch = "mips64", + target_arch = "s390x", + target_arch = "sparc64")))] + const MIN_ALIGN: usize = 16; + + fn layout_to_flags(align: usize, size: usize) -> c_int { + // If our alignment is less than the minimum alignment they we may not + // have to pass special flags asking for a higher alignment. If the + // alignment is greater than the size, however, then this hits a sort of odd + // case where we still need to ask for a custom alignment. See #25 for more + // info. + if align <= MIN_ALIGN && align <= size { + 0 + } else { + // Equivalent to the MALLOCX_ALIGN(a) macro. + align.trailing_zeros() as _ + } + } + + unsafe impl GlobalAlloc for Allocator { + #[inline] + unsafe fn alloc(&self, layout: Layout) -> *mut Opaque { + let flags = layout_to_flags(layout.align(), layout.size()); + ffi::mallocx(layout.size(), flags) as *mut Opaque + } + + #[inline] + unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut Opaque { + if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() { + ffi::calloc(1, layout.size()) as *mut Opaque + } else { + let flags = layout_to_flags(layout.align(), layout.size()) | ffi::MALLOCX_ZERO; + ffi::mallocx(layout.size(), flags) as *mut Opaque + } + } + + #[inline] + unsafe fn dealloc(&self, ptr: *mut Opaque, layout: Layout) { + let flags = layout_to_flags(layout.align(), layout.size()); + ffi::sdallocx(ptr as *mut _, layout.size(), flags) + } + + #[inline] + unsafe fn realloc(&self, + ptr: *mut Opaque, + layout: Layout, + new_size: usize) -> *mut Opaque { + let flags = layout_to_flags(layout.align(), new_size); + ffi::rallocx(ptr as *mut _, new_size, flags) as *mut Opaque + } + + #[inline] + fn oom(&self) -> ! { + System.oom() + } } } diff --git a/rust-toolchain b/rust-toolchain index 7b4c87520cbb5..84ce4f35a64f4 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1 +1 @@ -nightly-2018-04-08 +nightly-2018-04-15