hb-object-private.hh revision d5476a30a10da5e54783c8dbf04340225a9a00d7
1/* 2 * Copyright © 2007 Chris Wilson 3 * Copyright © 2009,2010 Red Hat, Inc. 4 * Copyright © 2011 Google, Inc. 5 * 6 * This is part of HarfBuzz, a text shaping library. 7 * 8 * Permission is hereby granted, without written agreement and without 9 * license or royalty fees, to use, copy, modify, and distribute this 10 * software and its documentation for any purpose, provided that the 11 * above copyright notice and the following two paragraphs appear in 12 * all copies of this software. 13 * 14 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR 15 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 16 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN 17 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 18 * DAMAGE. 19 * 20 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, 21 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 22 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS 23 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO 24 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 25 * 26 * Contributor(s): 27 * Chris Wilson <chris@chris-wilson.co.uk> 28 * Red Hat Author(s): Behdad Esfahbod 29 * Google Author(s): Behdad Esfahbod 30 */ 31 32#ifndef HB_OBJECT_PRIVATE_HH 33#define HB_OBJECT_PRIVATE_HH 34 35#include "hb-private.hh" 36 37#include "hb-mutex-private.hh" 38 39 40 41/* Debug */ 42 43#ifndef HB_DEBUG_OBJECT 44#define HB_DEBUG_OBJECT (HB_DEBUG+0) 45#endif 46 47 48/* atomic_int */ 49 50/* We need external help for these */ 51 52#ifdef HAVE_GLIB 53 54#include <glib.h> 55 56typedef volatile int hb_atomic_int_t; 57#if GLIB_CHECK_VERSION(2,29,5) 58#define hb_atomic_int_add(AI, V) g_atomic_int_add (&(AI), V) 59#else 60#define hb_atomic_int_add(AI, V) g_atomic_int_exchange_and_add (&(AI), V) 61#endif 62#define hb_atomic_int_get(AI) g_atomic_int_get (&(AI)) 63#define hb_atomic_int_set(AI, V) g_atomic_int_set (&(AI), V) 64 65 66#elif defined(_MSC_VER) 67 68#include <intrin.h> 69 70typedef long hb_atomic_int_t; 71#define hb_atomic_int_add(AI, V) _InterlockedExchangeAdd (&(AI), V) 72#define hb_atomic_int_get(AI) (_ReadBarrier (), (AI)) 73#define hb_atomic_int_set(AI, V) ((void) _InterlockedExchange (&(AI), (V))) 74 75 76#else 77 78#warning "Could not find any system to define atomic_int macros, library will NOT be thread-safe" 79 80typedef volatile int hb_atomic_int_t; 81#define hb_atomic_int_add(AI, V) ((AI) += (V), (AI) - (V)) 82#define hb_atomic_int_get(AI) (AI) 83#define hb_atomic_int_set(AI, V) ((void) ((AI) = (V))) 84 85 86#endif 87 88 89 90 91/* reference_count */ 92 93typedef struct { 94 hb_atomic_int_t ref_count; 95 96#define HB_REFERENCE_COUNT_INVALID_VALUE ((hb_atomic_int_t) -1) 97#define HB_REFERENCE_COUNT_INVALID {HB_REFERENCE_COUNT_INVALID_VALUE} 98 99 inline void init (int v) { ref_count = v; /* non-atomic is fine */ } 100 inline int inc (void) { return hb_atomic_int_add (ref_count, 1); } 101 inline int dec (void) { return hb_atomic_int_add (ref_count, -1); } 102 inline void set (int v) { hb_atomic_int_set (ref_count, v); } 103 104 inline int get (void) const { return hb_atomic_int_get (ref_count); } 105 inline bool is_invalid (void) const { return get () == HB_REFERENCE_COUNT_INVALID_VALUE; } 106 107} hb_reference_count_t; 108 109 110/* user_data */ 111 112struct hb_user_data_array_t { 113 114 struct hb_user_data_item_t { 115 hb_user_data_key_t *key; 116 void *data; 117 hb_destroy_func_t destroy; 118 119 inline bool operator == (hb_user_data_key_t *other_key) const { return key == other_key; } 120 inline bool operator == (hb_user_data_item_t &other) const { return key == other.key; } 121 122 void finish (void) { if (destroy) destroy (data); } 123 }; 124 125 hb_lockable_set_t<hb_user_data_item_t, hb_static_mutex_t> items; 126 127 HB_INTERNAL bool set (hb_user_data_key_t *key, 128 void * data, 129 hb_destroy_func_t destroy, 130 hb_bool_t replace); 131 132 HB_INTERNAL void *get (hb_user_data_key_t *key); 133 134 HB_INTERNAL void finish (void); 135}; 136 137 138/* object_header */ 139 140typedef struct _hb_object_header_t hb_object_header_t; 141 142struct _hb_object_header_t { 143 hb_reference_count_t ref_count; 144 hb_user_data_array_t user_data; 145 146#define HB_OBJECT_HEADER_STATIC {HB_REFERENCE_COUNT_INVALID} 147 148 static inline void *create (unsigned int size) { 149 hb_object_header_t *obj = (hb_object_header_t *) calloc (1, size); 150 151 if (likely (obj)) 152 obj->init (); 153 154 return obj; 155 } 156 157 inline void init (void) { 158 ref_count.init (1); 159 } 160 161 inline bool is_inert (void) const { 162 return unlikely (ref_count.is_invalid ()); 163 } 164 165 inline void reference (void) { 166 if (unlikely (!this || this->is_inert ())) 167 return; 168 ref_count.inc (); 169 } 170 171 inline bool destroy (void) { 172 if (unlikely (!this || this->is_inert ())) 173 return false; 174 if (ref_count.dec () != 1) 175 return false; 176 177 ref_count.init (HB_REFERENCE_COUNT_INVALID_VALUE); 178 179 user_data.finish (); 180 181 return true; 182 } 183 184 inline bool set_user_data (hb_user_data_key_t *key, 185 void * data, 186 hb_destroy_func_t destroy_func, 187 hb_bool_t replace) { 188 if (unlikely (!this || this->is_inert ())) 189 return false; 190 191 return user_data.set (key, data, destroy_func, replace); 192 } 193 194 inline void *get_user_data (hb_user_data_key_t *key) { 195 return user_data.get (key); 196 } 197 198 inline void trace (const char *function) const { 199 DEBUG_MSG (OBJECT, (void *) this, 200 "refcount=%d %s", 201 this ? ref_count.get () : 0, 202 function); 203 } 204 205}; 206 207 208 209 210/* object */ 211 212template <typename Type> 213static inline void hb_object_trace (const Type *obj, const char *function) 214{ 215 obj->header.trace (function); 216} 217template <typename Type> 218static inline Type *hb_object_create (void) 219{ 220 Type *obj = (Type *) hb_object_header_t::create (sizeof (Type)); 221 hb_object_trace (obj, HB_FUNC); 222 return obj; 223} 224template <typename Type> 225static inline bool hb_object_is_inert (const Type *obj) 226{ 227 return unlikely (obj->header.is_inert ()); 228} 229template <typename Type> 230static inline Type *hb_object_reference (Type *obj) 231{ 232 hb_object_trace (obj, HB_FUNC); 233 obj->header.reference (); 234 return obj; 235} 236template <typename Type> 237static inline bool hb_object_destroy (Type *obj) 238{ 239 hb_object_trace (obj, HB_FUNC); 240 return obj->header.destroy (); 241} 242template <typename Type> 243static inline bool hb_object_set_user_data (Type *obj, 244 hb_user_data_key_t *key, 245 void * data, 246 hb_destroy_func_t destroy, 247 hb_bool_t replace) 248{ 249 return obj->header.set_user_data (key, data, destroy, replace); 250} 251 252template <typename Type> 253static inline void *hb_object_get_user_data (Type *obj, 254 hb_user_data_key_t *key) 255{ 256 return obj->header.get_user_data (key); 257} 258 259 260 261 262 263#endif /* HB_OBJECT_PRIVATE_HH */ 264