2018-07-26 01:58:47 +02:00
|
|
|
/*
|
|
|
|
* Copyright © 2007,2008,2009,2010 Red Hat, Inc.
|
|
|
|
* Copyright © 2012,2018 Google, Inc.
|
|
|
|
*
|
|
|
|
* This is part of HarfBuzz, a text shaping library.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, without written agreement and without
|
|
|
|
* license or royalty fees, to use, copy, modify, and distribute this
|
|
|
|
* software and its documentation for any purpose, provided that the
|
|
|
|
* above copyright notice and the following two paragraphs appear in
|
|
|
|
* all copies of this software.
|
|
|
|
*
|
|
|
|
* IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
|
|
|
|
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
|
|
|
|
* IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
|
|
|
|
* DAMAGE.
|
|
|
|
*
|
|
|
|
* THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
|
|
|
|
* ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
|
|
|
|
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
|
|
|
|
*
|
|
|
|
* Red Hat Author(s): Behdad Esfahbod
|
|
|
|
* Google Author(s): Behdad Esfahbod
|
|
|
|
*/
|
|
|
|
|
2018-08-26 07:36:36 +02:00
|
|
|
#ifndef HB_MACHINERY_HH
|
|
|
|
#define HB_MACHINERY_HH
|
2018-07-26 01:58:47 +02:00
|
|
|
|
2018-08-26 07:36:36 +02:00
|
|
|
#include "hb.hh"
|
|
|
|
#include "hb-blob.hh"
|
2018-08-02 07:50:45 +02:00
|
|
|
|
2019-03-30 22:59:40 +01:00
|
|
|
#include "hb-dispatch.hh"
|
|
|
|
#include "hb-sanitize.hh"
|
2019-03-30 22:53:54 +01:00
|
|
|
#include "hb-serialize.hh"
|
2018-07-26 01:58:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Casts
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* StructAtOffset<T>(P,Ofs) returns the struct T& that is placed at memory
|
|
|
|
* location pointed to by P plus Ofs bytes. */
|
|
|
|
template<typename Type>
|
|
|
|
static inline const Type& StructAtOffset(const void *P, unsigned int offset)
|
|
|
|
{ return * reinterpret_cast<const Type*> ((const char *) P + offset); }
|
|
|
|
template<typename Type>
|
|
|
|
static inline Type& StructAtOffset(void *P, unsigned int offset)
|
|
|
|
{ return * reinterpret_cast<Type*> ((char *) P + offset); }
|
2019-01-22 12:45:40 +01:00
|
|
|
template<typename Type>
|
|
|
|
static inline const Type& StructAtOffsetUnaligned(const void *P, unsigned int offset)
|
|
|
|
{
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wcast-align"
|
2019-12-10 19:52:32 +01:00
|
|
|
return * reinterpret_cast<const Type*> ((const char *) P + offset);
|
2019-01-22 12:45:40 +01:00
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
}
|
|
|
|
template<typename Type>
|
|
|
|
static inline Type& StructAtOffsetUnaligned(void *P, unsigned int offset)
|
|
|
|
{
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wcast-align"
|
|
|
|
return * reinterpret_cast<Type*> ((char *) P + offset);
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
}
|
2018-07-26 01:58:47 +02:00
|
|
|
|
|
|
|
/* StructAfter<T>(X) returns the struct T& that is placed after X.
|
|
|
|
* Works with X of variable size also. X must implement get_size() */
|
|
|
|
template<typename Type, typename TObject>
|
|
|
|
static inline const Type& StructAfter(const TObject &X)
|
|
|
|
{ return StructAtOffset<Type>(&X, X.get_size()); }
|
|
|
|
template<typename Type, typename TObject>
|
|
|
|
static inline Type& StructAfter(TObject &X)
|
|
|
|
{ return StructAtOffset<Type>(&X, X.get_size()); }
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Size checking
|
|
|
|
*/
|
|
|
|
|
2020-06-29 10:53:21 +02:00
|
|
|
/* Size signifying variable-sized array */
|
|
|
|
#ifndef HB_VAR_ARRAY
|
|
|
|
#define HB_VAR_ARRAY 1
|
|
|
|
#endif
|
|
|
|
|
2018-07-26 01:58:47 +02:00
|
|
|
/* Check _assertion in a method environment */
|
|
|
|
#define _DEFINE_INSTANCE_ASSERTION1(_line, _assertion) \
|
2018-12-17 19:01:01 +01:00
|
|
|
void _instance_assertion_on_line_##_line () const \
|
2018-11-22 06:39:14 +01:00
|
|
|
{ static_assert ((_assertion), ""); }
|
2018-07-26 01:58:47 +02:00
|
|
|
# define _DEFINE_INSTANCE_ASSERTION0(_line, _assertion) _DEFINE_INSTANCE_ASSERTION1 (_line, _assertion)
|
|
|
|
# define DEFINE_INSTANCE_ASSERTION(_assertion) _DEFINE_INSTANCE_ASSERTION0 (__LINE__, _assertion)
|
|
|
|
|
|
|
|
/* Check that _code compiles in a method environment */
|
|
|
|
#define _DEFINE_COMPILES_ASSERTION1(_line, _code) \
|
2018-12-17 19:01:01 +01:00
|
|
|
void _compiles_assertion_on_line_##_line () const \
|
2018-11-22 06:39:14 +01:00
|
|
|
{ _code; }
|
2018-07-26 01:58:47 +02:00
|
|
|
# define _DEFINE_COMPILES_ASSERTION0(_line, _code) _DEFINE_COMPILES_ASSERTION1 (_line, _code)
|
|
|
|
# define DEFINE_COMPILES_ASSERTION(_code) _DEFINE_COMPILES_ASSERTION0 (__LINE__, _code)
|
|
|
|
|
|
|
|
|
|
|
|
#define DEFINE_SIZE_STATIC(size) \
|
2018-11-22 07:05:02 +01:00
|
|
|
DEFINE_INSTANCE_ASSERTION (sizeof (*this) == (size)) \
|
2018-12-17 19:01:01 +01:00
|
|
|
unsigned int get_size () const { return (size); } \
|
2019-01-22 12:15:23 +01:00
|
|
|
static constexpr unsigned null_size = (size); \
|
|
|
|
static constexpr unsigned min_size = (size); \
|
2019-01-25 16:08:25 +01:00
|
|
|
static constexpr unsigned static_size = (size)
|
2018-07-26 01:58:47 +02:00
|
|
|
|
|
|
|
#define DEFINE_SIZE_UNION(size, _member) \
|
2018-11-22 06:39:14 +01:00
|
|
|
DEFINE_COMPILES_ASSERTION ((void) this->u._member.static_size) \
|
|
|
|
DEFINE_INSTANCE_ASSERTION (sizeof(this->u._member) == (size)) \
|
2019-01-22 12:15:23 +01:00
|
|
|
static constexpr unsigned null_size = (size); \
|
2019-01-25 16:08:25 +01:00
|
|
|
static constexpr unsigned min_size = (size)
|
2018-07-26 01:58:47 +02:00
|
|
|
|
|
|
|
#define DEFINE_SIZE_MIN(size) \
|
2018-11-22 06:39:14 +01:00
|
|
|
DEFINE_INSTANCE_ASSERTION (sizeof (*this) >= (size)) \
|
2019-01-22 12:15:23 +01:00
|
|
|
static constexpr unsigned null_size = (size); \
|
2019-01-25 16:08:25 +01:00
|
|
|
static constexpr unsigned min_size = (size)
|
2018-07-26 01:58:47 +02:00
|
|
|
|
2018-11-22 07:18:55 +01:00
|
|
|
#define DEFINE_SIZE_UNBOUNDED(size) \
|
|
|
|
DEFINE_INSTANCE_ASSERTION (sizeof (*this) >= (size)) \
|
2019-01-25 16:08:25 +01:00
|
|
|
static constexpr unsigned min_size = (size)
|
2018-11-22 07:18:55 +01:00
|
|
|
|
2018-07-26 01:58:47 +02:00
|
|
|
#define DEFINE_SIZE_ARRAY(size, array) \
|
2018-12-01 02:45:40 +01:00
|
|
|
DEFINE_COMPILES_ASSERTION ((void) (array)[0].static_size) \
|
2019-10-31 21:19:44 +01:00
|
|
|
DEFINE_INSTANCE_ASSERTION (sizeof (*this) == (size) + (HB_VAR_ARRAY+0) * sizeof ((array)[0])) \
|
2019-01-22 12:15:23 +01:00
|
|
|
static constexpr unsigned null_size = (size); \
|
2019-01-25 16:08:25 +01:00
|
|
|
static constexpr unsigned min_size = (size)
|
2018-09-02 03:34:50 +02:00
|
|
|
|
|
|
|
#define DEFINE_SIZE_ARRAY_SIZED(size, array) \
|
2018-12-17 19:01:01 +01:00
|
|
|
unsigned int get_size () const { return (size - (array).min_size + (array).get_size ()); } \
|
2018-11-22 05:19:00 +01:00
|
|
|
DEFINE_SIZE_ARRAY(size, array)
|
2018-07-26 01:58:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2018-08-02 10:36:08 +02:00
|
|
|
* Lazy loaders.
|
2018-07-26 01:58:47 +02:00
|
|
|
*/
|
|
|
|
|
2018-08-12 22:12:29 +02:00
|
|
|
template <typename Data, unsigned int WheresData>
|
|
|
|
struct hb_data_wrapper_t
|
|
|
|
{
|
2018-09-16 19:33:48 +02:00
|
|
|
static_assert (WheresData > 0, "");
|
2018-08-12 22:12:29 +02:00
|
|
|
|
2018-12-17 19:01:01 +01:00
|
|
|
Data * get_data () const
|
|
|
|
{ return *(((Data **) (void *) this) - WheresData); }
|
2018-08-12 22:12:29 +02:00
|
|
|
|
2018-12-17 19:01:01 +01:00
|
|
|
bool is_inert () const { return !get_data (); }
|
2018-11-14 02:48:46 +01:00
|
|
|
|
2018-08-12 22:12:29 +02:00
|
|
|
template <typename Stored, typename Subclass>
|
2018-12-17 19:01:01 +01:00
|
|
|
Stored * call_create () const { return Subclass::create (get_data ()); }
|
2018-08-12 22:12:29 +02:00
|
|
|
};
|
|
|
|
template <>
|
|
|
|
struct hb_data_wrapper_t<void, 0>
|
|
|
|
{
|
2018-12-17 19:01:01 +01:00
|
|
|
bool is_inert () const { return false; }
|
2018-11-14 02:48:46 +01:00
|
|
|
|
2018-08-13 01:57:06 +02:00
|
|
|
template <typename Stored, typename Funcs>
|
2018-12-17 19:01:01 +01:00
|
|
|
Stored * call_create () const { return Funcs::create (); }
|
2018-08-12 22:12:29 +02:00
|
|
|
};
|
|
|
|
|
2018-08-13 01:57:06 +02:00
|
|
|
template <typename T1, typename T2> struct hb_non_void_t { typedef T1 value; };
|
|
|
|
template <typename T2> struct hb_non_void_t<void, T2> { typedef T2 value; };
|
|
|
|
|
|
|
|
template <typename Returned,
|
|
|
|
typename Subclass = void,
|
|
|
|
typename Data = void,
|
|
|
|
unsigned int WheresData = 0,
|
2018-08-02 09:38:46 +02:00
|
|
|
typename Stored = Returned>
|
2018-08-12 22:12:29 +02:00
|
|
|
struct hb_lazy_loader_t : hb_data_wrapper_t<Data, WheresData>
|
2018-07-26 01:58:47 +02:00
|
|
|
{
|
2018-08-13 01:57:06 +02:00
|
|
|
typedef typename hb_non_void_t<Subclass,
|
|
|
|
hb_lazy_loader_t<Returned,Subclass,Data,WheresData,Stored>
|
|
|
|
>::value Funcs;
|
|
|
|
|
2018-12-17 19:01:01 +01:00
|
|
|
void init0 () {} /* Init, when memory is already set to 0. No-op for us. */
|
|
|
|
void init () { instance.set_relaxed (nullptr); }
|
|
|
|
void fini () { do_destroy (instance.get ()); }
|
|
|
|
|
|
|
|
void free_instance ()
|
2018-08-12 22:16:40 +02:00
|
|
|
{
|
|
|
|
retry:
|
|
|
|
Stored *p = instance.get ();
|
2018-11-16 09:24:22 +01:00
|
|
|
if (unlikely (p && !cmpexch (p, nullptr)))
|
2018-08-12 22:16:40 +02:00
|
|
|
goto retry;
|
2018-08-12 22:39:01 +02:00
|
|
|
do_destroy (p);
|
2018-08-12 22:16:40 +02:00
|
|
|
}
|
2018-08-12 22:12:29 +02:00
|
|
|
|
2018-12-16 20:08:10 +01:00
|
|
|
static void do_destroy (Stored *p)
|
2018-07-26 01:58:47 +02:00
|
|
|
{
|
2018-11-14 02:48:46 +01:00
|
|
|
if (p && p != const_cast<Stored *> (Funcs::get_null ()))
|
2018-08-13 01:57:06 +02:00
|
|
|
Funcs::destroy (p);
|
2018-07-26 01:58:47 +02:00
|
|
|
}
|
2018-08-12 21:40:24 +02:00
|
|
|
|
2018-12-17 19:01:01 +01:00
|
|
|
const Returned * operator -> () const { return get (); }
|
|
|
|
const Returned & operator * () const { return *get (); }
|
2019-03-29 23:22:46 +01:00
|
|
|
explicit operator bool () const
|
2018-11-16 08:29:13 +01:00
|
|
|
{ return get_stored () != Funcs::get_null (); }
|
2018-12-17 19:01:01 +01:00
|
|
|
template <typename C> operator const C * () const { return get (); }
|
2018-08-12 21:40:24 +02:00
|
|
|
|
2018-12-17 19:01:01 +01:00
|
|
|
Stored * get_stored () const
|
2018-07-26 01:58:47 +02:00
|
|
|
{
|
|
|
|
retry:
|
2018-08-09 09:22:37 +02:00
|
|
|
Stored *p = this->instance.get ();
|
2018-07-26 01:58:47 +02:00
|
|
|
if (unlikely (!p))
|
|
|
|
{
|
2018-11-14 02:48:46 +01:00
|
|
|
if (unlikely (this->is_inert ()))
|
|
|
|
return const_cast<Stored *> (Funcs::get_null ());
|
|
|
|
|
2018-11-11 21:48:47 +01:00
|
|
|
p = this->template call_create<Stored, Funcs> ();
|
|
|
|
if (unlikely (!p))
|
2018-11-14 02:48:46 +01:00
|
|
|
p = const_cast<Stored *> (Funcs::get_null ());
|
2018-11-11 21:48:47 +01:00
|
|
|
|
2018-11-16 09:24:22 +01:00
|
|
|
if (unlikely (!cmpexch (nullptr, p)))
|
2018-07-26 01:58:47 +02:00
|
|
|
{
|
2019-08-24 15:27:14 +02:00
|
|
|
do_destroy (p);
|
2018-07-26 01:58:47 +02:00
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
2018-12-17 19:01:01 +01:00
|
|
|
Stored * get_stored_relaxed () const
|
2018-08-26 08:19:34 +02:00
|
|
|
{
|
|
|
|
return this->instance.get_relaxed ();
|
|
|
|
}
|
2018-08-02 10:27:40 +02:00
|
|
|
|
2018-12-16 20:08:10 +01:00
|
|
|
bool cmpexch (Stored *current, Stored *value) const
|
2018-08-04 01:57:40 +02:00
|
|
|
{
|
2018-11-16 09:24:22 +01:00
|
|
|
/* This *must* be called when there are no other threads accessing. */
|
|
|
|
return this->instance.cmpexch (current, value);
|
2018-08-04 01:57:40 +02:00
|
|
|
}
|
|
|
|
|
2018-12-17 19:01:01 +01:00
|
|
|
const Returned * get () const { return Funcs::convert (get_stored ()); }
|
|
|
|
const Returned * get_relaxed () const { return Funcs::convert (get_stored_relaxed ()); }
|
|
|
|
Returned * get_unconst () const { return const_cast<Returned *> (Funcs::convert (get_stored ())); }
|
2018-08-02 10:27:40 +02:00
|
|
|
|
2018-08-12 21:40:24 +02:00
|
|
|
/* To be possibly overloaded by subclasses. */
|
2018-12-16 20:08:10 +01:00
|
|
|
static Returned* convert (Stored *p) { return p; }
|
2018-08-13 01:40:08 +02:00
|
|
|
|
|
|
|
/* By default null/init/fini the object. */
|
2020-04-20 11:42:45 +02:00
|
|
|
static const Stored* get_null () { return &Null (Stored); }
|
2018-12-16 20:08:10 +01:00
|
|
|
static Stored *create (Data *data)
|
2018-08-13 01:40:08 +02:00
|
|
|
{
|
2021-07-08 18:58:50 +02:00
|
|
|
Stored *p = (Stored *) hb_calloc (1, sizeof (Stored));
|
2018-08-13 01:40:08 +02:00
|
|
|
if (likely (p))
|
2022-01-20 01:09:34 +01:00
|
|
|
p->Xinit (data);
|
2018-08-13 01:40:08 +02:00
|
|
|
return p;
|
|
|
|
}
|
2018-12-17 19:01:01 +01:00
|
|
|
static Stored *create ()
|
2018-08-13 01:57:06 +02:00
|
|
|
{
|
2021-07-08 18:58:50 +02:00
|
|
|
Stored *p = (Stored *) hb_calloc (1, sizeof (Stored));
|
2018-08-13 01:57:06 +02:00
|
|
|
if (likely (p))
|
2022-01-20 01:09:34 +01:00
|
|
|
p->Xinit ();
|
2018-08-13 01:57:06 +02:00
|
|
|
return p;
|
|
|
|
}
|
2018-12-16 20:08:10 +01:00
|
|
|
static void destroy (Stored *p)
|
2018-08-13 01:40:08 +02:00
|
|
|
{
|
2022-01-20 01:09:34 +01:00
|
|
|
p->Xfini ();
|
2021-07-08 18:58:50 +02:00
|
|
|
hb_free (p);
|
2018-08-13 01:40:08 +02:00
|
|
|
}
|
2018-08-02 10:27:40 +02:00
|
|
|
|
2018-11-16 08:29:13 +01:00
|
|
|
// private:
|
2018-08-02 10:27:40 +02:00
|
|
|
/* Must only have one pointer. */
|
2018-08-09 09:22:37 +02:00
|
|
|
hb_atomic_ptr_t<Stored *> instance;
|
2018-07-26 01:58:47 +02:00
|
|
|
};
|
|
|
|
|
2018-08-02 10:36:08 +02:00
|
|
|
/* Specializations. */
|
|
|
|
|
2018-08-26 09:21:29 +02:00
|
|
|
template <typename T, unsigned int WheresFace>
|
2018-08-13 01:57:06 +02:00
|
|
|
struct hb_face_lazy_loader_t : hb_lazy_loader_t<T,
|
2018-08-26 09:21:29 +02:00
|
|
|
hb_face_lazy_loader_t<T, WheresFace>,
|
2018-08-13 01:57:06 +02:00
|
|
|
hb_face_t, WheresFace> {};
|
2018-07-26 01:58:47 +02:00
|
|
|
|
2018-08-12 21:27:47 +02:00
|
|
|
template <typename T, unsigned int WheresFace>
|
2018-08-13 01:57:06 +02:00
|
|
|
struct hb_table_lazy_loader_t : hb_lazy_loader_t<T,
|
|
|
|
hb_table_lazy_loader_t<T, WheresFace>,
|
2018-08-12 21:09:20 +02:00
|
|
|
hb_face_t, WheresFace,
|
2018-08-13 01:57:06 +02:00
|
|
|
hb_blob_t>
|
2018-08-02 10:27:40 +02:00
|
|
|
{
|
2018-12-16 20:08:10 +01:00
|
|
|
static hb_blob_t *create (hb_face_t *face)
|
2018-12-17 19:01:01 +01:00
|
|
|
{ return hb_sanitize_context_t ().reference_table<T> (face); }
|
|
|
|
static void destroy (hb_blob_t *p) { hb_blob_destroy (p); }
|
|
|
|
|
|
|
|
static const hb_blob_t *get_null ()
|
|
|
|
{ return hb_blob_get_empty (); }
|
|
|
|
|
2018-12-16 20:08:10 +01:00
|
|
|
static const T* convert (const hb_blob_t *blob)
|
2018-12-17 19:01:01 +01:00
|
|
|
{ return blob->as<T> (); }
|
2018-08-02 10:27:40 +02:00
|
|
|
|
2018-12-17 19:01:01 +01:00
|
|
|
hb_blob_t* get_blob () const { return this->get_stored (); }
|
2018-07-26 01:58:47 +02:00
|
|
|
};
|
|
|
|
|
2018-08-12 22:46:53 +02:00
|
|
|
template <typename Subclass>
|
2018-08-13 01:57:06 +02:00
|
|
|
struct hb_font_funcs_lazy_loader_t : hb_lazy_loader_t<hb_font_funcs_t, Subclass>
|
2018-08-12 22:46:53 +02:00
|
|
|
{
|
2018-12-16 20:08:10 +01:00
|
|
|
static void destroy (hb_font_funcs_t *p)
|
2018-12-17 19:01:01 +01:00
|
|
|
{ hb_font_funcs_destroy (p); }
|
|
|
|
static const hb_font_funcs_t *get_null ()
|
|
|
|
{ return hb_font_funcs_get_empty (); }
|
2018-08-12 22:46:53 +02:00
|
|
|
};
|
2018-08-12 22:39:01 +02:00
|
|
|
template <typename Subclass>
|
2018-08-13 01:57:06 +02:00
|
|
|
struct hb_unicode_funcs_lazy_loader_t : hb_lazy_loader_t<hb_unicode_funcs_t, Subclass>
|
2018-08-12 22:39:01 +02:00
|
|
|
{
|
2018-12-16 20:08:10 +01:00
|
|
|
static void destroy (hb_unicode_funcs_t *p)
|
2018-12-17 19:01:01 +01:00
|
|
|
{ hb_unicode_funcs_destroy (p); }
|
|
|
|
static const hb_unicode_funcs_t *get_null ()
|
|
|
|
{ return hb_unicode_funcs_get_empty (); }
|
2018-08-12 22:39:01 +02:00
|
|
|
};
|
|
|
|
|
2018-07-26 01:58:47 +02:00
|
|
|
|
2018-08-26 07:36:36 +02:00
|
|
|
#endif /* HB_MACHINERY_HH */
|