README: Babeltrace 2 was released in 2020
[babeltrace.git] / src / lib / object.h
CommitLineData
83509119
JG
1#ifndef BABELTRACE_OBJECT_INTERNAL_H
2#define BABELTRACE_OBJECT_INTERNAL_H
273b65be
JG
3
4/*
f2b0325d 5 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
83509119 6 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
273b65be
JG
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
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
24 * SOFTWARE.
25 */
26
85e7137b 27#include "common/macros.h"
57952005 28#include "common/assert.h"
a6918753 29#include <stdbool.h>
273b65be 30
1d7bf349
PP
31struct bt_object;
32
33typedef void (*bt_object_release_func)(struct bt_object *);
34typedef void (*bt_object_parent_is_owner_listener_func)(
35 struct bt_object *);
36
37static inline
864aa43f 38void bt_object_get_ref_no_null_check(const void *obj);
1d7bf349
PP
39
40static inline
864aa43f 41void bt_object_put_ref_no_null_check(const void *obj);
1d7bf349
PP
42
43/*
44 * Babeltrace object base.
45 *
46 * All objects publicly exposed by Babeltrace APIs must contain this
47 * object as their first member.
de3dd40e 48 */
83509119 49struct bt_object {
a6918753 50 /*
1d7bf349
PP
51 * True if this object is shared, that is, it has a reference
52 * count.
a6918753
PP
53 */
54 bool is_shared;
1d7bf349
PP
55
56 /*
57 * Current reference count.
58 */
59 unsigned long long ref_count;
60
61 /*
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.
66 */
67 bt_object_release_func release_func;
68
69 /*
70 * Specific release function called by
71 * bt_object_with_parent_release_func() or directly by a
72 * parent object.
73 */
74 bt_object_release_func spec_release_func;
75
76 /*
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.
80 */
81 bt_object_parent_is_owner_listener_func
82 parent_is_owner_listener_func;
83
84 /*
85 * Optional parent object.
86 */
87 struct bt_object *parent;
273b65be
JG
88};
89
90static inline
4b70020d 91unsigned long long bt_object_get_ref_count(const struct bt_object *c_obj)
7c56f3cc 92{
4b70020d
PP
93 struct bt_object *obj = (void *) c_obj;
94
ec4a3354
PP
95 BT_ASSERT_DBG(obj);
96 BT_ASSERT_DBG(obj->is_shared);
1d7bf349
PP
97 return obj->ref_count;
98}
5d2d9981 99
1d7bf349 100static inline
4b70020d 101struct bt_object *bt_object_borrow_parent(const struct bt_object *c_obj)
1d7bf349 102{
4b70020d
PP
103 struct bt_object *obj = (void *) c_obj;
104
ec4a3354
PP
105 BT_ASSERT_DBG(obj);
106 BT_ASSERT_DBG(obj->is_shared);
1d7bf349 107 return obj->parent;
7c56f3cc
MD
108}
109
110static inline
4b70020d 111struct bt_object *bt_object_get_parent(const struct bt_object *c_obj)
5d2d9981 112{
4b70020d 113 struct bt_object *obj = (void *) c_obj;
1d7bf349
PP
114 struct bt_object *parent = bt_object_borrow_parent(obj);
115
116 if (parent) {
864aa43f 117 bt_object_get_ref_no_null_check(parent);
1d7bf349
PP
118 }
119
120 return parent;
121}
122
123static inline
124void bt_object_set_parent(struct bt_object *child, struct bt_object *parent)
125{
ec4a3354
PP
126 BT_ASSERT_DBG(child);
127 BT_ASSERT_DBG(child->is_shared);
5d2d9981 128
31ee2162 129#ifdef BT_LOGT
c9ecaa78 130 BT_LOGT("Setting object's parent: addr=%p, parent-addr=%p",
1d7bf349 131 child, parent);
0f5e83e5
PP
132#endif
133
1d7bf349
PP
134 /*
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.
139 */
140 if (parent) {
ec4a3354 141 BT_ASSERT_DBG(!child->parent);
1d7bf349 142 child->parent = parent;
864aa43f 143 bt_object_get_ref_no_null_check(parent);
1d7bf349
PP
144 } else {
145 if (child->parent) {
864aa43f 146 bt_object_put_ref_no_null_check(child->parent);
1d7bf349
PP
147 }
148
149 child->parent = NULL;
5d2d9981
JG
150 }
151}
152
7c56f3cc 153static inline
1d7bf349
PP
154void bt_object_try_spec_release(struct bt_object *obj)
155{
ec4a3354
PP
156 BT_ASSERT_DBG(obj);
157 BT_ASSERT_DBG(obj->is_shared);
158 BT_ASSERT_DBG(obj->spec_release_func);
1d7bf349
PP
159
160 if (bt_object_get_ref_count(obj) == 0) {
161 obj->spec_release_func(obj);
162 }
163}
164
165static inline
166void bt_object_with_parent_release_func(struct bt_object *obj)
5d2d9981
JG
167{
168 if (obj->parent) {
1d7bf349
PP
169 /*
170 * Keep our own copy of the parent address because `obj`
171 * could be destroyed in
172 * obj->parent_is_owner_listener_func().
173 */
0f5e83e5
PP
174 struct bt_object *parent = obj->parent;
175
31ee2162 176#ifdef BT_LOGT
c9ecaa78 177 BT_LOGT("Releasing parented object: addr=%p, ref-count=%llu, "
1d7bf349
PP
178 "parent-addr=%p, parent-ref-count=%llu",
179 obj, obj->ref_count,
180 parent, parent->ref_count);
0f5e83e5 181#endif
f167d3c0 182
1d7bf349 183 if (obj->parent_is_owner_listener_func) {
f167d3c0
PP
184 /*
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.
189 */
1d7bf349 190 obj->parent_is_owner_listener_func(obj);
f167d3c0
PP
191 }
192
5d2d9981 193 /* The release function will be invoked by the parent. */
864aa43f 194 bt_object_put_ref_no_null_check(parent);
5d2d9981 195 } else {
1d7bf349 196 bt_object_try_spec_release(obj);
5d2d9981
JG
197 }
198}
199
200static inline
1d7bf349
PP
201void bt_object_init(struct bt_object *obj, bool is_shared,
202 bt_object_release_func release_func)
5d2d9981 203{
ec4a3354
PP
204 BT_ASSERT_DBG(obj);
205 BT_ASSERT_DBG(!is_shared || release_func);
1d7bf349
PP
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;
210 obj->parent = NULL;
211 obj->ref_count = 1;
dc3fffef
PP
212}
213
214static inline
1d7bf349
PP
215void bt_object_init_shared(struct bt_object *obj,
216 bt_object_release_func release_func)
dc3fffef 217{
1d7bf349 218 bt_object_init(obj, true, release_func);
5d2d9981
JG
219}
220
221static inline
1d7bf349 222void bt_object_init_unique(struct bt_object *obj)
273b65be 223{
1d7bf349
PP
224 bt_object_init(obj, false, NULL);
225}
5d2d9981 226
1d7bf349
PP
227static inline
228void bt_object_init_shared_with_parent(struct bt_object *obj,
229 bt_object_release_func spec_release_func)
230{
ec4a3354
PP
231 BT_ASSERT_DBG(obj);
232 BT_ASSERT_DBG(spec_release_func);
1d7bf349
PP
233 bt_object_init_shared(obj, bt_object_with_parent_release_func);
234 obj->spec_release_func = spec_release_func;
235}
0f5e83e5 236
1d7bf349
PP
237static inline
238void bt_object_set_parent_is_owner_listener_func(struct bt_object *obj,
239 bt_object_parent_is_owner_listener_func func)
240{
ec4a3354
PP
241 BT_ASSERT_DBG(obj);
242 BT_ASSERT_DBG(obj->is_shared);
243 BT_ASSERT_DBG(obj->spec_release_func);
1d7bf349 244 ((struct bt_object *) obj)->parent_is_owner_listener_func = func;
5d2d9981
JG
245}
246
a6918753 247static inline
4b70020d 248void bt_object_inc_ref_count(const struct bt_object *c_obj)
a6918753 249{
4b70020d
PP
250 struct bt_object *obj = (void *) c_obj;
251
ec4a3354
PP
252 BT_ASSERT_DBG(obj);
253 BT_ASSERT_DBG(obj->is_shared);
1d7bf349 254 obj->ref_count++;
ec4a3354 255 BT_ASSERT_DBG(obj->ref_count != 0);
a6918753
PP
256}
257
c5a24b0a 258static inline
864aa43f 259void bt_object_get_ref_no_null_check_no_parent_check(const struct bt_object *c_obj)
c5a24b0a 260{
4b70020d
PP
261 struct bt_object *obj = (void *) c_obj;
262
ec4a3354
PP
263 BT_ASSERT_DBG(obj);
264 BT_ASSERT_DBG(obj->is_shared);
c5a24b0a 265
31ee2162 266#ifdef BT_LOGT
c9ecaa78 267 BT_LOGT("Incrementing object's reference count: %llu -> %llu: "
c5a24b0a
PP
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);
271#endif
272
273 bt_object_inc_ref_count(obj);
274}
275
5d2d9981 276static inline
864aa43f 277void bt_object_get_ref_no_null_check(const void *c_obj)
5d2d9981 278{
4b70020d
PP
279 struct bt_object *obj = (void *) c_obj;
280
ec4a3354
PP
281 BT_ASSERT_DBG(obj);
282 BT_ASSERT_DBG(obj->is_shared);
5d2d9981 283
85e7137b 284 if (G_UNLIKELY(obj->parent && bt_object_get_ref_count(obj) == 0)) {
31ee2162 285#ifdef BT_LOGT
c9ecaa78 286 BT_LOGT("Incrementing object's parent's reference count: "
1d7bf349
PP
287 "addr=%p, parent-addr=%p", obj, obj->parent);
288#endif
289
864aa43f 290 bt_object_get_ref_no_null_check(obj->parent);
1d7bf349
PP
291 }
292
31ee2162 293#ifdef BT_LOGT
c9ecaa78 294 BT_LOGT("Incrementing object's reference count: %llu -> %llu: "
1d7bf349
PP
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);
298#endif
299
300 bt_object_inc_ref_count(obj);
273b65be
JG
301}
302
f167d3c0 303static inline
864aa43f 304void bt_object_put_ref_no_null_check(const void *c_obj)
f167d3c0 305{
4b70020d
PP
306 struct bt_object *obj = (void *) c_obj;
307
ec4a3354
PP
308 BT_ASSERT_DBG(obj);
309 BT_ASSERT_DBG(obj->is_shared);
310 BT_ASSERT_DBG(obj->ref_count > 0);
1d7bf349 311
31ee2162 312#ifdef BT_LOGT
c9ecaa78 313 BT_LOGT("Decrementing object's reference count: %llu -> %llu: "
1d7bf349
PP
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);
317#endif
318
319 obj->ref_count--;
320
321 if (obj->ref_count == 0) {
ec4a3354 322 BT_ASSERT_DBG(obj->release_func);
1d7bf349
PP
323 obj->release_func(obj);
324 }
f167d3c0
PP
325}
326
8c6884d9
PP
327static inline
328void bt_object_get_ref(const void *ptr)
329{
330 struct bt_object *obj = (void *) ptr;
331
85e7137b 332 if (G_UNLIKELY(!obj)) {
8c6884d9
PP
333 return;
334 }
335
fa6cfec3
PP
336#ifdef BT_ASSERT_PRE_DEV
337 BT_ASSERT_PRE_DEV(obj->is_shared, "Object is not shared: %!+O", obj);
8c6884d9
PP
338#endif
339
864aa43f 340 bt_object_get_ref_no_null_check(obj);
8c6884d9
PP
341}
342
343static inline
344void bt_object_put_ref(const void *ptr)
345{
346 struct bt_object *obj = (void *) ptr;
347
85e7137b 348 if (G_UNLIKELY(!obj)) {
8c6884d9
PP
349 return;
350 }
351
fa6cfec3
PP
352#ifdef BT_ASSERT_PRE_DEV
353 BT_ASSERT_PRE_DEV(obj->is_shared, "Object is not shared: %!+O", obj);
354 BT_ASSERT_PRE_DEV(bt_object_get_ref_count(obj) > 0,
8c6884d9
PP
355 "Decrementing a reference count set to 0: %!+O", ptr);
356#endif
357
864aa43f 358 bt_object_put_ref_no_null_check(obj);
8c6884d9
PP
359}
360
361#define BT_OBJECT_PUT_REF_AND_RESET(_var) \
362 do { \
363 bt_object_put_ref(_var); \
364 (_var) = NULL; \
365 } while (0)
366
367#define BT_OBJECT_MOVE_REF(_var_dst, _var_src) \
368 do { \
369 bt_object_put_ref(_var_dst); \
370 (_var_dst) = (_var_src); \
371 (_var_src) = NULL; \
372 } while (0)
373
83509119 374#endif /* BABELTRACE_OBJECT_INTERNAL_H */
This page took 0.105547 seconds and 4 git commands to generate.