Skip to content
This repository has been archived by the owner on Jan 18, 2024. It is now read-only.

Commit

Permalink
Stop relying on linking details of std’s default allocator
Browse files Browse the repository at this point in the history
We’ve been bitten before by symbol names changing:
servo/heapsize#46
and upstream is planning to stop using jemalloc by default:
rust-lang/rust#33082 (comment)

So use the (relatively) new `#[global_allocator]` attribute
to explicitly select the system allocator on Windows
and jemalloc (now in an external crate) on other platforms.
This choice matches current defaults.
  • Loading branch information
SimonSapin committed Oct 19, 2017
1 parent a17e57d commit c7fc98e
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 34 deletions.
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ publish = false
[lib]
path = "lib.rs"

[target.'cfg(windows)'.dependencies]
kernel32-sys = "0.2.1"

[features]
servo = ["js", "string_cache", "url", "webrender_api", "xml5ever"]

Expand Down
31 changes: 0 additions & 31 deletions lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ extern crate euclid;
extern crate hashglobe;
#[cfg(feature = "servo")]
extern crate js;
#[cfg(target_os = "windows")]
extern crate kernel32;
extern crate servo_arc;
extern crate smallbitvec;
extern crate smallvec;
Expand All @@ -63,8 +61,6 @@ extern crate webrender_api;
#[cfg(feature = "servo")]
extern crate xml5ever;

#[cfg(target_os = "windows")]
use kernel32::{GetProcessHeap, HeapSize, HeapValidate};
use std::hash::{BuildHasher, Hash};
use std::mem::size_of;
use std::ops::Range;
Expand Down Expand Up @@ -146,33 +142,6 @@ impl MallocSizeOfOps {
}
}

/// Get the size of a heap block.
#[cfg(not(target_os = "windows"))]
pub unsafe extern "C" fn malloc_size_of(ptr: *const c_void) -> usize {
// The C prototype is `je_malloc_usable_size(JEMALLOC_USABLE_SIZE_CONST void *ptr)`. On some
// 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.
extern "C" {
#[cfg_attr(any(prefixed_jemalloc, target_os = "macos", target_os = "ios", target_os = "android"),
link_name = "je_malloc_usable_size")]
fn malloc_usable_size(ptr: *const c_void) -> usize;
}
malloc_usable_size(ptr)
}

/// Get the size of a heap block.
#[cfg(target_os = "windows")]
pub unsafe extern "C" fn malloc_size_of(mut ptr: *const c_void) -> usize {
let heap = GetProcessHeap();

if HeapValidate(heap, 0, ptr) == 0 {
ptr = *(ptr as *const *const c_void).offset(-1);
}

HeapSize(heap, 0, ptr) as usize
}

/// Trait for measuring the "deep" heap usage of a data structure. This is the
/// most commonly-used of the traits.
pub trait MallocSizeOf {
Expand Down

0 comments on commit c7fc98e

Please sign in to comment.