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