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 | |
50 | void init () { items.init (); } |
51 | |
52 | template <typename T> |
53 | item_t *replace_or_insert (T v, lock_t &l, bool replace) |
54 | { |
55 | l.lock (); |
56 | item_t *item = items.lsearch (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 items.in_error () ? nullptr : item; |
73 | } |
74 | |
75 | template <typename T> |
76 | void remove (T v, lock_t &l) |
77 | { |
78 | l.lock (); |
79 | item_t *item = items.lsearch (v); |
80 | if (item) |
81 | { |
82 | item_t old = *item; |
83 | *item = std::move (items.tail ()); |
84 | items.pop (); |
85 | l.unlock (); |
86 | old.fini (); |
87 | } else { |
88 | l.unlock (); |
89 | } |
90 | } |
91 | |
92 | template <typename T> |
93 | bool find (T v, item_t *i, lock_t &l) |
94 | { |
95 | l.lock (); |
96 | item_t *item = items.lsearch (v); |
97 | if (item) |
98 | *i = *item; |
99 | l.unlock (); |
100 | return !!item; |
101 | } |
102 | |
103 | template <typename T> |
104 | 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 | |
115 | 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.tail (); |
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 | struct hb_reference_count_t |
144 | { |
145 | mutable hb_atomic_int_t ref_count; |
146 | |
147 | void init (int v = 1) { ref_count = v; } |
148 | int get_relaxed () const { return ref_count; } |
149 | int inc () const { return ref_count.inc (); } |
150 | int dec () const { return ref_count.dec (); } |
151 | void fini () { ref_count = -0x0000DEAD; } |
152 | |
153 | bool is_inert () const { return !ref_count; } |
154 | bool is_valid () const { return ref_count > 0; } |
155 | }; |
156 | |
157 | |
158 | /* user_data */ |
159 | |
160 | struct hb_user_data_array_t |
161 | { |
162 | struct hb_user_data_item_t { |
163 | hb_user_data_key_t *key; |
164 | void *data; |
165 | hb_destroy_func_t destroy; |
166 | |
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; } |
169 | |
170 | void fini () { if (destroy) destroy (data); } |
171 | }; |
172 | |
173 | hb_mutex_t lock; |
174 | hb_lockable_set_t<hb_user_data_item_t, hb_mutex_t> items; |
175 | |
176 | void init () { lock.init (); items.init (); } |
177 | |
178 | void fini () { items.fini (lock); lock.fini (); } |
179 | |
180 | bool set (hb_user_data_key_t *key, |
181 | void * data, |
182 | hb_destroy_func_t destroy, |
183 | hb_bool_t replace) |
184 | { |
185 | if (!key) |
186 | return false; |
187 | |
188 | if (replace) { |
189 | if (!data && !destroy) { |
190 | items.remove (key, lock); |
191 | return true; |
192 | } |
193 | } |
194 | hb_user_data_item_t item = {key, data, destroy}; |
195 | bool ret = !!items.replace_or_insert (item, lock, (bool) replace); |
196 | |
197 | return ret; |
198 | } |
199 | |
200 | void *get (hb_user_data_key_t *key) |
201 | { |
202 | hb_user_data_item_t item = {nullptr, nullptr, nullptr}; |
203 | |
204 | return items.find (key, &item, lock) ? item.data : nullptr; |
205 | } |
206 | }; |
207 | |
208 | |
209 | /* |
210 | * Object header |
211 | */ |
212 | |
213 | struct |
214 | { |
215 | hb_reference_count_t ; |
216 | mutable hb_atomic_int_t = 0; |
217 | hb_atomic_ptr_t<hb_user_data_array_t> ; |
218 | |
219 | bool () const { return !ref_count.get_relaxed (); } |
220 | }; |
221 | #define {} |
222 | |
223 | |
224 | /* |
225 | * Object |
226 | */ |
227 | |
228 | template <typename Type> |
229 | static inline void hb_object_trace (const Type *obj, const char *function) |
230 | { |
231 | DEBUG_MSG (OBJECT, (void *) obj, |
232 | "%s refcount=%d" , |
233 | function, |
234 | obj ? obj->header.ref_count.get_relaxed () : 0); |
235 | } |
236 | |
237 | template <typename Type, typename ...Ts> |
238 | static inline Type *hb_object_create (Ts... ds) |
239 | { |
240 | Type *obj = (Type *) hb_calloc (1, sizeof (Type)); |
241 | |
242 | if (unlikely (!obj)) |
243 | return obj; |
244 | |
245 | new (obj) Type (std::forward<Ts> (ds)...); |
246 | |
247 | hb_object_init (obj); |
248 | hb_object_trace (obj, HB_FUNC); |
249 | |
250 | return obj; |
251 | } |
252 | template <typename Type> |
253 | static inline void hb_object_init (Type *obj) |
254 | { |
255 | obj->header.ref_count.init (); |
256 | obj->header.writable = true; |
257 | obj->header.user_data.init (); |
258 | } |
259 | template <typename Type> |
260 | static inline bool hb_object_is_valid (const Type *obj) |
261 | { |
262 | return likely (obj->header.ref_count.is_valid ()); |
263 | } |
264 | template <typename Type> |
265 | static inline bool hb_object_is_immutable (const Type *obj) |
266 | { |
267 | return !obj->header.writable; |
268 | } |
269 | template <typename Type> |
270 | static inline void hb_object_make_immutable (const Type *obj) |
271 | { |
272 | obj->header.writable = false; |
273 | } |
274 | template <typename Type> |
275 | static inline Type *hb_object_reference (Type *obj) |
276 | { |
277 | hb_object_trace (obj, HB_FUNC); |
278 | if (unlikely (!obj || obj->header.is_inert ())) |
279 | return obj; |
280 | assert (hb_object_is_valid (obj)); |
281 | obj->header.ref_count.inc (); |
282 | return obj; |
283 | } |
284 | template <typename Type> |
285 | static inline bool hb_object_destroy (Type *obj) |
286 | { |
287 | hb_object_trace (obj, HB_FUNC); |
288 | if (unlikely (!obj || obj->header.is_inert ())) |
289 | return false; |
290 | assert (hb_object_is_valid (obj)); |
291 | if (obj->header.ref_count.dec () != 1) |
292 | return false; |
293 | |
294 | hb_object_fini (obj); |
295 | |
296 | if (!std::is_trivially_destructible<Type>::value) |
297 | obj->~Type (); |
298 | |
299 | return true; |
300 | } |
301 | template <typename Type> |
302 | static inline void hb_object_fini (Type *obj) |
303 | { |
304 | obj->header.ref_count.fini (); /* Do this before user_data */ |
305 | hb_user_data_array_t *user_data = obj->header.user_data.get_acquire (); |
306 | if (user_data) |
307 | { |
308 | user_data->fini (); |
309 | hb_free (user_data); |
310 | obj->header.user_data.set_relaxed (nullptr); |
311 | } |
312 | } |
313 | template <typename Type> |
314 | static inline bool hb_object_set_user_data (Type *obj, |
315 | hb_user_data_key_t *key, |
316 | void * data, |
317 | hb_destroy_func_t destroy, |
318 | hb_bool_t replace) |
319 | { |
320 | if (unlikely (!obj || obj->header.is_inert ())) |
321 | return false; |
322 | assert (hb_object_is_valid (obj)); |
323 | |
324 | retry: |
325 | hb_user_data_array_t *user_data = obj->header.user_data.get_acquire (); |
326 | if (unlikely (!user_data)) |
327 | { |
328 | user_data = (hb_user_data_array_t *) hb_calloc (sizeof (hb_user_data_array_t), 1); |
329 | if (unlikely (!user_data)) |
330 | return false; |
331 | user_data->init (); |
332 | if (unlikely (!obj->header.user_data.cmpexch (nullptr, user_data))) |
333 | { |
334 | user_data->fini (); |
335 | hb_free (user_data); |
336 | goto retry; |
337 | } |
338 | } |
339 | |
340 | return user_data->set (key, data, destroy, replace); |
341 | } |
342 | |
343 | template <typename Type> |
344 | static inline void *hb_object_get_user_data (Type *obj, |
345 | hb_user_data_key_t *key) |
346 | { |
347 | if (unlikely (!obj || obj->header.is_inert ())) |
348 | return nullptr; |
349 | assert (hb_object_is_valid (obj)); |
350 | hb_user_data_array_t *user_data = obj->header.user_data.get_acquire (); |
351 | if (!user_data) |
352 | return nullptr; |
353 | return user_data->get (key); |
354 | } |
355 | |
356 | |
357 | #endif /* HB_OBJECT_HH */ |
358 | |