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 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
|
|
|
*/
|
|
|
|
|
2011-04-21 00:50:27 +02:00
|
|
|
#ifndef HB_OBJECT_PRIVATE_HH
|
|
|
|
#define HB_OBJECT_PRIVATE_HH
|
2009-07-30 21:33:57 +02:00
|
|
|
|
2011-04-21 00:50:27 +02:00
|
|
|
#include "hb-private.hh"
|
2009-08-04 03:40:20 +02:00
|
|
|
|
2011-05-11 01:12:49 +02:00
|
|
|
#include "hb-mutex-private.hh"
|
|
|
|
|
2009-11-06 01:37:58 +01:00
|
|
|
|
|
|
|
|
2010-04-28 19:29:55 +02:00
|
|
|
/* Debug */
|
|
|
|
|
|
|
|
#ifndef HB_DEBUG_OBJECT
|
2010-11-03 20:11:04 +01:00
|
|
|
#define HB_DEBUG_OBJECT (HB_DEBUG+0)
|
2010-04-28 19:29:55 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2011-05-05 01:27:37 +02:00
|
|
|
/* atomic_int */
|
2011-05-03 06:49:06 +02:00
|
|
|
|
|
|
|
/* We need external help for these */
|
|
|
|
|
2012-02-23 21:06:16 +01:00
|
|
|
#if !defined(HB_NO_MT) && defined(HAVE_GLIB)
|
2011-05-03 06:49:06 +02:00
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
typedef volatile int hb_atomic_int_t;
|
2011-09-16 18:30:50 +02:00
|
|
|
#if GLIB_CHECK_VERSION(2,29,5)
|
|
|
|
#define hb_atomic_int_add(AI, V) g_atomic_int_add (&(AI), V)
|
|
|
|
#else
|
|
|
|
#define hb_atomic_int_add(AI, V) g_atomic_int_exchange_and_add (&(AI), V)
|
|
|
|
#endif
|
|
|
|
#define hb_atomic_int_get(AI) g_atomic_int_get (&(AI))
|
2011-05-03 06:49:06 +02:00
|
|
|
|
|
|
|
|
2012-02-23 21:06:16 +01:00
|
|
|
#elif !defined(HB_NO_MT) && defined(_MSC_VER) && _MSC_VER >= 1600
|
2011-05-03 06:49:06 +02:00
|
|
|
|
|
|
|
#include <intrin.h>
|
|
|
|
typedef long hb_atomic_int_t;
|
2011-09-16 18:30:50 +02:00
|
|
|
#define hb_atomic_int_add(AI, V) _InterlockedExchangeAdd (&(AI), V)
|
|
|
|
#define hb_atomic_int_get(AI) (_ReadBarrier (), (AI))
|
2011-05-03 06:49:06 +02:00
|
|
|
|
2012-02-24 19:51:09 +01:00
|
|
|
#elif !defined(HB_NO_MT) && defined(__APPLE__)
|
|
|
|
|
|
|
|
#include <libkern/OSAtomic.h>
|
|
|
|
typedef int32_t hb_atomic_int_t;
|
2012-03-02 02:30:29 +01:00
|
|
|
#define hb_atomic_int_add(AI, V) (OSAtomicAdd32Barrier((V), &(AI)), (AI) - (V))
|
2012-02-24 19:51:09 +01:00
|
|
|
#define hb_atomic_int_get(AI) OSAtomicAdd32Barrier(0, &(AI))
|
2011-05-03 06:49:06 +02:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2012-02-23 21:06:16 +01:00
|
|
|
#define HB_ATOMIC_INT_NIL 1
|
2011-05-03 06:49:06 +02:00
|
|
|
|
|
|
|
typedef volatile int hb_atomic_int_t;
|
2011-09-16 18:30:50 +02:00
|
|
|
#define hb_atomic_int_add(AI, V) ((AI) += (V), (AI) - (V))
|
|
|
|
#define hb_atomic_int_get(AI) (AI)
|
2011-05-03 06:49:06 +02:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-04-28 03:45:51 +02:00
|
|
|
/* reference_count */
|
2011-04-28 03:22:32 +02:00
|
|
|
|
2011-04-28 03:45:51 +02:00
|
|
|
typedef struct {
|
|
|
|
hb_atomic_int_t ref_count;
|
2011-04-28 03:22:32 +02:00
|
|
|
|
2011-04-28 03:45:51 +02:00
|
|
|
#define HB_REFERENCE_COUNT_INVALID_VALUE ((hb_atomic_int_t) -1)
|
|
|
|
#define HB_REFERENCE_COUNT_INVALID {HB_REFERENCE_COUNT_INVALID_VALUE}
|
2011-04-28 03:22:32 +02:00
|
|
|
|
2011-04-28 03:45:51 +02:00
|
|
|
inline void init (int v) { ref_count = v; /* non-atomic is fine */ }
|
2011-09-16 18:30:50 +02:00
|
|
|
inline int inc (void) { return hb_atomic_int_add (ref_count, 1); }
|
|
|
|
inline int dec (void) { return hb_atomic_int_add (ref_count, -1); }
|
2011-04-28 03:22:32 +02:00
|
|
|
|
2012-02-24 19:51:09 +01:00
|
|
|
inline int get (void) { return hb_atomic_int_get (ref_count); }
|
|
|
|
inline int get_unsafe (void) const { return ref_count; }
|
|
|
|
inline bool is_invalid (void) const { return ref_count == HB_REFERENCE_COUNT_INVALID_VALUE; }
|
2011-04-28 03:22:32 +02:00
|
|
|
|
2011-04-28 03:45:51 +02:00
|
|
|
} hb_reference_count_t;
|
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
|
|
|
|
2011-05-05 20:47:53 +02:00
|
|
|
struct hb_user_data_array_t {
|
2011-04-28 03:22:32 +02:00
|
|
|
|
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
|
|
|
|
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; }
|
2011-04-28 03:22:32 +02:00
|
|
|
|
2011-05-05 20:47:53 +02:00
|
|
|
void finish (void) { if (destroy) destroy (data); }
|
|
|
|
};
|
2011-04-28 03:22:32 +02:00
|
|
|
|
2011-05-11 01:12:49 +02:00
|
|
|
hb_lockable_set_t<hb_user_data_item_t, hb_static_mutex_t> items;
|
2011-04-28 03:22:32 +02:00
|
|
|
|
2011-05-05 21:28:37 +02:00
|
|
|
HB_INTERNAL bool set (hb_user_data_key_t *key,
|
|
|
|
void * data,
|
2011-08-09 00:43:24 +02:00
|
|
|
hb_destroy_func_t destroy,
|
|
|
|
hb_bool_t replace);
|
2011-04-28 03:22:32 +02:00
|
|
|
|
2011-05-05 21:28:37 +02:00
|
|
|
HB_INTERNAL void *get (hb_user_data_key_t *key);
|
2011-04-28 03:22:32 +02:00
|
|
|
|
2011-05-11 01:12:49 +02:00
|
|
|
HB_INTERNAL void finish (void);
|
2011-04-28 03:22:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-04-28 03:45:51 +02:00
|
|
|
/* object_header */
|
2011-04-28 03:22:32 +02:00
|
|
|
|
2011-04-22 00:24:02 +02:00
|
|
|
typedef struct _hb_object_header_t hb_object_header_t;
|
2010-04-28 19:29:55 +02:00
|
|
|
|
2011-04-22 00:24:02 +02:00
|
|
|
struct _hb_object_header_t {
|
|
|
|
hb_reference_count_t ref_count;
|
2011-04-28 03:22:32 +02:00
|
|
|
hb_user_data_array_t user_data;
|
2010-04-28 19:29:55 +02:00
|
|
|
|
2011-04-22 00:24:02 +02:00
|
|
|
#define HB_OBJECT_HEADER_STATIC {HB_REFERENCE_COUNT_INVALID}
|
2010-04-28 19:29:55 +02:00
|
|
|
|
2011-04-22 00:24:02 +02:00
|
|
|
static inline void *create (unsigned int size) {
|
|
|
|
hb_object_header_t *obj = (hb_object_header_t *) calloc (1, size);
|
2009-08-02 01:30:31 +02:00
|
|
|
|
2011-04-22 00:24:02 +02:00
|
|
|
if (likely (obj))
|
|
|
|
obj->init ();
|
2009-08-02 01:54:49 +02:00
|
|
|
|
2011-04-22 00:24:02 +02:00
|
|
|
return obj;
|
|
|
|
}
|
2009-08-02 02:29:22 +02:00
|
|
|
|
2011-04-22 00:24:02 +02:00
|
|
|
inline void init (void) {
|
|
|
|
ref_count.init (1);
|
|
|
|
}
|
|
|
|
|
2011-04-28 03:22:32 +02:00
|
|
|
inline bool is_inert (void) const {
|
|
|
|
return unlikely (ref_count.is_invalid ());
|
|
|
|
}
|
2011-04-22 00:24:02 +02:00
|
|
|
|
|
|
|
inline void reference (void) {
|
|
|
|
if (unlikely (!this || this->is_inert ()))
|
|
|
|
return;
|
|
|
|
ref_count.inc ();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool destroy (void) {
|
|
|
|
if (unlikely (!this || this->is_inert ()))
|
|
|
|
return false;
|
2011-04-28 03:22:32 +02:00
|
|
|
if (ref_count.dec () != 1)
|
|
|
|
return false;
|
|
|
|
|
2011-05-12 07:19:39 +02:00
|
|
|
ref_count.init (HB_REFERENCE_COUNT_INVALID_VALUE);
|
|
|
|
|
2011-04-28 03:22:32 +02:00
|
|
|
user_data.finish ();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool set_user_data (hb_user_data_key_t *key,
|
|
|
|
void * data,
|
2011-08-09 00:43:24 +02:00
|
|
|
hb_destroy_func_t destroy_func,
|
|
|
|
hb_bool_t replace) {
|
2011-04-28 03:22:32 +02:00
|
|
|
if (unlikely (!this || this->is_inert ()))
|
|
|
|
return false;
|
|
|
|
|
2011-08-09 00:43:24 +02:00
|
|
|
return user_data.set (key, data, destroy_func, replace);
|
2011-04-28 03:22:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void *get_user_data (hb_user_data_key_t *key) {
|
|
|
|
return user_data.get (key);
|
2011-04-22 00:24:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void trace (const char *function) const {
|
2012-05-11 03:35:08 +02:00
|
|
|
if (unlikely (!this)) return;
|
2011-07-26 02:25:44 +02:00
|
|
|
DEBUG_MSG (OBJECT, (void *) this,
|
|
|
|
"refcount=%d %s",
|
2012-02-24 19:51:09 +01:00
|
|
|
this ? ref_count.get_unsafe () : 0,
|
2011-07-26 02:25:44 +02:00
|
|
|
function);
|
2011-04-22 00:24:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-04-22 00:35:58 +02:00
|
|
|
|
2011-04-28 03:45:51 +02:00
|
|
|
|
|
|
|
/* object */
|
|
|
|
|
2011-04-22 00:35:58 +02:00
|
|
|
template <typename Type>
|
2011-04-27 22:12:12 +02:00
|
|
|
static inline void hb_object_trace (const Type *obj, const char *function)
|
|
|
|
{
|
|
|
|
obj->header.trace (function);
|
|
|
|
}
|
2011-04-22 00:35:58 +02:00
|
|
|
template <typename Type>
|
2011-05-03 01:53:39 +02:00
|
|
|
static inline Type *hb_object_create (void)
|
2011-04-27 22:12:12 +02:00
|
|
|
{
|
|
|
|
Type *obj = (Type *) hb_object_header_t::create (sizeof (Type));
|
|
|
|
hb_object_trace (obj, HB_FUNC);
|
|
|
|
return obj;
|
|
|
|
}
|
2011-04-22 00:35:58 +02:00
|
|
|
template <typename Type>
|
2011-04-27 22:12:12 +02:00
|
|
|
static inline bool hb_object_is_inert (const Type *obj)
|
|
|
|
{
|
2011-05-03 01:53:39 +02:00
|
|
|
return unlikely (obj->header.is_inert ());
|
2011-04-27 22:12:12 +02:00
|
|
|
}
|
2011-04-22 00:35:58 +02:00
|
|
|
template <typename Type>
|
2011-04-27 22:12:12 +02:00
|
|
|
static inline Type *hb_object_reference (Type *obj)
|
|
|
|
{
|
|
|
|
hb_object_trace (obj, HB_FUNC);
|
|
|
|
obj->header.reference ();
|
|
|
|
return obj;
|
|
|
|
}
|
2011-04-22 00:35:58 +02:00
|
|
|
template <typename Type>
|
2011-04-27 22:12:12 +02:00
|
|
|
static inline bool hb_object_destroy (Type *obj)
|
|
|
|
{
|
|
|
|
hb_object_trace (obj, HB_FUNC);
|
|
|
|
return obj->header.destroy ();
|
|
|
|
}
|
2011-04-28 03:45:51 +02:00
|
|
|
template <typename Type>
|
|
|
|
static inline bool hb_object_set_user_data (Type *obj,
|
|
|
|
hb_user_data_key_t *key,
|
|
|
|
void * data,
|
2011-08-09 00:43:24 +02:00
|
|
|
hb_destroy_func_t destroy,
|
|
|
|
hb_bool_t replace)
|
2011-04-28 03:45:51 +02:00
|
|
|
{
|
2011-08-09 00:43:24 +02:00
|
|
|
return obj->header.set_user_data (key, data, destroy, replace);
|
2011-04-28 03:45:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Type>
|
|
|
|
static inline void *hb_object_get_user_data (Type *obj,
|
|
|
|
hb_user_data_key_t *key)
|
|
|
|
{
|
|
|
|
return obj->header.get_user_data (key);
|
|
|
|
}
|
2011-04-27 22:12:12 +02:00
|
|
|
|
2011-04-22 00:35:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2010-04-28 19:29:55 +02:00
|
|
|
|
2011-04-21 00:50:27 +02:00
|
|
|
#endif /* HB_OBJECT_PRIVATE_HH */
|