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

remove je_ link to libc #44

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
semver = "0.2"

[features]
unstable = []
18 changes: 18 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
extern crate semver;

use std::env::var;
use std::process::Command;
use semver::Version;

fn main() {
let unprefixed_jemalloc_version: Version = "1.8.0-dev".parse().unwrap();
let rustc_version: Version = Command::new(var("RUSTC").unwrap_or("rustc".into()))
.arg("--version").output().ok()
.and_then(|o| String::from_utf8(o.stdout).ok())
.and_then(|s| s.split_whitespace().skip(1).next().map(|r| r.to_string())).unwrap()
.parse().unwrap();

if rustc_version < unprefixed_jemalloc_version {
println!("cargo:rustc-cfg=prefixed_jemalloc");
}
}
10 changes: 9 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ use std::rc::Rc;

#[cfg(not(target_os = "windows"))]
extern {
#[cfg(not(prefixed_jemalloc))]
#[cfg_attr(any(target_os = "macos", target_os = "android"), link_name = "je_mallocx")]
// Get the size of a heap block.
//
// Ideally Rust would expose a function like this in std::rt::heap, which would avoid the
Expand All @@ -29,9 +31,15 @@ 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 malloc_usable_size(ptr: *const c_void) -> usize;

#[cfg(prefixed_jemalloc)]
fn je_malloc_usable_size(ptr: *const c_void) -> usize;
}

#[cfg(prefixed_jemalloc)]
use je_malloc_usable_size as malloc_usable_size;

/// A wrapper for je_malloc_usable_size that handles `EMPTY` and returns `usize`.
///
/// `unsafe` because the caller must ensure that the pointer is from jemalloc.
Expand All @@ -42,7 +50,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)
}
}

Expand Down