• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "hb-meta.hh"
32 
33 
34 /*
35  * Static pools
36  */
37 
38 /* Global nul-content Null pool.  Enlarge as necessary. */
39 
40 #define HB_NULL_POOL_SIZE 384
41 
42 /* Use SFINAE to sniff whether T has min_size; in which case return T::null_size,
43  * otherwise return sizeof(T). */
44 
45 /* The hard way...
46  * https://stackoverflow.com/questions/7776448/sfinae-tried-with-bool-gives-compiler-error-template-argument-tvalue-invol
47  */
48 
49 template <typename T, typename>
50 struct _hb_null_size : hb_integral_constant<unsigned, sizeof (T)> {};
51 template <typename T>
52 struct _hb_null_size<T, hb_void_t<decltype (T::min_size)>> : hb_integral_constant<unsigned, T::null_size> {};
53 
54 template <typename T>
55 using hb_null_size = _hb_null_size<T, void>;
56 #define hb_null_size(T) hb_null_size<T>::value
57 
58 /* These doesn't belong here, but since is copy/paste from above, put it here. */
59 
60 /* hb_static_size (T)
61  * Returns T::static_size if T::min_size is defined, or sizeof (T) otherwise. */
62 
63 template <typename T, typename>
64 struct _hb_static_size : hb_integral_constant<unsigned, sizeof (T)> {};
65 template <typename T>
66 struct _hb_static_size<T, hb_void_t<decltype (T::min_size)>> : hb_integral_constant<unsigned, T::static_size> {};
67 template <typename T>
68 using hb_static_size = _hb_static_size<T, void>;
69 #define hb_static_size(T) hb_static_size<T>::value
70 
71 
72 /*
73  * Null()
74  */
75 
76 extern HB_INTERNAL
77 uint64_t const _hb_NullPool[(HB_NULL_POOL_SIZE + sizeof (uint64_t) - 1) / sizeof (uint64_t)];
78 
79 /* Generic nul-content Null objects. */
80 template <typename Type>
81 struct Null {
get_nullNull82   static Type const & get_null ()
83   {
84     static_assert (hb_null_size (Type) <= HB_NULL_POOL_SIZE, "Increase HB_NULL_POOL_SIZE.");
85     return *reinterpret_cast<Type const *> (_hb_NullPool);
86   }
87 };
88 template <typename QType>
89 struct NullHelper
90 {
91   typedef hb_remove_const<hb_remove_reference<QType>> Type;
get_nullNullHelper92   static const Type & get_null () { return Null<Type>::get_null (); }
93 };
94 #define Null(Type) NullHelper<Type>::get_null ()
95 
96 /* Specializations for arbitrary-content Null objects expressed in bytes. */
97 #define DECLARE_NULL_NAMESPACE_BYTES(Namespace, Type) \
98 	} /* Close namespace. */ \
99 	extern HB_INTERNAL const unsigned char _hb_Null_##Namespace##_##Type[Namespace::Type::null_size]; \
100 	template <> \
101 	struct Null<Namespace::Type> { \
102 	  static Namespace::Type const & get_null () { \
103 	    return *reinterpret_cast<const Namespace::Type *> (_hb_Null_##Namespace##_##Type); \
104 	  } \
105 	}; \
106 	namespace Namespace { \
107 	static_assert (true, "Just so we take semicolon after.")
108 #define DEFINE_NULL_NAMESPACE_BYTES(Namespace, Type) \
109 	const unsigned char _hb_Null_##Namespace##_##Type[Namespace::Type::null_size]
110 
111 /* Specializations for arbitrary-content Null objects expressed as struct initializer. */
112 #define DECLARE_NULL_INSTANCE(Type) \
113 	extern HB_INTERNAL const Type _hb_Null_##Type; \
114 	template <> \
115 	struct Null<Type> { \
116 	  static Type const & get_null () { \
117 	    return _hb_Null_##Type; \
118 	  } \
119 	}; \
120 	static_assert (true, "Just so we take semicolon after.")
121 #define DEFINE_NULL_INSTANCE(Type) \
122 	const Type _hb_Null_##Type
123 
124 /* Global writable pool.  Enlarge as necessary. */
125 
126 /* To be fully correct, CrapPool must be thread_local. However, we do not rely on CrapPool
127  * for correct operation. It only exist to catch and divert program logic bugs instead of
128  * causing bad memory access. So, races there are not actually introducing incorrectness
129  * in the code. Has ~12kb binary size overhead to have it, also clang build fails with it. */
130 extern HB_INTERNAL
131 /*thread_local*/ uint64_t _hb_CrapPool[(HB_NULL_POOL_SIZE + sizeof (uint64_t) - 1) / sizeof (uint64_t)];
132 
133 /* CRAP pool: Common Region for Access Protection. */
134 template <typename Type>
Crap()135 static inline Type& Crap () {
136   static_assert (hb_null_size (Type) <= HB_NULL_POOL_SIZE, "Increase HB_NULL_POOL_SIZE.");
137   Type *obj = reinterpret_cast<Type *> (_hb_CrapPool);
138   memcpy (obj, &Null(Type), sizeof (*obj));
139   return *obj;
140 }
141 template <typename QType>
142 struct CrapHelper
143 {
144   typedef hb_remove_const<hb_remove_reference<QType>> Type;
get_crapCrapHelper145   static Type & get_crap () { return Crap<Type> (); }
146 };
147 #define Crap(Type) CrapHelper<Type>::get_crap ()
148 
149 template <typename Type>
150 struct CrapOrNullHelper {
getCrapOrNullHelper151   static Type & get () { return Crap(Type); }
152 };
153 template <typename Type>
154 struct CrapOrNullHelper<const Type> {
getCrapOrNullHelper155   static const Type & get () { return Null(Type); }
156 };
157 #define CrapOrNull(Type) CrapOrNullHelper<Type>::get ()
158 
159 
160 /*
161  * hb_nonnull_ptr_t
162  */
163 
164 template <typename P>
165 struct hb_nonnull_ptr_t
166 {
167   typedef hb_remove_pointer<P> T;
168 
hb_nonnull_ptr_thb_nonnull_ptr_t169   hb_nonnull_ptr_t (T *v_ = nullptr) : v (v_) {}
operator =hb_nonnull_ptr_t170   T * operator = (T *v_)   { return v = v_; }
operator ->hb_nonnull_ptr_t171   T * operator -> () const { return get (); }
operator *hb_nonnull_ptr_t172   T & operator * () const  { return *get (); }
operator &hb_nonnull_ptr_t173   T ** operator & () const { return &v; }
174   /* Only auto-cast to const types. */
operator const C*hb_nonnull_ptr_t175   template <typename C> operator const C * () const { return get (); }
operator const char*hb_nonnull_ptr_t176   operator const char * () const { return (const char *) get (); }
gethb_nonnull_ptr_t177   T * get () const { return v ? v : const_cast<T *> (&Null(T)); }
get_rawhb_nonnull_ptr_t178   T * get_raw () const { return v; }
179 
180   T *v;
181 };
182 
183 
184 #endif /* HB_NULL_HH */
185