hb-object-private.hh revision 8004429102d7d3a8c42e1cbfe231835de4d3d782
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>
55
56typedef volatile int hb_atomic_int_t;
57#if GLIB_CHECK_VERSION(2,29,5)
58#define hb_atomic_int_add(AI, V)	g_atomic_int_add (&(AI), V)
59#else
60#define hb_atomic_int_add(AI, V)	g_atomic_int_exchange_and_add (&(AI), V)
61#endif
62#define hb_atomic_int_get(AI)		g_atomic_int_get (&(AI))
63
64
65#elif !defined(HB_NO_MT) && defined(_MSC_VER) && _MSC_VER >= 1600
66
67#include <intrin.h>
68
69typedef long hb_atomic_int_t;
70#define hb_atomic_int_add(AI, V)	_InterlockedExchangeAdd (&(AI), V)
71#define hb_atomic_int_get(AI)		(_ReadBarrier (), (AI))
72
73
74#else
75
76#define HB_ATOMIC_INT_NIL 1
77
78typedef volatile int hb_atomic_int_t;
79#define hb_atomic_int_add(AI, V)	((AI) += (V), (AI) - (V))
80#define hb_atomic_int_get(AI)		(AI)
81
82#endif
83
84
85
86
87/* reference_count */
88
89typedef struct {
90  hb_atomic_int_t ref_count;
91
92#define HB_REFERENCE_COUNT_INVALID_VALUE ((hb_atomic_int_t) -1)
93#define HB_REFERENCE_COUNT_INVALID {HB_REFERENCE_COUNT_INVALID_VALUE}
94
95  inline void init (int v) { ref_count = v; /* non-atomic is fine */ }
96  inline int inc (void) { return hb_atomic_int_add (ref_count,  1); }
97  inline int dec (void) { return hb_atomic_int_add (ref_count, -1); }
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
107struct hb_user_data_array_t {
108
109  struct hb_user_data_item_t {
110    hb_user_data_key_t *key;
111    void *data;
112    hb_destroy_func_t destroy;
113
114    inline bool operator == (hb_user_data_key_t *other_key) const { return key == other_key; }
115    inline bool operator == (hb_user_data_item_t &other) const { return key == other.key; }
116
117    void finish (void) { if (destroy) destroy (data); }
118  };
119
120  hb_lockable_set_t<hb_user_data_item_t, hb_static_mutex_t> items;
121
122  HB_INTERNAL bool set (hb_user_data_key_t *key,
123			void *              data,
124			hb_destroy_func_t   destroy,
125			hb_bool_t           replace);
126
127  HB_INTERNAL void *get (hb_user_data_key_t *key);
128
129  HB_INTERNAL void finish (void);
130};
131
132
133/* object_header */
134
135typedef struct _hb_object_header_t hb_object_header_t;
136
137struct _hb_object_header_t {
138  hb_reference_count_t ref_count;
139  hb_user_data_array_t user_data;
140
141#define HB_OBJECT_HEADER_STATIC {HB_REFERENCE_COUNT_INVALID}
142
143  static inline void *create (unsigned int size) {
144    hb_object_header_t *obj = (hb_object_header_t *) calloc (1, size);
145
146    if (likely (obj))
147      obj->init ();
148
149    return obj;
150  }
151
152  inline void init (void) {
153    ref_count.init (1);
154  }
155
156  inline bool is_inert (void) const {
157    return unlikely (ref_count.is_invalid ());
158  }
159
160  inline void reference (void) {
161    if (unlikely (!this || this->is_inert ()))
162      return;
163    ref_count.inc ();
164  }
165
166  inline bool destroy (void) {
167    if (unlikely (!this || this->is_inert ()))
168      return false;
169    if (ref_count.dec () != 1)
170      return false;
171
172    ref_count.init (HB_REFERENCE_COUNT_INVALID_VALUE);
173
174    user_data.finish ();
175
176    return true;
177  }
178
179  inline bool set_user_data (hb_user_data_key_t *key,
180			     void *              data,
181			     hb_destroy_func_t   destroy_func,
182			     hb_bool_t           replace) {
183    if (unlikely (!this || this->is_inert ()))
184      return false;
185
186    return user_data.set (key, data, destroy_func, replace);
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, (void *) this,
195	       "refcount=%d %s",
196	       this ? ref_count.get () : 0,
197	       function);
198  }
199
200};
201
202
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					    hb_bool_t           replace)
243{
244  return obj->header.set_user_data (key, data, destroy, replace);
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
255
256
257
258#endif /* HB_OBJECT_PRIVATE_HH */
259