From d7541f7b55dc30cd0cbb68f9066b2fff5f60e754 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 29 Jul 2021 00:09:24 -0600 Subject: [PATCH] [util/hb-shape] Free cached resources on termination --- util/options.cc | 33 +++++++++++++++------------------ util/options.hh | 18 ++++++++++++++---- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/util/options.cc b/util/options.cc index f5817ae79..bec6bed9a 100644 --- a/util/options.cc +++ b/util/options.cc @@ -681,10 +681,7 @@ output_options_t::add_options (option_parser_t *parser) } -const char *font_options_t::cached_font_path = nullptr; -hb_blob_t *font_options_t::cached_blob = nullptr; -unsigned font_options_t::cached_face_index = (unsigned) -1; -hb_face_t *font_options_t::cached_face = nullptr; +font_options_t::cache_t font_options_t::cache {}; hb_font_t * font_options_t::get_font () const @@ -708,8 +705,8 @@ font_options_t::get_font () const #endif } - if (cached_font_path && 0 == strcmp (cached_font_path, font_path)) - blob = hb_blob_reference (cached_blob); + if (cache.font_path && 0 == strcmp (cache.font_path, font_path)) + blob = hb_blob_reference (cache.blob); else { blob = hb_blob_create_from_file_or_fail (font_path); @@ -719,27 +716,27 @@ font_options_t::get_font () const /* Update caches. */ - hb_face_destroy (cached_face); - cached_face = nullptr; - cached_face_index = (unsigned) -1; + hb_face_destroy (cache.face); + cache.face = nullptr; + cache.face_index = (unsigned) -1; - free ((char *) cached_font_path); - cached_font_path = strdup (font_path); - hb_blob_destroy (cached_blob); - cached_blob = hb_blob_reference (blob); + free ((char *) cache.font_path); + cache.font_path = strdup (font_path); + hb_blob_destroy (cache.blob); + cache.blob = hb_blob_reference (blob); } hb_face_t *face = nullptr; - if (cached_face_index == face_index) - face = hb_face_reference (cached_face); + if (cache.face_index == face_index) + face = hb_face_reference (cache.face); else { face = hb_face_create (blob, face_index); hb_blob_destroy (blob); - cached_face_index = face_index; - hb_face_destroy (cached_face); - cached_face = hb_face_reference (face); + cache.face_index = face_index; + hb_face_destroy (cache.face); + cache.face = hb_face_reference (face); } diff --git a/util/options.hh b/util/options.hh index 416a6f2a2..4e2303d78 100644 --- a/util/options.hh +++ b/util/options.hh @@ -507,10 +507,20 @@ struct font_options_t : option_group_t private: mutable hb_font_t *font; - static const char *cached_font_path; - static hb_blob_t *cached_blob; - static unsigned cached_face_index; - static hb_face_t *cached_face; + static struct cache_t + { + ~cache_t () + { + free ((void *) font_path); + hb_blob_destroy (blob); + hb_face_destroy (face); + } + + const char *font_path = nullptr; + hb_blob_t *blob = nullptr; + unsigned face_index = (unsigned) -1; + hb_face_t *face = nullptr; + } cache; };