diff --git a/src/hb-open-type-private.hh b/src/hb-open-type-private.hh index 2cc1fb20d..a8756d466 100644 --- a/src/hb-open-type-private.hh +++ b/src/hb-open-type-private.hh @@ -1061,6 +1061,53 @@ struct SortedArrayOf : ArrayOf }; +/* Lazy struct and blob loaders. */ + +template +struct hb_lazy_loader_t +{ + inline void init (hb_face_t *face_) + { + face = face_; + instance = NULL; + } + + inline void fini (void) + { + if (instance && instance != &OT::Null(T)) + { + instance->fini(); + free (instance); + } + } + + inline const T* operator-> (void) const + { + retry: + T *p = (T *) hb_atomic_ptr_get (&instance); + if (unlikely (!p)) + { + p = (T *) calloc (1, sizeof (T)); + if (unlikely (!p)) + p = const_cast (&OT::Null(T)); + else + p->init (face); + if (unlikely (!hb_atomic_ptr_cmpexch (const_cast(&instance), NULL, p))) + { + if (p != &OT::Null(T)) + p->fini (); + goto retry; + } + } + return p; + } + + private: + hb_face_t *face; + T *instance; +}; + + } /* namespace OT */ diff --git a/src/hb-ot-font.cc b/src/hb-ot-font.cc index c2431364d..c4ec612c8 100644 --- a/src/hb-ot-font.cc +++ b/src/hb-ot-font.cc @@ -421,57 +421,13 @@ struct hb_ot_face_cmap_accelerator_t } }; -template -struct hb_lazy_loader_t -{ - inline void init (hb_face_t *face_) - { - face = face_; - instance = NULL; - } - - inline void fini (void) - { - if (instance && instance != &OT::Null(T)) - { - instance->fini(); - free (instance); - } - } - - inline const T* operator-> (void) const - { - retry: - T *p = (T *) hb_atomic_ptr_get (&instance); - if (unlikely (!p)) - { - p = (T *) calloc (1, sizeof (T)); - if (unlikely (!p)) - p = const_cast (&OT::Null(T)); - else - p->init (face); - if (unlikely (!hb_atomic_ptr_cmpexch (const_cast(&instance), NULL, p))) - { - if (p != &OT::Null(T)) - p->fini (); - goto retry; - } - } - return p; - } - - private: - hb_face_t *face; - T *instance; -}; - struct hb_ot_font_t { hb_ot_face_cmap_accelerator_t cmap; hb_ot_face_metrics_accelerator_t h_metrics; hb_ot_face_metrics_accelerator_t v_metrics; - hb_lazy_loader_t glyf; - hb_lazy_loader_t cbdt; + OT::hb_lazy_loader_t glyf; + OT::hb_lazy_loader_t cbdt; };