blob: 0d7f4da79e7b1d1216dc3dfa38e63570b7c03b20 [file] [log] [blame]
Behdad Esfahbod25147ff2018-08-06 05:01:52 -07001/*
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
Behdad Esfahbodc77ae402018-08-25 22:36:36 -070030#include "hb.hh"
Behdad Esfahbod8c6cbbd2018-12-28 14:29:09 -050031#include "hb-meta.hh"
Behdad Esfahbod25147ff2018-08-06 05:01:52 -070032
33
34/*
35 * Static pools
36 */
37
38/* Global nul-content Null pool. Enlarge as necessary. */
39
Behdad Esfahbod1db6fdd2022-06-18 14:34:46 -060040#define HB_NULL_POOL_SIZE 448
Behdad Esfahbod25147ff2018-08-06 05:01:52 -070041
Behdad Esfahbode1d2fac2022-07-15 16:02:58 -060042template <typename T, typename>
43struct _hb_has_min_size : hb_false_type {};
44template <typename T>
45struct _hb_has_min_size<T, hb_void_t<decltype (T::min_size)>>
46 : hb_true_type {};
47template <typename T>
48using hb_has_min_size = _hb_has_min_size<T, void>;
49#define hb_has_min_size(T) hb_has_min_size<T>::value
50
51template <typename T, typename>
52struct _hb_has_null_size : hb_false_type {};
53template <typename T>
54struct _hb_has_null_size<T, hb_void_t<decltype (T::null_size)>>
55 : hb_true_type {};
56template <typename T>
57using hb_has_null_size = _hb_has_null_size<T, void>;
58#define hb_has_null_size(T) hb_has_null_size<T>::value
59
Behdad Esfahbod23a28f52021-04-16 13:22:05 -060060/* Use SFINAE to sniff whether T has min_size; in which case return the larger
61 * of sizeof(T) and T::null_size, otherwise return sizeof(T).
62 *
63 * The main purpose of this is to let structs communicate that they are not nullable,
64 * by defining min_size but *not* null_size. */
Behdad Esfahbod2737aa82018-11-22 01:44:27 -050065
66/* The hard way...
67 * https://stackoverflow.com/questions/7776448/sfinae-tried-with-bool-gives-compiler-error-template-argument-tvalue-invol
68 */
Behdad Esfahbod39b9d632018-11-24 00:25:40 -050069
Behdad Esfahbodc0485e32019-05-10 21:03:14 -070070template <typename T, typename>
Behdad Esfahbod5a171ed2019-05-10 20:11:29 -070071struct _hb_null_size : hb_integral_constant<unsigned, sizeof (T)> {};
Behdad Esfahbodf2b91d62018-11-22 01:10:22 -050072template <typename T>
Behdad Esfahbod23a28f52021-04-16 13:22:05 -060073struct _hb_null_size<T, hb_void_t<decltype (T::min_size)>>
74 : hb_integral_constant<unsigned,
75 (sizeof (T) > T::null_size ? sizeof (T) : T::null_size)> {};
Behdad Esfahbodf2b91d62018-11-22 01:10:22 -050076template <typename T>
Behdad Esfahbodc0485e32019-05-10 21:03:14 -070077using hb_null_size = _hb_null_size<T, void>;
Behdad Esfahbodf99abcc2018-11-24 00:22:21 -050078#define hb_null_size(T) hb_null_size<T>::value
Behdad Esfahbodf2b91d62018-11-22 01:10:22 -050079
Behdad Esfahbod5b700742018-12-20 15:38:59 -050080/* These doesn't belong here, but since is copy/paste from above, put it here. */
81
82/* hb_static_size (T)
83 * Returns T::static_size if T::min_size is defined, or sizeof (T) otherwise. */
Behdad Esfahbod39b9d632018-11-24 00:25:40 -050084
Behdad Esfahbodc0485e32019-05-10 21:03:14 -070085template <typename T, typename>
Behdad Esfahbod5a171ed2019-05-10 20:11:29 -070086struct _hb_static_size : hb_integral_constant<unsigned, sizeof (T)> {};
Behdad Esfahbod39b9d632018-11-24 00:25:40 -050087template <typename T>
Behdad Esfahbodc0485e32019-05-10 21:03:14 -070088struct _hb_static_size<T, hb_void_t<decltype (T::min_size)>> : hb_integral_constant<unsigned, T::static_size> {};
Behdad Esfahbod39b9d632018-11-24 00:25:40 -050089template <typename T>
Behdad Esfahbodc0485e32019-05-10 21:03:14 -070090using hb_static_size = _hb_static_size<T, void>;
Behdad Esfahbod39b9d632018-11-24 00:25:40 -050091#define hb_static_size(T) hb_static_size<T>::value
92
Behdad Esfahbod499248c2021-04-16 13:14:48 -060093template <typename T, typename>
94struct _hb_min_size : hb_integral_constant<unsigned, sizeof (T)> {};
95template <typename T>
96struct _hb_min_size<T, hb_void_t<decltype (T::min_size)>> : hb_integral_constant<unsigned, T::min_size> {};
97template <typename T>
98using hb_min_size = _hb_min_size<T, void>;
99#define hb_min_size(T) hb_min_size<T>::value
100
Behdad Esfahbod39b9d632018-11-24 00:25:40 -0500101
102/*
103 * Null()
104 */
Behdad Esfahbod282ce722018-11-29 12:18:14 -0500105
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700106extern HB_INTERNAL
Behdad Esfahbod60653a72019-06-18 13:01:11 -0700107uint64_t const _hb_NullPool[(HB_NULL_POOL_SIZE + sizeof (uint64_t) - 1) / sizeof (uint64_t)];
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700108
109/* Generic nul-content Null objects. */
110template <typename Type>
Behdad Esfahbodec2a5dc2019-03-26 16:18:03 -0700111struct Null {
112 static Type const & get_null ()
113 {
114 static_assert (hb_null_size (Type) <= HB_NULL_POOL_SIZE, "Increase HB_NULL_POOL_SIZE.");
115 return *reinterpret_cast<Type const *> (_hb_NullPool);
116 }
117};
Behdad Esfahbod282ce722018-11-29 12:18:14 -0500118template <typename QType>
119struct NullHelper
120{
Ebrahim Byagowi92588782019-04-30 13:05:10 -0700121 typedef hb_remove_const<hb_remove_reference<QType>> Type;
Behdad Esfahbodec2a5dc2019-03-26 16:18:03 -0700122 static const Type & get_null () { return Null<Type>::get_null (); }
Behdad Esfahbod282ce722018-11-29 12:18:14 -0500123};
124#define Null(Type) NullHelper<Type>::get_null ()
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700125
Bruce Mitchener257d0e52018-10-19 22:49:21 +0700126/* Specializations for arbitrary-content Null objects expressed in bytes. */
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700127#define DECLARE_NULL_NAMESPACE_BYTES(Namespace, Type) \
Behdad Esfahbod0d160d52018-09-03 20:50:11 -0700128 } /* Close namespace. */ \
Behdad Esfahbod8df9aba2022-05-26 03:59:21 -0600129 extern HB_INTERNAL const unsigned char _hb_Null_##Namespace##_##Type[hb_null_size (Namespace::Type)]; \
Behdad Esfahbod0d160d52018-09-03 20:50:11 -0700130 template <> \
Behdad Esfahbodec2a5dc2019-03-26 16:18:03 -0700131 struct Null<Namespace::Type> { \
132 static Namespace::Type const & get_null () { \
133 return *reinterpret_cast<const Namespace::Type *> (_hb_Null_##Namespace##_##Type); \
134 } \
135 }; \
Behdad Esfahbod0d160d52018-09-03 20:50:11 -0700136 namespace Namespace { \
Behdad Esfahbodbd8aa1b2020-04-21 22:19:46 -0700137 static_assert (true, "") /* Require semicolon after. */
Behdad Esfahbod1e503f52022-07-05 15:44:58 -0600138#define DECLARE_NULL_NAMESPACE_BYTES_TEMPLATE1(Namespace, Type, Size) \
139 } /* Close namespace. */ \
140 extern HB_INTERNAL const unsigned char _hb_Null_##Namespace##_##Type[Size]; \
141 template <typename Spec> \
142 struct Null<Namespace::Type<Spec>> { \
143 static Namespace::Type<Spec> const & get_null () { \
144 return *reinterpret_cast<const Namespace::Type<Spec> *> (_hb_Null_##Namespace##_##Type); \
145 } \
146 }; \
147 namespace Namespace { \
148 static_assert (true, "") /* Require semicolon after. */
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700149#define DEFINE_NULL_NAMESPACE_BYTES(Namespace, Type) \
Behdad Esfahbod1e503f52022-07-05 15:44:58 -0600150 const unsigned char _hb_Null_##Namespace##_##Type[sizeof (_hb_Null_##Namespace##_##Type)]
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700151
Bruce Mitchener257d0e52018-10-19 22:49:21 +0700152/* Specializations for arbitrary-content Null objects expressed as struct initializer. */
Behdad Esfahbod35066722018-08-06 06:17:48 -0700153#define DECLARE_NULL_INSTANCE(Type) \
Behdad Esfahbod0d160d52018-09-03 20:50:11 -0700154 extern HB_INTERNAL const Type _hb_Null_##Type; \
155 template <> \
Behdad Esfahbodec2a5dc2019-03-26 16:18:03 -0700156 struct Null<Type> { \
157 static Type const & get_null () { \
158 return _hb_Null_##Type; \
159 } \
160 }; \
Behdad Esfahbodbd8aa1b2020-04-21 22:19:46 -0700161 static_assert (true, "") /* Require semicolon after. */
Behdad Esfahbod35066722018-08-06 06:17:48 -0700162#define DEFINE_NULL_INSTANCE(Type) \
Behdad Esfahbod0d160d52018-09-03 20:50:11 -0700163 const Type _hb_Null_##Type
164
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700165/* Global writable pool. Enlarge as necessary. */
166
167/* To be fully correct, CrapPool must be thread_local. However, we do not rely on CrapPool
168 * for correct operation. It only exist to catch and divert program logic bugs instead of
169 * causing bad memory access. So, races there are not actually introducing incorrectness
170 * in the code. Has ~12kb binary size overhead to have it, also clang build fails with it. */
171extern HB_INTERNAL
Behdad Esfahbod60653a72019-06-18 13:01:11 -0700172/*thread_local*/ uint64_t _hb_CrapPool[(HB_NULL_POOL_SIZE + sizeof (uint64_t) - 1) / sizeof (uint64_t)];
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700173
174/* CRAP pool: Common Region for Access Protection. */
175template <typename Type>
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330176static inline Type& Crap () {
Behdad Esfahbodf99abcc2018-11-24 00:22:21 -0500177 static_assert (hb_null_size (Type) <= HB_NULL_POOL_SIZE, "Increase HB_NULL_POOL_SIZE.");
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700178 Type *obj = reinterpret_cast<Type *> (_hb_CrapPool);
Ebrahim Byagowi2dda6dd2020-04-20 14:12:45 +0430179 memcpy (obj, &Null (Type), sizeof (*obj));
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700180 return *obj;
181}
Behdad Esfahbod282ce722018-11-29 12:18:14 -0500182template <typename QType>
183struct CrapHelper
184{
Ebrahim Byagowi92588782019-04-30 13:05:10 -0700185 typedef hb_remove_const<hb_remove_reference<QType>> Type;
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330186 static Type & get_crap () { return Crap<Type> (); }
Behdad Esfahbod282ce722018-11-29 12:18:14 -0500187};
188#define Crap(Type) CrapHelper<Type>::get_crap ()
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700189
190template <typename Type>
Behdad Esfahbod282ce722018-11-29 12:18:14 -0500191struct CrapOrNullHelper {
Ebrahim Byagowi2dda6dd2020-04-20 14:12:45 +0430192 static Type & get () { return Crap (Type); }
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700193};
194template <typename Type>
Behdad Esfahbod282ce722018-11-29 12:18:14 -0500195struct CrapOrNullHelper<const Type> {
Ebrahim Byagowi2dda6dd2020-04-20 14:12:45 +0430196 static const Type & get () { return Null (Type); }
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700197};
Behdad Esfahbod282ce722018-11-29 12:18:14 -0500198#define CrapOrNull(Type) CrapOrNullHelper<Type>::get ()
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700199
200
Behdad Esfahbodfb0f30f2018-11-03 15:24:14 -0400201/*
202 * hb_nonnull_ptr_t
203 */
204
205template <typename P>
206struct hb_nonnull_ptr_t
207{
Behdad Esfahbod54ece292019-04-16 16:45:53 -0400208 typedef hb_remove_pointer<P> T;
Behdad Esfahbodfb0f30f2018-11-03 15:24:14 -0400209
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330210 hb_nonnull_ptr_t (T *v_ = nullptr) : v (v_) {}
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330211 T * operator = (T *v_) { return v = v_; }
212 T * operator -> () const { return get (); }
213 T & operator * () const { return *get (); }
214 T ** operator & () const { return &v; }
Behdad Esfahbod0da22fb2018-11-05 13:13:39 -0500215 /* Only auto-cast to const types. */
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330216 template <typename C> operator const C * () const { return get (); }
217 operator const char * () const { return (const char *) get (); }
Ebrahim Byagowi2dda6dd2020-04-20 14:12:45 +0430218 T * get () const { return v ? v : const_cast<T *> (&Null (T)); }
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330219 T * get_raw () const { return v; }
Behdad Esfahbodfb0f30f2018-11-03 15:24:14 -0400220
Behdad Esfahbod53806e52020-11-25 11:51:37 -0700221 private:
Behdad Esfahbodfb0f30f2018-11-03 15:24:14 -0400222 T *v;
223};
224
225
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700226#endif /* HB_NULL_HH */