hb-object-private.hh revision b9452bfc1696457e156e79037c863903da5454fc
116b95a68caaa7e021209e2cd6a877ae1e558f740Christopher Ferris/* 216b95a68caaa7e021209e2cd6a877ae1e558f740Christopher Ferris * Copyright © 2007 Chris Wilson 316b95a68caaa7e021209e2cd6a877ae1e558f740Christopher Ferris * Copyright © 2009,2010 Red Hat, Inc. 416b95a68caaa7e021209e2cd6a877ae1e558f740Christopher Ferris * Copyright © 2011 Google, Inc. 516b95a68caaa7e021209e2cd6a877ae1e558f740Christopher Ferris * 616b95a68caaa7e021209e2cd6a877ae1e558f740Christopher Ferris * This is part of HarfBuzz, a text shaping library. 716b95a68caaa7e021209e2cd6a877ae1e558f740Christopher Ferris * 816b95a68caaa7e021209e2cd6a877ae1e558f740Christopher Ferris * Permission is hereby granted, without written agreement and without 916b95a68caaa7e021209e2cd6a877ae1e558f740Christopher Ferris * license or royalty fees, to use, copy, modify, and distribute this 1016b95a68caaa7e021209e2cd6a877ae1e558f740Christopher Ferris * software and its documentation for any purpose, provided that the 1116b95a68caaa7e021209e2cd6a877ae1e558f740Christopher Ferris * above copyright notice and the following two paragraphs appear in 1216b95a68caaa7e021209e2cd6a877ae1e558f740Christopher Ferris * all copies of this software. 1316b95a68caaa7e021209e2cd6a877ae1e558f740Christopher Ferris * 1416b95a68caaa7e021209e2cd6a877ae1e558f740Christopher Ferris * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR 1516b95a68caaa7e021209e2cd6a877ae1e558f740Christopher Ferris * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 1616b95a68caaa7e021209e2cd6a877ae1e558f740Christopher Ferris * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN 1716b95a68caaa7e021209e2cd6a877ae1e558f740Christopher Ferris * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 1816b95a68caaa7e021209e2cd6a877ae1e558f740Christopher Ferris * DAMAGE. 1916b95a68caaa7e021209e2cd6a877ae1e558f740Christopher Ferris * 2016b95a68caaa7e021209e2cd6a877ae1e558f740Christopher Ferris * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, 2116b95a68caaa7e021209e2cd6a877ae1e558f740Christopher Ferris * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 2216b95a68caaa7e021209e2cd6a877ae1e558f740Christopher Ferris * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS 2316b95a68caaa7e021209e2cd6a877ae1e558f740Christopher Ferris * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO 2416b95a68caaa7e021209e2cd6a877ae1e558f740Christopher Ferris * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 2516b95a68caaa7e021209e2cd6a877ae1e558f740Christopher Ferris * 26f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris * Contributor(s): 27f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris * Chris Wilson <chris@chris-wilson.co.uk> 2816b95a68caaa7e021209e2cd6a877ae1e558f740Christopher Ferris * Red Hat Author(s): Behdad Esfahbod 29e64e77857aae2ed6c7e6a0e773bede5b877cabb3Christopher Ferris * Google Author(s): Behdad Esfahbod 30e64e77857aae2ed6c7e6a0e773bede5b877cabb3Christopher Ferris */ 31e64e77857aae2ed6c7e6a0e773bede5b877cabb3Christopher Ferris 3216b95a68caaa7e021209e2cd6a877ae1e558f740Christopher Ferris#ifndef HB_OBJECT_PRIVATE_HH 3316b95a68caaa7e021209e2cd6a877ae1e558f740Christopher Ferris#define HB_OBJECT_PRIVATE_HH 34f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris 35f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris#include "hb-private.hh" 36f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris 3716b95a68caaa7e021209e2cd6a877ae1e558f740Christopher Ferris#include "hb-mutex-private.hh" 38f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris 39f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher FerrisHB_BEGIN_DECLS 40f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris 41f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris 42f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris/* Debug */ 43f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris 44f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris#ifndef HB_DEBUG_OBJECT 45f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris#define HB_DEBUG_OBJECT (HB_DEBUG+0) 46f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris#endif 47f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris 48f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris 49f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris/* atomic_int */ 50f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris 51f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris/* We need external help for these */ 52f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris 53f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris#ifdef HAVE_GLIB 54f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris 55f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris#include <glib.h> 56f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferris 57f4a8df5f4f338f1a12c25213227e98b34b42447fChristopher Ferristypedef volatile int hb_atomic_int_t; 5816b95a68caaa7e021209e2cd6a877ae1e558f740Christopher Ferris#define hb_atomic_int_fetch_and_add(AI, V) g_atomic_int_exchange_and_add (&(AI), V) 59#define hb_atomic_int_get(AI) g_atomic_int_get (&(AI)) 60#define hb_atomic_int_set(AI, V) g_atomic_int_set (&(AI), V) 61 62 63#elif defined(_MSC_VER) 64 65#include <intrin.h> 66 67typedef long hb_atomic_int_t; 68#define hb_atomic_int_fetch_and_add(AI, V) _InterlockedExchangeAdd (&(AI), V) 69#define hb_atomic_int_get(AI) (_ReadBarrier (), (AI)) 70#define hb_atomic_int_set(AI, V) ((void) _InterlockedExchange (&(AI), (V))) 71 72 73#else 74 75#warning "Could not find any system to define atomic_int macros, library will NOT be thread-safe" 76 77typedef volatile int hb_atomic_int_t; 78#define hb_atomic_int_fetch_and_add(AI, V) ((AI) += (V), (AI) - (V)) 79#define hb_atomic_int_get(AI) (AI) 80#define hb_atomic_int_set(AI, V) ((void) ((AI) = (V))) 81 82 83#endif 84 85 86 87 88/* reference_count */ 89 90typedef struct { 91 hb_atomic_int_t ref_count; 92 93#define HB_REFERENCE_COUNT_INVALID_VALUE ((hb_atomic_int_t) -1) 94#define HB_REFERENCE_COUNT_INVALID {HB_REFERENCE_COUNT_INVALID_VALUE} 95 96 inline void init (int v) { ref_count = v; /* non-atomic is fine */ } 97 inline int inc (void) { return hb_atomic_int_fetch_and_add (ref_count, 1); } 98 inline int dec (void) { return hb_atomic_int_fetch_and_add (ref_count, -1); } 99 inline void set (int v) { hb_atomic_int_set (ref_count, v); } 100 101 inline int get (void) const { return hb_atomic_int_get (ref_count); } 102 inline bool is_invalid (void) const { return get () == HB_REFERENCE_COUNT_INVALID_VALUE; } 103 104} hb_reference_count_t; 105 106 107/* user_data */ 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_lockable_set_t<hb_user_data_item_t, hb_static_mutex_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 HB_INTERNAL void finish (void); 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 ref_count.init (HB_REFERENCE_COUNT_INVALID_VALUE); 174 175 user_data.finish (); 176 177 return true; 178 } 179 180 inline bool set_user_data (hb_user_data_key_t *key, 181 void * data, 182 hb_destroy_func_t destroy) { 183 if (unlikely (!this || this->is_inert ())) 184 return false; 185 186 return user_data.set (key, data, destroy); 187 } 188 189 inline void *get_user_data (hb_user_data_key_t *key) { 190 return user_data.get (key); 191 } 192 193 inline void trace (const char *function) const { 194 (void) (HB_DEBUG_OBJECT && 195 fprintf (stderr, "OBJECT(%p) refcount=%d %s\n", 196 (void *) this, 197 this ? ref_count.get () : 0, 198 function)); 199 } 200 201}; 202 203 204HB_END_DECLS 205 206 207/* object */ 208 209template <typename Type> 210static inline void hb_object_trace (const Type *obj, const char *function) 211{ 212 obj->header.trace (function); 213} 214template <typename Type> 215static inline Type *hb_object_create (void) 216{ 217 Type *obj = (Type *) hb_object_header_t::create (sizeof (Type)); 218 hb_object_trace (obj, HB_FUNC); 219 return obj; 220} 221template <typename Type> 222static inline bool hb_object_is_inert (const Type *obj) 223{ 224 return unlikely (obj->header.is_inert ()); 225} 226template <typename Type> 227static inline Type *hb_object_reference (Type *obj) 228{ 229 hb_object_trace (obj, HB_FUNC); 230 obj->header.reference (); 231 return obj; 232} 233template <typename Type> 234static inline bool hb_object_destroy (Type *obj) 235{ 236 hb_object_trace (obj, HB_FUNC); 237 return obj->header.destroy (); 238} 239template <typename Type> 240static inline bool hb_object_set_user_data (Type *obj, 241 hb_user_data_key_t *key, 242 void * data, 243 hb_destroy_func_t destroy) 244{ 245 return obj->header.set_user_data (key, data, destroy); 246} 247 248template <typename Type> 249static inline void *hb_object_get_user_data (Type *obj, 250 hb_user_data_key_t *key) 251{ 252 return obj->header.get_user_data (key); 253} 254 255 256HB_BEGIN_DECLS 257 258 259HB_END_DECLS 260 261#endif /* HB_OBJECT_PRIVATE_HH */ 262