diff options
author | Simon Sapin <simon.sapin@exyr.org> | 2017-10-30 15:00:41 +0100 |
---|---|---|
committer | Simon Sapin <simon.sapin@exyr.org> | 2017-10-30 18:22:30 +0100 |
commit | 6319ad01240bfd7abb41bc3b99d94407fa820e01 (patch) | |
tree | 31094dc07a220a4808c69fc7811771078a61937f | |
parent | 1b73cf33525afbbe2d077554d1965b74ef9ae5e3 (diff) | |
download | servo-6319ad01240bfd7abb41bc3b99d94407fa820e01.tar.gz servo-6319ad01240bfd7abb41bc3b99d94407fa820e01.zip |
FreeType: don’t use usable_size() as deallocation size
Instead use C-level malloc()/free() so that the size doesn’t need
to be known during deallocation, since FreeType doesn’t provide it.
Hopefully fixes https://github.com/servo/servo/issues/19058
-rw-r--r-- | Cargo.lock | 15 | ||||
-rw-r--r-- | components/allocator/Cargo.toml | 5 | ||||
-rw-r--r-- | components/allocator/lib.rs | 17 | ||||
-rw-r--r-- | components/gfx/platform/freetype/font_context.rs | 50 |
4 files changed, 37 insertions, 50 deletions
diff --git a/Cargo.lock b/Cargo.lock index 2962a40f119..2e3b0c19fcb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1447,7 +1447,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "jemalloc-sys" -version = "0.1.3" +version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "cc 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1456,10 +1456,10 @@ dependencies = [ [[package]] name = "jemallocator" -version = "0.1.3" +version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "jemalloc-sys 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "jemalloc-sys 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.23 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2391,7 +2391,7 @@ dependencies = [ "heartbeats-simple 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "influent 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "ipc-channel 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", - "jemalloc-sys 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "jemalloc-sys 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.23 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "profile_traits 0.0.1", @@ -2956,8 +2956,9 @@ dependencies = [ name = "servo_allocator" version = "0.0.1" dependencies = [ - "jemallocator 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "jemallocator 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.23 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -3912,8 +3913,8 @@ dependencies = [ "checksum ipc-channel 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c10ed089b1921b01ef342c736a37ee0788eeb9a5f373bb2df1ba88d01125064f" "checksum itertools 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4833d6978da405305126af4ac88569b5d71ff758581ce5a987dbfa3755f694fc" "checksum itoa 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "eb2f404fbc66fd9aac13e998248505e7ecb2ad8e44ab6388684c5fb11c6c251c" -"checksum jemalloc-sys 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "94fb624d7e8345e5c42caab8d1db6ec925fdadff3fd0cb7dd781b41be8442828" -"checksum jemallocator 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1850725977c344d63af66e8fd00857646e3ec936c490cd63667860b7b03ab5c1" +"checksum jemalloc-sys 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "479294d130502fada93c7a957e8d059b632b03d6204aca37af557dee947f30a9" +"checksum jemallocator 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "28b211ca65c440322b6d4d9b5b850b01e8e298393b7ebcb8205b7cbb14ea6329" "checksum jpeg-decoder 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "2805ccb10ffe4d10e06ef68a158ff94c255211ecbae848fbde2146b098f93ce7" "checksum js 0.1.6 (git+https://github.com/servo/rust-mozjs)" = "<none>" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" diff --git a/components/allocator/Cargo.toml b/components/allocator/Cargo.toml index fccec48a5e9..645815ba1f1 100644 --- a/components/allocator/Cargo.toml +++ b/components/allocator/Cargo.toml @@ -11,8 +11,11 @@ path = "lib.rs" [features] unstable = ["kernel32-sys", "jemallocator"] +[dependencies] +libc = "0.2" # Only used when 'unstable' is disabled, but looks like Cargo cannot express that. + [target.'cfg(not(windows))'.dependencies] -jemallocator = { version = "0.1.3", optional = true } +jemallocator = { version = "0.1.4", optional = true } [target.'cfg(windows)'.dependencies] kernel32-sys = { version = "0.2.1", optional = true } diff --git a/components/allocator/lib.rs b/components/allocator/lib.rs index e832e485575..6c248f08055 100644 --- a/components/allocator/lib.rs +++ b/components/allocator/lib.rs @@ -9,9 +9,9 @@ #[cfg(feature = "unstable")] #[global_allocator] -static ALLOC: platform::Allocator = platform::Allocator; +static ALLOC: Allocator = Allocator; -pub use platform::usable_size; +pub use platform::*; #[cfg(all(feature = "unstable", not(windows)))] @@ -25,6 +25,11 @@ mod platform { pub unsafe extern "C" fn usable_size(ptr: *const c_void) -> usize { jemallocator::usable_size(ptr) } + + /// Memory allocation APIs compatible with libc + pub mod libc_compat { + pub use super::jemallocator::ffi::{malloc, realloc, free}; + } } #[cfg(all(feature = "unstable", windows))] @@ -57,6 +62,10 @@ mod platform { pub unsafe extern "C" fn usable_size(_ptr: *const c_void) -> usize { 0 } -} - + /// Memory allocation APIs compatible with libc + pub mod libc_compat { + extern crate libc; + pub use self::libc::{malloc, realloc, free}; + } +} diff --git a/components/gfx/platform/freetype/font_context.rs b/components/gfx/platform/freetype/font_context.rs index f4a0745eb2c..2238561ba9c 100644 --- a/components/gfx/platform/freetype/font_context.rs +++ b/components/gfx/platform/freetype/font_context.rs @@ -9,8 +9,8 @@ use freetype::freetype::FT_Memory; use freetype::freetype::FT_MemoryRec_; use freetype::freetype::FT_New_Library; use malloc_size_of::{MallocSizeOf, MallocSizeOfOps}; +use servo_allocator::libc_compat::{malloc, realloc, free}; use servo_allocator::usable_size; -use std::mem; use std::os::raw::{c_long, c_void}; use std::ptr; use std::rc::Rc; @@ -22,64 +22,38 @@ pub struct User { size: usize, } -// FreeType doesn't require any particular alignment for allocations. -const FT_ALIGNMENT: usize = 1; - extern fn ft_alloc(mem: FT_Memory, req_size: c_long) -> *mut c_void { - assert!(FT_ALIGNMENT == 1); - let mut vec = Vec::<u8>::with_capacity(req_size as usize); - let ptr = vec.as_mut_ptr() as *mut c_void; - mem::forget(vec); - unsafe { - let actual_size = usable_size(ptr as *const _); + let ptr = malloc(req_size as usize); + let ptr = ptr as *mut c_void; // libc::c_void vs std::os::raw::c_void + let actual_size = usable_size(ptr); let user = (*mem).user as *mut User; (*user).size += actual_size; + ptr } - - ptr } extern fn ft_free(mem: FT_Memory, ptr: *mut c_void) { unsafe { - let actual_size = usable_size(ptr as *const _); + let actual_size = usable_size(ptr); let user = (*mem).user as *mut User; (*user).size -= actual_size; - - assert!(FT_ALIGNMENT == 1); - mem::drop(Vec::<u8>::from_raw_parts(ptr as *mut u8, actual_size, 0)) + free(ptr as *mut _); } } 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 = usable_size(old_ptr as *const _); - let old_size = old_actual_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; - if new_req_size > old_actual_size { - vec.reserve_exact(new_req_size - old_actual_size) - } else if new_req_size < old_actual_size { - vec.truncate(new_req_size); - vec.shrink_to_fit() - } - - let new_ptr = vec.as_mut_ptr() as *mut c_void; - mem::forget(vec); - unsafe { - let new_actual_size = usable_size(new_ptr as *const _); + let old_actual_size = usable_size(old_ptr); + let new_ptr = realloc(old_ptr as *mut _, new_req_size as usize); + let new_ptr = new_ptr as *mut c_void; + let new_actual_size = usable_size(new_ptr); let user = (*mem).user as *mut User; (*user).size += new_actual_size; (*user).size -= old_actual_size; + new_ptr } - - new_ptr } // A |*mut User| field in a struct triggers a "use of `#[derive]` with a raw pointer" warning from |