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-dispatch.hh"
36 #include "hb-sanitize.hh"
43 /* StructAtOffset<T>(P,Ofs) returns the struct T& that is placed at memory
44 * location pointed to by P plus Ofs bytes. */
45 template<typename Type
>
46 static inline const Type
& StructAtOffset(const void *P
, unsigned int offset
)
47 { return * reinterpret_cast<const Type
*> ((const char *) P
+ offset
); }
48 template<typename Type
>
49 static inline Type
& StructAtOffset(void *P
, unsigned int offset
)
50 { return * reinterpret_cast<Type
*> ((char *) P
+ offset
); }
51 template<typename Type
>
52 static inline const Type
& StructAtOffsetUnaligned(const void *P
, unsigned int offset
)
54 #pragma GCC diagnostic push
55 #pragma GCC diagnostic ignored "-Wcast-align"
56 return * reinterpret_cast<const Type
*> ((const char *) P
+ offset
);
57 #pragma GCC diagnostic pop
59 template<typename Type
>
60 static inline Type
& StructAtOffsetUnaligned(void *P
, unsigned int offset
)
62 #pragma GCC diagnostic push
63 #pragma GCC diagnostic ignored "-Wcast-align"
64 return * reinterpret_cast<Type
*> ((char *) P
+ offset
);
65 #pragma GCC diagnostic pop
68 /* StructAfter<T>(X) returns the struct T& that is placed after X.
69 * Works with X of variable size also. X must implement get_size() */
70 template<typename Type
, typename TObject
>
71 static inline const Type
& StructAfter(const TObject
&X
)
72 { return StructAtOffset
<Type
>(&X
, X
.get_size()); }
73 template<typename Type
, typename TObject
>
74 static inline Type
& StructAfter(TObject
&X
)
75 { return StructAtOffset
<Type
>(&X
, X
.get_size()); }
82 /* Size signifying variable-sized array */
84 #define HB_VAR_ARRAY 1
87 /* Check _assertion in a method environment */
88 #define _DEFINE_INSTANCE_ASSERTION1(_line, _assertion) \
89 void _instance_assertion_on_line_##_line () const \
90 { static_assert ((_assertion), ""); }
91 # define _DEFINE_INSTANCE_ASSERTION0(_line, _assertion) _DEFINE_INSTANCE_ASSERTION1 (_line, _assertion)
92 # define DEFINE_INSTANCE_ASSERTION(_assertion) _DEFINE_INSTANCE_ASSERTION0 (__LINE__, _assertion)
94 /* Check that _code compiles in a method environment */
95 #define _DEFINE_COMPILES_ASSERTION1(_line, _code) \
96 void _compiles_assertion_on_line_##_line () const \
98 # define _DEFINE_COMPILES_ASSERTION0(_line, _code) _DEFINE_COMPILES_ASSERTION1 (_line, _code)
99 # define DEFINE_COMPILES_ASSERTION(_code) _DEFINE_COMPILES_ASSERTION0 (__LINE__, _code)
102 #define DEFINE_SIZE_STATIC(size) \
103 DEFINE_INSTANCE_ASSERTION (sizeof (*this) == (size)) \
104 unsigned int get_size () const { return (size); } \
105 static constexpr unsigned null_size = (size); \
106 static constexpr unsigned min_size = (size); \
107 static constexpr unsigned static_size = (size)
109 #define DEFINE_SIZE_UNION(size, _member) \
110 DEFINE_COMPILES_ASSERTION ((void) this->u._member.static_size) \
111 DEFINE_INSTANCE_ASSERTION (sizeof(this->u._member) == (size)) \
112 static constexpr unsigned null_size = (size); \
113 static constexpr unsigned min_size = (size)
115 #define DEFINE_SIZE_MIN(size) \
116 DEFINE_INSTANCE_ASSERTION (sizeof (*this) >= (size)) \
117 static constexpr unsigned null_size = (size); \
118 static constexpr unsigned min_size = (size)
120 #define DEFINE_SIZE_UNBOUNDED(size) \
121 DEFINE_INSTANCE_ASSERTION (sizeof (*this) >= (size)) \
122 static constexpr unsigned min_size = (size)
124 #define DEFINE_SIZE_ARRAY(size, array) \
125 DEFINE_COMPILES_ASSERTION ((void) (array)[0].static_size) \
126 DEFINE_INSTANCE_ASSERTION (sizeof (*this) == (size) + (HB_VAR_ARRAY+0) * sizeof ((array)[0])) \
127 static constexpr unsigned null_size = (size); \
128 static constexpr unsigned min_size = (size)
130 #define DEFINE_SIZE_ARRAY_SIZED(size, array) \
131 unsigned int get_size () const { return (size - (array).min_size + (array).get_size ()); } \
132 DEFINE_SIZE_ARRAY(size, array)
134 #define DEFINE_SIZE_MAX(size) \
135 DEFINE_INSTANCE_ASSERTION (sizeof (*this) <= (size)) \
136 static constexpr unsigned max_size = (size)
143 * The lazy-loaders are thread-safe pointer-like objects that create their
144 * instead on-demand. They also support access to a "data" object that is
145 * necessary for creating their instance. The data object, if specified,
146 * is accessed via pointer math, located at a location before the position
147 * of the loader itself. This avoids having to store a pointer to data
148 * for every lazy-loader. Multiple lazy-loaders can access the same data.
151 template <typename Data
, unsigned int WheresData
>
152 struct hb_data_wrapper_t
154 static_assert (WheresData
> 0, "");
156 Data
* get_data () const
157 { return *(((Data
**) (void *) this) - WheresData
); }
159 bool is_inert () const { return !get_data (); }
161 template <typename Stored
, typename Subclass
>
162 Stored
* call_create () const { return Subclass::create (get_data ()); }
165 struct hb_data_wrapper_t
<void, 0>
167 bool is_inert () const { return false; }
169 template <typename Stored
, typename Funcs
>
170 Stored
* call_create () const { return Funcs::create (); }
173 template <typename T1
, typename T2
> struct hb_non_void_t
{ typedef T1 value
; };
174 template <typename T2
> struct hb_non_void_t
<void, T2
> { typedef T2 value
; };
176 template <typename Returned
,
177 typename Subclass
= void,
178 typename Data
= void,
179 unsigned int WheresData
= 0,
180 typename Stored
= Returned
>
181 struct hb_lazy_loader_t
: hb_data_wrapper_t
<Data
, WheresData
>
183 typedef typename hb_non_void_t
<Subclass
,
184 hb_lazy_loader_t
<Returned
,Subclass
,Data
,WheresData
,Stored
>
187 hb_lazy_loader_t () = default;
188 hb_lazy_loader_t (const hb_lazy_loader_t
&other
) = delete;
190 void init0 () {} /* Init, when memory is already set to 0. No-op for us. */
191 void init () { instance
.set_relaxed (nullptr); }
192 void fini () { do_destroy (instance
.get_acquire ()); init (); }
194 void free_instance ()
197 Stored
*p
= instance
.get_acquire ();
198 if (unlikely (p
&& !cmpexch (p
, nullptr)))
203 static void do_destroy (Stored
*p
)
205 if (p
&& p
!= const_cast<Stored
*> (Funcs::get_null ()))
209 const Returned
* operator -> () const { return get (); }
210 template <typename U
= Returned
, hb_enable_if (!hb_is_same (U
, void))>
211 const U
& operator * () const { return *get (); }
212 explicit operator bool () const
213 { return get_stored () != Funcs::get_null (); }
214 template <typename C
> operator const C
* () const { return get (); }
216 Stored
* get_stored () const
219 Stored
*p
= this->instance
.get_acquire ();
222 if (unlikely (this->is_inert ()))
223 return const_cast<Stored
*> (Funcs::get_null ());
225 p
= this->template call_create
<Stored
, Funcs
> ();
227 p
= const_cast<Stored
*> (Funcs::get_null ());
229 if (unlikely (!cmpexch (nullptr, p
)))
237 Stored
* get_stored_relaxed () const
239 return this->instance
.get_relaxed ();
242 bool cmpexch (Stored
*current
, Stored
*value
) const
244 /* This function can only be safely called directly if no
245 * other thread is accessing. */
246 return this->instance
.cmpexch (current
, value
);
249 const Returned
* get () const { return Funcs::convert (get_stored ()); }
250 const Returned
* get_relaxed () const { return Funcs::convert (get_stored_relaxed ()); }
251 Returned
* get_unconst () const { return const_cast<Returned
*> (Funcs::convert (get_stored ())); }
253 /* To be possibly overloaded by subclasses. */
254 static Returned
* convert (Stored
*p
) { return p
; }
256 /* By default null/init/fini the object. */
257 static const Stored
* get_null () { return &Null (Stored
); }
258 static Stored
*create (Data
*data
)
260 Stored
*p
= (Stored
*) hb_calloc (1, sizeof (Stored
));
262 p
= new (p
) Stored (data
);
265 static Stored
*create ()
267 Stored
*p
= (Stored
*) hb_calloc (1, sizeof (Stored
));
269 p
= new (p
) Stored ();
272 static void destroy (Stored
*p
)
279 /* Must only have one pointer. */
280 hb_atomic_ptr_t
<Stored
*> instance
;
283 /* Specializations. */
285 template <typename T
, unsigned int WheresFace
>
286 struct hb_face_lazy_loader_t
: hb_lazy_loader_t
<T
,
287 hb_face_lazy_loader_t
<T
, WheresFace
>,
288 hb_face_t
, WheresFace
>
290 // Hack; have them here for API parity with hb_table_lazy_loader_t
291 hb_blob_t
*get_blob () { return this->get ()->get_blob (); }
294 template <typename T
, unsigned int WheresFace
, bool core
=false>
295 struct hb_table_lazy_loader_t
: hb_lazy_loader_t
<T
,
296 hb_table_lazy_loader_t
<T
, WheresFace
, core
>,
297 hb_face_t
, WheresFace
,
300 static hb_blob_t
*create (hb_face_t
*face
)
302 hb_sanitize_context_t c
;
304 c
.set_num_glyphs (0); // So we don't recurse ad infinitum, or doesn't need num_glyphs
305 return c
.reference_table
<T
> (face
);
307 static void destroy (hb_blob_t
*p
) { hb_blob_destroy (p
); }
309 static const hb_blob_t
*get_null ()
310 { return hb_blob_get_empty (); }
312 static const T
* convert (const hb_blob_t
*blob
)
313 { return blob
->as
<T
> (); }
315 hb_blob_t
* get_blob () const { return this->get_stored (); }
318 #define HB_DEFINE_TYPE_FUNCS_LAZY_LOADER_T(Type) \
319 template <typename Subclass> \
320 struct hb_##Type##_funcs_lazy_loader_t : hb_lazy_loader_t<hb_##Type##_funcs_t, Subclass> \
322 static void destroy (hb_##Type##_funcs_t *p) \
323 { hb_##Type##_funcs_destroy (p); } \
324 static const hb_##Type##_funcs_t *get_null () \
325 { return hb_##Type##_funcs_get_empty (); } \
328 HB_DEFINE_TYPE_FUNCS_LAZY_LOADER_T (font
);
329 HB_DEFINE_TYPE_FUNCS_LAZY_LOADER_T (unicode
);
330 HB_DEFINE_TYPE_FUNCS_LAZY_LOADER_T (draw
);
331 HB_DEFINE_TYPE_FUNCS_LAZY_LOADER_T (paint
);
333 #undef HB_DEFINE_TYPE_FUNCS_LAZY_LOADER_T
336 #endif /* HB_MACHINERY_HH */