hb-object-private.hh revision c605bbbb6d4b2a98b1f40ca818760088d991f7d1
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#define hb_atomic_int_fetch_and_add(AI, V) g_atomic_int_exchange_and_add (&(AI), V) 58#define hb_atomic_int_get(AI) g_atomic_int_get (&(AI)) 59#define hb_atomic_int_set(AI, V) g_atomic_int_set (&(AI), V) 60 61 62#elif defined(_MSC_VER) 63 64#include <intrin.h> 65 66typedef long hb_atomic_int_t; 67#define hb_atomic_int_fetch_and_add(AI, V) _InterlockedExchangeAdd (&(AI), V) 68#define hb_atomic_int_get(AI) (_ReadBarrier (), (AI)) 69#define hb_atomic_int_set(AI, V) ((void) _InterlockedExchange (&(AI), (V))) 70 71 72#else 73 74#warning "Could not find any system to define atomic_int macros, library will NOT be thread-safe" 75 76typedef volatile int hb_atomic_int_t; 77#define hb_atomic_int_fetch_and_add(AI, V) ((AI) += (V), (AI) - (V)) 78#define hb_atomic_int_get(AI) (AI) 79#define hb_atomic_int_set(AI, V) ((void) ((AI) = (V))) 80 81 82#endif 83 84 85 86 87/* reference_count */ 88 89typedef struct { 90 hb_atomic_int_t ref_count; 91 92#define HB_REFERENCE_COUNT_INVALID_VALUE ((hb_atomic_int_t) -1) 93#define HB_REFERENCE_COUNT_INVALID {HB_REFERENCE_COUNT_INVALID_VALUE} 94 95 inline void init (int v) { ref_count = v; /* non-atomic is fine */ } 96 inline int inc (void) { return hb_atomic_int_fetch_and_add (ref_count, 1); } 97 inline int dec (void) { return hb_atomic_int_fetch_and_add (ref_count, -1); } 98 inline void set (int v) { hb_atomic_int_set (ref_count, v); } 99 100 inline int get (void) const { return hb_atomic_int_get (ref_count); } 101 inline bool is_invalid (void) const { return get () == HB_REFERENCE_COUNT_INVALID_VALUE; } 102 103} hb_reference_count_t; 104 105 106/* user_data */ 107 108struct hb_user_data_array_t { 109 110 struct hb_user_data_item_t { 111 hb_user_data_key_t *key; 112 void *data; 113 hb_destroy_func_t destroy; 114 115 inline bool operator == (hb_user_data_key_t *other_key) const { return key == other_key; } 116 inline bool operator == (hb_user_data_item_t &other) const { return key == other.key; } 117 118 void finish (void) { if (destroy) destroy (data); } 119 }; 120 121 hb_lockable_set_t<hb_user_data_item_t, hb_static_mutex_t> items; 122 123 HB_INTERNAL bool set (hb_user_data_key_t *key, 124 void * data, 125 hb_destroy_func_t destroy); 126 127 HB_INTERNAL void *get (hb_user_data_key_t *key); 128 129 HB_INTERNAL void finish (void); 130}; 131 132 133/* object_header */ 134 135typedef struct _hb_object_header_t hb_object_header_t; 136 137struct _hb_object_header_t { 138 hb_reference_count_t ref_count; 139 hb_user_data_array_t user_data; 140 141#define HB_OBJECT_HEADER_STATIC {HB_REFERENCE_COUNT_INVALID} 142 143 static inline void *create (unsigned int size) { 144 hb_object_header_t *obj = (hb_object_header_t *) calloc (1, size); 145 146 if (likely (obj)) 147 obj->init (); 148 149 return obj; 150 } 151 152 inline void init (void) { 153 ref_count.init (1); 154 } 155 156 inline bool is_inert (void) const { 157 return unlikely (ref_count.is_invalid ()); 158 } 159 160 inline void reference (void) { 161 if (unlikely (!this || this->is_inert ())) 162 return; 163 ref_count.inc (); 164 } 165 166 inline bool destroy (void) { 167 if (unlikely (!this || this->is_inert ())) 168 return false; 169 if (ref_count.dec () != 1) 170 return false; 171 172 ref_count.init (HB_REFERENCE_COUNT_INVALID_VALUE); 173 174 user_data.finish (); 175 176 return true; 177 } 178 179 inline bool set_user_data (hb_user_data_key_t *key, 180 void * data, 181 hb_destroy_func_t destroy_func) { 182 if (unlikely (!this || this->is_inert ())) 183 return false; 184 185 return user_data.set (key, data, destroy_func); 186 } 187 188 inline void *get_user_data (hb_user_data_key_t *key) { 189 return user_data.get (key); 190 } 191 192 inline void trace (const char *function) const { 193 DEBUG_MSG (OBJECT, (void *) this, 194 "refcount=%d %s", 195 this ? ref_count.get () : 0, 196 function); 197 } 198 199}; 200 201 202 203 204/* object */ 205 206template <typename Type> 207static inline void hb_object_trace (const Type *obj, const char *function) 208{ 209 obj->header.trace (function); 210} 211template <typename Type> 212static inline Type *hb_object_create (void) 213{ 214 Type *obj = (Type *) hb_object_header_t::create (sizeof (Type)); 215 hb_object_trace (obj, HB_FUNC); 216 return obj; 217} 218template <typename Type> 219static inline bool hb_object_is_inert (const Type *obj) 220{ 221 return unlikely (obj->header.is_inert ()); 222} 223template <typename Type> 224static inline Type *hb_object_reference (Type *obj) 225{ 226 hb_object_trace (obj, HB_FUNC); 227 obj->header.reference (); 228 return obj; 229} 230template <typename Type> 231static inline bool hb_object_destroy (Type *obj) 232{ 233 hb_object_trace (obj, HB_FUNC); 234 return obj->header.destroy (); 235} 236template <typename Type> 237static inline bool hb_object_set_user_data (Type *obj, 238 hb_user_data_key_t *key, 239 void * data, 240 hb_destroy_func_t destroy) 241{ 242 return obj->header.set_user_data (key, data, destroy); 243} 244 245template <typename Type> 246static inline void *hb_object_get_user_data (Type *obj, 247 hb_user_data_key_t *key) 248{ 249 return obj->header.get_user_data (key); 250} 251 252 253 254 255 256#endif /* HB_OBJECT_PRIVATE_HH */ 257