Skip to content
This repository was archived by the owner on Jul 10, 2023. It is now read-only.

Commit bf7f697

Browse files
quininerSimonSapin
authored andcommitted
parse rustc version
1 parent 13617ed commit bf7f697

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

Cargo.toml

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ authors = [ "The Servo Project Developers" ]
55
description = "Infrastructure for measuring the total runtime size of an object on the heap"
66
license = "MPL-2.0"
77
repository = "https://github.com/servo/heapsize"
8+
build = "build.rs"
9+
10+
[build-dependencies]
11+
semver = "0.2"
12+
chrono = "0.2"
813

914
[features]
1015
unstable = []
11-
nightly = []

build.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
extern crate semver;
2+
extern crate chrono;
3+
4+
use std::env::var;
5+
use std::process::Command;
6+
use semver::Version;
7+
use chrono::NaiveDate;
8+
9+
fn main() {
10+
let unprefixed_jemalloc_version: Version = "1.8.0-dev".parse().unwrap();
11+
let unprefixed_jemalloc_day: NaiveDate = "2016-02-16".parse().unwrap();
12+
13+
let rustc_version_string = Command::new(var("RUSTC").unwrap_or("rustc".into()))
14+
.arg("--version").output().ok()
15+
.and_then(|o| String::from_utf8(o.stdout).ok())
16+
.unwrap();
17+
let rustc_version: Version = rustc_version_string.split_whitespace()
18+
.skip(1).next()
19+
.and_then(|r| r.parse().ok())
20+
.unwrap();
21+
let rustc_day: NaiveDate = rustc_version_string.split_whitespace()
22+
.last()
23+
.and_then(|r| r[..r.len()-1].parse().ok())
24+
.unwrap();
25+
26+
if rustc_day < unprefixed_jemalloc_day
27+
|| rustc_version < unprefixed_jemalloc_version
28+
{
29+
println!("cargo:rustc-cfg=prefixed_jemalloc");
30+
}
31+
}

src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use std::rc::Rc;
2020

2121
#[cfg(not(target_os = "windows"))]
2222
extern {
23-
#![cfg_attr(feature = "nightly", link(name = "malloc_usable_size"))]
23+
#[cfg_attr(any(prefixed_jemalloc, target_os = "macos", target_os = "android"), link_name = "je_malloc_usable_size")]
2424
// Get the size of a heap block.
2525
//
2626
// Ideally Rust would expose a function like this in std::rt::heap, which would avoid the
@@ -30,7 +30,7 @@ extern {
3030
// platforms `JEMALLOC_USABLE_SIZE_CONST` is `const` and on some it is empty. But in practice
3131
// this function doesn't modify the contents of the block that `ptr` points to, so we use
3232
// `*const c_void` here.
33-
fn je_malloc_usable_size(ptr: *const c_void) -> usize;
33+
fn malloc_usable_size(ptr: *const c_void) -> usize;
3434
}
3535

3636
/// A wrapper for je_malloc_usable_size that handles `EMPTY` and returns `usize`.
@@ -43,7 +43,7 @@ pub unsafe fn heap_size_of(ptr: *const c_void) -> usize {
4343
if ptr == 0x01 as *const c_void {
4444
0
4545
} else {
46-
je_malloc_usable_size(ptr)
46+
malloc_usable_size(ptr)
4747
}
4848
}
4949

0 commit comments

Comments
 (0)