Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright  2018  Google, Inc.
      3  *
      4  *  This is part of HarfBuzz, a text shaping library.
      5  *
      6  * Permission is hereby granted, without written agreement and without
      7  * license or royalty fees, to use, copy, modify, and distribute this
      8  * software and its documentation for any purpose, provided that the
      9  * above copyright notice and the following two paragraphs appear in
     10  * all copies of this software.
     11  *
     12  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
     13  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
     14  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
     15  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
     16  * DAMAGE.
     17  *
     18  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
     19  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
     20  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
     21  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
     22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
     23  *
     24  * Google Author(s): Behdad Esfahbod
     25  */
     26 
     27 #ifndef HB_NULL_HH
     28 #define HB_NULL_HH
     29 
     30 #include "hb.hh"
     31 
     32 
     33 /*
     34  * Static pools
     35  */
     36 
     37 /* Global nul-content Null pool.  Enlarge as necessary. */
     38 
     39 #define HB_NULL_POOL_SIZE 9880
     40 
     41 /* Use SFINAE to sniff whether T has min_size; in which case return T::null_size,
     42  * otherwise return sizeof(T). */
     43 
     44 /* The hard way...
     45  * https://stackoverflow.com/questions/7776448/sfinae-tried-with-bool-gives-compiler-error-template-argument-tvalue-invol
     46  */
     47 
     48 template<bool> struct _hb_bool_type {};
     49 
     50 template <typename T, typename B>
     51 struct _hb_null_size
     52 { enum { value = sizeof (T) }; };
     53 template <typename T>
     54 struct _hb_null_size<T, _hb_bool_type<(bool) (1 + (unsigned int) T::min_size)> >
     55 { enum { value = T::null_size }; };
     56 
     57 template <typename T>
     58 struct hb_null_size
     59 { enum { value = _hb_null_size<T, _hb_bool_type<true> >::value }; };
     60 #define hb_null_size(T) hb_null_size<T>::value
     61 
     62 /* These doesn't belong here, but since is copy/paste from above, put it here. */
     63 
     64 /* hb_static_size (T)
     65  * Returns T::static_size if T::min_size is defined, or sizeof (T) otherwise. */
     66 
     67 template <typename T, typename B>
     68 struct _hb_static_size
     69 { enum { value = sizeof (T) }; };
     70 template <typename T>
     71 struct _hb_static_size<T, _hb_bool_type<(bool) (1 + (unsigned int) T::min_size)> >
     72 { enum { value = T::static_size }; };
     73 
     74 template <typename T>
     75 struct hb_static_size
     76 { enum { value = _hb_static_size<T, _hb_bool_type<true> >::value }; };
     77 #define hb_static_size(T) hb_static_size<T>::value
     78 
     79 
     80 /* hb_assign (obj, value)
     81  * Calls obj.set (value) if obj.min_size is defined and value has different type
     82  * from obj, or obj = v otherwise. */
     83 
     84 template <typename T, typename V, typename B>
     85 struct _hb_assign
     86 { static inline void value (T &o, const V v) { o = v; } };
     87 template <typename T, typename V>
     88 struct _hb_assign<T, V, _hb_bool_type<(bool) (1 + (unsigned int) T::min_size)> >
     89 { static inline void value (T &o, const V v) { o.set (v); } };
     90 template <typename T>
     91 struct _hb_assign<T, T, _hb_bool_type<(bool) (1 + (unsigned int) T::min_size)> >
     92 { static inline void value (T &o, const T v) { o = v; } };
     93 
     94 template <typename T, typename V>
     95 static inline void hb_assign (T &o, const V v)
     96 { _hb_assign<T, V, _hb_bool_type<true> >::value (o, v); };
     97 
     98 
     99 /*
    100  * Null()
    101  */
    102 
    103 extern HB_INTERNAL
    104 hb_vector_size_impl_t const _hb_NullPool[(HB_NULL_POOL_SIZE + sizeof (hb_vector_size_impl_t) - 1) / sizeof (hb_vector_size_impl_t)];
    105 
    106 /* Generic nul-content Null objects. */
    107 template <typename Type>
    108 static inline Type const & Null () {
    109   static_assert (hb_null_size (Type) <= HB_NULL_POOL_SIZE, "Increase HB_NULL_POOL_SIZE.");
    110   return *reinterpret_cast<Type const *> (_hb_NullPool);
    111 }
    112 template <typename QType>
    113 struct NullHelper
    114 {
    115   typedef typename hb_remove_const (typename hb_remove_reference (QType)) Type;
    116   static const Type & get_null () { return Null<Type> (); }
    117 };
    118 #define Null(Type) NullHelper<Type>::get_null ()
    119 
    120 /* Specializations for arbitrary-content Null objects expressed in bytes. */
    121 #define DECLARE_NULL_NAMESPACE_BYTES(Namespace, Type) \
    122 	} /* Close namespace. */ \
    123 	extern HB_INTERNAL const unsigned char _hb_Null_##Namespace##_##Type[Namespace::Type::null_size]; \
    124 	template <> \
    125 	/*static*/ inline const Namespace::Type& Null<Namespace::Type> () { \
    126 	  return *reinterpret_cast<const Namespace::Type *> (_hb_Null_##Namespace##_##Type); \
    127 	} \
    128 	namespace Namespace { \
    129 	static_assert (true, "Just so we take semicolon after.")
    130 #define DEFINE_NULL_NAMESPACE_BYTES(Namespace, Type) \
    131 	const unsigned char _hb_Null_##Namespace##_##Type[Namespace::Type::null_size]
    132 
    133 /* Specializations for arbitrary-content Null objects expressed as struct initializer. */
    134 #define DECLARE_NULL_INSTANCE(Type) \
    135 	extern HB_INTERNAL const Type _hb_Null_##Type; \
    136 	template <> \
    137 	/*static*/ inline const Type& Null<Type> () { \
    138 	  return _hb_Null_##Type; \
    139 	} \
    140 static_assert (true, "Just so we take semicolon after.")
    141 #define DEFINE_NULL_INSTANCE(Type) \
    142 	const Type _hb_Null_##Type
    143 
    144 /* Global writable pool.  Enlarge as necessary. */
    145 
    146 /* To be fully correct, CrapPool must be thread_local. However, we do not rely on CrapPool
    147  * for correct operation. It only exist to catch and divert program logic bugs instead of
    148  * causing bad memory access. So, races there are not actually introducing incorrectness
    149  * in the code. Has ~12kb binary size overhead to have it, also clang build fails with it. */
    150 extern HB_INTERNAL
    151 /*thread_local*/ hb_vector_size_impl_t _hb_CrapPool[(HB_NULL_POOL_SIZE + sizeof (hb_vector_size_impl_t) - 1) / sizeof (hb_vector_size_impl_t)];
    152 
    153 /* CRAP pool: Common Region for Access Protection. */
    154 template <typename Type>
    155 static inline Type& Crap () {
    156   static_assert (hb_null_size (Type) <= HB_NULL_POOL_SIZE, "Increase HB_NULL_POOL_SIZE.");
    157   Type *obj = reinterpret_cast<Type *> (_hb_CrapPool);
    158   memcpy (obj, &Null(Type), sizeof (*obj));
    159   return *obj;
    160 }
    161 template <typename QType>
    162 struct CrapHelper
    163 {
    164   typedef typename hb_remove_const (typename hb_remove_reference (QType)) Type;
    165   static Type & get_crap () { return Crap<Type> (); }
    166 };
    167 #define Crap(Type) CrapHelper<Type>::get_crap ()
    168 
    169 template <typename Type>
    170 struct CrapOrNullHelper {
    171   static Type & get () { return Crap(Type); }
    172 };
    173 template <typename Type>
    174 struct CrapOrNullHelper<const Type> {
    175   static const Type & get () { return Null(Type); }
    176 };
    177 #define CrapOrNull(Type) CrapOrNullHelper<Type>::get ()
    178 
    179 
    180 /*
    181  * hb_nonnull_ptr_t
    182  */
    183 
    184 template <typename P>
    185 struct hb_nonnull_ptr_t
    186 {
    187   typedef typename hb_remove_pointer (P) T;
    188 
    189   hb_nonnull_ptr_t (T *v_ = nullptr) : v (v_) {}
    190   T * operator = (T *v_)   { return v = v_; }
    191   T * operator -> () const { return get (); }
    192   T & operator * () const  { return *get (); }
    193   T ** operator & () const { return &v; }
    194   /* Only auto-cast to const types. */
    195   template <typename C> operator const C * () const { return get (); }
    196   operator const char * () const { return (const char *) get (); }
    197   T * get () const { return v ? v : const_cast<T *> (&Null(T)); }
    198   T * get_raw () const { return v; }
    199 
    200   T *v;
    201 };
    202 
    203 
    204 #endif /* HB_NULL_HH */
    205