Skip to content

Commit ac36ad8

Browse files
committed
servo: Merge #19061 - FreeType: don’t use usable_size() as deallocation size (from servo:ft-alloc); r=mbrubeck
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 servo/servo#19058 Depends on https://github.com/alexcrichton/jemallocator/pull/21 Source-Repo: https://github.com/servo/servo Source-Revision: f18099118a5be17b5b1d6fdcc3352a98a1499e6a --HG-- extra : subtree_source : https%3A//hg.mozilla.org/projects/converted-servo-linear extra : subtree_revision : 8087ee658a0ca822c6cdc85c00cfc8984b1fa668
1 parent 879ded9 commit ac36ad8

File tree

4 files changed

+37
-50
lines changed

4 files changed

+37
-50
lines changed

servo/Cargo.lock

Lines changed: 8 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

servo/components/allocator/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ path = "lib.rs"
1111
[features]
1212
unstable = ["kernel32-sys", "jemallocator"]
1313

14+
[dependencies]
15+
libc = "0.2" # Only used when 'unstable' is disabled, but looks like Cargo cannot express that.
16+
1417
[target.'cfg(not(windows))'.dependencies]
15-
jemallocator = { version = "0.1.3", optional = true }
18+
jemallocator = { version = "0.1.4", optional = true }
1619

1720
[target.'cfg(windows)'.dependencies]
1821
kernel32-sys = { version = "0.2.1", optional = true }

servo/components/allocator/lib.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
#[cfg(feature = "unstable")]
1111
#[global_allocator]
12-
static ALLOC: platform::Allocator = platform::Allocator;
12+
static ALLOC: Allocator = Allocator;
1313

14-
pub use platform::usable_size;
14+
pub use platform::*;
1515

1616

1717
#[cfg(all(feature = "unstable", not(windows)))]
@@ -25,6 +25,11 @@ mod platform {
2525
pub unsafe extern "C" fn usable_size(ptr: *const c_void) -> usize {
2626
jemallocator::usable_size(ptr)
2727
}
28+
29+
/// Memory allocation APIs compatible with libc
30+
pub mod libc_compat {
31+
pub use super::jemallocator::ffi::{malloc, realloc, free};
32+
}
2833
}
2934

3035
#[cfg(all(feature = "unstable", windows))]
@@ -57,6 +62,10 @@ mod platform {
5762
pub unsafe extern "C" fn usable_size(_ptr: *const c_void) -> usize {
5863
0
5964
}
60-
}
61-
6265

66+
/// Memory allocation APIs compatible with libc
67+
pub mod libc_compat {
68+
extern crate libc;
69+
pub use self::libc::{malloc, realloc, free};
70+
}
71+
}

servo/components/gfx/platform/freetype/font_context.rs

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use freetype::freetype::FT_Memory;
99
use freetype::freetype::FT_MemoryRec_;
1010
use freetype::freetype::FT_New_Library;
1111
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
12+
use servo_allocator::libc_compat::{malloc, realloc, free};
1213
use servo_allocator::usable_size;
13-
use std::mem;
1414
use std::os::raw::{c_long, c_void};
1515
use std::ptr;
1616
use std::rc::Rc;
@@ -22,64 +22,38 @@ pub struct User {
2222
size: usize,
2323
}
2424

25-
// FreeType doesn't require any particular alignment for allocations.
26-
const FT_ALIGNMENT: usize = 1;
27-
2825
extern fn ft_alloc(mem: FT_Memory, req_size: c_long) -> *mut c_void {
29-
assert!(FT_ALIGNMENT == 1);
30-
let mut vec = Vec::<u8>::with_capacity(req_size as usize);
31-
let ptr = vec.as_mut_ptr() as *mut c_void;
32-
mem::forget(vec);
33-
3426
unsafe {
35-
let actual_size = usable_size(ptr as *const _);
27+
let ptr = malloc(req_size as usize);
28+
let ptr = ptr as *mut c_void; // libc::c_void vs std::os::raw::c_void
29+
let actual_size = usable_size(ptr);
3630
let user = (*mem).user as *mut User;
3731
(*user).size += actual_size;
32+
ptr
3833
}
39-
40-
ptr
4134
}
4235

4336
extern fn ft_free(mem: FT_Memory, ptr: *mut c_void) {
4437
unsafe {
45-
let actual_size = usable_size(ptr as *const _);
38+
let actual_size = usable_size(ptr);
4639
let user = (*mem).user as *mut User;
4740
(*user).size -= actual_size;
48-
49-
assert!(FT_ALIGNMENT == 1);
50-
mem::drop(Vec::<u8>::from_raw_parts(ptr as *mut u8, actual_size, 0))
41+
free(ptr as *mut _);
5142
}
5243
}
5344

5445
extern fn ft_realloc(mem: FT_Memory, _old_size: c_long, new_req_size: c_long,
5546
old_ptr: *mut c_void) -> *mut c_void {
56-
let old_actual_size;
57-
let mut vec;
58-
unsafe {
59-
old_actual_size = usable_size(old_ptr as *const _);
60-
let old_size = old_actual_size as usize;
61-
vec = Vec::<u8>::from_raw_parts(old_ptr as *mut u8, old_size, old_size);
62-
};
63-
64-
let new_req_size = new_req_size as usize;
65-
if new_req_size > old_actual_size {
66-
vec.reserve_exact(new_req_size - old_actual_size)
67-
} else if new_req_size < old_actual_size {
68-
vec.truncate(new_req_size);
69-
vec.shrink_to_fit()
70-
}
71-
72-
let new_ptr = vec.as_mut_ptr() as *mut c_void;
73-
mem::forget(vec);
74-
7547
unsafe {
76-
let new_actual_size = usable_size(new_ptr as *const _);
48+
let old_actual_size = usable_size(old_ptr);
49+
let new_ptr = realloc(old_ptr as *mut _, new_req_size as usize);
50+
let new_ptr = new_ptr as *mut c_void;
51+
let new_actual_size = usable_size(new_ptr);
7752
let user = (*mem).user as *mut User;
7853
(*user).size += new_actual_size;
7954
(*user).size -= old_actual_size;
55+
new_ptr
8056
}
81-
82-
new_ptr
8357
}
8458

8559
// A |*mut User| field in a struct triggers a "use of `#[derive]` with a raw pointer" warning from

0 commit comments

Comments
 (0)