2 * Copyright © 2007 Chris Wilson
3 * Copyright © 2009,2010 Red Hat, Inc.
4 * Copyright © 2011,2012 Google, Inc.
6 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
27 * Chris Wilson <chris@chris-wilson.co.uk>
28 * Red Hat Author(s): Behdad Esfahbod
29 * Google Author(s): Behdad Esfahbod
36 #include "hb-atomic.hh"
37 #include "hb-mutex.hh"
38 #include "hb-vector.hh"
45 template <typename item_t, typename lock_t>
46 struct hb_lockable_set_t
48 hb_vector_t<item_t> items;
50 void init () { items.init (); }
53 item_t *replace_or_insert (T v, lock_t &l, bool replace)
56 item_t *item = items.lsearch (v);
69 item = items.push (v);
76 void remove (T v, lock_t &l)
79 item_t *item = items.lsearch (v);
83 *item = items[items.length - 1];
93 bool find (T v, item_t *i, lock_t &l)
96 item_t *item = items.lsearch (v);
103 template <typename T>
104 item_t *find_or_insert (T v, lock_t &l)
107 item_t *item = items.find (v);
109 item = items.push (v);
115 void fini (lock_t &l)
119 /* No need to lock. */
126 item_t old = items[items.length - 1];
143 struct hb_reference_count_t
145 mutable hb_atomic_int_t ref_count;
147 void init (int v = 1) { ref_count.set_relaxed (v); }
148 int get_relaxed () const { return ref_count.get_relaxed (); }
149 int inc () const { return ref_count.inc (); }
150 int dec () const { return ref_count.dec (); }
151 void fini () { ref_count.set_relaxed (-0x0000DEAD); }
153 bool is_inert () const { return !ref_count.get_relaxed (); }
154 bool is_valid () const { return ref_count.get_relaxed () > 0; }
160 struct hb_user_data_array_t
162 struct hb_user_data_item_t {
163 hb_user_data_key_t *key;
165 hb_destroy_func_t destroy;
167 bool operator == (const hb_user_data_key_t *other_key) const { return key == other_key; }
168 bool operator == (const hb_user_data_item_t &other) const { return key == other.key; }
170 void fini () { if (destroy) destroy (data); }
174 hb_lockable_set_t<hb_user_data_item_t, hb_mutex_t> items;
176 void init () { lock.init (); items.init (); }
178 HB_INTERNAL bool set (hb_user_data_key_t *key,
180 hb_destroy_func_t destroy,
183 HB_INTERNAL void *get (hb_user_data_key_t *key);
185 void fini () { items.fini (lock); lock.fini (); }
193 struct hb_object_header_t
195 hb_reference_count_t ref_count;
196 mutable hb_atomic_int_t writable = 0;
197 hb_atomic_ptr_t<hb_user_data_array_t> user_data;
199 bool is_inert () const { return !ref_count.get_relaxed (); }
201 #define HB_OBJECT_HEADER_STATIC {}
208 template <typename Type>
209 static inline void hb_object_trace (const Type *obj, const char *function)
211 DEBUG_MSG (OBJECT, (void *) obj,
214 obj ? obj->header.ref_count.get_relaxed () : 0);
217 template <typename Type>
218 static inline Type *hb_object_create ()
220 Type *obj = (Type *) hb_calloc (1, sizeof (Type));
225 hb_object_init (obj);
226 hb_object_trace (obj, HB_FUNC);
229 template <typename Type>
230 static inline void hb_object_init (Type *obj)
232 obj->header.ref_count.init ();
233 obj->header.writable.set_relaxed (true);
234 obj->header.user_data.init ();
236 template <typename Type>
237 static inline bool hb_object_is_valid (const Type *obj)
239 return likely (obj->header.ref_count.is_valid ());
241 template <typename Type>
242 static inline bool hb_object_is_immutable (const Type *obj)
244 return !obj->header.writable.get_relaxed ();
246 template <typename Type>
247 static inline void hb_object_make_immutable (const Type *obj)
249 obj->header.writable.set_relaxed (false);
251 template <typename Type>
252 static inline Type *hb_object_reference (Type *obj)
254 hb_object_trace (obj, HB_FUNC);
255 if (unlikely (!obj || obj->header.is_inert ()))
257 assert (hb_object_is_valid (obj));
258 obj->header.ref_count.inc ();
261 template <typename Type>
262 static inline bool hb_object_destroy (Type *obj)
264 hb_object_trace (obj, HB_FUNC);
265 if (unlikely (!obj || obj->header.is_inert ()))
267 assert (hb_object_is_valid (obj));
268 if (obj->header.ref_count.dec () != 1)
271 hb_object_fini (obj);
274 template <typename Type>
275 static inline void hb_object_fini (Type *obj)
277 obj->header.ref_count.fini (); /* Do this before user_data */
278 hb_user_data_array_t *user_data = obj->header.user_data.get ();
286 template <typename Type>
287 static inline bool hb_object_set_user_data (Type *obj,
288 hb_user_data_key_t *key,
290 hb_destroy_func_t destroy,
293 if (unlikely (!obj || obj->header.is_inert ()))
295 assert (hb_object_is_valid (obj));
298 hb_user_data_array_t *user_data = obj->header.user_data.get ();
299 if (unlikely (!user_data))
301 user_data = (hb_user_data_array_t *) hb_calloc (sizeof (hb_user_data_array_t), 1);
302 if (unlikely (!user_data))
305 if (unlikely (!obj->header.user_data.cmpexch (nullptr, user_data)))
313 return user_data->set (key, data, destroy, replace);
316 template <typename Type>
317 static inline void *hb_object_get_user_data (Type *obj,
318 hb_user_data_key_t *key)
320 if (unlikely (!obj || obj->header.is_inert ()))
322 assert (hb_object_is_valid (obj));
323 hb_user_data_array_t *user_data = obj->header.user_data.get ();
326 return user_data->get (key);
330 #endif /* HB_OBJECT_HH */