Fix: lib: pass down API function name to some helpers
[babeltrace.git] / src / lib / object.h
CommitLineData
273b65be 1/*
0235b0db
MJ
2 * SPDX-License-Identifier: MIT
3 *
e2f7325d 4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
83509119 5 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
273b65be
JG
6 */
7
0235b0db
MJ
8#ifndef BABELTRACE_OBJECT_INTERNAL_H
9#define BABELTRACE_OBJECT_INTERNAL_H
10
91d81473 11#include "common/macros.h"
578e048b 12#include "common/assert.h"
312c056a 13#include <stdbool.h>
273b65be 14
3fea54f6
PP
15struct bt_object;
16
17typedef void (*bt_object_release_func)(struct bt_object *);
18typedef void (*bt_object_parent_is_owner_listener_func)(
19 struct bt_object *);
20
21static inline
6871026b 22void bt_object_get_ref_no_null_check(const void *obj);
3fea54f6
PP
23
24static inline
6871026b 25void bt_object_put_ref_no_null_check(const void *obj);
3fea54f6
PP
26
27/*
28 * Babeltrace object base.
29 *
30 * All objects publicly exposed by Babeltrace APIs must contain this
31 * object as their first member.
de3dd40e 32 */
83509119 33struct bt_object {
312c056a 34 /*
3fea54f6
PP
35 * True if this object is shared, that is, it has a reference
36 * count.
312c056a
PP
37 */
38 bool is_shared;
3fea54f6
PP
39
40 /*
41 * Current reference count.
42 */
43 unsigned long long ref_count;
44
45 /*
46 * Release function called when the object's reference count
47 * falls to zero. For an object with a parent, this function is
48 * bt_object_with_parent_release_func(), which calls
49 * `spec_release_func` below if there's no current parent.
50 */
51 bt_object_release_func release_func;
52
53 /*
54 * Specific release function called by
55 * bt_object_with_parent_release_func() or directly by a
56 * parent object.
57 */
58 bt_object_release_func spec_release_func;
59
60 /*
61 * Optional callback for an object with a parent, called by
62 * bt_object_with_parent_release_func() to indicate to the
63 * object that its parent is its owner.
64 */
65 bt_object_parent_is_owner_listener_func
66 parent_is_owner_listener_func;
67
68 /*
69 * Optional parent object.
70 */
71 struct bt_object *parent;
273b65be
JG
72};
73
74static inline
398454ed 75unsigned long long bt_object_get_ref_count(const struct bt_object *c_obj)
7c56f3cc 76{
398454ed
PP
77 struct bt_object *obj = (void *) c_obj;
78
98b15851
PP
79 BT_ASSERT_DBG(obj);
80 BT_ASSERT_DBG(obj->is_shared);
3fea54f6
PP
81 return obj->ref_count;
82}
5d2d9981 83
3fea54f6 84static inline
398454ed 85struct bt_object *bt_object_borrow_parent(const struct bt_object *c_obj)
3fea54f6 86{
398454ed
PP
87 struct bt_object *obj = (void *) c_obj;
88
98b15851
PP
89 BT_ASSERT_DBG(obj);
90 BT_ASSERT_DBG(obj->is_shared);
3fea54f6 91 return obj->parent;
7c56f3cc
MD
92}
93
94static inline
398454ed 95struct bt_object *bt_object_get_parent(const struct bt_object *c_obj)
5d2d9981 96{
398454ed 97 struct bt_object *obj = (void *) c_obj;
3fea54f6
PP
98 struct bt_object *parent = bt_object_borrow_parent(obj);
99
100 if (parent) {
6871026b 101 bt_object_get_ref_no_null_check(parent);
3fea54f6
PP
102 }
103
104 return parent;
105}
106
107static inline
108void bt_object_set_parent(struct bt_object *child, struct bt_object *parent)
109{
98b15851
PP
110 BT_ASSERT_DBG(child);
111 BT_ASSERT_DBG(child->is_shared);
5d2d9981 112
b9e6ec43 113#ifdef BT_LOGT
ef267d12 114 BT_LOGT("Setting object's parent: addr=%p, parent-addr=%p",
3fea54f6 115 child, parent);
0f5e83e5
PP
116#endif
117
3fea54f6
PP
118 /*
119 * It is assumed that a "child" having a parent is publicly
120 * reachable. Therefore, a reference to its parent must be
121 * taken. The reference to the parent will be released once the
122 * object's reference count falls to zero.
123 */
124 if (parent) {
98b15851 125 BT_ASSERT_DBG(!child->parent);
3fea54f6 126 child->parent = parent;
6871026b 127 bt_object_get_ref_no_null_check(parent);
3fea54f6
PP
128 } else {
129 if (child->parent) {
6871026b 130 bt_object_put_ref_no_null_check(child->parent);
3fea54f6
PP
131 }
132
133 child->parent = NULL;
5d2d9981
JG
134 }
135}
136
7c56f3cc 137static inline
3fea54f6
PP
138void bt_object_try_spec_release(struct bt_object *obj)
139{
98b15851
PP
140 BT_ASSERT_DBG(obj);
141 BT_ASSERT_DBG(obj->is_shared);
142 BT_ASSERT_DBG(obj->spec_release_func);
3fea54f6
PP
143
144 if (bt_object_get_ref_count(obj) == 0) {
145 obj->spec_release_func(obj);
146 }
147}
148
149static inline
150void bt_object_with_parent_release_func(struct bt_object *obj)
5d2d9981
JG
151{
152 if (obj->parent) {
3fea54f6
PP
153 /*
154 * Keep our own copy of the parent address because `obj`
155 * could be destroyed in
156 * obj->parent_is_owner_listener_func().
157 */
0f5e83e5
PP
158 struct bt_object *parent = obj->parent;
159
b9e6ec43 160#ifdef BT_LOGT
ef267d12 161 BT_LOGT("Releasing parented object: addr=%p, ref-count=%llu, "
3fea54f6
PP
162 "parent-addr=%p, parent-ref-count=%llu",
163 obj, obj->ref_count,
164 parent, parent->ref_count);
0f5e83e5 165#endif
f167d3c0 166
3fea54f6 167 if (obj->parent_is_owner_listener_func) {
f167d3c0
PP
168 /*
169 * Object has a chance to destroy itself here
170 * under certain conditions and notify its
171 * parent. At this point the parent is
172 * guaranteed to exist because it's not put yet.
173 */
3fea54f6 174 obj->parent_is_owner_listener_func(obj);
f167d3c0
PP
175 }
176
5d2d9981 177 /* The release function will be invoked by the parent. */
6871026b 178 bt_object_put_ref_no_null_check(parent);
5d2d9981 179 } else {
3fea54f6 180 bt_object_try_spec_release(obj);
5d2d9981
JG
181 }
182}
183
184static inline
3fea54f6
PP
185void bt_object_init(struct bt_object *obj, bool is_shared,
186 bt_object_release_func release_func)
5d2d9981 187{
98b15851
PP
188 BT_ASSERT_DBG(obj);
189 BT_ASSERT_DBG(!is_shared || release_func);
3fea54f6
PP
190 obj->is_shared = is_shared;
191 obj->release_func = release_func;
192 obj->parent_is_owner_listener_func = NULL;
193 obj->spec_release_func = NULL;
194 obj->parent = NULL;
195 obj->ref_count = 1;
dc3fffef
PP
196}
197
198static inline
3fea54f6
PP
199void bt_object_init_shared(struct bt_object *obj,
200 bt_object_release_func release_func)
dc3fffef 201{
3fea54f6 202 bt_object_init(obj, true, release_func);
5d2d9981
JG
203}
204
205static inline
3fea54f6 206void bt_object_init_unique(struct bt_object *obj)
273b65be 207{
3fea54f6
PP
208 bt_object_init(obj, false, NULL);
209}
5d2d9981 210
3fea54f6
PP
211static inline
212void bt_object_init_shared_with_parent(struct bt_object *obj,
213 bt_object_release_func spec_release_func)
214{
98b15851
PP
215 BT_ASSERT_DBG(obj);
216 BT_ASSERT_DBG(spec_release_func);
3fea54f6
PP
217 bt_object_init_shared(obj, bt_object_with_parent_release_func);
218 obj->spec_release_func = spec_release_func;
219}
0f5e83e5 220
3fea54f6
PP
221static inline
222void bt_object_set_parent_is_owner_listener_func(struct bt_object *obj,
223 bt_object_parent_is_owner_listener_func func)
224{
98b15851
PP
225 BT_ASSERT_DBG(obj);
226 BT_ASSERT_DBG(obj->is_shared);
227 BT_ASSERT_DBG(obj->spec_release_func);
3fea54f6 228 ((struct bt_object *) obj)->parent_is_owner_listener_func = func;
5d2d9981
JG
229}
230
312c056a 231static inline
398454ed 232void bt_object_inc_ref_count(const struct bt_object *c_obj)
312c056a 233{
398454ed
PP
234 struct bt_object *obj = (void *) c_obj;
235
98b15851
PP
236 BT_ASSERT_DBG(obj);
237 BT_ASSERT_DBG(obj->is_shared);
3fea54f6 238 obj->ref_count++;
98b15851 239 BT_ASSERT_DBG(obj->ref_count != 0);
312c056a
PP
240}
241
6c677fb5 242static inline
6871026b 243void bt_object_get_ref_no_null_check_no_parent_check(const struct bt_object *c_obj)
6c677fb5 244{
398454ed
PP
245 struct bt_object *obj = (void *) c_obj;
246
98b15851
PP
247 BT_ASSERT_DBG(obj);
248 BT_ASSERT_DBG(obj->is_shared);
6c677fb5 249
b9e6ec43 250#ifdef BT_LOGT
ef267d12 251 BT_LOGT("Incrementing object's reference count: %llu -> %llu: "
6c677fb5
PP
252 "addr=%p, cur-count=%llu, new-count=%llu",
253 obj->ref_count, obj->ref_count + 1,
254 obj, obj->ref_count, obj->ref_count + 1);
255#endif
256
257 bt_object_inc_ref_count(obj);
258}
259
5d2d9981 260static inline
6871026b 261void bt_object_get_ref_no_null_check(const void *c_obj)
5d2d9981 262{
398454ed
PP
263 struct bt_object *obj = (void *) c_obj;
264
98b15851
PP
265 BT_ASSERT_DBG(obj);
266 BT_ASSERT_DBG(obj->is_shared);
5d2d9981 267
91d81473 268 if (G_UNLIKELY(obj->parent && bt_object_get_ref_count(obj) == 0)) {
b9e6ec43 269#ifdef BT_LOGT
ef267d12 270 BT_LOGT("Incrementing object's parent's reference count: "
3fea54f6
PP
271 "addr=%p, parent-addr=%p", obj, obj->parent);
272#endif
273
6871026b 274 bt_object_get_ref_no_null_check(obj->parent);
3fea54f6
PP
275 }
276
b9e6ec43 277#ifdef BT_LOGT
ef267d12 278 BT_LOGT("Incrementing object's reference count: %llu -> %llu: "
3fea54f6
PP
279 "addr=%p, cur-count=%llu, new-count=%llu",
280 obj->ref_count, obj->ref_count + 1,
281 obj, obj->ref_count, obj->ref_count + 1);
282#endif
283
284 bt_object_inc_ref_count(obj);
273b65be
JG
285}
286
f167d3c0 287static inline
6871026b 288void bt_object_put_ref_no_null_check(const void *c_obj)
f167d3c0 289{
398454ed
PP
290 struct bt_object *obj = (void *) c_obj;
291
98b15851
PP
292 BT_ASSERT_DBG(obj);
293 BT_ASSERT_DBG(obj->is_shared);
294 BT_ASSERT_DBG(obj->ref_count > 0);
3fea54f6 295
b9e6ec43 296#ifdef BT_LOGT
ef267d12 297 BT_LOGT("Decrementing object's reference count: %llu -> %llu: "
3fea54f6
PP
298 "addr=%p, cur-count=%llu, new-count=%llu",
299 obj->ref_count, obj->ref_count - 1,
300 obj, obj->ref_count, obj->ref_count - 1);
301#endif
302
303 obj->ref_count--;
304
305 if (obj->ref_count == 0) {
98b15851 306 BT_ASSERT_DBG(obj->release_func);
3fea54f6
PP
307 obj->release_func(obj);
308 }
f167d3c0
PP
309}
310
c5b9b441
PP
311static inline
312void bt_object_get_ref(const void *ptr)
313{
314 struct bt_object *obj = (void *) ptr;
315
91d81473 316 if (G_UNLIKELY(!obj)) {
c5b9b441
PP
317 return;
318 }
319
5d70aba9 320 BT_ASSERT_DBG(obj->is_shared);
6871026b 321 bt_object_get_ref_no_null_check(obj);
c5b9b441
PP
322}
323
324static inline
325void bt_object_put_ref(const void *ptr)
326{
327 struct bt_object *obj = (void *) ptr;
328
91d81473 329 if (G_UNLIKELY(!obj)) {
c5b9b441
PP
330 return;
331 }
332
5d70aba9
PP
333 BT_ASSERT_DBG(obj->is_shared);
334 BT_ASSERT_DBG(bt_object_get_ref_count(obj) > 0);
6871026b 335 bt_object_put_ref_no_null_check(obj);
c5b9b441
PP
336}
337
338#define BT_OBJECT_PUT_REF_AND_RESET(_var) \
339 do { \
340 bt_object_put_ref(_var); \
341 (_var) = NULL; \
342 } while (0)
343
344#define BT_OBJECT_MOVE_REF(_var_dst, _var_src) \
345 do { \
346 bt_object_put_ref(_var_dst); \
347 (_var_dst) = (_var_src); \
348 (_var_src) = NULL; \
349 } while (0)
350
83509119 351#endif /* BABELTRACE_OBJECT_INTERNAL_H */
This page took 0.10906 seconds and 4 git commands to generate.