Commit | Line | Data |
---|---|---|
83509119 JG |
1 | #ifndef BABELTRACE_OBJECT_INTERNAL_H |
2 | #define BABELTRACE_OBJECT_INTERNAL_H | |
273b65be JG |
3 | |
4 | /* | |
e2f7325d | 5 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
83509119 | 6 | * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
273b65be JG |
7 | * |
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
9 | * of this software and associated documentation files (the "Software"), to deal | |
10 | * in the Software without restriction, including without limitation the rights | |
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
12 | * copies of the Software, and to permit persons to whom the Software is | |
13 | * furnished to do so, subject to the following conditions: | |
14 | * | |
15 | * The above copyright notice and this permission notice shall be included in | |
16 | * all copies or substantial portions of the Software. | |
17 | * | |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
24 | * SOFTWARE. | |
25 | */ | |
26 | ||
312c056a | 27 | #include <babeltrace/babeltrace-internal.h> |
f6ccaed9 | 28 | #include <babeltrace/assert-internal.h> |
312c056a | 29 | #include <stdbool.h> |
273b65be | 30 | |
3fea54f6 PP |
31 | struct bt_object; |
32 | ||
33 | typedef void (*bt_object_release_func)(struct bt_object *); | |
34 | typedef void (*bt_object_parent_is_owner_listener_func)( | |
35 | struct bt_object *); | |
36 | ||
37 | static inline | |
398454ed | 38 | void bt_object_get_no_null_check(const void *obj); |
3fea54f6 PP |
39 | |
40 | static inline | |
398454ed | 41 | void bt_object_put_no_null_check(const void *obj); |
3fea54f6 PP |
42 | |
43 | /* | |
44 | * Babeltrace object base. | |
45 | * | |
46 | * All objects publicly exposed by Babeltrace APIs must contain this | |
47 | * object as their first member. | |
de3dd40e | 48 | */ |
83509119 | 49 | struct bt_object { |
312c056a | 50 | /* |
3fea54f6 PP |
51 | * True if this object is shared, that is, it has a reference |
52 | * count. | |
312c056a PP |
53 | */ |
54 | bool is_shared; | |
3fea54f6 PP |
55 | |
56 | /* | |
57 | * Current reference count. | |
58 | */ | |
59 | unsigned long long ref_count; | |
60 | ||
61 | /* | |
62 | * Release function called when the object's reference count | |
63 | * falls to zero. For an object with a parent, this function is | |
64 | * bt_object_with_parent_release_func(), which calls | |
65 | * `spec_release_func` below if there's no current parent. | |
66 | */ | |
67 | bt_object_release_func release_func; | |
68 | ||
69 | /* | |
70 | * Specific release function called by | |
71 | * bt_object_with_parent_release_func() or directly by a | |
72 | * parent object. | |
73 | */ | |
74 | bt_object_release_func spec_release_func; | |
75 | ||
76 | /* | |
77 | * Optional callback for an object with a parent, called by | |
78 | * bt_object_with_parent_release_func() to indicate to the | |
79 | * object that its parent is its owner. | |
80 | */ | |
81 | bt_object_parent_is_owner_listener_func | |
82 | parent_is_owner_listener_func; | |
83 | ||
84 | /* | |
85 | * Optional parent object. | |
86 | */ | |
87 | struct bt_object *parent; | |
273b65be JG |
88 | }; |
89 | ||
90 | static inline | |
398454ed | 91 | unsigned long long bt_object_get_ref_count(const struct bt_object *c_obj) |
7c56f3cc | 92 | { |
398454ed PP |
93 | struct bt_object *obj = (void *) c_obj; |
94 | ||
3fea54f6 PP |
95 | BT_ASSERT(obj); |
96 | BT_ASSERT(obj->is_shared); | |
97 | return obj->ref_count; | |
98 | } | |
5d2d9981 | 99 | |
3fea54f6 | 100 | static inline |
398454ed | 101 | struct bt_object *bt_object_borrow_parent(const struct bt_object *c_obj) |
3fea54f6 | 102 | { |
398454ed PP |
103 | struct bt_object *obj = (void *) c_obj; |
104 | ||
3fea54f6 PP |
105 | BT_ASSERT(obj); |
106 | BT_ASSERT(obj->is_shared); | |
107 | return obj->parent; | |
7c56f3cc MD |
108 | } |
109 | ||
110 | static inline | |
398454ed | 111 | struct bt_object *bt_object_get_parent(const struct bt_object *c_obj) |
5d2d9981 | 112 | { |
398454ed | 113 | struct bt_object *obj = (void *) c_obj; |
3fea54f6 PP |
114 | struct bt_object *parent = bt_object_borrow_parent(obj); |
115 | ||
116 | if (parent) { | |
117 | bt_object_get_no_null_check(parent); | |
118 | } | |
119 | ||
120 | return parent; | |
121 | } | |
122 | ||
123 | static inline | |
124 | void bt_object_set_parent(struct bt_object *child, struct bt_object *parent) | |
125 | { | |
126 | BT_ASSERT(child); | |
127 | BT_ASSERT(child->is_shared); | |
5d2d9981 | 128 | |
0f5e83e5 | 129 | #ifdef BT_LOGV |
3fea54f6 PP |
130 | BT_LOGV("Setting object's parent: addr=%p, parent-addr=%p", |
131 | child, parent); | |
0f5e83e5 PP |
132 | #endif |
133 | ||
3fea54f6 PP |
134 | /* |
135 | * It is assumed that a "child" having a parent is publicly | |
136 | * reachable. Therefore, a reference to its parent must be | |
137 | * taken. The reference to the parent will be released once the | |
138 | * object's reference count falls to zero. | |
139 | */ | |
140 | if (parent) { | |
141 | BT_ASSERT(!child->parent); | |
142 | child->parent = parent; | |
143 | bt_object_get_no_null_check(parent); | |
144 | } else { | |
145 | if (child->parent) { | |
146 | bt_object_put_no_null_check(child->parent); | |
147 | } | |
148 | ||
149 | child->parent = NULL; | |
5d2d9981 JG |
150 | } |
151 | } | |
152 | ||
7c56f3cc | 153 | static inline |
3fea54f6 PP |
154 | void bt_object_try_spec_release(struct bt_object *obj) |
155 | { | |
156 | BT_ASSERT(obj); | |
157 | BT_ASSERT(obj->is_shared); | |
158 | BT_ASSERT(obj->spec_release_func); | |
159 | ||
160 | if (bt_object_get_ref_count(obj) == 0) { | |
161 | obj->spec_release_func(obj); | |
162 | } | |
163 | } | |
164 | ||
165 | static inline | |
166 | void bt_object_with_parent_release_func(struct bt_object *obj) | |
5d2d9981 JG |
167 | { |
168 | if (obj->parent) { | |
3fea54f6 PP |
169 | /* |
170 | * Keep our own copy of the parent address because `obj` | |
171 | * could be destroyed in | |
172 | * obj->parent_is_owner_listener_func(). | |
173 | */ | |
0f5e83e5 PP |
174 | struct bt_object *parent = obj->parent; |
175 | ||
176 | #ifdef BT_LOGV | |
3fea54f6 PP |
177 | BT_LOGV("Releasing parented object: addr=%p, ref-count=%llu, " |
178 | "parent-addr=%p, parent-ref-count=%llu", | |
179 | obj, obj->ref_count, | |
180 | parent, parent->ref_count); | |
0f5e83e5 | 181 | #endif |
f167d3c0 | 182 | |
3fea54f6 | 183 | if (obj->parent_is_owner_listener_func) { |
f167d3c0 PP |
184 | /* |
185 | * Object has a chance to destroy itself here | |
186 | * under certain conditions and notify its | |
187 | * parent. At this point the parent is | |
188 | * guaranteed to exist because it's not put yet. | |
189 | */ | |
3fea54f6 | 190 | obj->parent_is_owner_listener_func(obj); |
f167d3c0 PP |
191 | } |
192 | ||
5d2d9981 | 193 | /* The release function will be invoked by the parent. */ |
3fea54f6 | 194 | bt_object_put_no_null_check(parent); |
5d2d9981 | 195 | } else { |
3fea54f6 | 196 | bt_object_try_spec_release(obj); |
5d2d9981 JG |
197 | } |
198 | } | |
199 | ||
200 | static inline | |
3fea54f6 PP |
201 | void bt_object_init(struct bt_object *obj, bool is_shared, |
202 | bt_object_release_func release_func) | |
5d2d9981 | 203 | { |
3fea54f6 PP |
204 | BT_ASSERT(obj); |
205 | BT_ASSERT(!is_shared || release_func); | |
206 | obj->is_shared = is_shared; | |
207 | obj->release_func = release_func; | |
208 | obj->parent_is_owner_listener_func = NULL; | |
209 | obj->spec_release_func = NULL; | |
210 | obj->parent = NULL; | |
211 | obj->ref_count = 1; | |
dc3fffef PP |
212 | } |
213 | ||
214 | static inline | |
3fea54f6 PP |
215 | void bt_object_init_shared(struct bt_object *obj, |
216 | bt_object_release_func release_func) | |
dc3fffef | 217 | { |
3fea54f6 | 218 | bt_object_init(obj, true, release_func); |
5d2d9981 JG |
219 | } |
220 | ||
221 | static inline | |
3fea54f6 | 222 | void bt_object_init_unique(struct bt_object *obj) |
273b65be | 223 | { |
3fea54f6 PP |
224 | bt_object_init(obj, false, NULL); |
225 | } | |
5d2d9981 | 226 | |
3fea54f6 PP |
227 | static inline |
228 | void bt_object_init_shared_with_parent(struct bt_object *obj, | |
229 | bt_object_release_func spec_release_func) | |
230 | { | |
231 | BT_ASSERT(obj); | |
232 | BT_ASSERT(spec_release_func); | |
233 | bt_object_init_shared(obj, bt_object_with_parent_release_func); | |
234 | obj->spec_release_func = spec_release_func; | |
235 | } | |
0f5e83e5 | 236 | |
3fea54f6 PP |
237 | static inline |
238 | void bt_object_set_parent_is_owner_listener_func(struct bt_object *obj, | |
239 | bt_object_parent_is_owner_listener_func func) | |
240 | { | |
241 | BT_ASSERT(obj); | |
242 | BT_ASSERT(obj->is_shared); | |
243 | BT_ASSERT(obj->spec_release_func); | |
244 | ((struct bt_object *) obj)->parent_is_owner_listener_func = func; | |
5d2d9981 JG |
245 | } |
246 | ||
312c056a | 247 | static inline |
398454ed | 248 | void bt_object_inc_ref_count(const struct bt_object *c_obj) |
312c056a | 249 | { |
398454ed PP |
250 | struct bt_object *obj = (void *) c_obj; |
251 | ||
3fea54f6 PP |
252 | BT_ASSERT(obj); |
253 | BT_ASSERT(obj->is_shared); | |
254 | obj->ref_count++; | |
255 | BT_ASSERT(obj->ref_count != 0); | |
312c056a PP |
256 | } |
257 | ||
6c677fb5 | 258 | static inline |
398454ed | 259 | void bt_object_get_no_null_check_no_parent_check(const struct bt_object *c_obj) |
6c677fb5 | 260 | { |
398454ed PP |
261 | struct bt_object *obj = (void *) c_obj; |
262 | ||
6c677fb5 PP |
263 | BT_ASSERT(obj); |
264 | BT_ASSERT(obj->is_shared); | |
265 | ||
266 | #ifdef BT_LOGV | |
267 | BT_LOGV("Incrementing object's reference count: %llu -> %llu: " | |
268 | "addr=%p, cur-count=%llu, new-count=%llu", | |
269 | obj->ref_count, obj->ref_count + 1, | |
270 | obj, obj->ref_count, obj->ref_count + 1); | |
271 | #endif | |
272 | ||
273 | bt_object_inc_ref_count(obj); | |
274 | } | |
275 | ||
5d2d9981 | 276 | static inline |
398454ed | 277 | void bt_object_get_no_null_check(const void *c_obj) |
5d2d9981 | 278 | { |
398454ed PP |
279 | struct bt_object *obj = (void *) c_obj; |
280 | ||
3fea54f6 PP |
281 | BT_ASSERT(obj); |
282 | BT_ASSERT(obj->is_shared); | |
5d2d9981 | 283 | |
3fea54f6 PP |
284 | if (unlikely(obj->parent && bt_object_get_ref_count(obj) == 0)) { |
285 | #ifdef BT_LOGV | |
286 | BT_LOGV("Incrementing object's parent's reference count: " | |
287 | "addr=%p, parent-addr=%p", obj, obj->parent); | |
288 | #endif | |
289 | ||
290 | bt_object_get_no_null_check(obj->parent); | |
291 | } | |
292 | ||
293 | #ifdef BT_LOGV | |
294 | BT_LOGV("Incrementing object's reference count: %llu -> %llu: " | |
295 | "addr=%p, cur-count=%llu, new-count=%llu", | |
296 | obj->ref_count, obj->ref_count + 1, | |
297 | obj, obj->ref_count, obj->ref_count + 1); | |
298 | #endif | |
299 | ||
300 | bt_object_inc_ref_count(obj); | |
273b65be JG |
301 | } |
302 | ||
f167d3c0 | 303 | static inline |
398454ed | 304 | void bt_object_put_no_null_check(const void *c_obj) |
f167d3c0 | 305 | { |
398454ed PP |
306 | struct bt_object *obj = (void *) c_obj; |
307 | ||
f6ccaed9 | 308 | BT_ASSERT(obj); |
3fea54f6 PP |
309 | BT_ASSERT(obj->is_shared); |
310 | BT_ASSERT(obj->ref_count > 0); | |
311 | ||
312 | #ifdef BT_LOGV | |
313 | BT_LOGV("Decrementing object's reference count: %llu -> %llu: " | |
314 | "addr=%p, cur-count=%llu, new-count=%llu", | |
315 | obj->ref_count, obj->ref_count - 1, | |
316 | obj, obj->ref_count, obj->ref_count - 1); | |
317 | #endif | |
318 | ||
319 | obj->ref_count--; | |
320 | ||
321 | if (obj->ref_count == 0) { | |
322 | BT_ASSERT(obj->release_func); | |
323 | obj->release_func(obj); | |
324 | } | |
f167d3c0 PP |
325 | } |
326 | ||
c5b9b441 PP |
327 | static inline |
328 | void bt_object_get_ref(const void *ptr) | |
329 | { | |
330 | struct bt_object *obj = (void *) ptr; | |
331 | ||
332 | if (unlikely(!obj)) { | |
333 | return; | |
334 | } | |
335 | ||
336 | #ifdef BT_ASSERT_PRE | |
337 | BT_ASSERT_PRE(obj->is_shared, "Object is not shared: %!+O", obj); | |
338 | #endif | |
339 | ||
340 | bt_object_get_no_null_check(obj); | |
341 | } | |
342 | ||
343 | static inline | |
344 | void bt_object_put_ref(const void *ptr) | |
345 | { | |
346 | struct bt_object *obj = (void *) ptr; | |
347 | ||
348 | if (unlikely(!obj)) { | |
349 | return; | |
350 | } | |
351 | ||
352 | #ifdef BT_ASSERT_PRE | |
353 | BT_ASSERT_PRE(obj->is_shared, "Object is not shared: %!+O", obj); | |
354 | BT_ASSERT_PRE(bt_object_get_ref_count(obj) > 0, | |
355 | "Decrementing a reference count set to 0: %!+O", ptr); | |
356 | #endif | |
357 | ||
358 | bt_object_put_no_null_check(obj); | |
359 | } | |
360 | ||
361 | #define BT_OBJECT_PUT_REF_AND_RESET(_var) \ | |
362 | do { \ | |
363 | bt_object_put_ref(_var); \ | |
364 | (_var) = NULL; \ | |
365 | } while (0) | |
366 | ||
367 | #define BT_OBJECT_MOVE_REF(_var_dst, _var_src) \ | |
368 | do { \ | |
369 | bt_object_put_ref(_var_dst); \ | |
370 | (_var_dst) = (_var_src); \ | |
371 | (_var_src) = NULL; \ | |
372 | } while (0) | |
373 | ||
83509119 | 374 | #endif /* BABELTRACE_OBJECT_INTERNAL_H */ |