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