Behdad Esfahbod | 25147ff | 2018-08-06 05:01:52 -0700 | [diff] [blame] | 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 | |
Behdad Esfahbod | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 30 | #include "hb.hh" |
Behdad Esfahbod | 8c6cbbd | 2018-12-28 14:29:09 -0500 | [diff] [blame] | 31 | #include "hb-meta.hh" |
Behdad Esfahbod | 25147ff | 2018-08-06 05:01:52 -0700 | [diff] [blame] | 32 | |
| 33 | |
| 34 | /* |
| 35 | * Static pools |
| 36 | */ |
| 37 | |
| 38 | /* Global nul-content Null pool. Enlarge as necessary. */ |
| 39 | |
Behdad Esfahbod | 155e92f | 2019-04-16 11:35:09 -0400 | [diff] [blame] | 40 | #define HB_NULL_POOL_SIZE 384 |
Behdad Esfahbod | 25147ff | 2018-08-06 05:01:52 -0700 | [diff] [blame] | 41 | |
Behdad Esfahbod | 23a28f5 | 2021-04-16 13:22:05 -0600 | [diff] [blame] | 42 | /* Use SFINAE to sniff whether T has min_size; in which case return the larger |
| 43 | * of sizeof(T) and T::null_size, otherwise return sizeof(T). |
| 44 | * |
| 45 | * The main purpose of this is to let structs communicate that they are not nullable, |
| 46 | * by defining min_size but *not* null_size. */ |
Behdad Esfahbod | 2737aa8 | 2018-11-22 01:44:27 -0500 | [diff] [blame] | 47 | |
| 48 | /* The hard way... |
| 49 | * https://stackoverflow.com/questions/7776448/sfinae-tried-with-bool-gives-compiler-error-template-argument-tvalue-invol |
| 50 | */ |
Behdad Esfahbod | 39b9d63 | 2018-11-24 00:25:40 -0500 | [diff] [blame] | 51 | |
Behdad Esfahbod | c0485e3 | 2019-05-10 21:03:14 -0700 | [diff] [blame] | 52 | template <typename T, typename> |
Behdad Esfahbod | 5a171ed | 2019-05-10 20:11:29 -0700 | [diff] [blame] | 53 | struct _hb_null_size : hb_integral_constant<unsigned, sizeof (T)> {}; |
Behdad Esfahbod | f2b91d6 | 2018-11-22 01:10:22 -0500 | [diff] [blame] | 54 | template <typename T> |
Behdad Esfahbod | 23a28f5 | 2021-04-16 13:22:05 -0600 | [diff] [blame] | 55 | struct _hb_null_size<T, hb_void_t<decltype (T::min_size)>> |
| 56 | : hb_integral_constant<unsigned, |
| 57 | (sizeof (T) > T::null_size ? sizeof (T) : T::null_size)> {}; |
Behdad Esfahbod | f2b91d6 | 2018-11-22 01:10:22 -0500 | [diff] [blame] | 58 | template <typename T> |
Behdad Esfahbod | c0485e3 | 2019-05-10 21:03:14 -0700 | [diff] [blame] | 59 | using hb_null_size = _hb_null_size<T, void>; |
Behdad Esfahbod | f99abcc | 2018-11-24 00:22:21 -0500 | [diff] [blame] | 60 | #define hb_null_size(T) hb_null_size<T>::value |
Behdad Esfahbod | f2b91d6 | 2018-11-22 01:10:22 -0500 | [diff] [blame] | 61 | |
Behdad Esfahbod | 5b70074 | 2018-12-20 15:38:59 -0500 | [diff] [blame] | 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. */ |
Behdad Esfahbod | 39b9d63 | 2018-11-24 00:25:40 -0500 | [diff] [blame] | 66 | |
Behdad Esfahbod | c0485e3 | 2019-05-10 21:03:14 -0700 | [diff] [blame] | 67 | template <typename T, typename> |
Behdad Esfahbod | 5a171ed | 2019-05-10 20:11:29 -0700 | [diff] [blame] | 68 | struct _hb_static_size : hb_integral_constant<unsigned, sizeof (T)> {}; |
Behdad Esfahbod | 39b9d63 | 2018-11-24 00:25:40 -0500 | [diff] [blame] | 69 | template <typename T> |
Behdad Esfahbod | c0485e3 | 2019-05-10 21:03:14 -0700 | [diff] [blame] | 70 | struct _hb_static_size<T, hb_void_t<decltype (T::min_size)>> : hb_integral_constant<unsigned, T::static_size> {}; |
Behdad Esfahbod | 39b9d63 | 2018-11-24 00:25:40 -0500 | [diff] [blame] | 71 | template <typename T> |
Behdad Esfahbod | c0485e3 | 2019-05-10 21:03:14 -0700 | [diff] [blame] | 72 | using hb_static_size = _hb_static_size<T, void>; |
Behdad Esfahbod | 39b9d63 | 2018-11-24 00:25:40 -0500 | [diff] [blame] | 73 | #define hb_static_size(T) hb_static_size<T>::value |
| 74 | |
Behdad Esfahbod | 499248c | 2021-04-16 13:14:48 -0600 | [diff] [blame] | 75 | template <typename T, typename> |
| 76 | struct _hb_min_size : hb_integral_constant<unsigned, sizeof (T)> {}; |
| 77 | template <typename T> |
| 78 | struct _hb_min_size<T, hb_void_t<decltype (T::min_size)>> : hb_integral_constant<unsigned, T::min_size> {}; |
| 79 | template <typename T> |
| 80 | using hb_min_size = _hb_min_size<T, void>; |
| 81 | #define hb_min_size(T) hb_min_size<T>::value |
| 82 | |
Behdad Esfahbod | 39b9d63 | 2018-11-24 00:25:40 -0500 | [diff] [blame] | 83 | |
| 84 | /* |
| 85 | * Null() |
| 86 | */ |
Behdad Esfahbod | 282ce72 | 2018-11-29 12:18:14 -0500 | [diff] [blame] | 87 | |
Behdad Esfahbod | 25147ff | 2018-08-06 05:01:52 -0700 | [diff] [blame] | 88 | extern HB_INTERNAL |
Behdad Esfahbod | 60653a7 | 2019-06-18 13:01:11 -0700 | [diff] [blame] | 89 | uint64_t const _hb_NullPool[(HB_NULL_POOL_SIZE + sizeof (uint64_t) - 1) / sizeof (uint64_t)]; |
Behdad Esfahbod | 25147ff | 2018-08-06 05:01:52 -0700 | [diff] [blame] | 90 | |
| 91 | /* Generic nul-content Null objects. */ |
| 92 | template <typename Type> |
Behdad Esfahbod | ec2a5dc | 2019-03-26 16:18:03 -0700 | [diff] [blame] | 93 | struct Null { |
| 94 | static Type const & get_null () |
| 95 | { |
| 96 | static_assert (hb_null_size (Type) <= HB_NULL_POOL_SIZE, "Increase HB_NULL_POOL_SIZE."); |
| 97 | return *reinterpret_cast<Type const *> (_hb_NullPool); |
| 98 | } |
| 99 | }; |
Behdad Esfahbod | 282ce72 | 2018-11-29 12:18:14 -0500 | [diff] [blame] | 100 | template <typename QType> |
| 101 | struct NullHelper |
| 102 | { |
Ebrahim Byagowi | 9258878 | 2019-04-30 13:05:10 -0700 | [diff] [blame] | 103 | typedef hb_remove_const<hb_remove_reference<QType>> Type; |
Behdad Esfahbod | ec2a5dc | 2019-03-26 16:18:03 -0700 | [diff] [blame] | 104 | static const Type & get_null () { return Null<Type>::get_null (); } |
Behdad Esfahbod | 282ce72 | 2018-11-29 12:18:14 -0500 | [diff] [blame] | 105 | }; |
| 106 | #define Null(Type) NullHelper<Type>::get_null () |
Behdad Esfahbod | 25147ff | 2018-08-06 05:01:52 -0700 | [diff] [blame] | 107 | |
Bruce Mitchener | 257d0e5 | 2018-10-19 22:49:21 +0700 | [diff] [blame] | 108 | /* Specializations for arbitrary-content Null objects expressed in bytes. */ |
Behdad Esfahbod | 25147ff | 2018-08-06 05:01:52 -0700 | [diff] [blame] | 109 | #define DECLARE_NULL_NAMESPACE_BYTES(Namespace, Type) \ |
Behdad Esfahbod | 0d160d5 | 2018-09-03 20:50:11 -0700 | [diff] [blame] | 110 | } /* Close namespace. */ \ |
Behdad Esfahbod | 8d77887 | 2018-11-21 23:46:09 -0500 | [diff] [blame] | 111 | extern HB_INTERNAL const unsigned char _hb_Null_##Namespace##_##Type[Namespace::Type::null_size]; \ |
Behdad Esfahbod | 0d160d5 | 2018-09-03 20:50:11 -0700 | [diff] [blame] | 112 | template <> \ |
Behdad Esfahbod | ec2a5dc | 2019-03-26 16:18:03 -0700 | [diff] [blame] | 113 | struct Null<Namespace::Type> { \ |
| 114 | static Namespace::Type const & get_null () { \ |
| 115 | return *reinterpret_cast<const Namespace::Type *> (_hb_Null_##Namespace##_##Type); \ |
| 116 | } \ |
| 117 | }; \ |
Behdad Esfahbod | 0d160d5 | 2018-09-03 20:50:11 -0700 | [diff] [blame] | 118 | namespace Namespace { \ |
Behdad Esfahbod | bd8aa1b | 2020-04-21 22:19:46 -0700 | [diff] [blame] | 119 | static_assert (true, "") /* Require semicolon after. */ |
Behdad Esfahbod | 25147ff | 2018-08-06 05:01:52 -0700 | [diff] [blame] | 120 | #define DEFINE_NULL_NAMESPACE_BYTES(Namespace, Type) \ |
Behdad Esfahbod | 8d77887 | 2018-11-21 23:46:09 -0500 | [diff] [blame] | 121 | const unsigned char _hb_Null_##Namespace##_##Type[Namespace::Type::null_size] |
Behdad Esfahbod | 25147ff | 2018-08-06 05:01:52 -0700 | [diff] [blame] | 122 | |
Bruce Mitchener | 257d0e5 | 2018-10-19 22:49:21 +0700 | [diff] [blame] | 123 | /* Specializations for arbitrary-content Null objects expressed as struct initializer. */ |
Behdad Esfahbod | 3506672 | 2018-08-06 06:17:48 -0700 | [diff] [blame] | 124 | #define DECLARE_NULL_INSTANCE(Type) \ |
Behdad Esfahbod | 0d160d5 | 2018-09-03 20:50:11 -0700 | [diff] [blame] | 125 | extern HB_INTERNAL const Type _hb_Null_##Type; \ |
| 126 | template <> \ |
Behdad Esfahbod | ec2a5dc | 2019-03-26 16:18:03 -0700 | [diff] [blame] | 127 | struct Null<Type> { \ |
| 128 | static Type const & get_null () { \ |
| 129 | return _hb_Null_##Type; \ |
| 130 | } \ |
| 131 | }; \ |
Behdad Esfahbod | bd8aa1b | 2020-04-21 22:19:46 -0700 | [diff] [blame] | 132 | static_assert (true, "") /* Require semicolon after. */ |
Behdad Esfahbod | 3506672 | 2018-08-06 06:17:48 -0700 | [diff] [blame] | 133 | #define DEFINE_NULL_INSTANCE(Type) \ |
Behdad Esfahbod | 0d160d5 | 2018-09-03 20:50:11 -0700 | [diff] [blame] | 134 | const Type _hb_Null_##Type |
| 135 | |
Behdad Esfahbod | 25147ff | 2018-08-06 05:01:52 -0700 | [diff] [blame] | 136 | /* Global writable pool. Enlarge as necessary. */ |
| 137 | |
| 138 | /* To be fully correct, CrapPool must be thread_local. However, we do not rely on CrapPool |
| 139 | * for correct operation. It only exist to catch and divert program logic bugs instead of |
| 140 | * causing bad memory access. So, races there are not actually introducing incorrectness |
| 141 | * in the code. Has ~12kb binary size overhead to have it, also clang build fails with it. */ |
| 142 | extern HB_INTERNAL |
Behdad Esfahbod | 60653a7 | 2019-06-18 13:01:11 -0700 | [diff] [blame] | 143 | /*thread_local*/ uint64_t _hb_CrapPool[(HB_NULL_POOL_SIZE + sizeof (uint64_t) - 1) / sizeof (uint64_t)]; |
Behdad Esfahbod | 25147ff | 2018-08-06 05:01:52 -0700 | [diff] [blame] | 144 | |
| 145 | /* CRAP pool: Common Region for Access Protection. */ |
| 146 | template <typename Type> |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 147 | static inline Type& Crap () { |
Behdad Esfahbod | f99abcc | 2018-11-24 00:22:21 -0500 | [diff] [blame] | 148 | static_assert (hb_null_size (Type) <= HB_NULL_POOL_SIZE, "Increase HB_NULL_POOL_SIZE."); |
Behdad Esfahbod | 25147ff | 2018-08-06 05:01:52 -0700 | [diff] [blame] | 149 | Type *obj = reinterpret_cast<Type *> (_hb_CrapPool); |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 150 | memcpy (obj, &Null (Type), sizeof (*obj)); |
Behdad Esfahbod | 25147ff | 2018-08-06 05:01:52 -0700 | [diff] [blame] | 151 | return *obj; |
| 152 | } |
Behdad Esfahbod | 282ce72 | 2018-11-29 12:18:14 -0500 | [diff] [blame] | 153 | template <typename QType> |
| 154 | struct CrapHelper |
| 155 | { |
Ebrahim Byagowi | 9258878 | 2019-04-30 13:05:10 -0700 | [diff] [blame] | 156 | typedef hb_remove_const<hb_remove_reference<QType>> Type; |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 157 | static Type & get_crap () { return Crap<Type> (); } |
Behdad Esfahbod | 282ce72 | 2018-11-29 12:18:14 -0500 | [diff] [blame] | 158 | }; |
| 159 | #define Crap(Type) CrapHelper<Type>::get_crap () |
Behdad Esfahbod | 25147ff | 2018-08-06 05:01:52 -0700 | [diff] [blame] | 160 | |
| 161 | template <typename Type> |
Behdad Esfahbod | 282ce72 | 2018-11-29 12:18:14 -0500 | [diff] [blame] | 162 | struct CrapOrNullHelper { |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 163 | static Type & get () { return Crap (Type); } |
Behdad Esfahbod | 25147ff | 2018-08-06 05:01:52 -0700 | [diff] [blame] | 164 | }; |
| 165 | template <typename Type> |
Behdad Esfahbod | 282ce72 | 2018-11-29 12:18:14 -0500 | [diff] [blame] | 166 | struct CrapOrNullHelper<const Type> { |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 167 | static const Type & get () { return Null (Type); } |
Behdad Esfahbod | 25147ff | 2018-08-06 05:01:52 -0700 | [diff] [blame] | 168 | }; |
Behdad Esfahbod | 282ce72 | 2018-11-29 12:18:14 -0500 | [diff] [blame] | 169 | #define CrapOrNull(Type) CrapOrNullHelper<Type>::get () |
Behdad Esfahbod | 25147ff | 2018-08-06 05:01:52 -0700 | [diff] [blame] | 170 | |
| 171 | |
Behdad Esfahbod | fb0f30f | 2018-11-03 15:24:14 -0400 | [diff] [blame] | 172 | /* |
| 173 | * hb_nonnull_ptr_t |
| 174 | */ |
| 175 | |
| 176 | template <typename P> |
| 177 | struct hb_nonnull_ptr_t |
| 178 | { |
Behdad Esfahbod | 54ece29 | 2019-04-16 16:45:53 -0400 | [diff] [blame] | 179 | typedef hb_remove_pointer<P> T; |
Behdad Esfahbod | fb0f30f | 2018-11-03 15:24:14 -0400 | [diff] [blame] | 180 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 181 | hb_nonnull_ptr_t (T *v_ = nullptr) : v (v_) {} |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 182 | T * operator = (T *v_) { return v = v_; } |
| 183 | T * operator -> () const { return get (); } |
| 184 | T & operator * () const { return *get (); } |
| 185 | T ** operator & () const { return &v; } |
Behdad Esfahbod | 0da22fb | 2018-11-05 13:13:39 -0500 | [diff] [blame] | 186 | /* Only auto-cast to const types. */ |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 187 | template <typename C> operator const C * () const { return get (); } |
| 188 | operator const char * () const { return (const char *) get (); } |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 189 | T * get () const { return v ? v : const_cast<T *> (&Null (T)); } |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 190 | T * get_raw () const { return v; } |
Behdad Esfahbod | fb0f30f | 2018-11-03 15:24:14 -0400 | [diff] [blame] | 191 | |
Behdad Esfahbod | 53806e5 | 2020-11-25 11:51:37 -0700 | [diff] [blame] | 192 | private: |
Behdad Esfahbod | fb0f30f | 2018-11-03 15:24:14 -0400 | [diff] [blame] | 193 | T *v; |
| 194 | }; |
| 195 | |
| 196 | |
Behdad Esfahbod | 25147ff | 2018-08-06 05:01:52 -0700 | [diff] [blame] | 197 | #endif /* HB_NULL_HH */ |