mirror of
https://github.com/servo/servo.git
synced 2025-08-03 12:40:06 +01:00
Reduce unsafe-ness in FontContextHandle::new().
Because `box + boxed::into_raw() + boxed::from_raw` is nicer than `libc::malloc() + mem::transmute() + ptr::write() + libc::free()`.
This commit is contained in:
parent
16ba32521b
commit
e8fc406520
1 changed files with 10 additions and 14 deletions
|
@ -13,9 +13,7 @@ use freetype::freetype::struct_FT_MemoryRec_;
|
|||
use std::ptr;
|
||||
use std::rc::Rc;
|
||||
|
||||
use libc;
|
||||
use libc::{c_void, c_long, size_t, malloc};
|
||||
use std::mem;
|
||||
use libc::{self, c_void, c_long, size_t};
|
||||
|
||||
extern fn ft_alloc(_mem: FT_Memory, size: c_long) -> *mut c_void {
|
||||
unsafe {
|
||||
|
@ -48,7 +46,7 @@ impl Drop for FreeTypeLibraryHandle {
|
|||
assert!(!self.ctx.is_null());
|
||||
unsafe {
|
||||
FT_Done_Library(self.ctx);
|
||||
libc::free(self.mem as *mut c_void);
|
||||
Box::from_raw(self.mem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -60,19 +58,17 @@ pub struct FontContextHandle {
|
|||
|
||||
impl FontContextHandle {
|
||||
pub fn new() -> FontContextHandle {
|
||||
unsafe {
|
||||
let ptr = libc::malloc(mem::size_of::<struct_FT_MemoryRec_>() as size_t);
|
||||
let mem: &mut struct_FT_MemoryRec_ = mem::transmute(ptr);
|
||||
ptr::write(mem, struct_FT_MemoryRec_ {
|
||||
let mem = box struct_FT_MemoryRec_ {
|
||||
user: ptr::null_mut(),
|
||||
alloc: ft_alloc,
|
||||
free: ft_free,
|
||||
realloc: ft_realloc,
|
||||
});
|
||||
|
||||
};
|
||||
unsafe {
|
||||
let mut ctx: FT_Library = ptr::null_mut();
|
||||
|
||||
let result = FT_New_Library(ptr as FT_Memory, &mut ctx);
|
||||
let mem = ::std::boxed::into_raw(mem);
|
||||
let result = FT_New_Library(mem, &mut ctx);
|
||||
if !result.succeeded() { panic!("Unable to initialize FreeType library"); }
|
||||
|
||||
FT_Add_Default_Modules(ctx);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue