2 * Copyright © 2007 Chris Wilson
3 * Copyright © 2009,2010 Red Hat, Inc.
4 * Copyright © 2011 Google, Inc.
6 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
27 * Chris Wilson <chris@chris-wilson.co.uk>
28 * Red Hat Author(s): Behdad Esfahbod
29 * Google Author(s): Behdad Esfahbod
32 #ifndef HB_OBJECT_PRIVATE_HH
33 #define HB_OBJECT_PRIVATE_HH
35 #include "hb-private.hh"
37 #include "hb-mutex-private.hh"
44 #ifndef HB_DEBUG_OBJECT
45 #define HB_DEBUG_OBJECT (HB_DEBUG+0)
51 /* We need external help for these */
57 typedef volatile int hb_atomic_int_t;
58 #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)
63 #elif defined(_MSC_VER)
67 typedef 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)))
75 #warning "Could not find any system to define atomic_int macros, library will NOT be thread-safe"
77 typedef 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)))
91 hb_atomic_int_t ref_count;
93 #define HB_REFERENCE_COUNT_INVALID_VALUE ((hb_atomic_int_t) -1)
94 #define HB_REFERENCE_COUNT_INVALID {HB_REFERENCE_COUNT_INVALID_VALUE}
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); }
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; }
104 } hb_reference_count_t;
109 struct hb_user_data_array_t {
111 struct hb_user_data_item_t {
112 hb_user_data_key_t *key;
114 hb_destroy_func_t destroy;
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; }
119 void finish (void) { if (destroy) destroy (data); }
122 hb_lockable_set_t<hb_user_data_item_t, hb_static_mutex_t> items;
124 HB_INTERNAL bool set (hb_user_data_key_t *key,
126 hb_destroy_func_t destroy);
128 HB_INTERNAL void *get (hb_user_data_key_t *key);
130 HB_INTERNAL void finish (void);
136 typedef struct _hb_object_header_t hb_object_header_t;
138 struct _hb_object_header_t {
139 hb_reference_count_t ref_count;
140 hb_user_data_array_t user_data;
142 #define HB_OBJECT_HEADER_STATIC {HB_REFERENCE_COUNT_INVALID}
144 static inline void *create (unsigned int size) {
145 hb_object_header_t *obj = (hb_object_header_t *) calloc (1, size);
153 inline void init (void) {
157 inline bool is_inert (void) const {
158 return unlikely (ref_count.is_invalid ());
161 inline void reference (void) {
162 if (unlikely (!this || this->is_inert ()))
167 inline bool destroy (void) {
168 if (unlikely (!this || this->is_inert ()))
170 if (ref_count.dec () != 1)
173 ref_count.init (HB_REFERENCE_COUNT_INVALID_VALUE);
180 inline bool set_user_data (hb_user_data_key_t *key,
182 hb_destroy_func_t destroy_func) {
183 if (unlikely (!this || this->is_inert ()))
186 return user_data.set (key, data, destroy_func);
189 inline void *get_user_data (hb_user_data_key_t *key) {
190 return user_data.get (key);
193 inline void trace (const char *function) const {
194 (void) (HB_DEBUG_OBJECT &&
195 fprintf (stderr, "OBJECT(%p) refcount=%d %s\n",
197 this ? ref_count.get () : 0,
209 template <typename Type>
210 static inline void hb_object_trace (const Type *obj, const char *function)
212 obj->header.trace (function);
214 template <typename Type>
215 static inline Type *hb_object_create (void)
217 Type *obj = (Type *) hb_object_header_t::create (sizeof (Type));
218 hb_object_trace (obj, HB_FUNC);
221 template <typename Type>
222 static inline bool hb_object_is_inert (const Type *obj)
224 return unlikely (obj->header.is_inert ());
226 template <typename Type>
227 static inline Type *hb_object_reference (Type *obj)
229 hb_object_trace (obj, HB_FUNC);
230 obj->header.reference ();
233 template <typename Type>
234 static inline bool hb_object_destroy (Type *obj)
236 hb_object_trace (obj, HB_FUNC);
237 return obj->header.destroy ();
239 template <typename Type>
240 static inline bool hb_object_set_user_data (Type *obj,
241 hb_user_data_key_t *key,
243 hb_destroy_func_t destroy)
245 return obj->header.set_user_data (key, data, destroy);
248 template <typename Type>
249 static inline void *hb_object_get_user_data (Type *obj,
250 hb_user_data_key_t *key)
252 return obj->header.get_user_data (key);
261 #endif /* HB_OBJECT_PRIVATE_HH */