| 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> |
| 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> |
| 50 | static inline Type& StructAtOffset(void *P, unsigned int offset) |
| 51 | { return * reinterpret_cast<Type*> ((char *) P + offset); } |
| 52 | template<typename Type> |
| 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> |
| 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> |
| 72 | static inline const Type& StructAfter(const TObject &X) |
| 73 | { return StructAtOffset<Type>(&X, X.get_size()); } |
| 74 | template<typename Type, typename TObject> |
| 75 | static inline Type& StructAfter(TObject &X) |
| 76 | { return StructAtOffset<Type>(&X, X.get_size()); } |
| 77 | |
| 78 | |
| 79 | /* |
| 80 | * Size checking |
| 81 | */ |
| 82 | |
| 83 | /* Check _assertion in a method environment */ |
| 84 | #define _DEFINE_INSTANCE_ASSERTION1(_line, _assertion) \ |
| 85 | void _instance_assertion_on_line_##_line () const \ |
| 86 | { static_assert ((_assertion), ""); } |
| 87 | # define _DEFINE_INSTANCE_ASSERTION0(_line, _assertion) _DEFINE_INSTANCE_ASSERTION1 (_line, _assertion) |
| 88 | # define DEFINE_INSTANCE_ASSERTION(_assertion) _DEFINE_INSTANCE_ASSERTION0 (__LINE__, _assertion) |
| 89 | |
| 90 | /* Check that _code compiles in a method environment */ |
| 91 | #define _DEFINE_COMPILES_ASSERTION1(_line, _code) \ |
| 92 | void _compiles_assertion_on_line_##_line () const \ |
| 93 | { _code; } |
| 94 | # define _DEFINE_COMPILES_ASSERTION0(_line, _code) _DEFINE_COMPILES_ASSERTION1 (_line, _code) |
| 95 | # define DEFINE_COMPILES_ASSERTION(_code) _DEFINE_COMPILES_ASSERTION0 (__LINE__, _code) |
| 96 | |
| 97 | |
| 98 | #define DEFINE_SIZE_STATIC(size) \ |
| 99 | DEFINE_INSTANCE_ASSERTION (sizeof (*this) == (size)) \ |
| 100 | unsigned int get_size () const { return (size); } \ |
| 101 | static constexpr unsigned null_size = (size); \ |
| 102 | static constexpr unsigned min_size = (size); \ |
| 103 | static constexpr unsigned static_size = (size) |
| 104 | |
| 105 | #define DEFINE_SIZE_UNION(size, _member) \ |
| 106 | DEFINE_COMPILES_ASSERTION ((void) this->u._member.static_size) \ |
| 107 | DEFINE_INSTANCE_ASSERTION (sizeof(this->u._member) == (size)) \ |
| 108 | static constexpr unsigned null_size = (size); \ |
| 109 | static constexpr unsigned min_size = (size) |
| 110 | |
| 111 | #define DEFINE_SIZE_MIN(size) \ |
| 112 | DEFINE_INSTANCE_ASSERTION (sizeof (*this) >= (size)) \ |
| 113 | static constexpr unsigned null_size = (size); \ |
| 114 | static constexpr unsigned min_size = (size) |
| 115 | |
| 116 | #define DEFINE_SIZE_UNBOUNDED(size) \ |
| 117 | DEFINE_INSTANCE_ASSERTION (sizeof (*this) >= (size)) \ |
| 118 | static constexpr unsigned min_size = (size) |
| 119 | |
| 120 | #define DEFINE_SIZE_ARRAY(size, array) \ |
| 121 | DEFINE_COMPILES_ASSERTION ((void) (array)[0].static_size) \ |
| 122 | DEFINE_INSTANCE_ASSERTION (sizeof (*this) == (size) + (HB_VAR_ARRAY+0) * sizeof ((array)[0])) \ |
| 123 | static constexpr unsigned null_size = (size); \ |
| 124 | static constexpr unsigned min_size = (size) |
| 125 | |
| 126 | #define DEFINE_SIZE_ARRAY_SIZED(size, array) \ |
| 127 | unsigned int get_size () const { return (size - (array).min_size + (array).get_size ()); } \ |
| 128 | DEFINE_SIZE_ARRAY(size, array) |
| 129 | |
| 130 | |
| 131 | |
| 132 | /* |
| 133 | * Lazy loaders. |
| 134 | */ |
| 135 | |
| 136 | template <typename Data, unsigned int WheresData> |
| 137 | struct hb_data_wrapper_t |
| 138 | { |
| 139 | static_assert (WheresData > 0, "" ); |
| 140 | |
| 141 | Data * get_data () const |
| 142 | { return *(((Data **) (void *) this) - WheresData); } |
| 143 | |
| 144 | bool is_inert () const { return !get_data (); } |
| 145 | |
| 146 | template <typename Stored, typename Subclass> |
| 147 | Stored * call_create () const { return Subclass::create (get_data ()); } |
| 148 | }; |
| 149 | template <> |
| 150 | struct hb_data_wrapper_t<void, 0> |
| 151 | { |
| 152 | bool is_inert () const { return false; } |
| 153 | |
| 154 | template <typename Stored, typename Funcs> |
| 155 | Stored * call_create () const { return Funcs::create (); } |
| 156 | }; |
| 157 | |
| 158 | template <typename T1, typename T2> struct hb_non_void_t { typedef T1 value; }; |
| 159 | template <typename T2> struct hb_non_void_t<void, T2> { typedef T2 value; }; |
| 160 | |
| 161 | template <typename Returned, |
| 162 | typename Subclass = void, |
| 163 | typename Data = void, |
| 164 | unsigned int WheresData = 0, |
| 165 | typename Stored = Returned> |
| 166 | struct hb_lazy_loader_t : hb_data_wrapper_t<Data, WheresData> |
| 167 | { |
| 168 | typedef typename hb_non_void_t<Subclass, |
| 169 | hb_lazy_loader_t<Returned,Subclass,Data,WheresData,Stored> |
| 170 | >::value Funcs; |
| 171 | |
| 172 | void init0 () {} /* Init, when memory is already set to 0. No-op for us. */ |
| 173 | void init () { instance.set_relaxed (nullptr); } |
| 174 | void fini () { do_destroy (instance.get ()); } |
| 175 | |
| 176 | void free_instance () |
| 177 | { |
| 178 | retry: |
| 179 | Stored *p = instance.get (); |
| 180 | if (unlikely (p && !cmpexch (p, nullptr))) |
| 181 | goto retry; |
| 182 | do_destroy (p); |
| 183 | } |
| 184 | |
| 185 | static void do_destroy (Stored *p) |
| 186 | { |
| 187 | if (p && p != const_cast<Stored *> (Funcs::get_null ())) |
| 188 | Funcs::destroy (p); |
| 189 | } |
| 190 | |
| 191 | const Returned * operator -> () const { return get (); } |
| 192 | const Returned & operator * () const { return *get (); } |
| 193 | explicit operator bool () const |
| 194 | { return get_stored () != Funcs::get_null (); } |
| 195 | template <typename C> operator const C * () const { return get (); } |
| 196 | |
| 197 | Stored * get_stored () const |
| 198 | { |
| 199 | retry: |
| 200 | Stored *p = this->instance.get (); |
| 201 | if (unlikely (!p)) |
| 202 | { |
| 203 | if (unlikely (this->is_inert ())) |
| 204 | return const_cast<Stored *> (Funcs::get_null ()); |
| 205 | |
| 206 | p = this->template call_create<Stored, Funcs> (); |
| 207 | if (unlikely (!p)) |
| 208 | p = const_cast<Stored *> (Funcs::get_null ()); |
| 209 | |
| 210 | if (unlikely (!cmpexch (nullptr, p))) |
| 211 | { |
| 212 | do_destroy (p); |
| 213 | goto retry; |
| 214 | } |
| 215 | } |
| 216 | return p; |
| 217 | } |
| 218 | Stored * get_stored_relaxed () const |
| 219 | { |
| 220 | return this->instance.get_relaxed (); |
| 221 | } |
| 222 | |
| 223 | bool cmpexch (Stored *current, Stored *value) const |
| 224 | { |
| 225 | /* This *must* be called when there are no other threads accessing. */ |
| 226 | return this->instance.cmpexch (current, value); |
| 227 | } |
| 228 | |
| 229 | const Returned * get () const { return Funcs::convert (get_stored ()); } |
| 230 | const Returned * get_relaxed () const { return Funcs::convert (get_stored_relaxed ()); } |
| 231 | Returned * get_unconst () const { return const_cast<Returned *> (Funcs::convert (get_stored ())); } |
| 232 | |
| 233 | /* To be possibly overloaded by subclasses. */ |
| 234 | static Returned* convert (Stored *p) { return p; } |
| 235 | |
| 236 | /* By default null/init/fini the object. */ |
| 237 | static const Stored* get_null () { return &Null (Stored); } |
| 238 | static Stored *create (Data *data) |
| 239 | { |
| 240 | Stored *p = (Stored *) calloc (1, sizeof (Stored)); |
| 241 | if (likely (p)) |
| 242 | p->init (data); |
| 243 | return p; |
| 244 | } |
| 245 | static Stored *create () |
| 246 | { |
| 247 | Stored *p = (Stored *) calloc (1, sizeof (Stored)); |
| 248 | if (likely (p)) |
| 249 | p->init (); |
| 250 | return p; |
| 251 | } |
| 252 | static void destroy (Stored *p) |
| 253 | { |
| 254 | p->fini (); |
| 255 | free (p); |
| 256 | } |
| 257 | |
| 258 | // private: |
| 259 | /* Must only have one pointer. */ |
| 260 | hb_atomic_ptr_t<Stored *> instance; |
| 261 | }; |
| 262 | |
| 263 | /* Specializations. */ |
| 264 | |
| 265 | template <typename T, unsigned int WheresFace> |
| 266 | struct hb_face_lazy_loader_t : hb_lazy_loader_t<T, |
| 267 | hb_face_lazy_loader_t<T, WheresFace>, |
| 268 | hb_face_t, WheresFace> {}; |
| 269 | |
| 270 | template <typename T, unsigned int WheresFace> |
| 271 | struct hb_table_lazy_loader_t : hb_lazy_loader_t<T, |
| 272 | hb_table_lazy_loader_t<T, WheresFace>, |
| 273 | hb_face_t, WheresFace, |
| 274 | hb_blob_t> |
| 275 | { |
| 276 | static hb_blob_t *create (hb_face_t *face) |
| 277 | { return hb_sanitize_context_t ().reference_table<T> (face); } |
| 278 | static void destroy (hb_blob_t *p) { hb_blob_destroy (p); } |
| 279 | |
| 280 | static const hb_blob_t *get_null () |
| 281 | { return hb_blob_get_empty (); } |
| 282 | |
| 283 | static const T* convert (const hb_blob_t *blob) |
| 284 | { return blob->as<T> (); } |
| 285 | |
| 286 | hb_blob_t* get_blob () const { return this->get_stored (); } |
| 287 | }; |
| 288 | |
| 289 | template <typename Subclass> |
| 290 | struct hb_font_funcs_lazy_loader_t : hb_lazy_loader_t<hb_font_funcs_t, Subclass> |
| 291 | { |
| 292 | static void destroy (hb_font_funcs_t *p) |
| 293 | { hb_font_funcs_destroy (p); } |
| 294 | static const hb_font_funcs_t *get_null () |
| 295 | { return hb_font_funcs_get_empty (); } |
| 296 | }; |
| 297 | template <typename Subclass> |
| 298 | struct hb_unicode_funcs_lazy_loader_t : hb_lazy_loader_t<hb_unicode_funcs_t, Subclass> |
| 299 | { |
| 300 | static void destroy (hb_unicode_funcs_t *p) |
| 301 | { hb_unicode_funcs_destroy (p); } |
| 302 | static const hb_unicode_funcs_t *get_null () |
| 303 | { return hb_unicode_funcs_get_empty (); } |
| 304 | }; |
| 305 | |
| 306 | |
| 307 | #endif /* HB_MACHINERY_HH */ |
| 308 | |