[util/hb-shape] Free cached resources on termination

This commit is contained in:
Behdad Esfahbod 2021-07-29 00:09:24 -06:00
parent ad03f34df7
commit d7541f7b55
2 changed files with 29 additions and 22 deletions

View File

@ -681,10 +681,7 @@ output_options_t::add_options (option_parser_t *parser)
} }
const char *font_options_t::cached_font_path = nullptr; font_options_t::cache_t font_options_t::cache {};
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;
hb_font_t * hb_font_t *
font_options_t::get_font () const font_options_t::get_font () const
@ -708,8 +705,8 @@ font_options_t::get_font () const
#endif #endif
} }
if (cached_font_path && 0 == strcmp (cached_font_path, font_path)) if (cache.font_path && 0 == strcmp (cache.font_path, font_path))
blob = hb_blob_reference (cached_blob); blob = hb_blob_reference (cache.blob);
else else
{ {
blob = hb_blob_create_from_file_or_fail (font_path); blob = hb_blob_create_from_file_or_fail (font_path);
@ -719,27 +716,27 @@ font_options_t::get_font () const
/* Update caches. */ /* Update caches. */
hb_face_destroy (cached_face); hb_face_destroy (cache.face);
cached_face = nullptr; cache.face = nullptr;
cached_face_index = (unsigned) -1; cache.face_index = (unsigned) -1;
free ((char *) cached_font_path); free ((char *) cache.font_path);
cached_font_path = strdup (font_path); cache.font_path = strdup (font_path);
hb_blob_destroy (cached_blob); hb_blob_destroy (cache.blob);
cached_blob = hb_blob_reference (blob); cache.blob = hb_blob_reference (blob);
} }
hb_face_t *face = nullptr; hb_face_t *face = nullptr;
if (cached_face_index == face_index) if (cache.face_index == face_index)
face = hb_face_reference (cached_face); face = hb_face_reference (cache.face);
else else
{ {
face = hb_face_create (blob, face_index); face = hb_face_create (blob, face_index);
hb_blob_destroy (blob); hb_blob_destroy (blob);
cached_face_index = face_index; cache.face_index = face_index;
hb_face_destroy (cached_face); hb_face_destroy (cache.face);
cached_face = hb_face_reference (face); cache.face = hb_face_reference (face);
} }

View File

@ -507,10 +507,20 @@ struct font_options_t : option_group_t
private: private:
mutable hb_font_t *font; mutable hb_font_t *font;
static const char *cached_font_path; static struct cache_t
static hb_blob_t *cached_blob; {
static unsigned cached_face_index; ~cache_t ()
static hb_face_t *cached_face; {
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;
}; };