2 * Copyright © 2007,2008,2009,2010 Red Hat, Inc.
3 * Copyright © 2012,2018 Google, Inc.
5 * This is part of HarfBuzz, a text shaping library.
7 * Permission is hereby granted, without written agreement and without
8 * license or royalty fees, to use, copy, modify, and distribute this
9 * software and its documentation for any purpose, provided that the
10 * above copyright notice and the following two paragraphs appear in
11 * all copies of this software.
13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25 * Red Hat Author(s): Behdad Esfahbod
26 * Google Author(s): Behdad Esfahbod
29 #ifndef HB_MACHINERY_HH
30 #define HB_MACHINERY_HH
35 #include "hb-array.hh"
36 #include "hb-vector.hh"
43 /* Cast to struct T, reference to reference */
44 template<typename Type, typename TObject>
45 static inline const Type& CastR(const TObject &X)
46 { return reinterpret_cast<const Type&> (X); }
47 template<typename Type, typename TObject>
48 static inline Type& CastR(TObject &X)
49 { return reinterpret_cast<Type&> (X); }
51 /* Cast to struct T, pointer to pointer */
52 template<typename Type, typename TObject>
53 static inline const Type* CastP(const TObject *X)
54 { return reinterpret_cast<const Type*> (X); }
55 template<typename Type, typename TObject>
56 static inline Type* CastP(TObject *X)
57 { return reinterpret_cast<Type*> (X); }
59 /* StructAtOffset<T>(P,Ofs) returns the struct T& that is placed at memory
60 * location pointed to by P plus Ofs bytes. */
61 template<typename Type>
62 static inline const Type& StructAtOffset(const void *P, unsigned int offset)
63 { return * reinterpret_cast<const Type*> ((const char *) P + offset); }
64 template<typename Type>
65 static inline Type& StructAtOffset(void *P, unsigned int offset)
66 { return * reinterpret_cast<Type*> ((char *) P + offset); }
67 template<typename Type>
68 static inline const Type& StructAtOffsetUnaligned(const void *P, unsigned int offset)
70 #pragma GCC diagnostic push
71 #pragma GCC diagnostic ignored "-Wcast-align"
72 return * reinterpret_cast<Type*> ((char *) P + offset);
73 #pragma GCC diagnostic pop
75 template<typename Type>
76 static inline Type& StructAtOffsetUnaligned(void *P, unsigned int offset)
78 #pragma GCC diagnostic push
79 #pragma GCC diagnostic ignored "-Wcast-align"
80 return * reinterpret_cast<Type*> ((char *) P + offset);
81 #pragma GCC diagnostic pop
84 /* StructAfter<T>(X) returns the struct T& that is placed after X.
85 * Works with X of variable size also. X must implement get_size() */
86 template<typename Type, typename TObject>
87 static inline const Type& StructAfter(const TObject &X)
88 { return StructAtOffset<Type>(&X, X.get_size()); }
89 template<typename Type, typename TObject>
90 static inline Type& StructAfter(TObject &X)
91 { return StructAtOffset<Type>(&X, X.get_size()); }
98 /* Check _assertion in a method environment */
99 #define _DEFINE_INSTANCE_ASSERTION1(_line, _assertion) \
100 void _instance_assertion_on_line_##_line () const \
101 { static_assert ((_assertion), ""); }
102 # define _DEFINE_INSTANCE_ASSERTION0(_line, _assertion) _DEFINE_INSTANCE_ASSERTION1 (_line, _assertion)
103 # define DEFINE_INSTANCE_ASSERTION(_assertion) _DEFINE_INSTANCE_ASSERTION0 (__LINE__, _assertion)
105 /* Check that _code compiles in a method environment */
106 #define _DEFINE_COMPILES_ASSERTION1(_line, _code) \
107 void _compiles_assertion_on_line_##_line () const \
109 # define _DEFINE_COMPILES_ASSERTION0(_line, _code) _DEFINE_COMPILES_ASSERTION1 (_line, _code)
110 # define DEFINE_COMPILES_ASSERTION(_code) _DEFINE_COMPILES_ASSERTION0 (__LINE__, _code)
113 #define DEFINE_SIZE_STATIC(size) \
114 DEFINE_INSTANCE_ASSERTION (sizeof (*this) == (size)) \
115 unsigned int get_size () const { return (size); } \
116 static constexpr unsigned null_size = (size); \
117 static constexpr unsigned min_size = (size); \
118 static constexpr unsigned static_size = (size)
120 #define DEFINE_SIZE_UNION(size, _member) \
121 DEFINE_COMPILES_ASSERTION ((void) this->u._member.static_size) \
122 DEFINE_INSTANCE_ASSERTION (sizeof(this->u._member) == (size)) \
123 static constexpr unsigned null_size = (size); \
124 static constexpr unsigned min_size = (size)
126 #define DEFINE_SIZE_MIN(size) \
127 DEFINE_INSTANCE_ASSERTION (sizeof (*this) >= (size)) \
128 static constexpr unsigned null_size = (size); \
129 static constexpr unsigned min_size = (size)
131 #define DEFINE_SIZE_UNBOUNDED(size) \
132 DEFINE_INSTANCE_ASSERTION (sizeof (*this) >= (size)) \
133 static constexpr unsigned min_size = (size)
135 #define DEFINE_SIZE_ARRAY(size, array) \
136 DEFINE_COMPILES_ASSERTION ((void) (array)[0].static_size) \
137 DEFINE_INSTANCE_ASSERTION (sizeof (*this) == (size) + VAR * sizeof ((array)[0])) \
138 static constexpr unsigned null_size = (size); \
139 static constexpr unsigned min_size = (size)
141 #define DEFINE_SIZE_ARRAY_SIZED(size, array) \
142 unsigned int get_size () const { return (size - (array).min_size + (array).get_size ()); } \
143 DEFINE_SIZE_ARRAY(size, array)
150 template <typename Context, typename Return, unsigned int MaxDebugDepth>
151 struct hb_dispatch_context_t
153 static constexpr unsigned max_debug_depth = MaxDebugDepth;
154 typedef Return return_t;
155 template <typename T, typename F>
156 bool may_dispatch (const T *obj HB_UNUSED, const F *format HB_UNUSED) { return true; }
157 static return_t no_dispatch_return_value () { return Context::default_return_value (); }
158 static bool stop_sublookup_iteration (const return_t r HB_UNUSED) { return false; }
166 * === Introduction ===
168 * The sanitize machinery is at the core of our zero-cost font loading. We
169 * mmap() font file into memory and create a blob out of it. Font subtables
170 * are returned as a readonly sub-blob of the main font blob. These table
171 * blobs are then sanitized before use, to ensure invalid memory access does
172 * not happen. The toplevel sanitize API use is like, eg. to load the 'head'
175 * hb_blob_t *head_blob = hb_sanitize_context_t ().reference_table<OT::head> (face);
177 * The blob then can be converted to a head table struct with:
179 * const head *head_table = head_blob->as<head> ();
181 * What the reference_table does is, to call hb_face_reference_table() to load
182 * the table blob, sanitize it and return either the sanitized blob, or empty
183 * blob if sanitization failed. The blob->as() function returns the null
184 * object of its template type argument if the blob is empty. Otherwise, it
185 * just casts the blob contents to the desired type.
187 * Sanitizing a blob of data with a type T works as follows (with minor
190 * - Cast blob content to T*, call sanitize() method of it,
191 * - If sanitize succeeded, return blob.
192 * - Otherwise, if blob is not writable, try making it writable,
193 * or copy if cannot be made writable in-place,
194 * - Call sanitize() again. Return blob if sanitize succeeded.
195 * - Return empty blob otherwise.
198 * === The sanitize() contract ===
200 * The sanitize() method of each object type shall return true if it's safe to
201 * call other methods of the object, and false otherwise.
203 * Note that what sanitize() checks for might align with what the specification
204 * describes as valid table data, but does not have to be. In particular, we
205 * do NOT want to be pedantic and concern ourselves with validity checks that
206 * are irrelevant to our use of the table. On the contrary, we want to be
207 * lenient with error handling and accept invalid data to the extent that it
208 * does not impose extra burden on us.
210 * Based on the sanitize contract, one can see that what we check for depends
211 * on how we use the data in other table methods. Ie. if other table methods
212 * assume that offsets do NOT point out of the table data block, then that's
213 * something sanitize() must check for (GSUB/GPOS/GDEF/etc work this way). On
214 * the other hand, if other methods do such checks themselves, then sanitize()
215 * does not have to bother with them (glyf/local work this way). The choice
216 * depends on the table structure and sanitize() performance. For example, to
217 * check glyf/loca offsets in sanitize() would cost O(num-glyphs). We try hard
218 * to avoid such costs during font loading. By postponing such checks to the
219 * actual glyph loading, we reduce the sanitize cost to O(1) and total runtime
220 * cost to O(used-glyphs). As such, this is preferred.
222 * The same argument can be made re GSUB/GPOS/GDEF, but there, the table
223 * structure is so complicated that by checking all offsets at sanitize() time,
224 * we make the code much simpler in other methods, as offsets and referenced
225 * objects do not need to be validated at each use site.
228 /* This limits sanitizing time on really broken fonts. */
229 #ifndef HB_SANITIZE_MAX_EDITS
230 #define HB_SANITIZE_MAX_EDITS 32
232 #ifndef HB_SANITIZE_MAX_OPS_FACTOR
233 #define HB_SANITIZE_MAX_OPS_FACTOR 8
235 #ifndef HB_SANITIZE_MAX_OPS_MIN
236 #define HB_SANITIZE_MAX_OPS_MIN 16384
238 #ifndef HB_SANITIZE_MAX_OPS_MAX
239 #define HB_SANITIZE_MAX_OPS_MAX 0x3FFFFFFF
242 struct hb_sanitize_context_t :
243 hb_dispatch_context_t<hb_sanitize_context_t, bool, HB_DEBUG_SANITIZE>
245 hb_sanitize_context_t () :
247 start (nullptr), end (nullptr),
249 writable (false), edit_count (0),
252 num_glyphs_set (false) {}
254 const char *get_name () { return "SANITIZE"; }
255 template <typename T, typename F>
256 bool may_dispatch (const T *obj HB_UNUSED, const F *format)
257 { return format->sanitize (this); }
258 template <typename T>
259 return_t dispatch (const T &obj) { return obj.sanitize (this); }
260 static return_t default_return_value () { return true; }
261 static return_t no_dispatch_return_value () { return false; }
262 bool stop_sublookup_iteration (const return_t r) const { return !r; }
264 void init (hb_blob_t *b)
266 this->blob = hb_blob_reference (b);
267 this->writable = false;
270 void set_num_glyphs (unsigned int num_glyphs_)
272 num_glyphs = num_glyphs_;
273 num_glyphs_set = true;
275 unsigned int get_num_glyphs () { return num_glyphs; }
277 void set_max_ops (int max_ops_) { max_ops = max_ops_; }
279 template <typename T>
280 void set_object (const T *obj)
286 const char *obj_start = (const char *) obj;
287 if (unlikely (obj_start < this->start || this->end <= obj_start))
288 this->start = this->end = nullptr;
291 this->start = obj_start;
292 this->end = obj_start + MIN<uintptr_t> (this->end - obj_start, obj->get_size ());
298 this->start = this->blob->data;
299 this->end = this->start + this->blob->length;
300 assert (this->start <= this->end); /* Must not overflow. */
303 void start_processing ()
306 this->max_ops = MAX ((unsigned int) (this->end - this->start) * HB_SANITIZE_MAX_OPS_FACTOR,
307 (unsigned) HB_SANITIZE_MAX_OPS_MIN);
308 this->edit_count = 0;
309 this->debug_depth = 0;
311 DEBUG_MSG_LEVEL (SANITIZE, start, 0, +1,
312 "start [%p..%p] (%lu bytes)",
313 this->start, this->end,
314 (unsigned long) (this->end - this->start));
317 void end_processing ()
319 DEBUG_MSG_LEVEL (SANITIZE, this->start, 0, -1,
320 "end [%p..%p] %u edit requests",
321 this->start, this->end, this->edit_count);
323 hb_blob_destroy (this->blob);
324 this->blob = nullptr;
325 this->start = this->end = nullptr;
328 bool check_range (const void *base,
329 unsigned int len) const
331 const char *p = (const char *) base;
332 bool ok = this->start <= p &&
334 (unsigned int) (this->end - p) >= len &&
337 DEBUG_MSG_LEVEL (SANITIZE, p, this->debug_depth+1, 0,
338 "check_range [%p..%p] (%d bytes) in [%p..%p] -> %s",
340 this->start, this->end,
341 ok ? "OK" : "OUT-OF-RANGE");
346 template <typename T>
347 bool check_range (const T *base,
349 unsigned int b) const
351 return !hb_unsigned_mul_overflows (a, b) &&
352 this->check_range (base, a * b);
355 template <typename T>
356 bool check_range (const T *base,
359 unsigned int c) const
361 return !hb_unsigned_mul_overflows (a, b) &&
362 this->check_range (base, a * b, c);
365 template <typename T>
366 bool check_array (const T *base, unsigned int len) const
368 return this->check_range (base, len, hb_static_size (T));
371 template <typename T>
372 bool check_array (const T *base,
374 unsigned int b) const
376 return this->check_range (base, a, b, hb_static_size (T));
379 template <typename Type>
380 bool check_struct (const Type *obj) const
381 { return likely (this->check_range (obj, obj->min_size)); }
383 bool may_edit (const void *base, unsigned int len)
385 if (this->edit_count >= HB_SANITIZE_MAX_EDITS)
388 const char *p = (const char *) base;
391 DEBUG_MSG_LEVEL (SANITIZE, p, this->debug_depth+1, 0,
392 "may_edit(%u) [%p..%p] (%d bytes) in [%p..%p] -> %s",
395 this->start, this->end,
396 this->writable ? "GRANTED" : "DENIED");
398 return this->writable;
401 template <typename Type, typename ValueType>
402 bool try_set (const Type *obj, const ValueType &v)
404 if (this->may_edit (obj, hb_static_size (Type)))
406 hb_assign (* const_cast<Type *> (obj), v);
412 template <typename Type>
413 hb_blob_t *sanitize_blob (hb_blob_t *blob)
420 DEBUG_MSG_FUNC (SANITIZE, start, "start");
424 if (unlikely (!start))
430 Type *t = CastP<Type> (const_cast<char *> (start));
432 sane = t->sanitize (this);
437 DEBUG_MSG_FUNC (SANITIZE, start, "passed first round with %d edits; going for second round", edit_count);
439 /* sanitize again to ensure no toe-stepping */
441 sane = t->sanitize (this);
443 DEBUG_MSG_FUNC (SANITIZE, start, "requested %d edits in second round; FAILLING", edit_count);
450 if (edit_count && !writable) {
451 start = hb_blob_get_data_writable (blob, nullptr);
452 end = start + blob->length;
457 /* ok, we made it writable by relocating. try again */
458 DEBUG_MSG_FUNC (SANITIZE, start, "retry");
466 DEBUG_MSG_FUNC (SANITIZE, start, sane ? "PASSED" : "FAILED");
469 hb_blob_make_immutable (blob);
474 hb_blob_destroy (blob);
475 return hb_blob_get_empty ();
479 template <typename Type>
480 hb_blob_t *reference_table (const hb_face_t *face, hb_tag_t tableTag = Type::tableTag)
483 set_num_glyphs (hb_face_get_glyph_count (face));
484 return sanitize_blob<Type> (hb_face_reference_table (face, tableTag));
487 mutable unsigned int debug_depth;
488 const char *start, *end;
492 unsigned int edit_count;
494 unsigned int num_glyphs;
498 struct hb_sanitize_with_object_t
500 template <typename T>
501 hb_sanitize_with_object_t (hb_sanitize_context_t *c,
502 const T& obj) : c (c)
503 { c->set_object (obj); }
504 ~hb_sanitize_with_object_t ()
505 { c->reset_object (); }
508 hb_sanitize_context_t *c;
516 struct hb_serialize_context_t
518 hb_serialize_context_t (void *start_, unsigned int size)
520 this->start = (char *) start_;
521 this->end = this->start + size;
525 bool in_error () const { return !this->successful; }
529 this->successful = true;
530 this->head = this->start;
531 this->debug_depth = 0;
534 bool propagate_error (bool e)
535 { return this->successful = this->successful && e; }
536 template <typename T> bool propagate_error (const T &obj)
537 { return this->successful = this->successful && !obj.in_error (); }
538 template <typename T> bool propagate_error (const T *obj)
539 { return this->successful = this->successful && !obj->in_error (); }
540 template <typename T1, typename T2> bool propagate_error (T1 &o1, T2 &o2)
541 { return propagate_error (o1) && propagate_error (o2); }
542 template <typename T1, typename T2> bool propagate_error (T1 *o1, T2 *o2)
543 { return propagate_error (o1) && propagate_error (o2); }
544 template <typename T1, typename T2, typename T3>
545 bool propagate_error (T1 &o1, T2 &o2, T3 &o3)
546 { return propagate_error (o1) && propagate_error (o2, o3); }
547 template <typename T1, typename T2, typename T3>
548 bool propagate_error (T1 *o1, T2 *o2, T3 *o3)
549 { return propagate_error (o1) && propagate_error (o2, o3); }
551 /* To be called around main operation. */
552 template <typename Type>
553 Type *start_serialize ()
555 DEBUG_MSG_LEVEL (SERIALIZE, this->start, 0, +1,
556 "start [%p..%p] (%lu bytes)",
557 this->start, this->end,
558 (unsigned long) (this->end - this->start));
560 return start_embed<Type> ();
562 void end_serialize ()
564 DEBUG_MSG_LEVEL (SERIALIZE, this->start, 0, -1,
565 "end [%p..%p] serialized %d bytes; %s",
566 this->start, this->end,
567 (int) (this->head - this->start),
568 this->successful ? "successful" : "UNSUCCESSFUL");
571 unsigned int length () const { return this->head - this->start; }
573 void align (unsigned int alignment)
575 unsigned int l = length () % alignment;
577 allocate_size<void> (alignment - l);
580 template <typename Type>
581 Type *start_embed (const Type *_ HB_UNUSED = nullptr) const
583 Type *ret = reinterpret_cast<Type *> (this->head);
587 template <typename Type>
588 Type *allocate_size (unsigned int size)
590 if (unlikely (!this->successful || this->end - this->head < ptrdiff_t (size))) {
591 this->successful = false;
594 memset (this->head, 0, size);
595 char *ret = this->head;
597 return reinterpret_cast<Type *> (ret);
600 template <typename Type>
601 Type *allocate_min ()
603 return this->allocate_size<Type> (Type::min_size);
606 template <typename Type>
607 Type *embed (const Type &obj)
609 unsigned int size = obj.get_size ();
610 Type *ret = this->allocate_size<Type> (size);
611 if (unlikely (!ret)) return nullptr;
612 memcpy (ret, &obj, size);
615 template <typename Type>
616 hb_serialize_context_t &operator << (const Type &obj) { embed (obj); return *this; }
618 template <typename Type>
619 Type *extend_size (Type &obj, unsigned int size)
621 assert (this->start <= (char *) &obj);
622 assert ((char *) &obj <= this->head);
623 assert ((char *) &obj + size >= this->head);
624 if (unlikely (!this->allocate_size<Type> (((char *) &obj) + size - this->head))) return nullptr;
625 return reinterpret_cast<Type *> (&obj);
628 template <typename Type>
629 Type *extend_min (Type &obj) { return extend_size (obj, obj.min_size); }
631 template <typename Type>
632 Type *extend (Type &obj) { return extend_size (obj, obj.get_size ()); }
634 /* Output routines. */
635 template <typename Type>
638 assert (this->successful);
639 unsigned int len = this->head - this->start;
640 void *p = malloc (len);
642 memcpy (p, this->start, len);
643 return reinterpret_cast<Type *> (p);
645 hb_bytes_t copy_bytes () const
647 assert (this->successful);
648 unsigned int len = this->head - this->start;
649 void *p = malloc (len);
651 memcpy (p, this->start, len);
653 return hb_bytes_t ();
654 return hb_bytes_t ((char *) p, len);
656 hb_blob_t *copy_blob () const
658 assert (this->successful);
659 return hb_blob_create (this->start,
660 this->head - this->start,
661 HB_MEMORY_MODE_DUPLICATE,
666 unsigned int debug_depth;
667 char *start, *end, *head;
674 * Big-endian integers.
677 template <typename Type, int Bytes> struct BEInt;
679 template <typename Type>
680 struct BEInt<Type, 1>
683 void set (Type V) { v = V; }
684 operator Type () const { return v; }
687 template <typename Type>
688 struct BEInt<Type, 2>
693 v[0] = (V >> 8) & 0xFF;
696 operator Type () const
698 #if ((defined(__GNUC__) && __GNUC__ >= 5) || defined(__clang__)) && \
699 defined(__BYTE_ORDER) && \
700 (__BYTE_ORDER == __LITTLE_ENDIAN || __BYTE_ORDER == __BIG_ENDIAN)
701 /* Spoon-feed the compiler a big-endian integer with alignment 1.
702 * https://github.com/harfbuzz/harfbuzz/pull/1398 */
703 struct __attribute__((packed)) packed_uint16_t { uint16_t v; };
704 #if __BYTE_ORDER == __LITTLE_ENDIAN
705 return __builtin_bswap16 (((packed_uint16_t *) this)->v);
706 #else /* __BYTE_ORDER == __BIG_ENDIAN */
707 return ((packed_uint16_t *) this)->v;
713 private: uint8_t v[2];
715 template <typename Type>
716 struct BEInt<Type, 3>
721 v[0] = (V >> 16) & 0xFF;
722 v[1] = (V >> 8) & 0xFF;
725 operator Type () const
731 private: uint8_t v[3];
733 template <typename Type>
734 struct BEInt<Type, 4>
740 v[0] = (V >> 24) & 0xFF;
741 v[1] = (V >> 16) & 0xFF;
742 v[2] = (V >> 8) & 0xFF;
745 operator Type () const
752 private: uint8_t v[4];
760 template <typename Data, unsigned int WheresData>
761 struct hb_data_wrapper_t
763 static_assert (WheresData > 0, "");
765 Data * get_data () const
766 { return *(((Data **) (void *) this) - WheresData); }
768 bool is_inert () const { return !get_data (); }
770 template <typename Stored, typename Subclass>
771 Stored * call_create () const { return Subclass::create (get_data ()); }
774 struct hb_data_wrapper_t<void, 0>
776 bool is_inert () const { return false; }
778 template <typename Stored, typename Funcs>
779 Stored * call_create () const { return Funcs::create (); }
782 template <typename T1, typename T2> struct hb_non_void_t { typedef T1 value; };
783 template <typename T2> struct hb_non_void_t<void, T2> { typedef T2 value; };
785 template <typename Returned,
786 typename Subclass = void,
787 typename Data = void,
788 unsigned int WheresData = 0,
789 typename Stored = Returned>
790 struct hb_lazy_loader_t : hb_data_wrapper_t<Data, WheresData>
792 typedef typename hb_non_void_t<Subclass,
793 hb_lazy_loader_t<Returned,Subclass,Data,WheresData,Stored>
796 void init0 () {} /* Init, when memory is already set to 0. No-op for us. */
797 void init () { instance.set_relaxed (nullptr); }
798 void fini () { do_destroy (instance.get ()); }
800 void free_instance ()
803 Stored *p = instance.get ();
804 if (unlikely (p && !cmpexch (p, nullptr)))
809 static void do_destroy (Stored *p)
811 if (p && p != const_cast<Stored *> (Funcs::get_null ()))
815 const Returned * operator -> () const { return get (); }
816 const Returned & operator * () const { return *get (); }
817 explicit_operator bool () const
818 { return get_stored () != Funcs::get_null (); }
819 template <typename C> operator const C * () const { return get (); }
821 Stored * get_stored () const
824 Stored *p = this->instance.get ();
827 if (unlikely (this->is_inert ()))
828 return const_cast<Stored *> (Funcs::get_null ());
830 p = this->template call_create<Stored, Funcs> ();
832 p = const_cast<Stored *> (Funcs::get_null ());
834 if (unlikely (!cmpexch (nullptr, p)))
842 Stored * get_stored_relaxed () const
844 return this->instance.get_relaxed ();
847 bool cmpexch (Stored *current, Stored *value) const
849 /* This *must* be called when there are no other threads accessing. */
850 return this->instance.cmpexch (current, value);
853 const Returned * get () const { return Funcs::convert (get_stored ()); }
854 const Returned * get_relaxed () const { return Funcs::convert (get_stored_relaxed ()); }
855 Returned * get_unconst () const { return const_cast<Returned *> (Funcs::convert (get_stored ())); }
857 /* To be possibly overloaded by subclasses. */
858 static Returned* convert (Stored *p) { return p; }
860 /* By default null/init/fini the object. */
861 static const Stored* get_null () { return &Null(Stored); }
862 static Stored *create (Data *data)
864 Stored *p = (Stored *) calloc (1, sizeof (Stored));
869 static Stored *create ()
871 Stored *p = (Stored *) calloc (1, sizeof (Stored));
876 static void destroy (Stored *p)
883 /* Must only have one pointer. */
884 hb_atomic_ptr_t<Stored *> instance;
887 /* Specializations. */
889 template <typename T, unsigned int WheresFace>
890 struct hb_face_lazy_loader_t : hb_lazy_loader_t<T,
891 hb_face_lazy_loader_t<T, WheresFace>,
892 hb_face_t, WheresFace> {};
894 template <typename T, unsigned int WheresFace>
895 struct hb_table_lazy_loader_t : hb_lazy_loader_t<T,
896 hb_table_lazy_loader_t<T, WheresFace>,
897 hb_face_t, WheresFace,
900 static hb_blob_t *create (hb_face_t *face)
901 { return hb_sanitize_context_t ().reference_table<T> (face); }
902 static void destroy (hb_blob_t *p) { hb_blob_destroy (p); }
904 static const hb_blob_t *get_null ()
905 { return hb_blob_get_empty (); }
907 static const T* convert (const hb_blob_t *blob)
908 { return blob->as<T> (); }
910 hb_blob_t* get_blob () const { return this->get_stored (); }
913 template <typename Subclass>
914 struct hb_font_funcs_lazy_loader_t : hb_lazy_loader_t<hb_font_funcs_t, Subclass>
916 static void destroy (hb_font_funcs_t *p)
917 { hb_font_funcs_destroy (p); }
918 static const hb_font_funcs_t *get_null ()
919 { return hb_font_funcs_get_empty (); }
921 template <typename Subclass>
922 struct hb_unicode_funcs_lazy_loader_t : hb_lazy_loader_t<hb_unicode_funcs_t, Subclass>
924 static void destroy (hb_unicode_funcs_t *p)
925 { hb_unicode_funcs_destroy (p); }
926 static const hb_unicode_funcs_t *get_null ()
927 { return hb_unicode_funcs_get_empty (); }
931 #endif /* HB_MACHINERY_HH */