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