harfbuzz/src/hb-object-private.hh

220 lines
6.1 KiB
C++
Raw Normal View History

2009-07-30 21:33:57 +02:00
/*
2011-04-21 23:14:28 +02:00
* Copyright © 2007 Chris Wilson
* Copyright © 2009,2010 Red Hat, Inc.
* Copyright © 2011,2012 Google, Inc.
2009-07-30 21:33:57 +02:00
*
2010-04-22 06:11:43 +02:00
* This is part of HarfBuzz, a text shaping library.
2009-07-30 21:33:57 +02:00
*
* 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.
*
* Contributor(s):
* Chris Wilson <chris@chris-wilson.co.uk>
2009-08-02 01:30:31 +02:00
* Red Hat Author(s): Behdad Esfahbod
2011-04-21 23:14:28 +02:00
* Google Author(s): Behdad Esfahbod
2009-07-30 21:33:57 +02:00
*/
#ifndef HB_OBJECT_PRIVATE_HH
#define HB_OBJECT_PRIVATE_HH
2009-07-30 21:33:57 +02:00
#include "hb-private.hh"
2018-07-10 16:05:03 +02:00
#include "hb-atomic-private.hh"
#include "hb-mutex-private.hh"
2009-11-06 01:37:58 +01:00
2011-04-28 03:45:51 +02:00
/* reference_count */
#define HB_REFERENCE_COUNT_INERT_VALUE 0
#define HB_REFERENCE_COUNT_POISON_VALUE -0x0000DEAD
2018-06-01 04:31:39 +02:00
#define HB_REFERENCE_COUNT_INIT {HB_ATOMIC_INT_INIT (HB_REFERENCE_COUNT_INERT_VALUE)}
2012-06-03 21:52:51 +02:00
struct hb_reference_count_t
{
hb_atomic_int_t ref_count;
inline void init (int v) { ref_count.set_relaxed (v); }
inline int get_relaxed (void) const { return ref_count.get_relaxed (); }
inline int inc (void) { return ref_count.inc (); }
inline int dec (void) { return ref_count.dec (); }
inline void fini (void) { ref_count.set_relaxed (HB_REFERENCE_COUNT_POISON_VALUE); }
inline bool is_inert (void) const { return ref_count.get_relaxed () == HB_REFERENCE_COUNT_INERT_VALUE; }
inline bool is_valid (void) const { return ref_count.get_relaxed () > 0; }
2012-06-03 21:52:51 +02:00
};
2011-04-28 03:45:51 +02:00
/* user_data */
2012-06-03 21:52:51 +02:00
struct hb_user_data_array_t
{
2011-05-05 20:47:53 +02:00
struct hb_user_data_item_t {
hb_user_data_key_t *key;
void *data;
hb_destroy_func_t destroy;
2011-05-05 20:47:53 +02:00
inline bool operator == (hb_user_data_key_t *other_key) const { return key == other_key; }
inline bool operator == (hb_user_data_item_t &other) const { return key == other.key; }
2018-05-02 01:01:25 +02:00
void fini (void) { if (destroy) destroy (data); }
2011-05-05 20:47:53 +02:00
};
hb_mutex_t lock;
hb_lockable_set_t<hb_user_data_item_t, hb_mutex_t> items;
inline void init (void) { lock.init (); items.init (); }
2011-05-05 21:28:37 +02:00
HB_INTERNAL bool set (hb_user_data_key_t *key,
void * data,
hb_destroy_func_t destroy,
hb_bool_t replace);
HB_INTERNAL void *get (hb_user_data_key_t *key);
2018-05-02 01:01:25 +02:00
inline void fini (void) { items.fini (lock); lock.fini (); }
};
2014-08-14 19:25:55 +02:00
/* object_header */
struct hb_object_header_t
{
hb_reference_count_t ref_count;
mutable hb_user_data_array_t *user_data;
2014-08-14 19:25:55 +02:00
#define HB_OBJECT_HEADER_STATIC {HB_REFERENCE_COUNT_INIT, nullptr}
2014-08-14 19:25:55 +02:00
private:
ASSERT_POD ();
};
2014-08-14 19:15:21 +02:00
/* object */
template <typename Type>
static inline void hb_object_trace (const Type *obj, const char *function)
{
2014-08-14 19:25:55 +02:00
DEBUG_MSG (OBJECT, (void *) obj,
"%s refcount=%d",
function,
obj ? obj->header.ref_count.get_relaxed () : 0);
2014-08-14 19:15:21 +02:00
}
template <typename Type>
static inline Type *hb_object_create (void)
{
Type *obj = (Type *) calloc (1, sizeof (Type));
if (unlikely (!obj))
return obj;
hb_object_init (obj);
hb_object_trace (obj, HB_FUNC);
return obj;
}
template <typename Type>
static inline void hb_object_init (Type *obj)
{
2014-08-14 19:25:55 +02:00
obj->header.ref_count.init (1);
obj->header.user_data = nullptr;
2014-08-14 19:15:21 +02:00
}
template <typename Type>
static inline bool hb_object_is_inert (const Type *obj)
{
return unlikely (obj->header.ref_count.is_inert ());
}
template <typename Type>
static inline bool hb_object_is_valid (const Type *obj)
{
return likely (obj->header.ref_count.is_valid ());
2014-08-14 19:15:21 +02:00
}
template <typename Type>
static inline Type *hb_object_reference (Type *obj)
{
hb_object_trace (obj, HB_FUNC);
2014-08-14 19:25:55 +02:00
if (unlikely (!obj || hb_object_is_inert (obj)))
return obj;
assert (hb_object_is_valid (obj));
2014-08-14 19:25:55 +02:00
obj->header.ref_count.inc ();
2014-08-14 19:15:21 +02:00
return obj;
}
template <typename Type>
static inline bool hb_object_destroy (Type *obj)
{
hb_object_trace (obj, HB_FUNC);
2014-08-14 19:25:55 +02:00
if (unlikely (!obj || hb_object_is_inert (obj)))
return false;
assert (hb_object_is_valid (obj));
2014-08-14 19:25:55 +02:00
if (obj->header.ref_count.dec () != 1)
return false;
2018-05-02 00:39:25 +02:00
hb_object_fini (obj);
return true;
}
template <typename Type>
static inline void hb_object_fini (Type *obj)
{
2018-05-02 01:01:25 +02:00
obj->header.ref_count.fini (); /* Do this before user_data */
if (obj->header.user_data)
{
obj->header.user_data->fini ();
free (obj->header.user_data);
}
2014-08-14 19:15:21 +02:00
}
template <typename Type>
static inline bool hb_object_set_user_data (Type *obj,
hb_user_data_key_t *key,
void * data,
hb_destroy_func_t destroy,
hb_bool_t replace)
{
2014-08-14 19:25:55 +02:00
if (unlikely (!obj || hb_object_is_inert (obj)))
2014-08-14 19:15:21 +02:00
return false;
assert (hb_object_is_valid (obj));
retry:
hb_user_data_array_t *user_data = (hb_user_data_array_t *) hb_atomic_ptr_get (&obj->header.user_data);
if (unlikely (!user_data))
{
user_data = (hb_user_data_array_t *) calloc (sizeof (hb_user_data_array_t), 1);
if (unlikely (!user_data))
return false;
user_data->init ();
if (unlikely (!hb_atomic_ptr_cmpexch (&obj->header.user_data, nullptr, user_data)))
{
user_data->fini ();
free (user_data);
goto retry;
}
}
return user_data->set (key, data, destroy, replace);
2014-08-14 19:15:21 +02:00
}
template <typename Type>
static inline void *hb_object_get_user_data (Type *obj,
hb_user_data_key_t *key)
{
if (unlikely (!obj || hb_object_is_inert (obj) || !obj->header.user_data))
2017-10-15 12:11:08 +02:00
return nullptr;
assert (hb_object_is_valid (obj));
return obj->header.user_data->get (key);
2014-08-14 19:15:21 +02:00
}
2011-04-22 00:35:58 +02:00
#endif /* HB_OBJECT_PRIVATE_HH */