2 * SPDX-License-Identifier: MIT
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 #ifndef BABELTRACE_OBJECT_INTERNAL_H
9 #define BABELTRACE_OBJECT_INTERNAL_H
11 #include "common/assert.h"
16 typedef void (*bt_object_release_func
)(struct bt_object
*);
17 typedef void (*bt_object_parent_is_owner_listener_func
)(
21 void bt_object_get_ref_no_null_check(const void *obj
);
24 void bt_object_put_ref_no_null_check(const void *obj
);
27 * Babeltrace object base.
29 * All objects publicly exposed by Babeltrace APIs must contain this
30 * object as their first member.
34 * True if this object is shared, that is, it has a reference
40 * Current reference count.
42 unsigned long long ref_count
;
45 * Release function called when the object's reference count
46 * falls to zero. For an object with a parent, this function is
47 * bt_object_with_parent_release_func(), which calls
48 * `spec_release_func` below if there's no current parent.
50 bt_object_release_func release_func
;
53 * Specific release function called by
54 * bt_object_with_parent_release_func() or directly by a
57 bt_object_release_func spec_release_func
;
60 * Optional callback for an object with a parent, called by
61 * bt_object_with_parent_release_func() to indicate to the
62 * object that its parent is its owner.
64 bt_object_parent_is_owner_listener_func
65 parent_is_owner_listener_func
;
68 * Optional parent object.
70 struct bt_object
*parent
;
74 unsigned long long bt_object_get_ref_count(const struct bt_object
*c_obj
)
76 struct bt_object
*obj
= (void *) c_obj
;
79 BT_ASSERT_DBG(obj
->is_shared
);
80 return obj
->ref_count
;
84 struct bt_object
*bt_object_borrow_parent(const struct bt_object
*c_obj
)
86 struct bt_object
*obj
= (void *) c_obj
;
89 BT_ASSERT_DBG(obj
->is_shared
);
94 struct bt_object
*bt_object_get_parent(const struct bt_object
*c_obj
)
96 struct bt_object
*obj
= (void *) c_obj
;
97 struct bt_object
*parent
= bt_object_borrow_parent(obj
);
100 bt_object_get_ref_no_null_check(parent
);
107 void bt_object_set_parent(struct bt_object
*child
, struct bt_object
*parent
)
109 BT_ASSERT_DBG(child
);
110 BT_ASSERT_DBG(child
->is_shared
);
113 BT_LOGT("Setting object's parent: addr=%p, parent-addr=%p",
118 * It is assumed that a "child" having a parent is publicly
119 * reachable. Therefore, a reference to its parent must be
120 * taken. The reference to the parent will be released once the
121 * object's reference count falls to zero.
124 BT_ASSERT_DBG(!child
->parent
);
125 child
->parent
= parent
;
126 bt_object_get_ref_no_null_check(parent
);
129 bt_object_put_ref_no_null_check(child
->parent
);
132 child
->parent
= NULL
;
137 void bt_object_try_spec_release(struct bt_object
*obj
)
140 BT_ASSERT_DBG(obj
->is_shared
);
141 BT_ASSERT_DBG(obj
->spec_release_func
);
143 if (bt_object_get_ref_count(obj
) == 0) {
144 obj
->spec_release_func(obj
);
149 void bt_object_with_parent_release_func(struct bt_object
*obj
)
153 * Keep our own copy of the parent address because `obj`
154 * could be destroyed in
155 * obj->parent_is_owner_listener_func().
157 struct bt_object
*parent
= obj
->parent
;
160 BT_LOGT("Releasing parented object: addr=%p, ref-count=%llu, "
161 "parent-addr=%p, parent-ref-count=%llu",
163 parent
, parent
->ref_count
);
166 if (obj
->parent_is_owner_listener_func
) {
168 * Object has a chance to destroy itself here
169 * under certain conditions and notify its
170 * parent. At this point the parent is
171 * guaranteed to exist because it's not put yet.
173 obj
->parent_is_owner_listener_func(obj
);
176 /* The release function will be invoked by the parent. */
177 bt_object_put_ref_no_null_check(parent
);
179 bt_object_try_spec_release(obj
);
184 void bt_object_init(struct bt_object
*obj
, bool is_shared
,
185 bt_object_release_func release_func
)
188 BT_ASSERT_DBG(!is_shared
|| release_func
);
189 obj
->is_shared
= is_shared
;
190 obj
->release_func
= release_func
;
191 obj
->parent_is_owner_listener_func
= NULL
;
192 obj
->spec_release_func
= NULL
;
198 void bt_object_init_shared(struct bt_object
*obj
,
199 bt_object_release_func release_func
)
201 bt_object_init(obj
, true, release_func
);
205 void bt_object_init_unique(struct bt_object
*obj
)
207 bt_object_init(obj
, false, NULL
);
211 void bt_object_init_shared_with_parent(struct bt_object
*obj
,
212 bt_object_release_func spec_release_func
)
215 BT_ASSERT_DBG(spec_release_func
);
216 bt_object_init_shared(obj
, bt_object_with_parent_release_func
);
217 obj
->spec_release_func
= spec_release_func
;
221 void bt_object_set_parent_is_owner_listener_func(struct bt_object
*obj
,
222 bt_object_parent_is_owner_listener_func func
)
225 BT_ASSERT_DBG(obj
->is_shared
);
226 BT_ASSERT_DBG(obj
->spec_release_func
);
227 ((struct bt_object
*) obj
)->parent_is_owner_listener_func
= func
;
231 void bt_object_inc_ref_count(const struct bt_object
*c_obj
)
233 struct bt_object
*obj
= (void *) c_obj
;
236 BT_ASSERT_DBG(obj
->is_shared
);
238 BT_ASSERT_DBG(obj
->ref_count
!= 0);
242 void bt_object_get_ref_no_null_check_no_parent_check(const struct bt_object
*c_obj
)
244 struct bt_object
*obj
= (void *) c_obj
;
247 BT_ASSERT_DBG(obj
->is_shared
);
250 BT_LOGT("Incrementing object's reference count: %llu -> %llu: "
251 "addr=%p, cur-count=%llu, new-count=%llu",
252 obj
->ref_count
, obj
->ref_count
+ 1,
253 obj
, obj
->ref_count
, obj
->ref_count
+ 1);
256 bt_object_inc_ref_count(obj
);
260 void bt_object_get_ref_no_null_check(const void *c_obj
)
262 struct bt_object
*obj
= (void *) c_obj
;
265 BT_ASSERT_DBG(obj
->is_shared
);
267 if (G_UNLIKELY(obj
->parent
&& bt_object_get_ref_count(obj
) == 0)) {
269 BT_LOGT("Incrementing object's parent's reference count: "
270 "addr=%p, parent-addr=%p", obj
, obj
->parent
);
273 bt_object_get_ref_no_null_check(obj
->parent
);
277 BT_LOGT("Incrementing object's reference count: %llu -> %llu: "
278 "addr=%p, cur-count=%llu, new-count=%llu",
279 obj
->ref_count
, obj
->ref_count
+ 1,
280 obj
, obj
->ref_count
, obj
->ref_count
+ 1);
283 bt_object_inc_ref_count(obj
);
287 void bt_object_put_ref_no_null_check(const void *c_obj
)
289 struct bt_object
*obj
= (void *) c_obj
;
292 BT_ASSERT_DBG(obj
->is_shared
);
293 BT_ASSERT_DBG(obj
->ref_count
> 0);
296 BT_LOGT("Decrementing object's reference count: %llu -> %llu: "
297 "addr=%p, cur-count=%llu, new-count=%llu",
298 obj
->ref_count
, obj
->ref_count
- 1,
299 obj
, obj
->ref_count
, obj
->ref_count
- 1);
304 if (obj
->ref_count
== 0) {
305 BT_ASSERT_DBG(obj
->release_func
);
306 obj
->release_func(obj
);
311 void bt_object_get_ref(const void *ptr
)
313 struct bt_object
*obj
= (void *) ptr
;
315 if (G_UNLIKELY(!obj
)) {
319 BT_ASSERT_DBG(obj
->is_shared
);
320 bt_object_get_ref_no_null_check(obj
);
324 void bt_object_put_ref(const void *ptr
)
326 struct bt_object
*obj
= (void *) ptr
;
328 if (G_UNLIKELY(!obj
)) {
332 BT_ASSERT_DBG(obj
->is_shared
);
333 BT_ASSERT_DBG(bt_object_get_ref_count(obj
) > 0);
334 bt_object_put_ref_no_null_check(obj
);
337 #define BT_OBJECT_PUT_REF_AND_RESET(_var) \
339 bt_object_put_ref(_var); \
343 #define BT_OBJECT_MOVE_REF(_var_dst, _var_src) \
345 bt_object_put_ref(_var_dst); \
346 (_var_dst) = (_var_src); \
350 #endif /* BABELTRACE_OBJECT_INTERNAL_H */