2 * SPDX-License-Identifier: MIT
4 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 #ifndef BABELTRACE_CTF_WRITER_OBJECT_INTERNAL_H
8 #define BABELTRACE_CTF_WRITER_OBJECT_INTERNAL_H
10 #include "common/macros.h"
11 #include "common/assert.h"
16 typedef void (*bt_ctf_object_release_func
)(struct bt_ctf_object
*);
17 typedef void (*bt_ctf_object_parent_is_owner_listener_func
)(
18 struct bt_ctf_object
*);
21 void *bt_ctf_object_get_no_null_check(struct bt_ctf_object
*obj
);
24 void bt_ctf_object_put_no_null_check(struct bt_ctf_object
*obj
);
27 * Babeltrace object base.
29 * All objects publicly exposed by Babeltrace APIs must contain this
30 * object as their first member.
32 struct bt_ctf_object
{
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_ctf_object_with_parent_release_func(), which calls
48 * `spec_release_func` below if there's no current parent.
50 bt_ctf_object_release_func release_func
;
53 * Specific release function called by
54 * bt_ctf_object_with_parent_release_func() or directly by a
57 bt_ctf_object_release_func spec_release_func
;
60 * Optional callback for an object with a parent, called by
61 * bt_ctf_object_with_parent_release_func() to indicate to the
62 * object that its parent is its owner.
64 bt_ctf_object_parent_is_owner_listener_func
65 parent_is_owner_listener_func
;
68 * Optional parent object.
70 struct bt_ctf_object
*parent
;
74 unsigned long long bt_ctf_object_get_ref_count(struct bt_ctf_object
*obj
)
77 BT_ASSERT_DBG(obj
->is_shared
);
78 return obj
->ref_count
;
82 struct bt_ctf_object
*bt_ctf_object_borrow_parent(struct bt_ctf_object
*obj
)
85 BT_ASSERT_DBG(obj
->is_shared
);
90 struct bt_ctf_object
*bt_ctf_object_get_parent(struct bt_ctf_object
*obj
)
92 struct bt_ctf_object
*parent
= bt_ctf_object_borrow_parent(obj
);
95 bt_ctf_object_get_no_null_check(parent
);
102 void bt_ctf_object_set_parent(struct bt_ctf_object
*child
, struct bt_ctf_object
*parent
)
104 BT_ASSERT_DBG(child
);
105 BT_ASSERT_DBG(child
->is_shared
);
108 BT_LOGT("Setting object's parent: addr=%p, parent-addr=%p",
113 * It is assumed that a "child" having a parent is publicly
114 * reachable. Therefore, a reference to its parent must be
115 * taken. The reference to the parent will be released once the
116 * object's reference count falls to zero.
119 BT_ASSERT_DBG(!child
->parent
);
120 child
->parent
= parent
;
121 bt_ctf_object_get_no_null_check(parent
);
124 bt_ctf_object_put_no_null_check(child
->parent
);
127 child
->parent
= NULL
;
132 void bt_ctf_object_try_spec_release(struct bt_ctf_object
*obj
)
135 BT_ASSERT_DBG(obj
->is_shared
);
136 BT_ASSERT_DBG(obj
->spec_release_func
);
138 if (bt_ctf_object_get_ref_count(obj
) == 0) {
139 obj
->spec_release_func(obj
);
144 void bt_ctf_object_with_parent_release_func(struct bt_ctf_object
*obj
)
148 * Keep our own copy of the parent address because `obj`
149 * could be destroyed in
150 * obj->parent_is_owner_listener_func().
152 struct bt_ctf_object
*parent
= obj
->parent
;
155 BT_LOGT("Releasing parented object: addr=%p, ref-count=%llu, "
156 "parent-addr=%p, parent-ref-count=%llu",
158 parent
, parent
->ref_count
);
161 if (obj
->parent_is_owner_listener_func
) {
163 * Object has a chance to destroy itself here
164 * under certain conditions and notify its
165 * parent. At this point the parent is
166 * guaranteed to exist because it's not put yet.
168 obj
->parent_is_owner_listener_func(obj
);
171 /* The release function will be invoked by the parent. */
172 bt_ctf_object_put_no_null_check(parent
);
174 bt_ctf_object_try_spec_release(obj
);
179 void bt_ctf_object_init(struct bt_ctf_object
*obj
, bool is_shared
,
180 bt_ctf_object_release_func release_func
)
183 BT_ASSERT_DBG(!is_shared
|| release_func
);
184 obj
->is_shared
= is_shared
;
185 obj
->release_func
= release_func
;
186 obj
->parent_is_owner_listener_func
= NULL
;
187 obj
->spec_release_func
= NULL
;
193 void bt_ctf_object_init_shared(struct bt_ctf_object
*obj
,
194 bt_ctf_object_release_func release_func
)
196 bt_ctf_object_init(obj
, true, release_func
);
200 void bt_ctf_object_init_unique(struct bt_ctf_object
*obj
)
202 bt_ctf_object_init(obj
, false, NULL
);
206 void bt_ctf_object_init_shared_with_parent(struct bt_ctf_object
*obj
,
207 bt_ctf_object_release_func spec_release_func
)
210 BT_ASSERT_DBG(spec_release_func
);
211 bt_ctf_object_init_shared(obj
, bt_ctf_object_with_parent_release_func
);
212 obj
->spec_release_func
= spec_release_func
;
216 void bt_ctf_object_set_parent_is_owner_listener_func(struct bt_ctf_object
*obj
,
217 bt_ctf_object_parent_is_owner_listener_func func
)
220 BT_ASSERT_DBG(obj
->is_shared
);
221 BT_ASSERT_DBG(obj
->spec_release_func
);
222 ((struct bt_ctf_object
*) obj
)->parent_is_owner_listener_func
= func
;
226 void bt_ctf_object_inc_ref_count(struct bt_ctf_object
*obj
)
229 BT_ASSERT_DBG(obj
->is_shared
);
231 BT_ASSERT_DBG(obj
->ref_count
!= 0);
235 void *bt_ctf_object_get_no_null_check_no_parent_check(struct bt_ctf_object
*obj
)
238 BT_ASSERT_DBG(obj
->is_shared
);
241 BT_LOGT("Incrementing object's reference count: %llu -> %llu: "
242 "addr=%p, cur-count=%llu, new-count=%llu",
243 obj
->ref_count
, obj
->ref_count
+ 1,
244 obj
, obj
->ref_count
, obj
->ref_count
+ 1);
247 bt_ctf_object_inc_ref_count(obj
);
252 void *bt_ctf_object_get_no_null_check(struct bt_ctf_object
*obj
)
255 BT_ASSERT_DBG(obj
->is_shared
);
257 if (G_UNLIKELY(obj
->parent
&& bt_ctf_object_get_ref_count(obj
) == 0)) {
259 BT_LOGT("Incrementing object's parent's reference count: "
260 "addr=%p, parent-addr=%p", obj
, obj
->parent
);
263 bt_ctf_object_get_no_null_check(obj
->parent
);
267 BT_LOGT("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);
273 bt_ctf_object_inc_ref_count(obj
);
278 void bt_ctf_object_put_no_null_check(struct bt_ctf_object
*obj
)
281 BT_ASSERT_DBG(obj
->is_shared
);
282 BT_ASSERT_DBG(obj
->ref_count
> 0);
285 BT_LOGT("Decrementing object's reference count: %llu -> %llu: "
286 "addr=%p, cur-count=%llu, new-count=%llu",
287 obj
->ref_count
, obj
->ref_count
- 1,
288 obj
, obj
->ref_count
, obj
->ref_count
- 1);
293 if (obj
->ref_count
== 0) {
294 BT_ASSERT_DBG(obj
->release_func
);
295 obj
->release_func(obj
);
299 #endif /* BABELTRACE_CTF_WRITER_OBJECT_INTERNAL_H */