1/*
2 * Copyright © 2007,2008,2009,2010 Red Hat, Inc.
3 * Copyright © 2012,2018 Google, Inc.
4 *
5 * This is part of HarfBuzz, a text shaping library.
6 *
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.
12 *
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
17 * DAMAGE.
18 *
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.
24 *
25 * Red Hat Author(s): Behdad Esfahbod
26 * Google Author(s): Behdad Esfahbod
27 */
28
29#ifndef HB_MACHINERY_HH
30#define HB_MACHINERY_HH
31
32#include "hb.hh"
33#include "hb-blob.hh"
34
35#include "hb-dispatch.hh"
36#include "hb-sanitize.hh"
37
38
39/*
40 * Casts
41 */
42
43/* StructAtOffset<T>(P,Ofs) returns the struct T& that is placed at memory
44 * location pointed to by P plus Ofs bytes. */
45template<typename Type>
46static inline const Type& StructAtOffset(const void *P, unsigned int offset)
47{ return * reinterpret_cast<const Type*> ((const char *) P + offset); }
48template<typename Type>
49static inline Type& StructAtOffset(void *P, unsigned int offset)
50{ return * reinterpret_cast<Type*> ((char *) P + offset); }
51template<typename Type>
52static inline const Type& StructAtOffsetUnaligned(const void *P, unsigned int offset)
53{
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
58}
59template<typename Type>
60static inline Type& StructAtOffsetUnaligned(void *P, unsigned int offset)
61{
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
66}
67
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() */
70template<typename Type, typename TObject>
71static inline const Type& StructAfter(const TObject &X)
72{ return StructAtOffset<Type>(&X, X.get_size()); }
73template<typename Type, typename TObject>
74static inline Type& StructAfter(TObject &X)
75{ return StructAtOffset<Type>(&X, X.get_size()); }
76
77
78/*
79 * Size checking
80 */
81
82/* Size signifying variable-sized array */
83#ifndef HB_VAR_ARRAY
84#define HB_VAR_ARRAY 1
85#endif
86
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)
93
94/* Check that _code compiles in a method environment */
95#define _DEFINE_COMPILES_ASSERTION1(_line, _code) \
96 void _compiles_assertion_on_line_##_line () const \
97 { _code; }
98# define _DEFINE_COMPILES_ASSERTION0(_line, _code) _DEFINE_COMPILES_ASSERTION1 (_line, _code)
99# define DEFINE_COMPILES_ASSERTION(_code) _DEFINE_COMPILES_ASSERTION0 (__LINE__, _code)
100
101
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)
108
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)
114
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)
119
120#define DEFINE_SIZE_UNBOUNDED(size) \
121 DEFINE_INSTANCE_ASSERTION (sizeof (*this) >= (size)) \
122 static constexpr unsigned min_size = (size)
123
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)
129
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)
133
134
135
136/*
137 * Lazy loaders.
138 *
139 * The lazy-loaders are thread-safe pointer-like objects that create their
140 * instead on-demand. They also support access to a "data" object that is
141 * necessary for creating their instance. The data object, if specified,
142 * is accessed via pointer math, located at a location before the position
143 * of the loader itself. This avoids having to store a pointer to data
144 * for every lazy-loader. Multiple lazy-loaders can access the same data.
145 */
146
147template <typename Data, unsigned int WheresData>
148struct hb_data_wrapper_t
149{
150 static_assert (WheresData > 0, "");
151
152 Data * get_data () const
153 { return *(((Data **) (void *) this) - WheresData); }
154
155 bool is_inert () const { return !get_data (); }
156
157 template <typename Stored, typename Subclass>
158 Stored * call_create () const { return Subclass::create (get_data ()); }
159};
160template <>
161struct hb_data_wrapper_t<void, 0>
162{
163 bool is_inert () const { return false; }
164
165 template <typename Stored, typename Funcs>
166 Stored * call_create () const { return Funcs::create (); }
167};
168
169template <typename T1, typename T2> struct hb_non_void_t { typedef T1 value; };
170template <typename T2> struct hb_non_void_t<void, T2> { typedef T2 value; };
171
172template <typename Returned,
173 typename Subclass = void,
174 typename Data = void,
175 unsigned int WheresData = 0,
176 typename Stored = Returned>
177struct hb_lazy_loader_t : hb_data_wrapper_t<Data, WheresData>
178{
179 typedef typename hb_non_void_t<Subclass,
180 hb_lazy_loader_t<Returned,Subclass,Data,WheresData,Stored>
181 >::value Funcs;
182
183 void init0 () {} /* Init, when memory is already set to 0. No-op for us. */
184 void init () { instance.set_relaxed (nullptr); }
185 void fini () { do_destroy (p: instance.get_acquire ()); init (); }
186
187 void free_instance ()
188 {
189 retry:
190 Stored *p = instance.get_acquire ();
191 if (unlikely (p && !cmpexch (p, nullptr)))
192 goto retry;
193 do_destroy (p);
194 }
195
196 static void do_destroy (Stored *p)
197 {
198 if (p && p != const_cast<Stored *> (Funcs::get_null ()))
199 Funcs::destroy (p);
200 }
201
202 const Returned * operator -> () const { return get (); }
203 template <typename U = Returned, hb_enable_if (!hb_is_same (U, void))>
204 const U & operator * () const { return *get (); }
205 explicit operator bool () const
206 { return get_stored () != Funcs::get_null (); }
207 template <typename C> operator const C * () const { return get (); }
208
209 Stored * get_stored () const
210 {
211 retry:
212 Stored *p = this->instance.get_acquire ();
213 if (unlikely (!p))
214 {
215 if (unlikely (this->is_inert ()))
216 return const_cast<Stored *> (Funcs::get_null ());
217
218 p = this->template call_create<Stored, Funcs> ();
219 if (unlikely (!p))
220 p = const_cast<Stored *> (Funcs::get_null ());
221
222 if (unlikely (!cmpexch (nullptr, p)))
223 {
224 do_destroy (p);
225 goto retry;
226 }
227 }
228 return p;
229 }
230 Stored * get_stored_relaxed () const
231 {
232 return this->instance.get_relaxed ();
233 }
234
235 bool cmpexch (Stored *current, Stored *value) const
236 {
237 /* This function can only be safely called directly if no
238 * other thread is accessing. */
239 return this->instance.cmpexch (current, value);
240 }
241
242 const Returned * get () const { return Funcs::convert (get_stored ()); }
243 const Returned * get_relaxed () const { return Funcs::convert (get_stored_relaxed ()); }
244 Returned * get_unconst () const { return const_cast<Returned *> (Funcs::convert (get_stored ())); }
245
246 /* To be possibly overloaded by subclasses. */
247 static Returned* convert (Stored *p) { return p; }
248
249 /* By default null/init/fini the object. */
250 static const Stored* get_null () { return &Null (Stored); }
251 static Stored *create (Data *data)
252 {
253 Stored *p = (Stored *) hb_calloc (nmemb: 1, size: sizeof (Stored));
254 if (likely (p))
255 p = new (p) Stored (data);
256 return p;
257 }
258 static Stored *create ()
259 {
260 Stored *p = (Stored *) hb_calloc (nmemb: 1, size: sizeof (Stored));
261 if (likely (p))
262 p = new (p) Stored ();
263 return p;
264 }
265 static void destroy (Stored *p)
266 {
267 p->~Stored ();
268 hb_free (p);
269 }
270
271 private:
272 /* Must only have one pointer. */
273 hb_atomic_ptr_t<Stored *> instance;
274};
275
276/* Specializations. */
277
278template <typename T, unsigned int WheresFace>
279struct hb_face_lazy_loader_t : hb_lazy_loader_t<T,
280 hb_face_lazy_loader_t<T, WheresFace>,
281 hb_face_t, WheresFace> {};
282
283template <typename T, unsigned int WheresFace, bool core=false>
284struct hb_table_lazy_loader_t : hb_lazy_loader_t<T,
285 hb_table_lazy_loader_t<T, WheresFace, core>,
286 hb_face_t, WheresFace,
287 hb_blob_t>
288{
289 static hb_blob_t *create (hb_face_t *face)
290 {
291 auto c = hb_sanitize_context_t ();
292 if (core)
293 c.set_num_glyphs (0); // So we don't recurse ad infinitum, or doesn't need num_glyphs
294 return c.reference_table<T> (face);
295 }
296 static void destroy (hb_blob_t *p) { hb_blob_destroy (blob: p); }
297
298 static const hb_blob_t *get_null ()
299 { return hb_blob_get_empty (); }
300
301 static const T* convert (const hb_blob_t *blob)
302 { return blob->as<T> (); }
303
304 hb_blob_t* get_blob () const { return this->get_stored (); }
305};
306
307#define HB_DEFINE_TYPE_FUNCS_LAZY_LOADER_T(Type) \
308 template <typename Subclass> \
309 struct hb_##Type##_funcs_lazy_loader_t : hb_lazy_loader_t<hb_##Type##_funcs_t, Subclass> \
310 { \
311 static void destroy (hb_##Type##_funcs_t *p) \
312 { hb_##Type##_funcs_destroy (p); } \
313 static const hb_##Type##_funcs_t *get_null () \
314 { return hb_##Type##_funcs_get_empty (); } \
315 }
316
317HB_DEFINE_TYPE_FUNCS_LAZY_LOADER_T (font);
318HB_DEFINE_TYPE_FUNCS_LAZY_LOADER_T (unicode);
319HB_DEFINE_TYPE_FUNCS_LAZY_LOADER_T (draw);
320HB_DEFINE_TYPE_FUNCS_LAZY_LOADER_T (paint);
321
322#undef HB_DEFINE_TYPE_FUNCS_LAZY_LOADER_T
323
324
325#endif /* HB_MACHINERY_HH */
326

source code of flutter_engine/third_party/harfbuzz/src/hb-machinery.hh