blob: 1e20a47e9ac0dcad3d61848419badf42fbd1e33e [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 Esfahbod155e92f2019-04-16 11:35:09 -040040#define HB_NULL_POOL_SIZE 384
Behdad Esfahbod25147ff2018-08-06 05:01:52 -070041
Behdad Esfahbodf2b91d62018-11-22 01:10:22 -050042/* Use SFINAE to sniff whether T has min_size; in which case return T::null_size,
43 * otherwise return sizeof(T). */
Behdad Esfahbod2737aa82018-11-22 01:44:27 -050044
45/* The hard way...
46 * https://stackoverflow.com/questions/7776448/sfinae-tried-with-bool-gives-compiler-error-template-argument-tvalue-invol
47 */
Behdad Esfahbod39b9d632018-11-24 00:25:40 -050048
Behdad Esfahbod2737aa82018-11-22 01:44:27 -050049template <typename T, typename B>
Behdad Esfahbodf2b91d62018-11-22 01:10:22 -050050struct _hb_null_size
51{ enum { value = sizeof (T) }; };
52template <typename T>
Behdad Esfahbod8570da12018-12-28 14:40:30 -050053struct _hb_null_size<T, hb_bool_tt<true || sizeof (T::min_size)> >
Behdad Esfahbodf2b91d62018-11-22 01:10:22 -050054{ enum { value = T::null_size }; };
55
56template <typename T>
57struct hb_null_size
Behdad Esfahbod8c6cbbd2018-12-28 14:29:09 -050058{ enum { value = _hb_null_size<T, hb_true_t>::value }; };
Behdad Esfahbodf99abcc2018-11-24 00:22:21 -050059#define hb_null_size(T) hb_null_size<T>::value
Behdad Esfahbodf2b91d62018-11-22 01:10:22 -050060
Behdad Esfahbod5b700742018-12-20 15:38:59 -050061/* These doesn't belong here, but since is copy/paste from above, put it here. */
62
63/* hb_static_size (T)
64 * Returns T::static_size if T::min_size is defined, or sizeof (T) otherwise. */
Behdad Esfahbod39b9d632018-11-24 00:25:40 -050065
66template <typename T, typename B>
67struct _hb_static_size
68{ enum { value = sizeof (T) }; };
69template <typename T>
Behdad Esfahbod8570da12018-12-28 14:40:30 -050070struct _hb_static_size<T, hb_bool_tt<true || sizeof (T::min_size)> >
Behdad Esfahbod39b9d632018-11-24 00:25:40 -050071{ enum { value = T::static_size }; };
72
73template <typename T>
74struct hb_static_size
Behdad Esfahbod8c6cbbd2018-12-28 14:29:09 -050075{ enum { value = _hb_static_size<T, hb_true_t>::value }; };
Behdad Esfahbod39b9d632018-11-24 00:25:40 -050076#define hb_static_size(T) hb_static_size<T>::value
77
78
79/*
80 * Null()
81 */
Behdad Esfahbod282ce722018-11-29 12:18:14 -050082
Behdad Esfahbod25147ff2018-08-06 05:01:52 -070083extern HB_INTERNAL
84hb_vector_size_impl_t const _hb_NullPool[(HB_NULL_POOL_SIZE + sizeof (hb_vector_size_impl_t) - 1) / sizeof (hb_vector_size_impl_t)];
85
86/* Generic nul-content Null objects. */
87template <typename Type>
Behdad Esfahbodec2a5dc2019-03-26 16:18:03 -070088struct Null {
89 static Type const & get_null ()
90 {
91 static_assert (hb_null_size (Type) <= HB_NULL_POOL_SIZE, "Increase HB_NULL_POOL_SIZE.");
92 return *reinterpret_cast<Type const *> (_hb_NullPool);
93 }
94};
Behdad Esfahbod282ce722018-11-29 12:18:14 -050095template <typename QType>
96struct NullHelper
97{
Behdad Esfahbod54ece292019-04-16 16:45:53 -040098 typedef hb_remove_const<hb_remove_reference<QType> > Type;
Behdad Esfahbodec2a5dc2019-03-26 16:18:03 -070099 static const Type & get_null () { return Null<Type>::get_null (); }
Behdad Esfahbod282ce722018-11-29 12:18:14 -0500100};
101#define Null(Type) NullHelper<Type>::get_null ()
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700102
Bruce Mitchener257d0e52018-10-19 22:49:21 +0700103/* Specializations for arbitrary-content Null objects expressed in bytes. */
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700104#define DECLARE_NULL_NAMESPACE_BYTES(Namespace, Type) \
Behdad Esfahbod0d160d52018-09-03 20:50:11 -0700105 } /* Close namespace. */ \
Behdad Esfahbod8d778872018-11-21 23:46:09 -0500106 extern HB_INTERNAL const unsigned char _hb_Null_##Namespace##_##Type[Namespace::Type::null_size]; \
Behdad Esfahbod0d160d52018-09-03 20:50:11 -0700107 template <> \
Behdad Esfahbodec2a5dc2019-03-26 16:18:03 -0700108 struct Null<Namespace::Type> { \
109 static Namespace::Type const & get_null () { \
110 return *reinterpret_cast<const Namespace::Type *> (_hb_Null_##Namespace##_##Type); \
111 } \
112 }; \
Behdad Esfahbod0d160d52018-09-03 20:50:11 -0700113 namespace Namespace { \
114 static_assert (true, "Just so we take semicolon after.")
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700115#define DEFINE_NULL_NAMESPACE_BYTES(Namespace, Type) \
Behdad Esfahbod8d778872018-11-21 23:46:09 -0500116 const unsigned char _hb_Null_##Namespace##_##Type[Namespace::Type::null_size]
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700117
Bruce Mitchener257d0e52018-10-19 22:49:21 +0700118/* Specializations for arbitrary-content Null objects expressed as struct initializer. */
Behdad Esfahbod35066722018-08-06 06:17:48 -0700119#define DECLARE_NULL_INSTANCE(Type) \
Behdad Esfahbod0d160d52018-09-03 20:50:11 -0700120 extern HB_INTERNAL const Type _hb_Null_##Type; \
121 template <> \
Behdad Esfahbodec2a5dc2019-03-26 16:18:03 -0700122 struct Null<Type> { \
123 static Type const & get_null () { \
124 return _hb_Null_##Type; \
125 } \
126 }; \
127 static_assert (true, "Just so we take semicolon after.")
Behdad Esfahbod35066722018-08-06 06:17:48 -0700128#define DEFINE_NULL_INSTANCE(Type) \
Behdad Esfahbod0d160d52018-09-03 20:50:11 -0700129 const Type _hb_Null_##Type
130
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700131/* Global writable pool. Enlarge as necessary. */
132
133/* To be fully correct, CrapPool must be thread_local. However, we do not rely on CrapPool
134 * for correct operation. It only exist to catch and divert program logic bugs instead of
135 * causing bad memory access. So, races there are not actually introducing incorrectness
136 * in the code. Has ~12kb binary size overhead to have it, also clang build fails with it. */
137extern HB_INTERNAL
138/*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)];
139
140/* CRAP pool: Common Region for Access Protection. */
141template <typename Type>
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330142static inline Type& Crap () {
Behdad Esfahbodf99abcc2018-11-24 00:22:21 -0500143 static_assert (hb_null_size (Type) <= HB_NULL_POOL_SIZE, "Increase HB_NULL_POOL_SIZE.");
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700144 Type *obj = reinterpret_cast<Type *> (_hb_CrapPool);
Behdad Esfahbodde96e5c2018-11-01 18:13:58 -0400145 memcpy (obj, &Null(Type), sizeof (*obj));
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700146 return *obj;
147}
Behdad Esfahbod282ce722018-11-29 12:18:14 -0500148template <typename QType>
149struct CrapHelper
150{
Behdad Esfahbod54ece292019-04-16 16:45:53 -0400151 typedef hb_remove_const<hb_remove_reference<QType> > Type;
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330152 static Type & get_crap () { return Crap<Type> (); }
Behdad Esfahbod282ce722018-11-29 12:18:14 -0500153};
154#define Crap(Type) CrapHelper<Type>::get_crap ()
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700155
156template <typename Type>
Behdad Esfahbod282ce722018-11-29 12:18:14 -0500157struct CrapOrNullHelper {
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330158 static Type & get () { return Crap(Type); }
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700159};
160template <typename Type>
Behdad Esfahbod282ce722018-11-29 12:18:14 -0500161struct CrapOrNullHelper<const Type> {
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330162 static const Type & get () { return Null(Type); }
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700163};
Behdad Esfahbod282ce722018-11-29 12:18:14 -0500164#define CrapOrNull(Type) CrapOrNullHelper<Type>::get ()
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700165
166
Behdad Esfahbodfb0f30f2018-11-03 15:24:14 -0400167/*
168 * hb_nonnull_ptr_t
169 */
170
171template <typename P>
172struct hb_nonnull_ptr_t
173{
Behdad Esfahbod54ece292019-04-16 16:45:53 -0400174 typedef hb_remove_pointer<P> T;
Behdad Esfahbodfb0f30f2018-11-03 15:24:14 -0400175
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330176 hb_nonnull_ptr_t (T *v_ = nullptr) : v (v_) {}
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330177 T * operator = (T *v_) { return v = v_; }
178 T * operator -> () const { return get (); }
179 T & operator * () const { return *get (); }
180 T ** operator & () const { return &v; }
Behdad Esfahbod0da22fb2018-11-05 13:13:39 -0500181 /* Only auto-cast to const types. */
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330182 template <typename C> operator const C * () const { return get (); }
183 operator const char * () const { return (const char *) get (); }
184 T * get () const { return v ? v : const_cast<T *> (&Null(T)); }
185 T * get_raw () const { return v; }
Behdad Esfahbodfb0f30f2018-11-03 15:24:14 -0400186
187 T *v;
188};
189
190
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700191#endif /* HB_NULL_HH */