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 | |
50 | inline void init (void) { items.init (); } |
51 | |
52 | template <typename T> |
53 | inline 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> |
76 | inline 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> |
92 | inline 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> |
103 | inline 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 | |
114 | inline 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 | |
148 | inline void init (int v = 1) { ref_count.set_relaxed (v); } |
149 | inline int get_relaxed (void) const { return ref_count.get_relaxed (); } |
150 | inline int inc (void) const { return ref_count.inc (); } |
151 | inline int dec (void) const { return ref_count.dec (); } |
152 | inline void fini (void) { ref_count.set_relaxed (HB_REFERENCE_COUNT_POISON_VALUE); } |
153 | |
154 | inline bool is_inert (void) const { return ref_count.get_relaxed () == HB_REFERENCE_COUNT_INERT_VALUE; } |
155 | inline bool is_valid (void) 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 | |
168 | inline bool operator == (hb_user_data_key_t *other_key) const { return key == other_key; } |
169 | inline bool operator == (hb_user_data_item_t &other) const { return key == other.key; } |
170 | |
171 | void fini (void) { 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 | |
177 | inline void init (void) { 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 | |
186 | inline void fini (void) { items.fini (lock); lock.fini (); } |
187 | }; |
188 | |
189 | |
190 | /* |
191 | * Object header |
192 | */ |
193 | |
194 | struct |
195 | { |
196 | hb_reference_count_t ; |
197 | hb_atomic_ptr_t<hb_user_data_array_t> ; |
198 | |
199 | #define {HB_REFERENCE_COUNT_INIT, HB_ATOMIC_PTR_INIT (nullptr)} |
200 | |
201 | private: |
202 | ASSERT_POD (); |
203 | }; |
204 | |
205 | |
206 | /* |
207 | * Object |
208 | */ |
209 | |
210 | template <typename Type> |
211 | static inline void hb_object_trace (const Type *obj, const char *function) |
212 | { |
213 | DEBUG_MSG (OBJECT, (void *) obj, |
214 | "%s refcount=%d" , |
215 | function, |
216 | obj ? obj->header.ref_count.get_relaxed () : 0); |
217 | } |
218 | |
219 | template <typename Type> |
220 | static inline Type *hb_object_create (void) |
221 | { |
222 | Type *obj = (Type *) calloc (1, sizeof (Type)); |
223 | |
224 | if (unlikely (!obj)) |
225 | return obj; |
226 | |
227 | hb_object_init (obj); |
228 | hb_object_trace (obj, HB_FUNC); |
229 | return obj; |
230 | } |
231 | template <typename Type> |
232 | static inline void hb_object_init (Type *obj) |
233 | { |
234 | obj->header.ref_count.init (); |
235 | obj->header.user_data.init (); |
236 | } |
237 | template <typename Type> |
238 | static inline bool hb_object_is_inert (const Type *obj) |
239 | { |
240 | return unlikely (obj->header.ref_count.is_inert ()); |
241 | } |
242 | template <typename Type> |
243 | static inline bool hb_object_is_valid (const Type *obj) |
244 | { |
245 | return likely (obj->header.ref_count.is_valid ()); |
246 | } |
247 | template <typename Type> |
248 | static inline Type *hb_object_reference (Type *obj) |
249 | { |
250 | hb_object_trace (obj, HB_FUNC); |
251 | if (unlikely (!obj || hb_object_is_inert (obj))) |
252 | return obj; |
253 | assert (hb_object_is_valid (obj)); |
254 | obj->header.ref_count.inc (); |
255 | return obj; |
256 | } |
257 | template <typename Type> |
258 | static inline bool hb_object_destroy (Type *obj) |
259 | { |
260 | hb_object_trace (obj, HB_FUNC); |
261 | if (unlikely (!obj || hb_object_is_inert (obj))) |
262 | return false; |
263 | assert (hb_object_is_valid (obj)); |
264 | if (obj->header.ref_count.dec () != 1) |
265 | return false; |
266 | |
267 | hb_object_fini (obj); |
268 | return true; |
269 | } |
270 | template <typename Type> |
271 | static inline void hb_object_fini (Type *obj) |
272 | { |
273 | obj->header.ref_count.fini (); /* Do this before user_data */ |
274 | hb_user_data_array_t *user_data = obj->header.user_data.get (); |
275 | if (user_data) |
276 | { |
277 | user_data->fini (); |
278 | free (user_data); |
279 | } |
280 | } |
281 | template <typename Type> |
282 | static inline bool hb_object_set_user_data (Type *obj, |
283 | hb_user_data_key_t *key, |
284 | void * data, |
285 | hb_destroy_func_t destroy, |
286 | hb_bool_t replace) |
287 | { |
288 | if (unlikely (!obj || hb_object_is_inert (obj))) |
289 | return false; |
290 | assert (hb_object_is_valid (obj)); |
291 | |
292 | retry: |
293 | hb_user_data_array_t *user_data = obj->header.user_data.get (); |
294 | if (unlikely (!user_data)) |
295 | { |
296 | user_data = (hb_user_data_array_t *) calloc (sizeof (hb_user_data_array_t), 1); |
297 | if (unlikely (!user_data)) |
298 | return false; |
299 | user_data->init (); |
300 | if (unlikely (!obj->header.user_data.cmpexch (nullptr, user_data))) |
301 | { |
302 | user_data->fini (); |
303 | free (user_data); |
304 | goto retry; |
305 | } |
306 | } |
307 | |
308 | return user_data->set (key, data, destroy, replace); |
309 | } |
310 | |
311 | template <typename Type> |
312 | static inline void *hb_object_get_user_data (Type *obj, |
313 | hb_user_data_key_t *key) |
314 | { |
315 | if (unlikely (!obj || hb_object_is_inert (obj))) |
316 | return nullptr; |
317 | assert (hb_object_is_valid (obj)); |
318 | hb_user_data_array_t *user_data = obj->header.user_data.get (); |
319 | if (!user_data) |
320 | return nullptr; |
321 | return user_data->get (key); |
322 | } |
323 | |
324 | |
325 | #endif /* HB_OBJECT_HH */ |
326 | |