1 #ifndef BABELTRACE_OBJECT_INTERNAL_H
2 #define BABELTRACE_OBJECT_INTERNAL_H
5 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
6 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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
27 #include "common/macros.h"
28 #include "common/assert.h"
32 * Some parts of the Babeltrace project use this internal library header
33 * for internal reference counting. Until we make this header generic
34 * for the whole project, make it possible to disable logging in this
35 * file by defining `BT_OBJECT_DONT_LOG` because it's possible that the
36 * BT_LOGT() statements here won't find the log level
37 * (`BT_LOG_OUTPUT_LEVEL`).
39 #if defined(BT_LOGT) && !defined(BT_OBJECT_DONT_LOG)
40 # define _BT_OBJECT_LOGGING_ENABLED
45 typedef void (*bt_object_release_func
)(struct bt_object
*);
46 typedef void (*bt_object_parent_is_owner_listener_func
)(
50 void bt_object_get_no_null_check(const void *obj
);
53 void bt_object_put_no_null_check(const void *obj
);
56 * Babeltrace object base.
58 * All objects publicly exposed by Babeltrace APIs must contain this
59 * object as their first member.
63 * True if this object is shared, that is, it has a reference
69 * Current reference count.
71 unsigned long long ref_count
;
74 * Release function called when the object's reference count
75 * falls to zero. For an object with a parent, this function is
76 * bt_object_with_parent_release_func(), which calls
77 * `spec_release_func` below if there's no current parent.
79 bt_object_release_func release_func
;
82 * Specific release function called by
83 * bt_object_with_parent_release_func() or directly by a
86 bt_object_release_func spec_release_func
;
89 * Optional callback for an object with a parent, called by
90 * bt_object_with_parent_release_func() to indicate to the
91 * object that its parent is its owner.
93 bt_object_parent_is_owner_listener_func
94 parent_is_owner_listener_func
;
97 * Optional parent object.
99 struct bt_object
*parent
;
103 unsigned long long bt_object_get_ref_count(const struct bt_object
*c_obj
)
105 struct bt_object
*obj
= (void *) c_obj
;
108 BT_ASSERT(obj
->is_shared
);
109 return obj
->ref_count
;
113 struct bt_object
*bt_object_borrow_parent(const struct bt_object
*c_obj
)
115 struct bt_object
*obj
= (void *) c_obj
;
118 BT_ASSERT(obj
->is_shared
);
123 struct bt_object
*bt_object_get_parent(const struct bt_object
*c_obj
)
125 struct bt_object
*obj
= (void *) c_obj
;
126 struct bt_object
*parent
= bt_object_borrow_parent(obj
);
129 bt_object_get_no_null_check(parent
);
136 void bt_object_set_parent(struct bt_object
*child
, struct bt_object
*parent
)
139 BT_ASSERT(child
->is_shared
);
141 #ifdef _BT_OBJECT_LOGGING_ENABLED
142 BT_LOGT("Setting object's parent: addr=%p, parent-addr=%p",
147 * It is assumed that a "child" having a parent is publicly
148 * reachable. Therefore, a reference to its parent must be
149 * taken. The reference to the parent will be released once the
150 * object's reference count falls to zero.
153 BT_ASSERT(!child
->parent
);
154 child
->parent
= parent
;
155 bt_object_get_no_null_check(parent
);
158 bt_object_put_no_null_check(child
->parent
);
161 child
->parent
= NULL
;
166 void bt_object_try_spec_release(struct bt_object
*obj
)
169 BT_ASSERT(obj
->is_shared
);
170 BT_ASSERT(obj
->spec_release_func
);
172 if (bt_object_get_ref_count(obj
) == 0) {
173 obj
->spec_release_func(obj
);
178 void bt_object_with_parent_release_func(struct bt_object
*obj
)
182 * Keep our own copy of the parent address because `obj`
183 * could be destroyed in
184 * obj->parent_is_owner_listener_func().
186 struct bt_object
*parent
= obj
->parent
;
188 #ifdef _BT_OBJECT_LOGGING_ENABLED
189 BT_LOGT("Releasing parented object: addr=%p, ref-count=%llu, "
190 "parent-addr=%p, parent-ref-count=%llu",
192 parent
, parent
->ref_count
);
195 if (obj
->parent_is_owner_listener_func
) {
197 * Object has a chance to destroy itself here
198 * under certain conditions and notify its
199 * parent. At this point the parent is
200 * guaranteed to exist because it's not put yet.
202 obj
->parent_is_owner_listener_func(obj
);
205 /* The release function will be invoked by the parent. */
206 bt_object_put_no_null_check(parent
);
208 bt_object_try_spec_release(obj
);
213 void bt_object_init(struct bt_object
*obj
, bool is_shared
,
214 bt_object_release_func release_func
)
217 BT_ASSERT(!is_shared
|| release_func
);
218 obj
->is_shared
= is_shared
;
219 obj
->release_func
= release_func
;
220 obj
->parent_is_owner_listener_func
= NULL
;
221 obj
->spec_release_func
= NULL
;
227 void bt_object_init_shared(struct bt_object
*obj
,
228 bt_object_release_func release_func
)
230 bt_object_init(obj
, true, release_func
);
234 void bt_object_init_unique(struct bt_object
*obj
)
236 bt_object_init(obj
, false, NULL
);
240 void bt_object_init_shared_with_parent(struct bt_object
*obj
,
241 bt_object_release_func spec_release_func
)
244 BT_ASSERT(spec_release_func
);
245 bt_object_init_shared(obj
, bt_object_with_parent_release_func
);
246 obj
->spec_release_func
= spec_release_func
;
250 void bt_object_set_parent_is_owner_listener_func(struct bt_object
*obj
,
251 bt_object_parent_is_owner_listener_func func
)
254 BT_ASSERT(obj
->is_shared
);
255 BT_ASSERT(obj
->spec_release_func
);
256 ((struct bt_object
*) obj
)->parent_is_owner_listener_func
= func
;
260 void bt_object_inc_ref_count(const struct bt_object
*c_obj
)
262 struct bt_object
*obj
= (void *) c_obj
;
265 BT_ASSERT(obj
->is_shared
);
267 BT_ASSERT(obj
->ref_count
!= 0);
271 void bt_object_get_no_null_check_no_parent_check(const struct bt_object
*c_obj
)
273 struct bt_object
*obj
= (void *) c_obj
;
276 BT_ASSERT(obj
->is_shared
);
278 #ifdef _BT_OBJECT_LOGGING_ENABLED
279 BT_LOGT("Incrementing object's reference count: %llu -> %llu: "
280 "addr=%p, cur-count=%llu, new-count=%llu",
281 obj
->ref_count
, obj
->ref_count
+ 1,
282 obj
, obj
->ref_count
, obj
->ref_count
+ 1);
285 bt_object_inc_ref_count(obj
);
289 void bt_object_get_no_null_check(const void *c_obj
)
291 struct bt_object
*obj
= (void *) c_obj
;
294 BT_ASSERT(obj
->is_shared
);
296 if (G_UNLIKELY(obj
->parent
&& bt_object_get_ref_count(obj
) == 0)) {
297 #ifdef _BT_OBJECT_LOGGING_ENABLED
298 BT_LOGT("Incrementing object's parent's reference count: "
299 "addr=%p, parent-addr=%p", obj
, obj
->parent
);
302 bt_object_get_no_null_check(obj
->parent
);
305 #ifdef _BT_OBJECT_LOGGING_ENABLED
306 BT_LOGT("Incrementing object's reference count: %llu -> %llu: "
307 "addr=%p, cur-count=%llu, new-count=%llu",
308 obj
->ref_count
, obj
->ref_count
+ 1,
309 obj
, obj
->ref_count
, obj
->ref_count
+ 1);
312 bt_object_inc_ref_count(obj
);
316 void bt_object_put_no_null_check(const void *c_obj
)
318 struct bt_object
*obj
= (void *) c_obj
;
321 BT_ASSERT(obj
->is_shared
);
322 BT_ASSERT(obj
->ref_count
> 0);
324 #ifdef _BT_OBJECT_LOGGING_ENABLED
325 BT_LOGT("Decrementing object's reference count: %llu -> %llu: "
326 "addr=%p, cur-count=%llu, new-count=%llu",
327 obj
->ref_count
, obj
->ref_count
- 1,
328 obj
, obj
->ref_count
, obj
->ref_count
- 1);
333 if (obj
->ref_count
== 0) {
334 BT_ASSERT(obj
->release_func
);
335 obj
->release_func(obj
);
340 void bt_object_get_ref(const void *ptr
)
342 struct bt_object
*obj
= (void *) ptr
;
344 if (G_UNLIKELY(!obj
)) {
348 #ifdef BT_ASSERT_PRE_DEV
349 BT_ASSERT_PRE_DEV(obj
->is_shared
, "Object is not shared: %!+O", obj
);
352 bt_object_get_no_null_check(obj
);
356 void bt_object_put_ref(const void *ptr
)
358 struct bt_object
*obj
= (void *) ptr
;
360 if (G_UNLIKELY(!obj
)) {
364 #ifdef BT_ASSERT_PRE_DEV
365 BT_ASSERT_PRE_DEV(obj
->is_shared
, "Object is not shared: %!+O", obj
);
366 BT_ASSERT_PRE_DEV(bt_object_get_ref_count(obj
) > 0,
367 "Decrementing a reference count set to 0: %!+O", ptr
);
370 bt_object_put_no_null_check(obj
);
373 #define BT_OBJECT_PUT_REF_AND_RESET(_var) \
375 bt_object_put_ref(_var); \
379 #define BT_OBJECT_MOVE_REF(_var_dst, _var_src) \
381 bt_object_put_ref(_var_dst); \
382 (_var_dst) = (_var_src); \
386 #endif /* BABELTRACE_OBJECT_INTERNAL_H */