hb-object-private.hh revision 7bd2b04fea5649d77d796d58b7f4918fe0378ee5
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#if !defined(HB_NO_MT) && defined(HAVE_GLIB) 53 54#include <glib.h> 55typedef volatile int hb_atomic_int_t; 56#if GLIB_CHECK_VERSION(2,29,5) 57#define hb_atomic_int_add(AI, V) g_atomic_int_add (&(AI), V) 58#else 59#define hb_atomic_int_add(AI, V) g_atomic_int_exchange_and_add (&(AI), V) 60#endif 61#define hb_atomic_int_get(AI) g_atomic_int_get (&(AI)) 62 63 64#elif !defined(HB_NO_MT) && defined(_MSC_VER) && _MSC_VER >= 1600 65 66#include <intrin.h> 67typedef long hb_atomic_int_t; 68#define hb_atomic_int_add(AI, V) _InterlockedExchangeAdd (&(AI), V) 69#define hb_atomic_int_get(AI) (_ReadBarrier (), (AI)) 70 71#elif !defined(HB_NO_MT) && defined(__APPLE__) 72 73#include <libkern/OSAtomic.h> 74typedef int32_t hb_atomic_int_t; 75#define hb_atomic_int_add(AI, V) (OSAtomicAdd32Barrier((V), &(AI)), (AI) - (V)) 76#define hb_atomic_int_get(AI) OSAtomicAdd32Barrier(0, &(AI)) 77 78#else 79 80#define HB_ATOMIC_INT_NIL 1 81 82typedef volatile int hb_atomic_int_t; 83#define hb_atomic_int_add(AI, V) ((AI) += (V), (AI) - (V)) 84#define hb_atomic_int_get(AI) (AI) 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 103 inline int get (void) { return hb_atomic_int_get (ref_count); } 104 inline int get_unsafe (void) const { return ref_count; } 105 inline bool is_invalid (void) const { return ref_count == 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 if (unlikely (!this)) return; 200 /* XXX We cannot use DEBUG_MSG_FUNC here since that one currecntly only 201 * prints the class name and throughs away the template info. */ 202 DEBUG_MSG (OBJECT, (void *) this, 203 "%s refcount=%d", 204 function, 205 this ? ref_count.get_unsafe () : 0); 206 } 207 208}; 209 210 211 212 213/* object */ 214 215template <typename Type> 216static inline void hb_object_trace (const Type *obj, const char *function) 217{ 218 obj->header.trace (function); 219} 220template <typename Type> 221static inline Type *hb_object_create (void) 222{ 223 Type *obj = (Type *) hb_object_header_t::create (sizeof (Type)); 224 hb_object_trace (obj, HB_FUNC); 225 return obj; 226} 227template <typename Type> 228static inline bool hb_object_is_inert (const Type *obj) 229{ 230 return unlikely (obj->header.is_inert ()); 231} 232template <typename Type> 233static inline Type *hb_object_reference (Type *obj) 234{ 235 hb_object_trace (obj, HB_FUNC); 236 obj->header.reference (); 237 return obj; 238} 239template <typename Type> 240static inline bool hb_object_destroy (Type *obj) 241{ 242 hb_object_trace (obj, HB_FUNC); 243 return obj->header.destroy (); 244} 245template <typename Type> 246static inline bool hb_object_set_user_data (Type *obj, 247 hb_user_data_key_t *key, 248 void * data, 249 hb_destroy_func_t destroy, 250 hb_bool_t replace) 251{ 252 return obj->header.set_user_data (key, data, destroy, replace); 253} 254 255template <typename Type> 256static inline void *hb_object_get_user_data (Type *obj, 257 hb_user_data_key_t *key) 258{ 259 return obj->header.get_user_data (key); 260} 261 262 263 264 265 266#endif /* HB_OBJECT_PRIVATE_HH */ 267