hb-object-private.hh revision 3b8fd9c48f4bde368bf2d465c148b9743a9216ee
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-atomic-private.hh"
38#include "hb-mutex-private.hh"
39
40
41/* Debug */
42
43#ifndef HB_DEBUG_OBJECT
44#define HB_DEBUG_OBJECT (HB_DEBUG+0)
45#endif
46
47
48/* reference_count */
49
50struct hb_reference_count_t
51{
52  hb_atomic_int_t ref_count;
53
54#define HB_REFERENCE_COUNT_INVALID_VALUE ((hb_atomic_int_t) -1)
55#define HB_REFERENCE_COUNT_INVALID {HB_REFERENCE_COUNT_INVALID_VALUE}
56
57  inline void init (int v) { const_cast<hb_atomic_int_t &> (ref_count) = v; }
58  inline int inc (void) { return hb_atomic_int_add (const_cast<hb_atomic_int_t &> (ref_count),  1); }
59  inline int dec (void) { return hb_atomic_int_add (const_cast<hb_atomic_int_t &> (ref_count), -1); }
60
61  inline bool is_invalid (void) const { return ref_count == HB_REFERENCE_COUNT_INVALID_VALUE; }
62
63};
64
65
66/* user_data */
67
68struct hb_user_data_array_t
69{
70  struct hb_user_data_item_t {
71    hb_user_data_key_t *key;
72    void *data;
73    hb_destroy_func_t destroy;
74
75    inline bool operator == (hb_user_data_key_t *other_key) const { return key == other_key; }
76    inline bool operator == (hb_user_data_item_t &other) const { return key == other.key; }
77
78    void finish (void) { if (destroy) destroy (data); }
79  };
80
81  hb_lockable_set_t<hb_user_data_item_t, hb_static_mutex_t> items;
82
83  HB_INTERNAL bool set (hb_user_data_key_t *key,
84			void *              data,
85			hb_destroy_func_t   destroy,
86			hb_bool_t           replace);
87
88  HB_INTERNAL void *get (hb_user_data_key_t *key);
89
90  HB_INTERNAL void finish (void);
91};
92
93
94/* object_header */
95
96struct hb_object_header_t
97{
98  hb_reference_count_t ref_count;
99  hb_user_data_array_t user_data;
100
101#define HB_OBJECT_HEADER_STATIC {HB_REFERENCE_COUNT_INVALID}
102
103  static inline void *create (unsigned int size) {
104    hb_object_header_t *obj = (hb_object_header_t *) calloc (1, size);
105
106    if (likely (obj))
107      obj->init ();
108
109    return obj;
110  }
111
112  inline void init (void) {
113    ref_count.init (1);
114  }
115
116  inline bool is_inert (void) const {
117    return unlikely (ref_count.is_invalid ());
118  }
119
120  inline void reference (void) {
121    if (unlikely (!this || this->is_inert ()))
122      return;
123    ref_count.inc ();
124  }
125
126  inline bool destroy (void) {
127    if (unlikely (!this || this->is_inert ()))
128      return false;
129    if (ref_count.dec () != 1)
130      return false;
131
132    ref_count.init (HB_REFERENCE_COUNT_INVALID_VALUE);
133
134    user_data.finish ();
135
136    return true;
137  }
138
139  inline bool set_user_data (hb_user_data_key_t *key,
140			     void *              data,
141			     hb_destroy_func_t   destroy_func,
142			     hb_bool_t           replace) {
143    if (unlikely (!this || this->is_inert ()))
144      return false;
145
146    return user_data.set (key, data, destroy_func, replace);
147  }
148
149  inline void *get_user_data (hb_user_data_key_t *key) {
150    return user_data.get (key);
151  }
152
153  inline void trace (const char *function) const {
154    if (unlikely (!this)) return;
155    /* XXX We cannot use DEBUG_MSG_FUNC here since that one currecntly only
156     * prints the class name and throws away the template info. */
157    DEBUG_MSG (OBJECT, (void *) this,
158	       "%s refcount=%d",
159	       function,
160	       this ? ref_count.ref_count : 0);
161  }
162
163};
164
165
166/* object */
167
168template <typename Type>
169static inline void hb_object_trace (const Type *obj, const char *function)
170{
171  obj->header.trace (function);
172}
173template <typename Type>
174static inline Type *hb_object_create (void)
175{
176  Type *obj = (Type *) hb_object_header_t::create (sizeof (Type));
177  hb_object_trace (obj, HB_FUNC);
178  return obj;
179}
180template <typename Type>
181static inline bool hb_object_is_inert (const Type *obj)
182{
183  return unlikely (obj->header.is_inert ());
184}
185template <typename Type>
186static inline Type *hb_object_reference (Type *obj)
187{
188  hb_object_trace (obj, HB_FUNC);
189  obj->header.reference ();
190  return obj;
191}
192template <typename Type>
193static inline bool hb_object_destroy (Type *obj)
194{
195  hb_object_trace (obj, HB_FUNC);
196  return obj->header.destroy ();
197}
198template <typename Type>
199static inline bool hb_object_set_user_data (Type               *obj,
200					    hb_user_data_key_t *key,
201					    void *              data,
202					    hb_destroy_func_t   destroy,
203					    hb_bool_t           replace)
204{
205  return obj->header.set_user_data (key, data, destroy, replace);
206}
207
208template <typename Type>
209static inline void *hb_object_get_user_data (Type               *obj,
210					     hb_user_data_key_t *key)
211{
212  return obj->header.get_user_data (key);
213}
214
215
216#endif /* HB_OBJECT_PRIVATE_HH */
217