lib: update and simplify the `bt_object` API
[babeltrace.git] / include / babeltrace / object-internal.h
... / ...
CommitLineData
1#ifndef BABELTRACE_OBJECT_INTERNAL_H
2#define BABELTRACE_OBJECT_INTERNAL_H
3
4/*
5 * Babeltrace - Base object
6 *
7 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 *
9 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
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
30#include <babeltrace/babeltrace-internal.h>
31#include <babeltrace/assert-internal.h>
32#include <stdbool.h>
33
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.
51 */
52struct bt_object {
53 /*
54 * True if this object is shared, that is, it has a reference
55 * count.
56 */
57 bool is_shared;
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;
91};
92
93static inline
94unsigned long long bt_object_get_ref_count(struct bt_object *obj)
95{
96 BT_ASSERT(obj);
97 BT_ASSERT(obj->is_shared);
98 return obj->ref_count;
99}
100
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;
107}
108
109static inline
110struct bt_object *bt_object_get_parent(struct bt_object *obj)
111{
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);
126
127#ifdef BT_LOGV
128 BT_LOGV("Setting object's parent: addr=%p, parent-addr=%p",
129 child, parent);
130#endif
131
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;
148 }
149}
150
151static inline
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)
165{
166 if (obj->parent) {
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 */
172 struct bt_object *parent = obj->parent;
173
174#ifdef BT_LOGV
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);
179#endif
180
181 if (obj->parent_is_owner_listener_func) {
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 */
188 obj->parent_is_owner_listener_func(obj);
189 }
190
191 /* The release function will be invoked by the parent. */
192 bt_object_put_no_null_check(parent);
193 } else {
194 bt_object_try_spec_release(obj);
195 }
196}
197
198static inline
199void bt_object_init(struct bt_object *obj, bool is_shared,
200 bt_object_release_func release_func)
201{
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;
210}
211
212static inline
213void bt_object_init_shared(struct bt_object *obj,
214 bt_object_release_func release_func)
215{
216 bt_object_init(obj, true, release_func);
217}
218
219static inline
220void bt_object_init_unique(struct bt_object *obj)
221{
222 bt_object_init(obj, false, NULL);
223}
224
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}
234
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;
243}
244
245static inline
246void bt_object_inc_ref_count(struct bt_object *obj)
247{
248 BT_ASSERT(obj);
249 BT_ASSERT(obj->is_shared);
250 obj->ref_count++;
251 BT_ASSERT(obj->ref_count != 0);
252}
253
254static inline
255void bt_object_get_no_null_check(struct bt_object *obj)
256{
257 BT_ASSERT(obj);
258 BT_ASSERT(obj->is_shared);
259
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);
277}
278
279static inline
280void bt_object_put_no_null_check(struct bt_object *obj)
281{
282 BT_ASSERT(obj);
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 }
299}
300
301#endif /* BABELTRACE_OBJECT_INTERNAL_H */
This page took 0.023601 seconds and 4 git commands to generate.