michael@0: /* michael@0: * Copyright © 2007 Chris Wilson michael@0: * Copyright © 2009,2010 Red Hat, Inc. michael@0: * Copyright © 2011,2012 Google, Inc. michael@0: * michael@0: * This is part of HarfBuzz, a text shaping library. michael@0: * michael@0: * Permission is hereby granted, without written agreement and without michael@0: * license or royalty fees, to use, copy, modify, and distribute this michael@0: * software and its documentation for any purpose, provided that the michael@0: * above copyright notice and the following two paragraphs appear in michael@0: * all copies of this software. michael@0: * michael@0: * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR michael@0: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES michael@0: * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN michael@0: * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH michael@0: * DAMAGE. michael@0: * michael@0: * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, michael@0: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND michael@0: * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS michael@0: * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO michael@0: * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. michael@0: * michael@0: * Contributor(s): michael@0: * Chris Wilson michael@0: * Red Hat Author(s): Behdad Esfahbod michael@0: * Google Author(s): Behdad Esfahbod michael@0: */ michael@0: michael@0: #ifndef HB_OBJECT_PRIVATE_HH michael@0: #define HB_OBJECT_PRIVATE_HH michael@0: michael@0: #include "hb-private.hh" michael@0: michael@0: #include "hb-atomic-private.hh" michael@0: #include "hb-mutex-private.hh" michael@0: michael@0: michael@0: /* Debug */ michael@0: michael@0: #ifndef HB_DEBUG_OBJECT michael@0: #define HB_DEBUG_OBJECT (HB_DEBUG+0) michael@0: #endif michael@0: michael@0: michael@0: /* reference_count */ michael@0: michael@0: #define HB_REFERENCE_COUNT_INVALID_VALUE ((hb_atomic_int_t) -1) michael@0: #define HB_REFERENCE_COUNT_INVALID {HB_REFERENCE_COUNT_INVALID_VALUE} michael@0: struct hb_reference_count_t michael@0: { michael@0: hb_atomic_int_t ref_count; michael@0: michael@0: inline void init (int v) { ref_count = v; } michael@0: inline int inc (void) { return hb_atomic_int_add (const_cast (ref_count), 1); } michael@0: inline int dec (void) { return hb_atomic_int_add (const_cast (ref_count), -1); } michael@0: inline void finish (void) { ref_count = HB_REFERENCE_COUNT_INVALID_VALUE; } michael@0: michael@0: inline bool is_invalid (void) const { return ref_count == HB_REFERENCE_COUNT_INVALID_VALUE; } michael@0: michael@0: }; michael@0: michael@0: michael@0: /* user_data */ michael@0: michael@0: #define HB_USER_DATA_ARRAY_INIT {HB_MUTEX_INIT, HB_LOCKABLE_SET_INIT} michael@0: struct hb_user_data_array_t michael@0: { michael@0: /* TODO Add tracing. */ michael@0: michael@0: struct hb_user_data_item_t { michael@0: hb_user_data_key_t *key; michael@0: void *data; michael@0: hb_destroy_func_t destroy; michael@0: michael@0: inline bool operator == (hb_user_data_key_t *other_key) const { return key == other_key; } michael@0: inline bool operator == (hb_user_data_item_t &other) const { return key == other.key; } michael@0: michael@0: void finish (void) { if (destroy) destroy (data); } michael@0: }; michael@0: michael@0: hb_mutex_t lock; michael@0: hb_lockable_set_t items; michael@0: michael@0: inline void init (void) { lock.init (); items.init (); } michael@0: michael@0: HB_INTERNAL bool set (hb_user_data_key_t *key, michael@0: void * data, michael@0: hb_destroy_func_t destroy, michael@0: hb_bool_t replace); michael@0: michael@0: HB_INTERNAL void *get (hb_user_data_key_t *key); michael@0: michael@0: inline void finish (void) { items.finish (lock); lock.finish (); } michael@0: }; michael@0: michael@0: michael@0: /* object_header */ michael@0: michael@0: struct hb_object_header_t michael@0: { michael@0: hb_reference_count_t ref_count; michael@0: hb_user_data_array_t user_data; michael@0: michael@0: #define HB_OBJECT_HEADER_STATIC {HB_REFERENCE_COUNT_INVALID, HB_USER_DATA_ARRAY_INIT} michael@0: michael@0: static inline void *create (unsigned int size) { michael@0: hb_object_header_t *obj = (hb_object_header_t *) calloc (1, size); michael@0: michael@0: if (likely (obj)) michael@0: obj->init (); michael@0: michael@0: return obj; michael@0: } michael@0: michael@0: inline void init (void) { michael@0: ref_count.init (1); michael@0: user_data.init (); michael@0: } michael@0: michael@0: inline bool is_inert (void) const { michael@0: return unlikely (ref_count.is_invalid ()); michael@0: } michael@0: michael@0: inline void reference (void) { michael@0: if (unlikely (!this || this->is_inert ())) michael@0: return; michael@0: ref_count.inc (); michael@0: } michael@0: michael@0: inline bool destroy (void) { michael@0: if (unlikely (!this || this->is_inert ())) michael@0: return false; michael@0: if (ref_count.dec () != 1) michael@0: return false; michael@0: michael@0: ref_count.finish (); /* Do this before user_data */ michael@0: user_data.finish (); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: inline bool set_user_data (hb_user_data_key_t *key, michael@0: void * data, michael@0: hb_destroy_func_t destroy_func, michael@0: hb_bool_t replace) { michael@0: if (unlikely (!this || this->is_inert ())) michael@0: return false; michael@0: michael@0: return user_data.set (key, data, destroy_func, replace); michael@0: } michael@0: michael@0: inline void *get_user_data (hb_user_data_key_t *key) { michael@0: if (unlikely (!this || this->is_inert ())) michael@0: return NULL; michael@0: michael@0: return user_data.get (key); michael@0: } michael@0: michael@0: inline void trace (const char *function) const { michael@0: if (unlikely (!this)) return; michael@0: /* TODO We cannot use DEBUG_MSG_FUNC here since that one currently only michael@0: * prints the class name and throws away the template info. */ michael@0: DEBUG_MSG (OBJECT, (void *) this, michael@0: "%s refcount=%d", michael@0: function, michael@0: this ? ref_count.ref_count : 0); michael@0: } michael@0: michael@0: private: michael@0: ASSERT_POD (); michael@0: }; michael@0: michael@0: michael@0: /* object */ michael@0: michael@0: template michael@0: static inline void hb_object_trace (const Type *obj, const char *function) michael@0: { michael@0: obj->header.trace (function); michael@0: } michael@0: template michael@0: static inline Type *hb_object_create (void) michael@0: { michael@0: Type *obj = (Type *) hb_object_header_t::create (sizeof (Type)); michael@0: hb_object_trace (obj, HB_FUNC); michael@0: return obj; michael@0: } michael@0: template michael@0: static inline bool hb_object_is_inert (const Type *obj) michael@0: { michael@0: return unlikely (obj->header.is_inert ()); michael@0: } michael@0: template michael@0: static inline Type *hb_object_reference (Type *obj) michael@0: { michael@0: hb_object_trace (obj, HB_FUNC); michael@0: obj->header.reference (); michael@0: return obj; michael@0: } michael@0: template michael@0: static inline bool hb_object_destroy (Type *obj) michael@0: { michael@0: hb_object_trace (obj, HB_FUNC); michael@0: return obj->header.destroy (); michael@0: } michael@0: template michael@0: static inline bool hb_object_set_user_data (Type *obj, michael@0: hb_user_data_key_t *key, michael@0: void * data, michael@0: hb_destroy_func_t destroy, michael@0: hb_bool_t replace) michael@0: { michael@0: return obj->header.set_user_data (key, data, destroy, replace); michael@0: } michael@0: michael@0: template michael@0: static inline void *hb_object_get_user_data (Type *obj, michael@0: hb_user_data_key_t *key) michael@0: { michael@0: return obj->header.get_user_data (key); michael@0: } michael@0: michael@0: michael@0: #endif /* HB_OBJECT_PRIVATE_HH */