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
30#include "hb.hh"
31
32
33/*
34 * Static pools
35 */
36
37/* Global nul-content Null pool. Enlarge as necessary. */
38
39#define HB_NULL_POOL_SIZE 264
40
41extern HB_INTERNAL
42hb_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. */
45template <typename Type>
46static 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
52/* Specializaitons for arbitrary-content Null objects expressed in bytes. */
53#define DECLARE_NULL_NAMESPACE_BYTES(Namespace, Type) \
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.")
62#define DEFINE_NULL_NAMESPACE_BYTES(Namespace, Type) \
63 const unsigned char _hb_Null_##Namespace##_##Type[Namespace::Type::min_size]
64
65/* Specializaitons for arbitrary-content Null objects expressed as struct initializer. */
66#define DECLARE_NULL_INSTANCE(Type) \
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 } \
72static_assert (true, "Just so we take semicolon after.")
73#define DEFINE_NULL_INSTANCE(Type) \
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.")
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. */
95extern 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. */
99template <typename Type>
100static 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
108template <typename Type>
109struct CrapOrNull {
110 static inline Type & get (void) { return Crap(Type); }
111};
112template <typename Type>
113struct 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 */
120