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 | 25147ff | 2018-08-06 05:01:52 -0700 | [diff] [blame] | 31 | |
| 32 | |
| 33 | /* |
| 34 | * Static pools |
| 35 | */ |
| 36 | |
| 37 | /* Global nul-content Null pool. Enlarge as necessary. */ |
| 38 | |
Michiharu Ariza | 161ece4 | 2018-08-06 11:23:50 -0700 | [diff] [blame] | 39 | #define HB_NULL_POOL_SIZE 800 |
Behdad Esfahbod | 25147ff | 2018-08-06 05:01:52 -0700 | [diff] [blame] | 40 | |
| 41 | extern HB_INTERNAL |
| 42 | 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)]; |
| 43 | |
| 44 | /* Generic nul-content Null objects. */ |
| 45 | template <typename Type> |
| 46 | static inline Type const & Null (void) { |
| 47 | static_assert (sizeof (Type) <= HB_NULL_POOL_SIZE, "Increase HB_NULL_POOL_SIZE."); |
| 48 | return *reinterpret_cast<Type const *> (_hb_NullPool); |
| 49 | } |
| 50 | #define Null(Type) Null<Type>() |
| 51 | |
Behdad Esfahbod | f9cfa5c | 2018-08-06 05:29:15 -0700 | [diff] [blame] | 52 | /* Specializaitons for arbitrary-content Null objects expressed in bytes. */ |
Behdad Esfahbod | 25147ff | 2018-08-06 05:01:52 -0700 | [diff] [blame] | 53 | #define DECLARE_NULL_NAMESPACE_BYTES(Namespace, Type) \ |
Behdad Esfahbod | 0d160d5 | 2018-09-03 20:50:11 -0700 | [diff] [blame] | 54 | } /* Close namespace. */ \ |
| 55 | extern HB_INTERNAL const unsigned char _hb_Null_##Namespace##_##Type[Namespace::Type::min_size]; \ |
| 56 | template <> \ |
| 57 | /*static*/ inline const Namespace::Type& Null<Namespace::Type> (void) { \ |
| 58 | return *reinterpret_cast<const Namespace::Type *> (_hb_Null_##Namespace##_##Type); \ |
| 59 | } \ |
| 60 | namespace Namespace { \ |
| 61 | static_assert (true, "Just so we take semicolon after.") |
Behdad Esfahbod | 25147ff | 2018-08-06 05:01:52 -0700 | [diff] [blame] | 62 | #define DEFINE_NULL_NAMESPACE_BYTES(Namespace, Type) \ |
Behdad Esfahbod | 0d160d5 | 2018-09-03 20:50:11 -0700 | [diff] [blame] | 63 | const unsigned char _hb_Null_##Namespace##_##Type[Namespace::Type::min_size] |
Behdad Esfahbod | 25147ff | 2018-08-06 05:01:52 -0700 | [diff] [blame] | 64 | |
Behdad Esfahbod | 3506672 | 2018-08-06 06:17:48 -0700 | [diff] [blame] | 65 | /* Specializaitons for arbitrary-content Null objects expressed as struct initializer. */ |
| 66 | #define DECLARE_NULL_INSTANCE(Type) \ |
Behdad Esfahbod | 0d160d5 | 2018-09-03 20:50:11 -0700 | [diff] [blame] | 67 | extern HB_INTERNAL const Type _hb_Null_##Type; \ |
| 68 | template <> \ |
| 69 | /*static*/ inline const Type& Null<Type> (void) { \ |
| 70 | return _hb_Null_##Type; \ |
| 71 | } \ |
Behdad Esfahbod | 3506672 | 2018-08-06 06:17:48 -0700 | [diff] [blame] | 72 | static_assert (true, "Just so we take semicolon after.") |
| 73 | #define DEFINE_NULL_INSTANCE(Type) \ |
Behdad Esfahbod | 0d160d5 | 2018-09-03 20:50:11 -0700 | [diff] [blame] | 74 | const Type _hb_Null_##Type |
| 75 | |
| 76 | /* Specializaiton to disallow Null objects. */ |
| 77 | #define DECLARE_NULL_DISALLOW(Type) \ |
| 78 | template <> inline const Type& Null<Type> (void) |
| 79 | #define DECLARE_NULL_NAMSPACE_DISALLOW(Namespace, Type) \ |
| 80 | } /* Close namespace. */ \ |
| 81 | template <> \ |
| 82 | /*static*/ inline const Namespace::Type& Null<Namespace::Type> (void) { \ |
| 83 | extern void *_hb_undefined; \ |
| 84 | return *reinterpret_cast<const Namespace::Type *> (_hb_undefined); \ |
| 85 | } \ |
| 86 | namespace Namespace { \ |
| 87 | static_assert (true, "Just so we take semicolon after.") |
Behdad Esfahbod | 25147ff | 2018-08-06 05:01:52 -0700 | [diff] [blame] | 88 | |
| 89 | /* Global writable pool. Enlarge as necessary. */ |
| 90 | |
| 91 | /* To be fully correct, CrapPool must be thread_local. However, we do not rely on CrapPool |
| 92 | * for correct operation. It only exist to catch and divert program logic bugs instead of |
| 93 | * causing bad memory access. So, races there are not actually introducing incorrectness |
| 94 | * in the code. Has ~12kb binary size overhead to have it, also clang build fails with it. */ |
| 95 | extern HB_INTERNAL |
| 96 | /*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)]; |
| 97 | |
| 98 | /* CRAP pool: Common Region for Access Protection. */ |
| 99 | template <typename Type> |
| 100 | static inline Type& Crap (void) { |
| 101 | static_assert (sizeof (Type) <= HB_NULL_POOL_SIZE, "Increase HB_NULL_POOL_SIZE."); |
| 102 | Type *obj = reinterpret_cast<Type *> (_hb_CrapPool); |
| 103 | *obj = Null(Type); |
| 104 | return *obj; |
| 105 | } |
| 106 | #define Crap(Type) Crap<Type>() |
| 107 | |
| 108 | template <typename Type> |
| 109 | struct CrapOrNull { |
| 110 | static inline Type & get (void) { return Crap(Type); } |
| 111 | }; |
| 112 | template <typename Type> |
| 113 | struct CrapOrNull<const Type> { |
| 114 | static inline Type const & get (void) { return Null(Type); } |
| 115 | }; |
| 116 | #define CrapOrNull(Type) CrapOrNull<Type>::get () |
| 117 | |
| 118 | |
| 119 | #endif /* HB_NULL_HH */ |