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 <babeltrace/babeltrace-internal.h>
28 #include <babeltrace/assert-internal.h>
33 typedef void (*bt_object_release_func
)(struct bt_object
*);
34 typedef void (*bt_object_parent_is_owner_listener_func
)(
38 void bt_object_get_no_null_check(const void *obj
);
41 void bt_object_put_no_null_check(const void *obj
);
44 * Babeltrace object base.
46 * All objects publicly exposed by Babeltrace APIs must contain this
47 * object as their first member.
51 * True if this object is shared, that is, it has a reference
57 * Current reference count.
59 unsigned long long ref_count
;
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.
67 bt_object_release_func release_func
;
70 * Specific release function called by
71 * bt_object_with_parent_release_func() or directly by a
74 bt_object_release_func spec_release_func
;
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.
81 bt_object_parent_is_owner_listener_func
82 parent_is_owner_listener_func
;
85 * Optional parent object.
87 struct bt_object
*parent
;
91 unsigned long long bt_object_get_ref_count(const struct bt_object
*c_obj
)
93 struct bt_object
*obj
= (void *) c_obj
;
96 BT_ASSERT(obj
->is_shared
);
97 return obj
->ref_count
;
101 struct bt_object
*bt_object_borrow_parent(const struct bt_object
*c_obj
)
103 struct bt_object
*obj
= (void *) c_obj
;
106 BT_ASSERT(obj
->is_shared
);
111 struct bt_object
*bt_object_get_parent(const struct bt_object
*c_obj
)
113 struct bt_object
*obj
= (void *) c_obj
;
114 struct bt_object
*parent
= bt_object_borrow_parent(obj
);
117 bt_object_get_no_null_check(parent
);
124 void bt_object_set_parent(struct bt_object
*child
, struct bt_object
*parent
)
127 BT_ASSERT(child
->is_shared
);
130 BT_LOGV("Setting object's parent: addr=%p, parent-addr=%p",
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.
141 BT_ASSERT(!child
->parent
);
142 child
->parent
= parent
;
143 bt_object_get_no_null_check(parent
);
146 bt_object_put_no_null_check(child
->parent
);
149 child
->parent
= NULL
;
154 void bt_object_try_spec_release(struct bt_object
*obj
)
157 BT_ASSERT(obj
->is_shared
);
158 BT_ASSERT(obj
->spec_release_func
);
160 if (bt_object_get_ref_count(obj
) == 0) {
161 obj
->spec_release_func(obj
);
166 void bt_object_with_parent_release_func(struct bt_object
*obj
)
170 * Keep our own copy of the parent address because `obj`
171 * could be destroyed in
172 * obj->parent_is_owner_listener_func().
174 struct bt_object
*parent
= obj
->parent
;
177 BT_LOGV("Releasing parented object: addr=%p, ref-count=%llu, "
178 "parent-addr=%p, parent-ref-count=%llu",
180 parent
, parent
->ref_count
);
183 if (obj
->parent_is_owner_listener_func
) {
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.
190 obj
->parent_is_owner_listener_func(obj
);
193 /* The release function will be invoked by the parent. */
194 bt_object_put_no_null_check(parent
);
196 bt_object_try_spec_release(obj
);
201 void bt_object_init(struct bt_object
*obj
, bool is_shared
,
202 bt_object_release_func release_func
)
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
;
215 void bt_object_init_shared(struct bt_object
*obj
,
216 bt_object_release_func release_func
)
218 bt_object_init(obj
, true, release_func
);
222 void bt_object_init_unique(struct bt_object
*obj
)
224 bt_object_init(obj
, false, NULL
);
228 void bt_object_init_shared_with_parent(struct bt_object
*obj
,
229 bt_object_release_func spec_release_func
)
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
;
238 void bt_object_set_parent_is_owner_listener_func(struct bt_object
*obj
,
239 bt_object_parent_is_owner_listener_func func
)
242 BT_ASSERT(obj
->is_shared
);
243 BT_ASSERT(obj
->spec_release_func
);
244 ((struct bt_object
*) obj
)->parent_is_owner_listener_func
= func
;
248 void bt_object_inc_ref_count(const struct bt_object
*c_obj
)
250 struct bt_object
*obj
= (void *) c_obj
;
253 BT_ASSERT(obj
->is_shared
);
255 BT_ASSERT(obj
->ref_count
!= 0);
259 void bt_object_get_no_null_check_no_parent_check(const struct bt_object
*c_obj
)
261 struct bt_object
*obj
= (void *) c_obj
;
264 BT_ASSERT(obj
->is_shared
);
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);
273 bt_object_inc_ref_count(obj
);
277 void bt_object_get_no_null_check(const void *c_obj
)
279 struct bt_object
*obj
= (void *) c_obj
;
282 BT_ASSERT(obj
->is_shared
);
284 if (unlikely(obj
->parent
&& bt_object_get_ref_count(obj
) == 0)) {
286 BT_LOGV("Incrementing object's parent's reference count: "
287 "addr=%p, parent-addr=%p", obj
, obj
->parent
);
290 bt_object_get_no_null_check(obj
->parent
);
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);
300 bt_object_inc_ref_count(obj
);
304 void bt_object_put_no_null_check(const void *c_obj
)
306 struct bt_object
*obj
= (void *) c_obj
;
309 BT_ASSERT(obj
->is_shared
);
310 BT_ASSERT(obj
->ref_count
> 0);
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);
321 if (obj
->ref_count
== 0) {
322 BT_ASSERT(obj
->release_func
);
323 obj
->release_func(obj
);
328 void bt_object_get_ref(const void *ptr
)
330 struct bt_object
*obj
= (void *) ptr
;
332 if (unlikely(!obj
)) {
337 BT_ASSERT_PRE(obj
->is_shared
, "Object is not shared: %!+O", obj
);
340 bt_object_get_no_null_check(obj
);
344 void bt_object_put_ref(const void *ptr
)
346 struct bt_object
*obj
= (void *) ptr
;
348 if (unlikely(!obj
)) {
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
);
358 bt_object_put_no_null_check(obj
);
361 #define BT_OBJECT_PUT_REF_AND_RESET(_var) \
363 bt_object_put_ref(_var); \
367 #define BT_OBJECT_MOVE_REF(_var_dst, _var_src) \
369 bt_object_put_ref(_var_dst); \
370 (_var_dst) = (_var_src); \
374 #endif /* BABELTRACE_OBJECT_INTERNAL_H */