blob: db38a4dfd24195ba810e9cfbb0e7126fbed24b1b [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 Esfahbod23a28f52021-04-16 13:22:05 -060042/* 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 Esfahbod2737aa82018-11-22 01:44:27 -050047
48/* The hard way...
49 * https://stackoverflow.com/questions/7776448/sfinae-tried-with-bool-gives-compiler-error-template-argument-tvalue-invol
50 */
Behdad Esfahbod39b9d632018-11-24 00:25:40 -050051
Behdad Esfahbodc0485e32019-05-10 21:03:14 -070052template <typename T, typename>
Behdad Esfahbod5a171ed2019-05-10 20:11:29 -070053struct _hb_null_size : hb_integral_constant<unsigned, sizeof (T)> {};
Behdad Esfahbodf2b91d62018-11-22 01:10:22 -050054template <typename T>
Behdad Esfahbod23a28f52021-04-16 13:22:05 -060055struct _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 Esfahbodf2b91d62018-11-22 01:10:22 -050058template <typename T>
Behdad Esfahbodc0485e32019-05-10 21:03:14 -070059using hb_null_size = _hb_null_size<T, void>;
Behdad Esfahbodf99abcc2018-11-24 00:22:21 -050060#define hb_null_size(T) hb_null_size<T>::value
Behdad Esfahbodf2b91d62018-11-22 01:10:22 -050061
Behdad Esfahbod5b700742018-12-20 15:38:59 -050062/* 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 Esfahbod39b9d632018-11-24 00:25:40 -050066
Behdad Esfahbodc0485e32019-05-10 21:03:14 -070067template <typename T, typename>
Behdad Esfahbod5a171ed2019-05-10 20:11:29 -070068struct _hb_static_size : hb_integral_constant<unsigned, sizeof (T)> {};
Behdad Esfahbod39b9d632018-11-24 00:25:40 -050069template <typename T>
Behdad Esfahbodc0485e32019-05-10 21:03:14 -070070struct _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 -050071template <typename T>
Behdad Esfahbodc0485e32019-05-10 21:03:14 -070072using hb_static_size = _hb_static_size<T, void>;
Behdad Esfahbod39b9d632018-11-24 00:25:40 -050073#define hb_static_size(T) hb_static_size<T>::value
74
Behdad Esfahbod499248c2021-04-16 13:14:48 -060075template <typename T, typename>
76struct _hb_min_size : hb_integral_constant<unsigned, sizeof (T)> {};
77template <typename T>
78struct _hb_min_size<T, hb_void_t<decltype (T::min_size)>> : hb_integral_constant<unsigned, T::min_size> {};
79template <typename T>
80using hb_min_size = _hb_min_size<T, void>;
81#define hb_min_size(T) hb_min_size<T>::value
82
Behdad Esfahbod39b9d632018-11-24 00:25:40 -050083
84/*
85 * Null()
86 */
Behdad Esfahbod282ce722018-11-29 12:18:14 -050087
Behdad Esfahbod25147ff2018-08-06 05:01:52 -070088extern HB_INTERNAL
Behdad Esfahbod60653a72019-06-18 13:01:11 -070089uint64_t const _hb_NullPool[(HB_NULL_POOL_SIZE + sizeof (uint64_t) - 1) / sizeof (uint64_t)];
Behdad Esfahbod25147ff2018-08-06 05:01:52 -070090
91/* Generic nul-content Null objects. */
92template <typename Type>
Behdad Esfahbodec2a5dc2019-03-26 16:18:03 -070093struct 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 Esfahbod282ce722018-11-29 12:18:14 -0500100template <typename QType>
101struct NullHelper
102{
Ebrahim Byagowi92588782019-04-30 13:05:10 -0700103 typedef hb_remove_const<hb_remove_reference<QType>> Type;
Behdad Esfahbodec2a5dc2019-03-26 16:18:03 -0700104 static const Type & get_null () { return Null<Type>::get_null (); }
Behdad Esfahbod282ce722018-11-29 12:18:14 -0500105};
106#define Null(Type) NullHelper<Type>::get_null ()
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700107
Bruce Mitchener257d0e52018-10-19 22:49:21 +0700108/* Specializations for arbitrary-content Null objects expressed in bytes. */
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700109#define DECLARE_NULL_NAMESPACE_BYTES(Namespace, Type) \
Behdad Esfahbod0d160d52018-09-03 20:50:11 -0700110 } /* Close namespace. */ \
Behdad Esfahbod8d778872018-11-21 23:46:09 -0500111 extern HB_INTERNAL const unsigned char _hb_Null_##Namespace##_##Type[Namespace::Type::null_size]; \
Behdad Esfahbod0d160d52018-09-03 20:50:11 -0700112 template <> \
Behdad Esfahbodec2a5dc2019-03-26 16:18:03 -0700113 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 Esfahbod0d160d52018-09-03 20:50:11 -0700118 namespace Namespace { \
Behdad Esfahbodbd8aa1b2020-04-21 22:19:46 -0700119 static_assert (true, "") /* Require semicolon after. */
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700120#define DEFINE_NULL_NAMESPACE_BYTES(Namespace, Type) \
Behdad Esfahbod8d778872018-11-21 23:46:09 -0500121 const unsigned char _hb_Null_##Namespace##_##Type[Namespace::Type::null_size]
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700122
Bruce Mitchener257d0e52018-10-19 22:49:21 +0700123/* Specializations for arbitrary-content Null objects expressed as struct initializer. */
Behdad Esfahbod35066722018-08-06 06:17:48 -0700124#define DECLARE_NULL_INSTANCE(Type) \
Behdad Esfahbod0d160d52018-09-03 20:50:11 -0700125 extern HB_INTERNAL const Type _hb_Null_##Type; \
126 template <> \
Behdad Esfahbodec2a5dc2019-03-26 16:18:03 -0700127 struct Null<Type> { \
128 static Type const & get_null () { \
129 return _hb_Null_##Type; \
130 } \
131 }; \
Behdad Esfahbodbd8aa1b2020-04-21 22:19:46 -0700132 static_assert (true, "") /* Require semicolon after. */
Behdad Esfahbod35066722018-08-06 06:17:48 -0700133#define DEFINE_NULL_INSTANCE(Type) \
Behdad Esfahbod0d160d52018-09-03 20:50:11 -0700134 const Type _hb_Null_##Type
135
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700136/* 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. */
142extern HB_INTERNAL
Behdad Esfahbod60653a72019-06-18 13:01:11 -0700143/*thread_local*/ uint64_t _hb_CrapPool[(HB_NULL_POOL_SIZE + sizeof (uint64_t) - 1) / sizeof (uint64_t)];
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700144
145/* CRAP pool: Common Region for Access Protection. */
146template <typename Type>
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330147static inline Type& Crap () {
Behdad Esfahbodf99abcc2018-11-24 00:22:21 -0500148 static_assert (hb_null_size (Type) <= HB_NULL_POOL_SIZE, "Increase HB_NULL_POOL_SIZE.");
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700149 Type *obj = reinterpret_cast<Type *> (_hb_CrapPool);
Ebrahim Byagowi2dda6dd2020-04-20 14:12:45 +0430150 memcpy (obj, &Null (Type), sizeof (*obj));
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700151 return *obj;
152}
Behdad Esfahbod282ce722018-11-29 12:18:14 -0500153template <typename QType>
154struct CrapHelper
155{
Ebrahim Byagowi92588782019-04-30 13:05:10 -0700156 typedef hb_remove_const<hb_remove_reference<QType>> Type;
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330157 static Type & get_crap () { return Crap<Type> (); }
Behdad Esfahbod282ce722018-11-29 12:18:14 -0500158};
159#define Crap(Type) CrapHelper<Type>::get_crap ()
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700160
161template <typename Type>
Behdad Esfahbod282ce722018-11-29 12:18:14 -0500162struct CrapOrNullHelper {
Ebrahim Byagowi2dda6dd2020-04-20 14:12:45 +0430163 static Type & get () { return Crap (Type); }
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700164};
165template <typename Type>
Behdad Esfahbod282ce722018-11-29 12:18:14 -0500166struct CrapOrNullHelper<const Type> {
Ebrahim Byagowi2dda6dd2020-04-20 14:12:45 +0430167 static const Type & get () { return Null (Type); }
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700168};
Behdad Esfahbod282ce722018-11-29 12:18:14 -0500169#define CrapOrNull(Type) CrapOrNullHelper<Type>::get ()
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700170
171
Behdad Esfahbodfb0f30f2018-11-03 15:24:14 -0400172/*
173 * hb_nonnull_ptr_t
174 */
175
176template <typename P>
177struct hb_nonnull_ptr_t
178{
Behdad Esfahbod54ece292019-04-16 16:45:53 -0400179 typedef hb_remove_pointer<P> T;
Behdad Esfahbodfb0f30f2018-11-03 15:24:14 -0400180
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330181 hb_nonnull_ptr_t (T *v_ = nullptr) : v (v_) {}
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330182 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 Esfahbod0da22fb2018-11-05 13:13:39 -0500186 /* Only auto-cast to const types. */
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330187 template <typename C> operator const C * () const { return get (); }
188 operator const char * () const { return (const char *) get (); }
Ebrahim Byagowi2dda6dd2020-04-20 14:12:45 +0430189 T * get () const { return v ? v : const_cast<T *> (&Null (T)); }
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330190 T * get_raw () const { return v; }
Behdad Esfahbodfb0f30f2018-11-03 15:24:14 -0400191
Behdad Esfahbod53806e52020-11-25 11:51:37 -0700192 private:
Behdad Esfahbodfb0f30f2018-11-03 15:24:14 -0400193 T *v;
194};
195
196
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700197#endif /* HB_NULL_HH */