From 04aed572f112b96a6033cd6c3df7bdba5e29e93c Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 5 Jun 2012 18:30:19 -0400 Subject: [PATCH] Make hb-ft static-initializer free --- src/hb-common.cc | 5 ++--- src/hb-ft.cc | 46 +++++++++++++++++++++++++++++++--------------- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/src/hb-common.cc b/src/hb-common.cc index 6972aa5bf..331d255b4 100644 --- a/src/hb-common.cc +++ b/src/hb-common.cc @@ -182,7 +182,6 @@ void free_langs (void) static hb_language_item_t * lang_find_or_insert (const char *key) { - retry: hb_language_item_t *first_lang = (hb_language_item_t *) hb_atomic_ptr_get (&langs); @@ -203,8 +202,8 @@ retry: } #ifdef HAVE_ATEXIT - if (!first_lang) /* First person registers atexit() callback. */ - atexit (free_langs); + if (!first_lang) + atexit (free_langs); /* First person registers atexit() callback. */ #endif return lang; diff --git a/src/hb-ft.cc b/src/hb-ft.cc index aa09bd70a..ce68ca751 100644 --- a/src/hb-ft.cc +++ b/src/hb-ft.cc @@ -398,21 +398,37 @@ hb_ft_font_create (FT_Face ft_face, } -static FT_Library -_get_ft_library (void) -{ - static struct ft_library_singleton - { - ft_library_singleton (void) { - FT_Init_FreeType (&ft_library); - } - ~ft_library_singleton (void) { - FT_Done_FreeType (ft_library); - } - FT_Library ft_library; - } ft_library_singleton; +static FT_Library ft_library; - return ft_library_singleton.ft_library; +static +void free_ft_library (void) +{ + FT_Done_FreeType (ft_library); +} + +static FT_Library +get_ft_library (void) +{ +retry: + FT_Library library = (FT_Library) hb_atomic_ptr_get (&ft_library); + + if (unlikely (!library)) + { + /* Not found; allocate one. */ + if (FT_Init_FreeType (&library)) + return NULL; + + if (!hb_atomic_ptr_cmpexch (&ft_library, NULL, library)) { + FT_Done_FreeType (library); + goto retry; + } + +#ifdef HAVE_ATEXIT + atexit (free_ft_library); /* First person registers atexit() callback. */ +#endif + } + + return library; } static void @@ -431,7 +447,7 @@ hb_ft_font_set_funcs (hb_font_t *font) DEBUG_MSG (FT, font, "Font face has empty blob"); FT_Face ft_face = NULL; - FT_Error err = FT_New_Memory_Face (_get_ft_library (), + FT_Error err = FT_New_Memory_Face (get_ft_library (), (const FT_Byte *) blob_data, blob_length, hb_face_get_index (font->face),