• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, 1> 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       item_t old = *item;
82       *item = items[items.len - 1];
83       items.pop ();
84       l.unlock ();
85       old.fini ();
86     } else {
87       l.unlock ();
88     }
89   }
90 
91   template <typename T>
findhb_lockable_set_t92   bool find (T v, item_t *i, lock_t &l)
93   {
94     l.lock ();
95     item_t *item = items.find (v);
96     if (item)
97       *i = *item;
98     l.unlock ();
99     return !!item;
100   }
101 
102   template <typename T>
find_or_inserthb_lockable_set_t103   item_t *find_or_insert (T v, lock_t &l)
104   {
105     l.lock ();
106     item_t *item = items.find (v);
107     if (!item) {
108       item = items.push (v);
109     }
110     l.unlock ();
111     return item;
112   }
113 
finihb_lockable_set_t114   void fini (lock_t &l)
115   {
116     if (!items.len) {
117       /* No need for locking. */
118       items.fini ();
119       return;
120     }
121     l.lock ();
122     while (items.len) {
123       item_t old = items[items.len - 1];
124 	items.pop ();
125 	l.unlock ();
126 	old.fini ();
127 	l.lock ();
128     }
129     items.fini ();
130     l.unlock ();
131   }
132 
133 };
134 
135 
136 /*
137  * Reference-count.
138  */
139 
140 #define HB_REFERENCE_COUNT_INERT_VALUE 0
141 #define HB_REFERENCE_COUNT_POISON_VALUE -0x0000DEAD
142 #define HB_REFERENCE_COUNT_INIT {HB_ATOMIC_INT_INIT (HB_REFERENCE_COUNT_INERT_VALUE)}
143 
144 struct hb_reference_count_t
145 {
146   mutable hb_atomic_int_t ref_count;
147 
inithb_reference_count_t148   void init (int v = 1) { ref_count.set_relaxed (v); }
get_relaxedhb_reference_count_t149   int get_relaxed () const { return ref_count.get_relaxed (); }
inchb_reference_count_t150   int inc () const { return ref_count.inc (); }
dechb_reference_count_t151   int dec () const { return ref_count.dec (); }
finihb_reference_count_t152   void fini () { ref_count.set_relaxed (HB_REFERENCE_COUNT_POISON_VALUE); }
153 
is_inerthb_reference_count_t154   bool is_inert () const { return ref_count.get_relaxed () == HB_REFERENCE_COUNT_INERT_VALUE; }
is_validhb_reference_count_t155   bool is_valid () const { return ref_count.get_relaxed () > 0; }
156 };
157 
158 
159 /* user_data */
160 
161 struct hb_user_data_array_t
162 {
163   struct hb_user_data_item_t {
164     hb_user_data_key_t *key;
165     void *data;
166     hb_destroy_func_t destroy;
167 
operator ==hb_user_data_array_t::hb_user_data_item_t168     bool operator == (hb_user_data_key_t *other_key) const { return key == other_key; }
operator ==hb_user_data_array_t::hb_user_data_item_t169     bool operator == (hb_user_data_item_t &other) const { return key == other.key; }
170 
finihb_user_data_array_t::hb_user_data_item_t171     void fini () { if (destroy) destroy (data); }
172   };
173 
174   hb_mutex_t lock;
175   hb_lockable_set_t<hb_user_data_item_t, hb_mutex_t> items;
176 
inithb_user_data_array_t177   void init () { lock.init (); items.init (); }
178 
179   HB_INTERNAL bool set (hb_user_data_key_t *key,
180 			void *              data,
181 			hb_destroy_func_t   destroy,
182 			hb_bool_t           replace);
183 
184   HB_INTERNAL void *get (hb_user_data_key_t *key);
185 
finihb_user_data_array_t186   void fini () { items.fini (lock); lock.fini (); }
187 };
188 
189 
190 /*
191  * Object header
192  */
193 
194 struct hb_object_header_t
195 {
196   hb_reference_count_t ref_count;
197   mutable hb_atomic_int_t writable;
198   hb_atomic_ptr_t<hb_user_data_array_t> user_data;
199 };
200 #define HB_OBJECT_HEADER_STATIC \
201 	{ \
202 	  HB_REFERENCE_COUNT_INIT, \
203 	  HB_ATOMIC_INT_INIT (false), \
204 	  HB_ATOMIC_PTR_INIT (nullptr) \
205 	}
206 
207 
208 /*
209  * Object
210  */
211 
212 template <typename Type>
hb_object_trace(const Type * obj,const char * function)213 static inline void hb_object_trace (const Type *obj, const char *function)
214 {
215   DEBUG_MSG (OBJECT, (void *) obj,
216 	     "%s refcount=%d",
217 	     function,
218 	     obj ? obj->header.ref_count.get_relaxed () : 0);
219 }
220 
221 template <typename Type>
hb_object_create()222 static inline Type *hb_object_create ()
223 {
224   Type *obj = (Type *) calloc (1, sizeof (Type));
225 
226   if (unlikely (!obj))
227     return obj;
228 
229   hb_object_init (obj);
230   hb_object_trace (obj, HB_FUNC);
231   return obj;
232 }
233 template <typename Type>
hb_object_init(Type * obj)234 static inline void hb_object_init (Type *obj)
235 {
236   obj->header.ref_count.init ();
237   obj->header.writable.set_relaxed (true);
238   obj->header.user_data.init ();
239 }
240 template <typename Type>
hb_object_is_inert(const Type * obj)241 static inline bool hb_object_is_inert (const Type *obj)
242 {
243   return unlikely (obj->header.ref_count.is_inert ());
244 }
245 template <typename Type>
hb_object_is_valid(const Type * obj)246 static inline bool hb_object_is_valid (const Type *obj)
247 {
248   return likely (obj->header.ref_count.is_valid ());
249 }
250 template <typename Type>
hb_object_is_immutable(const Type * obj)251 static inline bool hb_object_is_immutable (const Type *obj)
252 {
253   return !obj->header.writable.get_relaxed ();
254 }
255 template <typename Type>
hb_object_make_immutable(const Type * obj)256 static inline void hb_object_make_immutable (const Type *obj)
257 {
258   obj->header.writable.set_relaxed (false);
259 }
260 template <typename Type>
hb_object_reference(Type * obj)261 static inline Type *hb_object_reference (Type *obj)
262 {
263   hb_object_trace (obj, HB_FUNC);
264   if (unlikely (!obj || hb_object_is_inert (obj)))
265     return obj;
266   assert (hb_object_is_valid (obj));
267   obj->header.ref_count.inc ();
268   return obj;
269 }
270 template <typename Type>
hb_object_destroy(Type * obj)271 static inline bool hb_object_destroy (Type *obj)
272 {
273   hb_object_trace (obj, HB_FUNC);
274   if (unlikely (!obj || hb_object_is_inert (obj)))
275     return false;
276   assert (hb_object_is_valid (obj));
277   if (obj->header.ref_count.dec () != 1)
278     return false;
279 
280   hb_object_fini (obj);
281   return true;
282 }
283 template <typename Type>
hb_object_fini(Type * obj)284 static inline void hb_object_fini (Type *obj)
285 {
286   obj->header.ref_count.fini (); /* Do this before user_data */
287   hb_user_data_array_t *user_data = obj->header.user_data.get ();
288   if (user_data)
289   {
290     user_data->fini ();
291     free (user_data);
292     user_data = nullptr;
293   }
294 }
295 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)296 static inline bool hb_object_set_user_data (Type               *obj,
297 					    hb_user_data_key_t *key,
298 					    void *              data,
299 					    hb_destroy_func_t   destroy,
300 					    hb_bool_t           replace)
301 {
302   if (unlikely (!obj || hb_object_is_inert (obj)))
303     return false;
304   assert (hb_object_is_valid (obj));
305 
306 retry:
307   hb_user_data_array_t *user_data = obj->header.user_data.get ();
308   if (unlikely (!user_data))
309   {
310     user_data = (hb_user_data_array_t *) calloc (sizeof (hb_user_data_array_t), 1);
311     if (unlikely (!user_data))
312       return false;
313     user_data->init ();
314     if (unlikely (!obj->header.user_data.cmpexch (nullptr, user_data)))
315     {
316       user_data->fini ();
317       free (user_data);
318       goto retry;
319     }
320   }
321 
322   return user_data->set (key, data, destroy, replace);
323 }
324 
325 template <typename Type>
hb_object_get_user_data(Type * obj,hb_user_data_key_t * key)326 static inline void *hb_object_get_user_data (Type               *obj,
327 					     hb_user_data_key_t *key)
328 {
329   if (unlikely (!obj || hb_object_is_inert (obj)))
330     return nullptr;
331   assert (hb_object_is_valid (obj));
332   hb_user_data_array_t *user_data = obj->header.user_data.get ();
333   if (!user_data)
334     return nullptr;
335   return user_data->get (key);
336 }
337 
338 
339 #endif /* HB_OBJECT_HH */
340