From a47530475448561a862cfadbd8f937d43e222bb7 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Thu, 13 Sep 2012 19:13:01 -0700 Subject: [PATCH] Fonts should keep their libraries alive --- src/servo/text/font.rs | 5 ++++- src/servo/text/font_library.rs | 21 +++++++++++++++------ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/servo/text/font.rs b/src/servo/text/font.rs index 72dd946bf8b..e570c2f509e 100644 --- a/src/servo/text/font.rs +++ b/src/servo/text/font.rs @@ -12,6 +12,8 @@ A font handle. Layout can use this to calculate glyph metrics and the renderer can use it to render text. "] struct Font { + // A back reference to keep the library alive + lib: @FontLibrary, fontbuf: @~[u8], native_font: NativeFont, } @@ -33,8 +35,9 @@ impl Font { } } -fn Font(fontbuf: @~[u8], +native_font: NativeFont) -> Font { +fn Font(lib: @FontLibrary, fontbuf: @~[u8], +native_font: NativeFont) -> Font { Font { + lib: lib, fontbuf : fontbuf, native_font : move native_font, } diff --git a/src/servo/text/font_library.rs b/src/servo/text/font_library.rs index 066c97a674c..0073d657af2 100644 --- a/src/servo/text/font_library.rs +++ b/src/servo/text/font_library.rs @@ -3,6 +3,10 @@ export FontLibrary, native; use font::{Font, test_font_bin}; struct FontLibrary { + // FIXME: This is a hack to hold onto a boxed reference to + // the self pointer until explicit self types work on methods. + // This is a huge space leak. + mut at_self: Option<@FontLibrary>, native_lib: native::NativeFontLibrary, drop { @@ -12,7 +16,8 @@ struct FontLibrary { impl FontLibrary { fn get_font() -> @Font { - match create_font(&self.native_lib) { + assert self.at_self.is_some(); + match create_font(self.at_self.get(), &self.native_lib) { Ok(font) => font, Err(*) => /* FIXME */ fail } @@ -23,13 +28,17 @@ impl FontLibrary { } } -fn FontLibrary() -> FontLibrary { - FontLibrary { +fn FontLibrary() -> @FontLibrary { + let lib = @FontLibrary { + mut at_self: None, native_lib: native::create_native_lib() - } + }; + + lib.at_self = Some(lib); + return lib; } -fn create_font(native_lib: &native::NativeFontLibrary) -> Result<@Font, ()> { +fn create_font(lib: @FontLibrary, native_lib: &native::NativeFontLibrary) -> Result<@Font, ()> { let font_bin = @test_font_bin(); let native_font = native_font::create(native_lib, font_bin); let native_font = if native_font.is_ok() { @@ -37,7 +46,7 @@ fn create_font(native_lib: &native::NativeFontLibrary) -> Result<@Font, ()> { } else { return Err(native_font.get_err()); }; - return Ok(@Font(font_bin, native_font)); + return Ok(@Font(lib, font_bin, native_font)); } #[cfg(target_os = "linux")]