hb-object-private.hh revision 56eb5ad6f94c32189ad219438db9a18683ca6846
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
37HB_BEGIN_DECLS
38
39
40/* Debug */
41
42#ifndef HB_DEBUG_OBJECT
43#define HB_DEBUG_OBJECT (HB_DEBUG+0)
44#endif
45
46
47/* atomic_int */
48
49/* We need external help for these */
50
51#ifdef HAVE_GLIB
52
53#include <glib.h>
54
55typedef volatile int hb_atomic_int_t;
56#define hb_atomic_int_fetch_and_add(AI, V)	g_atomic_int_exchange_and_add (&(AI), V)
57#define hb_atomic_int_get(AI)			g_atomic_int_get (&(AI))
58#define hb_atomic_int_set(AI, V)		g_atomic_int_set (&(AI), V)
59
60
61#elif defined(_MSC_VER)
62
63#include <intrin.h>
64
65typedef long hb_atomic_int_t;
66#define hb_atomic_int_fetch_and_add(AI, V)	_InterlockedExchangeAdd (&(AI), V)
67#define hb_atomic_int_get(AI)			(_ReadBarrier (), (AI))
68#define hb_atomic_int_set(AI, V)		((void) _InterlockedExchange (&(AI), (V)))
69
70
71#else
72
73#warning "Could not find any system to define atomic_int macros, library will NOT be thread-safe"
74
75typedef volatile int hb_atomic_int_t;
76#define hb_atomic_int_fetch_and_add(AI, V)	((AI) += (V), (AI) - (V))
77#define hb_atomic_int_get(AI)			(AI)
78#define hb_atomic_int_set(AI, V)		((void) ((AI) = (V)))
79
80
81#endif
82
83
84
85
86/* reference_count */
87
88typedef struct {
89  hb_atomic_int_t ref_count;
90
91#define HB_REFERENCE_COUNT_INVALID_VALUE ((hb_atomic_int_t) -1)
92#define HB_REFERENCE_COUNT_INVALID {HB_REFERENCE_COUNT_INVALID_VALUE}
93
94  inline void init (int v) { ref_count = v; /* non-atomic is fine */ }
95  inline int inc (void) { return hb_atomic_int_fetch_and_add (ref_count,  1); }
96  inline int dec (void) { return hb_atomic_int_fetch_and_add (ref_count, -1); }
97  inline void set (int v) { hb_atomic_int_set (ref_count, v); }
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
107/* XXX make this thread-safe, somehow! */
108
109typedef struct {
110  void *data;
111  hb_destroy_func_t destroy;
112
113  void finish (void) { if (destroy) destroy (data); }
114} hb_user_data_t;
115
116struct hb_user_data_array_t {
117
118  hb_map_t<hb_user_data_key_t *, hb_user_data_t> map;
119
120  inline bool set (hb_user_data_key_t *key,
121		   void *              data,
122		   hb_destroy_func_t   destroy)
123  {
124    if (!data && !destroy) {
125      map.unset (key);
126      return true;
127    }
128    hb_user_data_t user_data = {data, destroy};
129    return map.set (key, user_data);
130  }
131
132  inline void *get (hb_user_data_key_t *key) {
133    hb_user_data_t *user_data = map.get (key);
134    return user_data ? user_data->data : NULL;
135  }
136
137  void finish (void) { map.finish (); }
138};
139
140
141/* object_header */
142
143typedef struct _hb_object_header_t hb_object_header_t;
144
145struct _hb_object_header_t {
146  hb_reference_count_t ref_count;
147  hb_user_data_array_t user_data;
148
149#define HB_OBJECT_HEADER_STATIC {HB_REFERENCE_COUNT_INVALID}
150
151  static inline void *create (unsigned int size) {
152    hb_object_header_t *obj = (hb_object_header_t *) calloc (1, size);
153
154    if (likely (obj))
155      obj->init ();
156
157    return obj;
158  }
159
160  inline void init (void) {
161    ref_count.init (1);
162  }
163
164  inline bool is_inert (void) const {
165    return unlikely (ref_count.is_invalid ());
166  }
167
168  inline void reference (void) {
169    if (unlikely (!this || this->is_inert ()))
170      return;
171    ref_count.inc ();
172  }
173
174  inline bool destroy (void) {
175    if (unlikely (!this || this->is_inert ()))
176      return false;
177    if (ref_count.dec () != 1)
178      return false;
179
180    user_data.finish ();
181
182    return true;
183  }
184
185  inline bool set_user_data (hb_user_data_key_t *key,
186			     void *              data,
187			     hb_destroy_func_t   destroy) {
188    if (unlikely (!this || this->is_inert ()))
189      return false;
190
191    return user_data.set (key, data, destroy);
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    (void) (HB_DEBUG_OBJECT &&
200	    fprintf (stderr, "OBJECT(%p) refcount=%d %s\n",
201		     this,
202		     this ? ref_count.get () : 0,
203		     function));
204  }
205
206};
207
208
209HB_END_DECLS
210
211
212/* object */
213
214template <typename Type>
215static inline void hb_object_trace (const Type *obj, const char *function)
216{
217  obj->header.trace (function);
218}
219template <typename Type>
220static inline Type *hb_object_create (void)
221{
222  Type *obj = (Type *) hb_object_header_t::create (sizeof (Type));
223  hb_object_trace (obj, HB_FUNC);
224  return obj;
225}
226template <typename Type>
227static inline bool hb_object_is_inert (const Type *obj)
228{
229  return unlikely (obj->header.is_inert ());
230}
231template <typename Type>
232static inline Type *hb_object_reference (Type *obj)
233{
234  hb_object_trace (obj, HB_FUNC);
235  obj->header.reference ();
236  return obj;
237}
238template <typename Type>
239static inline bool hb_object_destroy (Type *obj)
240{
241  hb_object_trace (obj, HB_FUNC);
242  return obj->header.destroy ();
243}
244template <typename Type>
245static inline bool hb_object_set_user_data (Type               *obj,
246					    hb_user_data_key_t *key,
247					    void *              data,
248					    hb_destroy_func_t   destroy)
249{
250  return obj->header.set_user_data (key, data, destroy);
251}
252
253template <typename Type>
254static inline void *hb_object_get_user_data (Type               *obj,
255					     hb_user_data_key_t *key)
256{
257  return obj->header.get_user_data (key);
258}
259
260
261HB_BEGIN_DECLS
262
263
264HB_END_DECLS
265
266#endif /* HB_OBJECT_PRIVATE_HH */
267