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