2 * Copyright © 2018 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
24 * Google Author(s): Behdad Esfahbod
37 /* Global nul-content Null pool. Enlarge as necessary. */
39 #define HB_NULL_POOL_SIZE 9880
41 /* Use SFINAE to sniff whether T has min_size; in which case return T::null_size,
42 * otherwise return sizeof(T). */
45 * https://stackoverflow.com/questions/7776448/sfinae-tried-with-bool-gives-compiler-error-template-argument-tvalue-invol
48 template<bool> struct _hb_bool_type {};
50 template <typename T, typename B>
52 { enum { value = sizeof (T) }; };
54 struct _hb_null_size<T, _hb_bool_type<(bool) (1 + (unsigned int) T::min_size)> >
55 { enum { value = T::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
62 /* These doesn't belong here, but since is copy/paste from above, put it here. */
65 * Returns T::static_size if T::min_size is defined, or sizeof (T) otherwise. */
67 template <typename T, typename B>
68 struct _hb_static_size
69 { enum { value = sizeof (T) }; };
71 struct _hb_static_size<T, _hb_bool_type<(bool) (1 + (unsigned int) T::min_size)> >
72 { enum { value = T::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
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. */
84 template <typename T, typename V, typename B>
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); } };
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; } };
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); }
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)];
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);
112 template <typename QType>
115 typedef typename hb_remove_const (typename hb_remove_reference (QType)) Type;
116 static const Type & get_null () { return Null<Type> (); }
118 #define Null(Type) NullHelper<Type>::get_null ()
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]; \
125 /*static*/ inline const Namespace::Type& Null<Namespace::Type> () { \
126 return *reinterpret_cast<const Namespace::Type *> (_hb_Null_##Namespace##_##Type); \
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]
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; \
137 /*static*/ inline const Type& Null<Type> () { \
138 return _hb_Null_##Type; \
140 static_assert (true, "Just so we take semicolon after.")
141 #define DEFINE_NULL_INSTANCE(Type) \
142 const Type _hb_Null_##Type
144 /* Global writable pool. Enlarge as necessary. */
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. */
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)];
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));
161 template <typename QType>
164 typedef typename hb_remove_const (typename hb_remove_reference (QType)) Type;
165 static Type & get_crap () { return Crap<Type> (); }
167 #define Crap(Type) CrapHelper<Type>::get_crap ()
169 template <typename Type>
170 struct CrapOrNullHelper {
171 static Type & get () { return Crap(Type); }
173 template <typename Type>
174 struct CrapOrNullHelper<const Type> {
175 static const Type & get () { return Null(Type); }
177 #define CrapOrNull(Type) CrapOrNullHelper<Type>::get ()
184 template <typename P>
185 struct hb_nonnull_ptr_t
187 typedef typename hb_remove_pointer (P) T;
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; }
204 #endif /* HB_NULL_HH */