Imported Upstream version 0.9.3
[platform/upstream/harfbuzz.git] / src / hb-object-private.hh
1 /*
2  * Copyright © 2007  Chris Wilson
3  * Copyright © 2009,2010  Red Hat, Inc.
4  * Copyright © 2011,2012  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
50 #define HB_REFERENCE_COUNT_INVALID_VALUE ((hb_atomic_int_t) -1)
51 #define HB_REFERENCE_COUNT_INVALID {HB_REFERENCE_COUNT_INVALID_VALUE}
52 struct hb_reference_count_t
53 {
54   hb_atomic_int_t ref_count;
55
56   inline void init (int v) { ref_count = v; }
57   inline int inc (void) { return hb_atomic_int_add (const_cast<hb_atomic_int_t &> (ref_count),  1); }
58   inline int dec (void) { return hb_atomic_int_add (const_cast<hb_atomic_int_t &> (ref_count), -1); }
59   inline void finish (void) { ref_count = HB_REFERENCE_COUNT_INVALID_VALUE; }
60
61   inline bool is_invalid (void) const { return ref_count == HB_REFERENCE_COUNT_INVALID_VALUE; }
62
63 };
64
65
66 /* user_data */
67
68 #define HB_USER_DATA_ARRAY_INIT {HB_LOCKABLE_SET_INIT}
69 struct hb_user_data_array_t
70 {
71   /* TODO Add tracing. */
72
73   struct hb_user_data_item_t {
74     hb_user_data_key_t *key;
75     void *data;
76     hb_destroy_func_t destroy;
77
78     inline bool operator == (hb_user_data_key_t *other_key) const { return key == other_key; }
79     inline bool operator == (hb_user_data_item_t &other) const { return key == other.key; }
80
81     void finish (void) { if (destroy) destroy (data); }
82   };
83
84   hb_lockable_set_t<hb_user_data_item_t, hb_mutex_t> items;
85
86   inline void init (void) { items.init (); }
87
88   HB_INTERNAL bool set (hb_user_data_key_t *key,
89                         void *              data,
90                         hb_destroy_func_t   destroy,
91                         hb_bool_t           replace,
92                         hb_mutex_t         &lock);
93
94   HB_INTERNAL void *get (hb_user_data_key_t *key,
95                         hb_mutex_t          &lock);
96
97   HB_INTERNAL void finish (hb_mutex_t &lock);
98 };
99
100
101 /* object_header */
102
103 struct hb_object_header_t
104 {
105   hb_reference_count_t ref_count;
106   hb_mutex_t mutex;
107   hb_user_data_array_t user_data;
108
109 #define HB_OBJECT_HEADER_STATIC {HB_REFERENCE_COUNT_INVALID, HB_MUTEX_INIT, HB_USER_DATA_ARRAY_INIT}
110
111   static inline void *create (unsigned int size) {
112     hb_object_header_t *obj = (hb_object_header_t *) calloc (1, size);
113
114     if (likely (obj))
115       obj->init ();
116
117     return obj;
118   }
119
120   inline void init (void) {
121     ref_count.init (1);
122     mutex.init ();
123     user_data.init ();
124   }
125
126   inline bool is_inert (void) const {
127     return unlikely (ref_count.is_invalid ());
128   }
129
130   inline void reference (void) {
131     if (unlikely (!this || this->is_inert ()))
132       return;
133     ref_count.inc ();
134   }
135
136   inline bool destroy (void) {
137     if (unlikely (!this || this->is_inert ()))
138       return false;
139     if (ref_count.dec () != 1)
140       return false;
141
142     ref_count.finish (); /* Do this before user_data */
143     user_data.finish (mutex);
144     mutex.finish ();
145
146     return true;
147   }
148
149   inline void lock (void) {
150     mutex.lock ();
151   }
152
153   inline void unlock (void) {
154     mutex.unlock ();
155   }
156
157   inline bool set_user_data (hb_user_data_key_t *key,
158                              void *              data,
159                              hb_destroy_func_t   destroy_func,
160                              hb_bool_t           replace) {
161     if (unlikely (!this || this->is_inert ()))
162       return false;
163
164     return user_data.set (key, data, destroy_func, replace, mutex);
165   }
166
167   inline void *get_user_data (hb_user_data_key_t *key) {
168     if (unlikely (!this || this->is_inert ()))
169       return NULL;
170
171     return user_data.get (key, mutex);
172   }
173
174   inline void trace (const char *function) const {
175     if (unlikely (!this)) return;
176     /* TODO We cannot use DEBUG_MSG_FUNC here since that one currently only
177      * prints the class name and throws away the template info. */
178     DEBUG_MSG (OBJECT, (void *) this,
179                "%s refcount=%d",
180                function,
181                this ? ref_count.ref_count : 0);
182   }
183
184   private:
185   ASSERT_POD ();
186 };
187
188
189 /* object */
190
191 template <typename Type>
192 static inline void hb_object_trace (const Type *obj, const char *function)
193 {
194   obj->header.trace (function);
195 }
196 template <typename Type>
197 static inline Type *hb_object_create (void)
198 {
199   Type *obj = (Type *) hb_object_header_t::create (sizeof (Type));
200   hb_object_trace (obj, HB_FUNC);
201   return obj;
202 }
203 template <typename Type>
204 static inline bool hb_object_is_inert (const Type *obj)
205 {
206   return unlikely (obj->header.is_inert ());
207 }
208 template <typename Type>
209 static inline Type *hb_object_reference (Type *obj)
210 {
211   hb_object_trace (obj, HB_FUNC);
212   obj->header.reference ();
213   return obj;
214 }
215 template <typename Type>
216 static inline bool hb_object_destroy (Type *obj)
217 {
218   hb_object_trace (obj, HB_FUNC);
219   return obj->header.destroy ();
220 }
221 template <typename Type>
222 static inline void hb_object_lock (Type *obj)
223 {
224   hb_object_trace (obj, HB_FUNC);
225   return obj->header.lock ();
226 }
227 template <typename Type>
228 static inline void hb_object_unlock (Type *obj)
229 {
230   hb_object_trace (obj, HB_FUNC);
231   return obj->header.unlock ();
232 }
233 template <typename Type>
234 static inline bool hb_object_set_user_data (Type               *obj,
235                                             hb_user_data_key_t *key,
236                                             void *              data,
237                                             hb_destroy_func_t   destroy,
238                                             hb_bool_t           replace)
239 {
240   return obj->header.set_user_data (key, data, destroy, replace);
241 }
242
243 template <typename Type>
244 static inline void *hb_object_get_user_data (Type               *obj,
245                                              hb_user_data_key_t *key)
246 {
247   return obj->header.get_user_data (key);
248 }
249
250
251 #endif /* HB_OBJECT_PRIVATE_HH */