hb-object-private.hh revision 43ff203d8ea3e1b09e316e3aae1a4e5ec15bfdd2
18b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org/* 28b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org * Copyright © 2007 Chris Wilson 38b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org * Copyright © 2009,2010 Red Hat, Inc. 48b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org * Copyright © 2011 Google, Inc. 58b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org * 68b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org * This is part of HarfBuzz, a text shaping library. 78b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org * 88b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org * Permission is hereby granted, without written agreement and without 98b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org * license or royalty fees, to use, copy, modify, and distribute this 108b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org * software and its documentation for any purpose, provided that the 118b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org * above copyright notice and the following two paragraphs appear in 128b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org * all copies of this software. 138b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org * 148b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR 158b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 168b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN 178b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 188b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org * DAMAGE. 198b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org * 208b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, 218b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 228b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS 238b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO 248b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 258b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org * 268b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org * Contributor(s): 278b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org * Chris Wilson <chris@chris-wilson.co.uk> 288b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org * Red Hat Author(s): Behdad Esfahbod 298b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org * Google Author(s): Behdad Esfahbod 308b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org */ 318b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org 328b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org#ifndef HB_OBJECT_PRIVATE_HH 338b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org#define HB_OBJECT_PRIVATE_HH 348b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org 358b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org#include "hb-private.hh" 368b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org 378b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org#include "hb-mutex-private.hh" 38135ece137b471219eea06a652069b86a3b6ec349robertphillips@google.com 398b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.orgHB_BEGIN_DECLS 408b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org 418b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org 428b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org/* Debug */ 438b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org 448b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org#ifndef HB_DEBUG_OBJECT 458b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org#define HB_DEBUG_OBJECT (HB_DEBUG+0) 468b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org#endif 478b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org 488b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org 498b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org/* atomic_int */ 508b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org 518b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org/* We need external help for these */ 528b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org 538b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org#ifdef HAVE_GLIB 548b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org 558b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org#include <glib.h> 568b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org 578b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.orgtypedef volatile int hb_atomic_int_t; 588b79028d27653fafcde6901affba48b987b52e43commit-bot@chromium.org#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_func) { 183 if (unlikely (!this || this->is_inert ())) 184 return false; 185 186 return user_data.set (key, data, destroy_func); 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 DEBUG_MSG (OBJECT, "OBJECT(%p) refcount=%d %s\n", 195 (void *) this, 196 this ? ref_count.get () : 0, 197 function); 198 } 199 200}; 201 202 203HB_END_DECLS 204 205 206/* object */ 207 208template <typename Type> 209static inline void hb_object_trace (const Type *obj, const char *function) 210{ 211 obj->header.trace (function); 212} 213template <typename Type> 214static inline Type *hb_object_create (void) 215{ 216 Type *obj = (Type *) hb_object_header_t::create (sizeof (Type)); 217 hb_object_trace (obj, HB_FUNC); 218 return obj; 219} 220template <typename Type> 221static inline bool hb_object_is_inert (const Type *obj) 222{ 223 return unlikely (obj->header.is_inert ()); 224} 225template <typename Type> 226static inline Type *hb_object_reference (Type *obj) 227{ 228 hb_object_trace (obj, HB_FUNC); 229 obj->header.reference (); 230 return obj; 231} 232template <typename Type> 233static inline bool hb_object_destroy (Type *obj) 234{ 235 hb_object_trace (obj, HB_FUNC); 236 return obj->header.destroy (); 237} 238template <typename Type> 239static inline bool hb_object_set_user_data (Type *obj, 240 hb_user_data_key_t *key, 241 void * data, 242 hb_destroy_func_t destroy) 243{ 244 return obj->header.set_user_data (key, data, destroy); 245} 246 247template <typename Type> 248static inline void *hb_object_get_user_data (Type *obj, 249 hb_user_data_key_t *key) 250{ 251 return obj->header.get_user_data (key); 252} 253 254 255HB_BEGIN_DECLS 256 257 258HB_END_DECLS 259 260#endif /* HB_OBJECT_PRIVATE_HH */ 261