From f2b91d6510face95008151bb0d25837723536f9f Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 22 Nov 2018 01:10:22 -0500 Subject: [PATCH] Use Type::null_size for our structs in Null(), sizeof() for other types --- src/hb-null.hh | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/hb-null.hh b/src/hb-null.hh index 51f653d2a..92c7a3519 100644 --- a/src/hb-null.hh +++ b/src/hb-null.hh @@ -38,13 +38,26 @@ #define HB_NULL_POOL_SIZE 1024 +/* Use SFINAE to sniff whether T has min_size; in which case return T::null_size, + * otherwise return sizeof(T). */ +template +struct _hb_null_size +{ enum { value = sizeof (T) }; }; +template +struct _hb_null_size +{ enum { value = T::null_size }; }; + +template +struct hb_null_size +{ enum { value = _hb_null_size::value }; }; + extern HB_INTERNAL 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)]; /* Generic nul-content Null objects. */ template static inline Type const & Null (void) { - static_assert (sizeof (Type) <= HB_NULL_POOL_SIZE, "Increase HB_NULL_POOL_SIZE."); + static_assert (hb_null_size::value <= HB_NULL_POOL_SIZE, "Increase HB_NULL_POOL_SIZE."); return *reinterpret_cast (_hb_NullPool); } #define Null(Type) Null::value>::value>() @@ -85,7 +98,7 @@ extern HB_INTERNAL /* CRAP pool: Common Region for Access Protection. */ template static inline Type& Crap (void) { - static_assert (sizeof (Type) <= HB_NULL_POOL_SIZE, "Increase HB_NULL_POOL_SIZE."); + static_assert (hb_null_size::value <= HB_NULL_POOL_SIZE, "Increase HB_NULL_POOL_SIZE."); Type *obj = reinterpret_cast (_hb_CrapPool); memcpy (obj, &Null(Type), sizeof (*obj)); return *obj;