Stop relying on linking details of std’s default allocator

We’ve been bitten before by symbol names changing:
https://github.com/servo/heapsize/pull/46
and upstream is planning to stop using jemalloc by default:
https://github.com/rust-lang/rust/issues/33082#issuecomment-309781465

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.
This commit is contained in:
Simon Sapin 2017-10-18 13:38:07 +02:00
parent 4c538b642e
commit 959ce482dd
20 changed files with 158 additions and 62 deletions

View file

@ -53,6 +53,7 @@ core-text = "7.0"
[target.'cfg(any(target_os = "linux", target_os = "android"))'.dependencies]
freetype = "0.3"
servo_allocator = {path = "../allocator"}
[target.'cfg(target_os = "linux")'.dependencies]
servo-fontconfig = "0.2.1"

View file

@ -27,8 +27,8 @@ extern crate fnv;
#[cfg(target_os = "linux")]
extern crate fontconfig;
extern crate fontsan;
#[cfg(any(target_os = "linux", target_os = "android"))]
extern crate freetype;
#[cfg(any(target_os = "linux", target_os = "android"))] extern crate freetype;
#[cfg(any(target_os = "linux", target_os = "android"))] extern crate servo_allocator;
extern crate gfx_traits;
// Eventually we would like the shaper to be pluggable, as many operating systems have their own

View file

@ -8,7 +8,8 @@ use freetype::freetype::FT_Library;
use freetype::freetype::FT_Memory;
use freetype::freetype::FT_MemoryRec_;
use freetype::freetype::FT_New_Library;
use malloc_size_of::{malloc_size_of, MallocSizeOf, MallocSizeOfOps};
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
use servo_allocator::usable_size;
use std::mem;
use std::os::raw::{c_long, c_void};
use std::ptr;
@ -31,7 +32,7 @@ extern fn ft_alloc(mem: FT_Memory, req_size: c_long) -> *mut c_void {
mem::forget(vec);
unsafe {
let actual_size = malloc_size_of(ptr as *const _);
let actual_size = usable_size(ptr as *const _);
let user = (*mem).user as *mut User;
(*user).size += actual_size;
}
@ -41,7 +42,7 @@ extern fn ft_alloc(mem: FT_Memory, req_size: c_long) -> *mut c_void {
extern fn ft_free(mem: FT_Memory, ptr: *mut c_void) {
unsafe {
let actual_size = malloc_size_of(ptr as *const _);
let actual_size = usable_size(ptr as *const _);
let user = (*mem).user as *mut User;
(*user).size -= actual_size;
@ -50,13 +51,14 @@ extern fn ft_free(mem: FT_Memory, ptr: *mut c_void) {
}
}
extern fn ft_realloc(mem: FT_Memory, _cur_size: c_long, new_req_size: c_long,
extern fn ft_realloc(mem: FT_Memory, old_size: c_long, new_req_size: c_long,
old_ptr: *mut c_void) -> *mut c_void {
let old_actual_size;
let mut vec;
unsafe {
old_actual_size = malloc_size_of(old_ptr as *const _);
vec = Vec::<u8>::from_raw_parts(old_ptr as *mut u8, old_actual_size, old_actual_size);
old_actual_size = usable_size(old_ptr as *const _);
let old_size = old_size as usize;
vec = Vec::<u8>::from_raw_parts(old_ptr as *mut u8, old_size, old_size);
};
let new_req_size = new_req_size as usize;
@ -71,7 +73,7 @@ extern fn ft_realloc(mem: FT_Memory, _cur_size: c_long, new_req_size: c_long,
mem::forget(vec);
unsafe {
let new_actual_size = malloc_size_of(new_ptr as *const _);
let new_actual_size = usable_size(new_ptr as *const _);
let user = (*mem).user as *mut User;
(*user).size += new_actual_size;
(*user).size -= old_actual_size;