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_HH
33 #define HB_OBJECT_HH
34 
35 #include "hb.hh"
36 #include "hb-atomic.hh"
37 #include "hb-mutex.hh"
38 #include "hb-vector.hh"
39 
40 
41 /*
42  * Lockable set
43  */
44 
45 template <typename item_t, typename lock_t>
46 struct hb_lockable_set_t
47 {
48   hb_vector_t<item_t> items;
49 
inithb_lockable_set_t50   void init () { items.init (); }
51 
52   template <typename T>
replace_or_inserthb_lockable_set_t53   item_t *replace_or_insert (T v, lock_t &l, bool replace)
54   {
55     l.lock ();
56     item_t *item = items.find (v);
57     if (item) {
58       if (replace) {
59         item_t old = *item;
60         *item = v;
61         l.unlock ();
62         old.fini ();
63       }
64       else {
65         item = nullptr;
66         l.unlock ();
67       }
68     } else {
69       item = items.push (v);
70       l.unlock ();
71     }
72     return item;
73   }
74 
75   template <typename T>
removehb_lockable_set_t76   void remove (T v, lock_t &l)
77   {
78     l.lock ();
79     item_t *item = items.find (v);
80     if (item)
81     {
82       item_t old = *item;
83       *item = items[items.length - 1];
84       items.pop ();
85       l.unlock ();
86       old.fini ();
87     } else {
88       l.unlock ();
89     }
90   }
91 
92   template <typename T>
findhb_lockable_set_t93   bool find (T v, item_t *i, lock_t &l)
94   {
95     l.lock ();
96     item_t *item = items.find (v);
97     if (item)
98       *i = *item;
99     l.unlock ();
100     return !!item;
101   }
102 
103   template <typename T>
find_or_inserthb_lockable_set_t104   item_t *find_or_insert (T v, lock_t &l)
105   {
106     l.lock ();
107     item_t *item = items.find (v);
108     if (!item) {
109       item = items.push (v);
110     }
111     l.unlock ();
112     return item;
113   }
114 
finihb_lockable_set_t115   void fini (lock_t &l)
116   {
117     if (!items.length)
118     {
119       /* No need to lock. */
120       items.fini ();
121       return;
122     }
123     l.lock ();
124     while (items.length)
125     {
126       item_t old = items[items.length - 1];
127       items.pop ();
128       l.unlock ();
129       old.fini ();
130       l.lock ();
131     }
132     items.fini ();
133     l.unlock ();
134   }
135 
136 };
137 
138 
139 /*
140  * Reference-count.
141  */
142 
143 #define HB_REFERENCE_COUNT_INIT {0}
144 
145 struct hb_reference_count_t
146 {
147   mutable hb_atomic_int_t ref_count;
148 
inithb_reference_count_t149   void init (int v = 1) { ref_count.set_relaxed (v); }
get_relaxedhb_reference_count_t150   int get_relaxed () const { return ref_count.get_relaxed (); }
inchb_reference_count_t151   int inc () const { return ref_count.inc (); }
dechb_reference_count_t152   int dec () const { return ref_count.dec (); }
finihb_reference_count_t153   void fini () { ref_count.set_relaxed (-0x0000DEAD); }
154 
is_inerthb_reference_count_t155   bool is_inert () const { return !ref_count.get_relaxed (); }
is_validhb_reference_count_t156   bool is_valid () const { return ref_count.get_relaxed () > 0; }
157 };
158 
159 
160 /* user_data */
161 
162 struct hb_user_data_array_t
163 {
164   struct hb_user_data_item_t {
165     hb_user_data_key_t *key;
166     void *data;
167     hb_destroy_func_t destroy;
168 
operator ==hb_user_data_array_t::hb_user_data_item_t169     bool operator == (const hb_user_data_key_t *other_key) const { return key == other_key; }
operator ==hb_user_data_array_t::hb_user_data_item_t170     bool operator == (const hb_user_data_item_t &other) const { return key == other.key; }
171 
finihb_user_data_array_t::hb_user_data_item_t172     void fini () { if (destroy) destroy (data); }
173   };
174 
175   hb_mutex_t lock;
176   hb_lockable_set_t<hb_user_data_item_t, hb_mutex_t> items;
177 
inithb_user_data_array_t178   void init () { lock.init (); items.init (); }
179 
180   HB_INTERNAL bool set (hb_user_data_key_t *key,
181                         void *              data,
182                         hb_destroy_func_t   destroy,
183                         hb_bool_t           replace);
184 
185   HB_INTERNAL void *get (hb_user_data_key_t *key);
186 
finihb_user_data_array_t187   void fini () { items.fini (lock); lock.fini (); }
188 };
189 
190 
191 /*
192  * Object header
193  */
194 
195 struct hb_object_header_t
196 {
197   hb_reference_count_t ref_count;
198   mutable hb_atomic_int_t writable = 0;
199   hb_atomic_ptr_t<hb_user_data_array_t> user_data;
200 };
201 #define HB_OBJECT_HEADER_STATIC {}
202 
203 
204 /*
205  * Object
206  */
207 
208 template <typename Type>
hb_object_trace(const Type * obj,const char * function)209 static inline void hb_object_trace (const Type *obj, const char *function)
210 {
211   DEBUG_MSG (OBJECT, (void *) obj,
212              "%s refcount=%d",
213              function,
214              obj ? obj->header.ref_count.get_relaxed () : 0);
215 }
216 
217 template <typename Type>
hb_object_create()218 static inline Type *hb_object_create ()
219 {
220   Type *obj = (Type *) calloc (1, sizeof (Type));
221 
222   if (unlikely (!obj))
223     return obj;
224 
225   hb_object_init (obj);
226   hb_object_trace (obj, HB_FUNC);
227   return obj;
228 }
229 template <typename Type>
hb_object_init(Type * obj)230 static inline void hb_object_init (Type *obj)
231 {
232   obj->header.ref_count.init ();
233   obj->header.writable.set_relaxed (true);
234   obj->header.user_data.init ();
235 }
236 template <typename Type>
hb_object_is_inert(const Type * obj)237 static inline bool hb_object_is_inert (const Type *obj)
238 {
239   return unlikely (obj->header.ref_count.is_inert ());
240 }
241 template <typename Type>
hb_object_is_valid(const Type * obj)242 static inline bool hb_object_is_valid (const Type *obj)
243 {
244   return likely (obj->header.ref_count.is_valid ());
245 }
246 template <typename Type>
hb_object_is_immutable(const Type * obj)247 static inline bool hb_object_is_immutable (const Type *obj)
248 {
249   return !obj->header.writable.get_relaxed ();
250 }
251 template <typename Type>
hb_object_make_immutable(const Type * obj)252 static inline void hb_object_make_immutable (const Type *obj)
253 {
254   obj->header.writable.set_relaxed (false);
255 }
256 template <typename Type>
hb_object_reference(Type * obj)257 static inline Type *hb_object_reference (Type *obj)
258 {
259   hb_object_trace (obj, HB_FUNC);
260   if (unlikely (!obj || hb_object_is_inert (obj)))
261     return obj;
262   assert (hb_object_is_valid (obj));
263   obj->header.ref_count.inc ();
264   return obj;
265 }
266 template <typename Type>
hb_object_destroy(Type * obj)267 static inline bool hb_object_destroy (Type *obj)
268 {
269   hb_object_trace (obj, HB_FUNC);
270   if (unlikely (!obj || hb_object_is_inert (obj)))
271     return false;
272   assert (hb_object_is_valid (obj));
273   if (obj->header.ref_count.dec () != 1)
274     return false;
275 
276   hb_object_fini (obj);
277   return true;
278 }
279 template <typename Type>
hb_object_fini(Type * obj)280 static inline void hb_object_fini (Type *obj)
281 {
282   obj->header.ref_count.fini (); /* Do this before user_data */
283   hb_user_data_array_t *user_data = obj->header.user_data.get ();
284   if (user_data)
285   {
286     user_data->fini ();
287     free (user_data);
288     user_data = nullptr;
289   }
290 }
291 template <typename Type>
hb_object_set_user_data(Type * obj,hb_user_data_key_t * key,void * data,hb_destroy_func_t destroy,hb_bool_t replace)292 static inline bool hb_object_set_user_data (Type               *obj,
293                                             hb_user_data_key_t *key,
294                                             void *              data,
295                                             hb_destroy_func_t   destroy,
296                                             hb_bool_t           replace)
297 {
298   if (unlikely (!obj || hb_object_is_inert (obj)))
299     return false;
300   assert (hb_object_is_valid (obj));
301 
302 retry:
303   hb_user_data_array_t *user_data = obj->header.user_data.get ();
304   if (unlikely (!user_data))
305   {
306     user_data = (hb_user_data_array_t *) calloc (sizeof (hb_user_data_array_t), 1);
307     if (unlikely (!user_data))
308       return false;
309     user_data->init ();
310     if (unlikely (!obj->header.user_data.cmpexch (nullptr, user_data)))
311     {
312       user_data->fini ();
313       free (user_data);
314       goto retry;
315     }
316   }
317 
318   return user_data->set (key, data, destroy, replace);
319 }
320 
321 template <typename Type>
hb_object_get_user_data(Type * obj,hb_user_data_key_t * key)322 static inline void *hb_object_get_user_data (Type               *obj,
323                                              hb_user_data_key_t *key)
324 {
325   if (unlikely (!obj || hb_object_is_inert (obj)))
326     return nullptr;
327   assert (hb_object_is_valid (obj));
328   hb_user_data_array_t *user_data = obj->header.user_data.get ();
329   if (!user_data)
330     return nullptr;
331   return user_data->get (key);
332 }
333 
334 
335 #endif /* HB_OBJECT_HH */
336