diff --git a/.travis.yml b/.travis.yml index fe7cfb8..6bbf80d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,18 @@ language: rust -rust: nightly +rust: + # Jemalloc prefixing change between these two versions + - nightly-2016-02-14 + - nightly-2016-02-17 + + - nightly + - beta + - stable + notifications: webhooks: http://build.servo.org:54856/travis script: - cargo test - - "[ $TRAVIS_RUST_VERSION = nightly ] || cargo test --features unstable" - - "[ $TRAVIS_RUST_VERSION = nightly ] || cargo test --manifest-path plugin/Cargo.toml" + - "[ $TRAVIS_RUST_VERSION != nightly ] || cargo test --features unstable" + - "[ $TRAVIS_RUST_VERSION != nightly ] || cargo test --manifest-path plugin/Cargo.toml" diff --git a/Cargo.toml b/Cargo.toml index 5e7b909..70391d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,10 @@ authors = [ "The Servo Project Developers" ] description = "Infrastructure for measuring the total runtime size of an object on the heap" license = "MPL-2.0" repository = "https://github.com/servo/heapsize" +build = "build.rs" + +[build-dependencies] +regex = "0.1" [features] unstable = [] diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..5874b5a --- /dev/null +++ b/build.rs @@ -0,0 +1,26 @@ +extern crate regex; + +use std::env::var; +use std::process::Command; +use std::str; + +fn main() { + let version_line = Command::new(var("RUSTC").unwrap_or("rustc".into())) + .arg("--version") + .output() + .unwrap() + .stdout; + let captures = regex::Regex::new(r"rustc (\d+)\.(\d+)\.(\d+).+(\d{4}-\d{2}-\d{2})\)") + .unwrap() + .captures(str::from_utf8(&version_line).unwrap()) + .unwrap(); + let version = ( + captures[1].parse::().unwrap(), + captures[2].parse::().unwrap(), + captures[3].parse::().unwrap(), + &captures[4], + ); + if version < (1, 8, 0, "2016-02-14") { + println!("cargo:rustc-cfg=prefixed_jemalloc"); + } +} diff --git a/src/lib.rs b/src/lib.rs index e92aea7..aef991e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,6 +20,7 @@ use std::rc::Rc; #[cfg(not(target_os = "windows"))] extern { + #[cfg_attr(any(prefixed_jemalloc, target_os = "macos", target_os = "android"), link_name = "je_malloc_usable_size")] // Get the size of a heap block. // // Ideally Rust would expose a function like this in std::rt::heap, which would avoid the @@ -29,7 +30,7 @@ extern { // platforms `JEMALLOC_USABLE_SIZE_CONST` is `const` and on some it is empty. But in practice // this function doesn't modify the contents of the block that `ptr` points to, so we use // `*const c_void` here. - fn je_malloc_usable_size(ptr: *const c_void) -> usize; + fn malloc_usable_size(ptr: *const c_void) -> usize; } /// A wrapper for je_malloc_usable_size that handles `EMPTY` and returns `usize`. @@ -42,7 +43,7 @@ pub unsafe fn heap_size_of(ptr: *const c_void) -> usize { if ptr == 0x01 as *const c_void { 0 } else { - je_malloc_usable_size(ptr) + malloc_usable_size(ptr) } }