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 #include "hb-serialize.hh"
38 
39 
40 /*
41  * Casts
42  */
43 
44 /* StructAtOffset<T>(P,Ofs) returns the struct T& that is placed at memory
45  * location pointed to by P plus Ofs bytes. */
46 template<typename Type>
StructAtOffset(const void * P,unsigned int offset)47 static inline const Type& StructAtOffset(const void *P, unsigned int offset)
48 { return * reinterpret_cast<const Type*> ((const char *) P + offset); }
49 template<typename Type>
StructAtOffset(void * P,unsigned int offset)50 static inline Type& StructAtOffset(void *P, unsigned int offset)
51 { return * reinterpret_cast<Type*> ((char *) P + offset); }
52 template<typename Type>
StructAtOffsetUnaligned(const void * P,unsigned int offset)53 static inline const Type& StructAtOffsetUnaligned(const void *P, unsigned int offset)
54 {
55 #pragma GCC diagnostic push
56 #pragma GCC diagnostic ignored "-Wcast-align"
57   return * reinterpret_cast<const Type*> ((const char *) P + offset);
58 #pragma GCC diagnostic pop
59 }
60 template<typename Type>
StructAtOffsetUnaligned(void * P,unsigned int offset)61 static inline Type& StructAtOffsetUnaligned(void *P, unsigned int offset)
62 {
63 #pragma GCC diagnostic push
64 #pragma GCC diagnostic ignored "-Wcast-align"
65   return * reinterpret_cast<Type*> ((char *) P + offset);
66 #pragma GCC diagnostic pop
67 }
68 
69 /* StructAfter<T>(X) returns the struct T& that is placed after X.
70  * Works with X of variable size also.  X must implement get_size() */
71 template<typename Type, typename TObject>
StructAfter(const TObject & X)72 static inline const Type& StructAfter(const TObject &X)
73 { return StructAtOffset<Type>(&X, X.get_size()); }
74 template<typename Type, typename TObject>
StructAfter(TObject & X)75 static inline Type& StructAfter(TObject &X)
76 { return StructAtOffset<Type>(&X, X.get_size()); }
77 
78 
79 /*
80  * Size checking
81  */
82 
83 /* Size signifying variable-sized array */
84 #ifndef HB_VAR_ARRAY
85 #define HB_VAR_ARRAY 1
86 #endif
87 
88 /* Check _assertion in a method environment */
89 #define _DEFINE_INSTANCE_ASSERTION1(_line, _assertion) \
90   void _instance_assertion_on_line_##_line () const \
91   { static_assert ((_assertion), ""); }
92 # define _DEFINE_INSTANCE_ASSERTION0(_line, _assertion) _DEFINE_INSTANCE_ASSERTION1 (_line, _assertion)
93 # define DEFINE_INSTANCE_ASSERTION(_assertion) _DEFINE_INSTANCE_ASSERTION0 (__LINE__, _assertion)
94 
95 /* Check that _code compiles in a method environment */
96 #define _DEFINE_COMPILES_ASSERTION1(_line, _code) \
97   void _compiles_assertion_on_line_##_line () const \
98   { _code; }
99 # define _DEFINE_COMPILES_ASSERTION0(_line, _code) _DEFINE_COMPILES_ASSERTION1 (_line, _code)
100 # define DEFINE_COMPILES_ASSERTION(_code) _DEFINE_COMPILES_ASSERTION0 (__LINE__, _code)
101 
102 
103 #define DEFINE_SIZE_STATIC(size) \
104   DEFINE_INSTANCE_ASSERTION (sizeof (*this) == (size)) \
105   unsigned int get_size () const { return (size); } \
106   static constexpr unsigned null_size = (size); \
107   static constexpr unsigned min_size = (size); \
108   static constexpr unsigned static_size = (size)
109 
110 #define DEFINE_SIZE_UNION(size, _member) \
111   DEFINE_COMPILES_ASSERTION ((void) this->u._member.static_size) \
112   DEFINE_INSTANCE_ASSERTION (sizeof(this->u._member) == (size)) \
113   static constexpr unsigned null_size = (size); \
114   static constexpr unsigned min_size = (size)
115 
116 #define DEFINE_SIZE_MIN(size) \
117   DEFINE_INSTANCE_ASSERTION (sizeof (*this) >= (size)) \
118   static constexpr unsigned null_size = (size); \
119   static constexpr unsigned min_size = (size)
120 
121 #define DEFINE_SIZE_UNBOUNDED(size) \
122   DEFINE_INSTANCE_ASSERTION (sizeof (*this) >= (size)) \
123   static constexpr unsigned min_size = (size)
124 
125 #define DEFINE_SIZE_ARRAY(size, array) \
126   DEFINE_COMPILES_ASSERTION ((void) (array)[0].static_size) \
127   DEFINE_INSTANCE_ASSERTION (sizeof (*this) == (size) + (HB_VAR_ARRAY+0) * sizeof ((array)[0])) \
128   static constexpr unsigned null_size = (size); \
129   static constexpr unsigned min_size = (size)
130 
131 #define DEFINE_SIZE_ARRAY_SIZED(size, array) \
132   unsigned int get_size () const { return (size - (array).min_size + (array).get_size ()); } \
133   DEFINE_SIZE_ARRAY(size, array)
134 
135 
136 
137 /*
138  * Lazy loaders.
139  */
140 
141 template <typename Data, unsigned int WheresData>
142 struct hb_data_wrapper_t
143 {
144   static_assert (WheresData > 0, "");
145 
get_datahb_data_wrapper_t146   Data * get_data () const
147   { return *(((Data **) (void *) this) - WheresData); }
148 
is_inerthb_data_wrapper_t149   bool is_inert () const { return !get_data (); }
150 
151   template <typename Stored, typename Subclass>
152   Stored * call_create () const { return Subclass::create (get_data ()); }
153 };
154 template <>
155 struct hb_data_wrapper_t<void, 0>
156 {
is_inerthb_data_wrapper_t157   bool is_inert () const { return false; }
158 
159   template <typename Stored, typename Funcs>
160   Stored * call_create () const { return Funcs::create (); }
161 };
162 
163 template <typename T1, typename T2> struct hb_non_void_t { typedef T1 value; };
164 template <typename T2> struct hb_non_void_t<void, T2> { typedef T2 value; };
165 
166 template <typename Returned,
167 	  typename Subclass = void,
168 	  typename Data = void,
169 	  unsigned int WheresData = 0,
170 	  typename Stored = Returned>
171 struct hb_lazy_loader_t : hb_data_wrapper_t<Data, WheresData>
172 {
173   typedef typename hb_non_void_t<Subclass,
174 				 hb_lazy_loader_t<Returned,Subclass,Data,WheresData,Stored>
175 				>::value Funcs;
176 
init0hb_lazy_loader_t177   void init0 () {} /* Init, when memory is already set to 0. No-op for us. */
inithb_lazy_loader_t178   void init ()  { instance.set_relaxed (nullptr); }
finihb_lazy_loader_t179   void fini ()  { do_destroy (instance.get ()); }
180 
free_instancehb_lazy_loader_t181   void free_instance ()
182   {
183   retry:
184     Stored *p = instance.get ();
185     if (unlikely (p && !cmpexch (p, nullptr)))
186       goto retry;
187     do_destroy (p);
188   }
189 
do_destroyhb_lazy_loader_t190   static void do_destroy (Stored *p)
191   {
192     if (p && p != const_cast<Stored *> (Funcs::get_null ()))
193       Funcs::destroy (p);
194   }
195 
operator ->hb_lazy_loader_t196   const Returned * operator -> () const { return get (); }
operator *hb_lazy_loader_t197   const Returned & operator * () const  { return *get (); }
operator boolhb_lazy_loader_t198   explicit operator bool () const
199   { return get_stored () != Funcs::get_null (); }
operator const C*hb_lazy_loader_t200   template <typename C> operator const C * () const { return get (); }
201 
get_storedhb_lazy_loader_t202   Stored * get_stored () const
203   {
204   retry:
205     Stored *p = this->instance.get ();
206     if (unlikely (!p))
207     {
208       if (unlikely (this->is_inert ()))
209 	return const_cast<Stored *> (Funcs::get_null ());
210 
211       p = this->template call_create<Stored, Funcs> ();
212       if (unlikely (!p))
213 	p = const_cast<Stored *> (Funcs::get_null ());
214 
215       if (unlikely (!cmpexch (nullptr, p)))
216       {
217 	do_destroy (p);
218 	goto retry;
219       }
220     }
221     return p;
222   }
get_stored_relaxedhb_lazy_loader_t223   Stored * get_stored_relaxed () const
224   {
225     return this->instance.get_relaxed ();
226   }
227 
cmpexchhb_lazy_loader_t228   bool cmpexch (Stored *current, Stored *value) const
229   {
230     /* This *must* be called when there are no other threads accessing. */
231     return this->instance.cmpexch (current, value);
232   }
233 
gethb_lazy_loader_t234   const Returned * get () const { return Funcs::convert (get_stored ()); }
get_relaxedhb_lazy_loader_t235   const Returned * get_relaxed () const { return Funcs::convert (get_stored_relaxed ()); }
get_unconsthb_lazy_loader_t236   Returned * get_unconst () const { return const_cast<Returned *> (Funcs::convert (get_stored ())); }
237 
238   /* To be possibly overloaded by subclasses. */
converthb_lazy_loader_t239   static Returned* convert (Stored *p) { return p; }
240 
241   /* By default null/init/fini the object. */
get_nullhb_lazy_loader_t242   static const Stored* get_null () { return &Null (Stored); }
createhb_lazy_loader_t243   static Stored *create (Data *data)
244   {
245     Stored *p = (Stored *) calloc (1, sizeof (Stored));
246     if (likely (p))
247       p->init (data);
248     return p;
249   }
createhb_lazy_loader_t250   static Stored *create ()
251   {
252     Stored *p = (Stored *) calloc (1, sizeof (Stored));
253     if (likely (p))
254       p->init ();
255     return p;
256   }
destroyhb_lazy_loader_t257   static void destroy (Stored *p)
258   {
259     p->fini ();
260     free (p);
261   }
262 
263 //  private:
264   /* Must only have one pointer. */
265   hb_atomic_ptr_t<Stored *> instance;
266 };
267 
268 /* Specializations. */
269 
270 template <typename T, unsigned int WheresFace>
271 struct hb_face_lazy_loader_t : hb_lazy_loader_t<T,
272 						hb_face_lazy_loader_t<T, WheresFace>,
273 						hb_face_t, WheresFace> {};
274 
275 template <typename T, unsigned int WheresFace>
276 struct hb_table_lazy_loader_t : hb_lazy_loader_t<T,
277 						 hb_table_lazy_loader_t<T, WheresFace>,
278 						 hb_face_t, WheresFace,
279 						 hb_blob_t>
280 {
createhb_table_lazy_loader_t281   static hb_blob_t *create (hb_face_t *face)
282   { return hb_sanitize_context_t ().reference_table<T> (face); }
destroyhb_table_lazy_loader_t283   static void destroy (hb_blob_t *p) { hb_blob_destroy (p); }
284 
get_nullhb_table_lazy_loader_t285   static const hb_blob_t *get_null ()
286   { return hb_blob_get_empty (); }
287 
converthb_table_lazy_loader_t288   static const T* convert (const hb_blob_t *blob)
289   { return blob->as<T> (); }
290 
get_blobhb_table_lazy_loader_t291   hb_blob_t* get_blob () const { return this->get_stored (); }
292 };
293 
294 template <typename Subclass>
295 struct hb_font_funcs_lazy_loader_t : hb_lazy_loader_t<hb_font_funcs_t, Subclass>
296 {
destroyhb_font_funcs_lazy_loader_t297   static void destroy (hb_font_funcs_t *p)
298   { hb_font_funcs_destroy (p); }
get_nullhb_font_funcs_lazy_loader_t299   static const hb_font_funcs_t *get_null ()
300   { return hb_font_funcs_get_empty (); }
301 };
302 template <typename Subclass>
303 struct hb_unicode_funcs_lazy_loader_t : hb_lazy_loader_t<hb_unicode_funcs_t, Subclass>
304 {
destroyhb_unicode_funcs_lazy_loader_t305   static void destroy (hb_unicode_funcs_t *p)
306   { hb_unicode_funcs_destroy (p); }
get_nullhb_unicode_funcs_lazy_loader_t307   static const hb_unicode_funcs_t *get_null ()
308   { return hb_unicode_funcs_get_empty (); }
309 };
310 
311 
312 #endif /* HB_MACHINERY_HH */
313