From 15b0ed4296a87419f5d692ddbb407fdd949612b9 Mon Sep 17 00:00:00 2001 From: Glenn Watson Date: Tue, 24 Jun 2014 10:46:18 +1000 Subject: [PATCH] Linux freetype context uses custom allocator callbacks to enable memory profiling. --- .../gfx/platform/linux/font_context.rs | 49 +++++++++++++++++-- src/platform/linux/rust-freetype | 2 +- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/components/gfx/platform/linux/font_context.rs b/src/components/gfx/platform/linux/font_context.rs index cea1cbf5cb0..520ac5111b6 100644 --- a/src/components/gfx/platform/linux/font_context.rs +++ b/src/components/gfx/platform/linux/font_context.rs @@ -7,12 +7,41 @@ use platform::font::FontHandle; use font_context::FontContextHandleMethods; use platform::font_list::path_from_identifier; -use freetype::freetype::{FTErrorMethods, FT_Library}; -use freetype::freetype::{FT_Done_FreeType, FT_Init_FreeType}; +use freetype::freetype::FTErrorMethods; +use freetype::freetype::FT_Add_Default_Modules; +use freetype::freetype::FT_Done_FreeType; +use freetype::freetype::FT_Library; +use freetype::freetype::FT_Memory; +use freetype::freetype::FT_New_Library; +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; + +extern fn ft_alloc(_mem: FT_Memory, size: c_long) -> *c_void { + unsafe { + let ptr = libc::malloc(size as size_t); + ptr as *c_void + } +} + +extern fn ft_free(_mem: FT_Memory, block: *c_void) { + unsafe { + libc::free(block as *mut c_void); + } +} + +extern fn ft_realloc(_mem: FT_Memory, _cur_size: c_long, new_size: c_long, block: *c_void) -> *c_void { + unsafe { + let ptr = libc::realloc(block as *mut c_void, new_size as size_t); + ptr as *c_void + } +} + #[deriving(Clone)] pub struct FreeTypeLibraryHandle { pub ctx: FT_Library, @@ -33,9 +62,23 @@ impl Drop for FreeTypeLibraryHandle { impl FontContextHandle { pub fn new() -> FontContextHandle { unsafe { + + let ptr = libc::malloc(mem::size_of::() as size_t); + let allocator: &mut struct_FT_MemoryRec_ = mem::transmute(ptr); + mem::overwrite(allocator, struct_FT_MemoryRec_ { + user: ptr::null(), + alloc: ft_alloc, + free: ft_free, + realloc: ft_realloc, + }); + let ctx: FT_Library = ptr::null(); - let result = FT_Init_FreeType(&ctx); + + let result = FT_New_Library(ptr as FT_Memory, &ctx); if !result.succeeded() { fail!("Unable to initialize FreeType library"); } + + FT_Add_Default_Modules(ctx); + FontContextHandle { ctx: Rc::new(FreeTypeLibraryHandle { ctx: ctx }), } diff --git a/src/platform/linux/rust-freetype b/src/platform/linux/rust-freetype index cc667034983..4fbb9c38fc0 160000 --- a/src/platform/linux/rust-freetype +++ b/src/platform/linux/rust-freetype @@ -1 +1 @@ -Subproject commit cc667034983c88bc8e1b5be7e1091bff23888734 +Subproject commit 4fbb9c38fc06d3116f44f4da36dd378d73f71b58