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.
|
2012-06-05 22:34:49 +02:00
|
|
|
* 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
|
|
|
*/
|
|
|
|
|
2018-08-26 07:36:36 +02:00
|
|
|
#ifndef HB_OBJECT_HH
|
|
|
|
#define HB_OBJECT_HH
|
2009-07-30 21:33:57 +02:00
|
|
|
|
2018-08-26 07:36:36 +02:00
|
|
|
#include "hb.hh"
|
|
|
|
#include "hb-atomic.hh"
|
|
|
|
#include "hb-mutex.hh"
|
|
|
|
#include "hb-vector.hh"
|
2011-05-11 01:12:49 +02:00
|
|
|
|
2009-11-06 01:37:58 +01:00
|
|
|
|
2018-08-06 13:42:46 +02:00
|
|
|
/*
|
|
|
|
* Lockable set
|
|
|
|
*/
|
|
|
|
|
|
|
|
template <typename item_t, typename lock_t>
|
|
|
|
struct hb_lockable_set_t
|
|
|
|
{
|
2018-12-27 23:56:22 +01:00
|
|
|
hb_vector_t<item_t> items;
|
2018-08-06 13:42:46 +02:00
|
|
|
|
2018-12-17 19:01:01 +01:00
|
|
|
void init () { items.init (); }
|
2018-08-06 13:42:46 +02:00
|
|
|
|
|
|
|
template <typename T>
|
2018-12-16 20:08:10 +01:00
|
|
|
item_t *replace_or_insert (T v, lock_t &l, bool replace)
|
2018-08-06 13:42:46 +02:00
|
|
|
{
|
|
|
|
l.lock ();
|
2022-01-19 19:14:25 +01:00
|
|
|
item_t *item = items.lsearch (v);
|
2018-08-06 13:42:46 +02:00
|
|
|
if (item) {
|
|
|
|
if (replace) {
|
|
|
|
item_t old = *item;
|
|
|
|
*item = v;
|
|
|
|
l.unlock ();
|
|
|
|
old.fini ();
|
|
|
|
}
|
|
|
|
else {
|
2019-08-24 15:27:14 +02:00
|
|
|
item = nullptr;
|
2018-08-06 13:42:46 +02:00
|
|
|
l.unlock ();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
item = items.push (v);
|
|
|
|
l.unlock ();
|
|
|
|
}
|
2023-01-29 18:03:52 +01:00
|
|
|
return items.in_error () ? nullptr : item;
|
2018-08-06 13:42:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2018-12-16 20:08:10 +01:00
|
|
|
void remove (T v, lock_t &l)
|
2018-08-06 13:42:46 +02:00
|
|
|
{
|
|
|
|
l.lock ();
|
2022-01-19 19:14:25 +01:00
|
|
|
item_t *item = items.lsearch (v);
|
2018-12-22 00:46:51 +01:00
|
|
|
if (item)
|
|
|
|
{
|
2018-08-06 13:42:46 +02:00
|
|
|
item_t old = *item;
|
2022-11-20 22:10:39 +01:00
|
|
|
*item = std::move (items.tail ());
|
2018-08-06 13:42:46 +02:00
|
|
|
items.pop ();
|
|
|
|
l.unlock ();
|
|
|
|
old.fini ();
|
|
|
|
} else {
|
|
|
|
l.unlock ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2018-12-16 20:08:10 +01:00
|
|
|
bool find (T v, item_t *i, lock_t &l)
|
2018-08-06 13:42:46 +02:00
|
|
|
{
|
|
|
|
l.lock ();
|
2022-01-19 19:14:25 +01:00
|
|
|
item_t *item = items.lsearch (v);
|
2018-08-06 13:42:46 +02:00
|
|
|
if (item)
|
|
|
|
*i = *item;
|
|
|
|
l.unlock ();
|
|
|
|
return !!item;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2018-12-16 20:08:10 +01:00
|
|
|
item_t *find_or_insert (T v, lock_t &l)
|
2018-08-06 13:42:46 +02:00
|
|
|
{
|
|
|
|
l.lock ();
|
|
|
|
item_t *item = items.find (v);
|
|
|
|
if (!item) {
|
|
|
|
item = items.push (v);
|
|
|
|
}
|
|
|
|
l.unlock ();
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2018-12-16 20:08:10 +01:00
|
|
|
void fini (lock_t &l)
|
2018-08-06 13:42:46 +02:00
|
|
|
{
|
2018-12-22 00:46:51 +01:00
|
|
|
if (!items.length)
|
|
|
|
{
|
|
|
|
/* No need to lock. */
|
2018-08-06 13:42:46 +02:00
|
|
|
items.fini ();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
l.lock ();
|
2018-12-22 00:46:51 +01:00
|
|
|
while (items.length)
|
|
|
|
{
|
2022-11-20 22:10:39 +01:00
|
|
|
item_t old = items.tail ();
|
2018-12-22 00:46:51 +01:00
|
|
|
items.pop ();
|
|
|
|
l.unlock ();
|
|
|
|
old.fini ();
|
|
|
|
l.lock ();
|
2018-08-06 13:42:46 +02:00
|
|
|
}
|
|
|
|
items.fini ();
|
|
|
|
l.unlock ();
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reference-count.
|
|
|
|
*/
|
2011-04-28 03:22:32 +02:00
|
|
|
|
2012-06-03 21:52:51 +02:00
|
|
|
struct hb_reference_count_t
|
|
|
|
{
|
2018-08-09 09:22:37 +02:00
|
|
|
mutable hb_atomic_int_t ref_count;
|
2011-04-28 03:22:32 +02:00
|
|
|
|
2022-08-03 20:54:03 +02:00
|
|
|
void init (int v = 1) { ref_count = v; }
|
|
|
|
int get_relaxed () const { return ref_count; }
|
2018-12-17 19:01:01 +01:00
|
|
|
int inc () const { return ref_count.inc (); }
|
|
|
|
int dec () const { return ref_count.dec (); }
|
2022-08-03 20:54:03 +02:00
|
|
|
void fini () { ref_count = -0x0000DEAD; }
|
2017-11-14 19:59:54 +01:00
|
|
|
|
2022-08-03 20:54:03 +02:00
|
|
|
bool is_inert () const { return !ref_count; }
|
|
|
|
bool is_valid () const { return ref_count > 0; }
|
2012-06-03 21:52:51 +02:00
|
|
|
};
|
2011-04-28 03:22:32 +02:00
|
|
|
|
|
|
|
|
2011-04-28 03:45:51 +02:00
|
|
|
/* user_data */
|
2011-04-28 03:22:32 +02:00
|
|
|
|
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 19:21:04 +02:00
|
|
|
|
2020-07-09 01:17:40 +02:00
|
|
|
bool operator == (const hb_user_data_key_t *other_key) const { return key == other_key; }
|
|
|
|
bool operator == (const hb_user_data_item_t &other) const { return key == other.key; }
|
2011-04-28 03:22:32 +02:00
|
|
|
|
2018-12-17 19:01:01 +01:00
|
|
|
void fini () { if (destroy) destroy (data); }
|
2011-05-05 20:47:53 +02:00
|
|
|
};
|
2011-04-28 03:22:32 +02:00
|
|
|
|
2012-12-03 23:35:54 +01:00
|
|
|
hb_mutex_t lock;
|
2012-06-05 21:37:19 +02:00
|
|
|
hb_lockable_set_t<hb_user_data_item_t, hb_mutex_t> items;
|
2011-04-28 03:22:32 +02:00
|
|
|
|
2018-12-17 19:01:01 +01:00
|
|
|
void init () { lock.init (); items.init (); }
|
2012-06-05 20:17:32 +02:00
|
|
|
|
2023-01-29 18:10:48 +01:00
|
|
|
void fini () { items.fini (lock); lock.fini (); }
|
2011-04-28 03:22:32 +02:00
|
|
|
|
2023-01-29 18:10:48 +01:00
|
|
|
bool set (hb_user_data_key_t *key,
|
|
|
|
void * data,
|
|
|
|
hb_destroy_func_t destroy,
|
|
|
|
hb_bool_t replace)
|
|
|
|
{
|
|
|
|
if (!key)
|
|
|
|
return false;
|
2011-04-28 03:22:32 +02:00
|
|
|
|
2023-01-29 18:10:48 +01:00
|
|
|
if (replace) {
|
|
|
|
if (!data && !destroy) {
|
|
|
|
items.remove (key, lock);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hb_user_data_item_t item = {key, data, destroy};
|
|
|
|
bool ret = !!items.replace_or_insert (item, lock, (bool) replace);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *get (hb_user_data_key_t *key)
|
|
|
|
{
|
|
|
|
hb_user_data_item_t item = {nullptr, nullptr, nullptr};
|
|
|
|
|
|
|
|
return items.find (key, &item, lock) ? item.data : nullptr;
|
|
|
|
}
|
2011-04-28 03:22:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-08-06 13:42:46 +02:00
|
|
|
/*
|
|
|
|
* Object header
|
|
|
|
*/
|
2014-08-14 19:25:55 +02:00
|
|
|
|
|
|
|
struct hb_object_header_t
|
|
|
|
{
|
|
|
|
hb_reference_count_t ref_count;
|
2020-06-29 12:51:09 +02:00
|
|
|
mutable hb_atomic_int_t writable = 0;
|
2018-08-09 09:22:37 +02:00
|
|
|
hb_atomic_ptr_t<hb_user_data_array_t> user_data;
|
2021-08-17 04:17:53 +02:00
|
|
|
|
|
|
|
bool is_inert () const { return !ref_count.get_relaxed (); }
|
2014-08-14 19:25:55 +02:00
|
|
|
};
|
2020-06-29 12:51:09 +02:00
|
|
|
#define HB_OBJECT_HEADER_STATIC {}
|
2014-08-14 19:25:55 +02:00
|
|
|
|
|
|
|
|
2018-08-06 13:42:46 +02:00
|
|
|
/*
|
|
|
|
* Object
|
|
|
|
*/
|
2014-08-14 19:15:21 +02:00
|
|
|
|
|
|
|
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,
|
2018-08-01 06:05:51 +02:00
|
|
|
obj ? obj->header.ref_count.get_relaxed () : 0);
|
2014-08-14 19:15:21 +02:00
|
|
|
}
|
|
|
|
|
2022-08-19 00:15:54 +02:00
|
|
|
template <typename Type, typename ...Ts>
|
|
|
|
static inline Type *hb_object_create (Ts... ds)
|
2014-08-14 19:15:21 +02:00
|
|
|
{
|
2021-07-08 18:58:50 +02:00
|
|
|
Type *obj = (Type *) hb_calloc (1, sizeof (Type));
|
2014-08-14 19:15:21 +02:00
|
|
|
|
|
|
|
if (unlikely (!obj))
|
|
|
|
return obj;
|
|
|
|
|
2022-08-19 00:15:54 +02:00
|
|
|
new (obj) Type (std::forward<Ts> (ds)...);
|
2022-07-20 22:57:32 +02:00
|
|
|
|
2014-08-14 19:15:21 +02:00
|
|
|
hb_object_init (obj);
|
|
|
|
hb_object_trace (obj, HB_FUNC);
|
2022-07-20 22:57:32 +02:00
|
|
|
|
2014-08-14 19:15:21 +02:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
template <typename Type>
|
|
|
|
static inline void hb_object_init (Type *obj)
|
|
|
|
{
|
2018-08-09 09:22:37 +02:00
|
|
|
obj->header.ref_count.init ();
|
2022-08-03 20:54:03 +02:00
|
|
|
obj->header.writable = true;
|
2018-08-09 09:22:37 +02:00
|
|
|
obj->header.user_data.init ();
|
2014-08-14 19:15:21 +02:00
|
|
|
}
|
|
|
|
template <typename Type>
|
2015-08-30 18:29:21 +02:00
|
|
|
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>
|
2018-11-03 19:28:55 +01:00
|
|
|
static inline bool hb_object_is_immutable (const Type *obj)
|
|
|
|
{
|
2022-08-03 20:54:03 +02:00
|
|
|
return !obj->header.writable;
|
2018-11-03 19:28:55 +01:00
|
|
|
}
|
|
|
|
template <typename Type>
|
2018-11-03 19:51:38 +01:00
|
|
|
static inline void hb_object_make_immutable (const Type *obj)
|
2018-11-03 19:28:55 +01:00
|
|
|
{
|
2022-08-03 20:54:03 +02:00
|
|
|
obj->header.writable = false;
|
2018-11-03 19:28:55 +01:00
|
|
|
}
|
|
|
|
template <typename Type>
|
2014-08-14 19:15:21 +02:00
|
|
|
static inline Type *hb_object_reference (Type *obj)
|
|
|
|
{
|
|
|
|
hb_object_trace (obj, HB_FUNC);
|
2021-08-17 04:17:53 +02:00
|
|
|
if (unlikely (!obj || obj->header.is_inert ()))
|
2014-08-14 19:25:55 +02:00
|
|
|
return obj;
|
2015-08-30 18:29:21 +02:00
|
|
|
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);
|
2021-08-17 04:17:53 +02:00
|
|
|
if (unlikely (!obj || obj->header.is_inert ()))
|
2014-08-14 19:25:55 +02:00
|
|
|
return false;
|
2015-08-30 18:29:21 +02:00
|
|
|
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);
|
2022-07-20 23:03:20 +02:00
|
|
|
|
2022-08-18 23:21:48 +02:00
|
|
|
if (!std::is_trivially_destructible<Type>::value)
|
|
|
|
obj->~Type ();
|
2022-07-20 23:03:20 +02:00
|
|
|
|
2018-05-02 00:39:25 +02:00
|
|
|
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 */
|
2022-08-03 20:43:28 +02:00
|
|
|
hb_user_data_array_t *user_data = obj->header.user_data.get_acquire ();
|
2018-08-09 09:22:37 +02:00
|
|
|
if (user_data)
|
2018-05-08 10:47:05 +02:00
|
|
|
{
|
2018-08-09 09:22:37 +02:00
|
|
|
user_data->fini ();
|
2021-07-08 18:58:50 +02:00
|
|
|
hb_free (user_data);
|
2022-07-20 23:03:20 +02:00
|
|
|
obj->header.user_data.set_relaxed (nullptr);
|
2018-05-08 10:47:05 +02:00
|
|
|
}
|
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)
|
|
|
|
{
|
2021-08-17 04:17:53 +02:00
|
|
|
if (unlikely (!obj || obj->header.is_inert ()))
|
2014-08-14 19:15:21 +02:00
|
|
|
return false;
|
2015-08-30 18:29:21 +02:00
|
|
|
assert (hb_object_is_valid (obj));
|
2018-05-08 10:47:05 +02:00
|
|
|
|
|
|
|
retry:
|
2022-08-03 20:43:28 +02:00
|
|
|
hb_user_data_array_t *user_data = obj->header.user_data.get_acquire ();
|
2018-05-08 10:47:05 +02:00
|
|
|
if (unlikely (!user_data))
|
|
|
|
{
|
2021-07-08 18:58:50 +02:00
|
|
|
user_data = (hb_user_data_array_t *) hb_calloc (sizeof (hb_user_data_array_t), 1);
|
2018-05-08 10:47:05 +02:00
|
|
|
if (unlikely (!user_data))
|
|
|
|
return false;
|
|
|
|
user_data->init ();
|
2018-08-09 09:22:37 +02:00
|
|
|
if (unlikely (!obj->header.user_data.cmpexch (nullptr, user_data)))
|
2018-05-08 10:47:05 +02:00
|
|
|
{
|
|
|
|
user_data->fini ();
|
2021-07-08 18:58:50 +02:00
|
|
|
hb_free (user_data);
|
2018-05-08 10:47:05 +02:00
|
|
|
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)
|
|
|
|
{
|
2021-08-17 04:17:53 +02:00
|
|
|
if (unlikely (!obj || obj->header.is_inert ()))
|
2017-10-15 12:11:08 +02:00
|
|
|
return nullptr;
|
2015-08-30 18:29:21 +02:00
|
|
|
assert (hb_object_is_valid (obj));
|
2022-08-03 20:43:28 +02:00
|
|
|
hb_user_data_array_t *user_data = obj->header.user_data.get_acquire ();
|
2018-08-09 09:53:25 +02:00
|
|
|
if (!user_data)
|
|
|
|
return nullptr;
|
|
|
|
return user_data->get (key);
|
2014-08-14 19:15:21 +02:00
|
|
|
}
|
|
|
|
|
2011-04-22 00:35:58 +02:00
|
|
|
|
2018-08-26 07:36:36 +02:00
|
|
|
#endif /* HB_OBJECT_HH */
|