Rename VERBOSE log level to TRACE
[babeltrace.git] / src / lib / object.h
1 #ifndef BABELTRACE_OBJECT_INTERNAL_H
2 #define BABELTRACE_OBJECT_INTERNAL_H
3
4 /*
5 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
6 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
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
27 #include "common/macros.h"
28 #include "common/assert.h"
29 #include <stdbool.h>
30
31 /*
32 * Some parts of the Babeltrace project use this internal library header
33 * for internal reference counting. Until we make this header generic
34 * for the whole project, make it possible to disable logging in this
35 * file by defining `BT_OBJECT_DONT_LOG` because it's possible that the
36 * BT_LOGT() statements here won't find the log level
37 * (`BT_LOG_OUTPUT_LEVEL`).
38 */
39 #if defined(BT_LOGT) && !defined(BT_OBJECT_DONT_LOG)
40 # define _BT_OBJECT_LOGGING_ENABLED
41 #endif
42
43 struct bt_object;
44
45 typedef void (*bt_object_release_func)(struct bt_object *);
46 typedef void (*bt_object_parent_is_owner_listener_func)(
47 struct bt_object *);
48
49 static inline
50 void bt_object_get_no_null_check(const void *obj);
51
52 static inline
53 void bt_object_put_no_null_check(const void *obj);
54
55 /*
56 * Babeltrace object base.
57 *
58 * All objects publicly exposed by Babeltrace APIs must contain this
59 * object as their first member.
60 */
61 struct bt_object {
62 /*
63 * True if this object is shared, that is, it has a reference
64 * count.
65 */
66 bool is_shared;
67
68 /*
69 * Current reference count.
70 */
71 unsigned long long ref_count;
72
73 /*
74 * Release function called when the object's reference count
75 * falls to zero. For an object with a parent, this function is
76 * bt_object_with_parent_release_func(), which calls
77 * `spec_release_func` below if there's no current parent.
78 */
79 bt_object_release_func release_func;
80
81 /*
82 * Specific release function called by
83 * bt_object_with_parent_release_func() or directly by a
84 * parent object.
85 */
86 bt_object_release_func spec_release_func;
87
88 /*
89 * Optional callback for an object with a parent, called by
90 * bt_object_with_parent_release_func() to indicate to the
91 * object that its parent is its owner.
92 */
93 bt_object_parent_is_owner_listener_func
94 parent_is_owner_listener_func;
95
96 /*
97 * Optional parent object.
98 */
99 struct bt_object *parent;
100 };
101
102 static inline
103 unsigned long long bt_object_get_ref_count(const struct bt_object *c_obj)
104 {
105 struct bt_object *obj = (void *) c_obj;
106
107 BT_ASSERT(obj);
108 BT_ASSERT(obj->is_shared);
109 return obj->ref_count;
110 }
111
112 static inline
113 struct bt_object *bt_object_borrow_parent(const struct bt_object *c_obj)
114 {
115 struct bt_object *obj = (void *) c_obj;
116
117 BT_ASSERT(obj);
118 BT_ASSERT(obj->is_shared);
119 return obj->parent;
120 }
121
122 static inline
123 struct bt_object *bt_object_get_parent(const struct bt_object *c_obj)
124 {
125 struct bt_object *obj = (void *) c_obj;
126 struct bt_object *parent = bt_object_borrow_parent(obj);
127
128 if (parent) {
129 bt_object_get_no_null_check(parent);
130 }
131
132 return parent;
133 }
134
135 static inline
136 void bt_object_set_parent(struct bt_object *child, struct bt_object *parent)
137 {
138 BT_ASSERT(child);
139 BT_ASSERT(child->is_shared);
140
141 #ifdef _BT_OBJECT_LOGGING_ENABLED
142 BT_LOGT("Setting object's parent: addr=%p, parent-addr=%p",
143 child, parent);
144 #endif
145
146 /*
147 * It is assumed that a "child" having a parent is publicly
148 * reachable. Therefore, a reference to its parent must be
149 * taken. The reference to the parent will be released once the
150 * object's reference count falls to zero.
151 */
152 if (parent) {
153 BT_ASSERT(!child->parent);
154 child->parent = parent;
155 bt_object_get_no_null_check(parent);
156 } else {
157 if (child->parent) {
158 bt_object_put_no_null_check(child->parent);
159 }
160
161 child->parent = NULL;
162 }
163 }
164
165 static inline
166 void bt_object_try_spec_release(struct bt_object *obj)
167 {
168 BT_ASSERT(obj);
169 BT_ASSERT(obj->is_shared);
170 BT_ASSERT(obj->spec_release_func);
171
172 if (bt_object_get_ref_count(obj) == 0) {
173 obj->spec_release_func(obj);
174 }
175 }
176
177 static inline
178 void bt_object_with_parent_release_func(struct bt_object *obj)
179 {
180 if (obj->parent) {
181 /*
182 * Keep our own copy of the parent address because `obj`
183 * could be destroyed in
184 * obj->parent_is_owner_listener_func().
185 */
186 struct bt_object *parent = obj->parent;
187
188 #ifdef _BT_OBJECT_LOGGING_ENABLED
189 BT_LOGT("Releasing parented object: addr=%p, ref-count=%llu, "
190 "parent-addr=%p, parent-ref-count=%llu",
191 obj, obj->ref_count,
192 parent, parent->ref_count);
193 #endif
194
195 if (obj->parent_is_owner_listener_func) {
196 /*
197 * Object has a chance to destroy itself here
198 * under certain conditions and notify its
199 * parent. At this point the parent is
200 * guaranteed to exist because it's not put yet.
201 */
202 obj->parent_is_owner_listener_func(obj);
203 }
204
205 /* The release function will be invoked by the parent. */
206 bt_object_put_no_null_check(parent);
207 } else {
208 bt_object_try_spec_release(obj);
209 }
210 }
211
212 static inline
213 void bt_object_init(struct bt_object *obj, bool is_shared,
214 bt_object_release_func release_func)
215 {
216 BT_ASSERT(obj);
217 BT_ASSERT(!is_shared || release_func);
218 obj->is_shared = is_shared;
219 obj->release_func = release_func;
220 obj->parent_is_owner_listener_func = NULL;
221 obj->spec_release_func = NULL;
222 obj->parent = NULL;
223 obj->ref_count = 1;
224 }
225
226 static inline
227 void bt_object_init_shared(struct bt_object *obj,
228 bt_object_release_func release_func)
229 {
230 bt_object_init(obj, true, release_func);
231 }
232
233 static inline
234 void bt_object_init_unique(struct bt_object *obj)
235 {
236 bt_object_init(obj, false, NULL);
237 }
238
239 static inline
240 void bt_object_init_shared_with_parent(struct bt_object *obj,
241 bt_object_release_func spec_release_func)
242 {
243 BT_ASSERT(obj);
244 BT_ASSERT(spec_release_func);
245 bt_object_init_shared(obj, bt_object_with_parent_release_func);
246 obj->spec_release_func = spec_release_func;
247 }
248
249 static inline
250 void bt_object_set_parent_is_owner_listener_func(struct bt_object *obj,
251 bt_object_parent_is_owner_listener_func func)
252 {
253 BT_ASSERT(obj);
254 BT_ASSERT(obj->is_shared);
255 BT_ASSERT(obj->spec_release_func);
256 ((struct bt_object *) obj)->parent_is_owner_listener_func = func;
257 }
258
259 static inline
260 void bt_object_inc_ref_count(const struct bt_object *c_obj)
261 {
262 struct bt_object *obj = (void *) c_obj;
263
264 BT_ASSERT(obj);
265 BT_ASSERT(obj->is_shared);
266 obj->ref_count++;
267 BT_ASSERT(obj->ref_count != 0);
268 }
269
270 static inline
271 void bt_object_get_no_null_check_no_parent_check(const struct bt_object *c_obj)
272 {
273 struct bt_object *obj = (void *) c_obj;
274
275 BT_ASSERT(obj);
276 BT_ASSERT(obj->is_shared);
277
278 #ifdef _BT_OBJECT_LOGGING_ENABLED
279 BT_LOGT("Incrementing object's reference count: %llu -> %llu: "
280 "addr=%p, cur-count=%llu, new-count=%llu",
281 obj->ref_count, obj->ref_count + 1,
282 obj, obj->ref_count, obj->ref_count + 1);
283 #endif
284
285 bt_object_inc_ref_count(obj);
286 }
287
288 static inline
289 void bt_object_get_no_null_check(const void *c_obj)
290 {
291 struct bt_object *obj = (void *) c_obj;
292
293 BT_ASSERT(obj);
294 BT_ASSERT(obj->is_shared);
295
296 if (G_UNLIKELY(obj->parent && bt_object_get_ref_count(obj) == 0)) {
297 #ifdef _BT_OBJECT_LOGGING_ENABLED
298 BT_LOGT("Incrementing object's parent's reference count: "
299 "addr=%p, parent-addr=%p", obj, obj->parent);
300 #endif
301
302 bt_object_get_no_null_check(obj->parent);
303 }
304
305 #ifdef _BT_OBJECT_LOGGING_ENABLED
306 BT_LOGT("Incrementing object's reference count: %llu -> %llu: "
307 "addr=%p, cur-count=%llu, new-count=%llu",
308 obj->ref_count, obj->ref_count + 1,
309 obj, obj->ref_count, obj->ref_count + 1);
310 #endif
311
312 bt_object_inc_ref_count(obj);
313 }
314
315 static inline
316 void bt_object_put_no_null_check(const void *c_obj)
317 {
318 struct bt_object *obj = (void *) c_obj;
319
320 BT_ASSERT(obj);
321 BT_ASSERT(obj->is_shared);
322 BT_ASSERT(obj->ref_count > 0);
323
324 #ifdef _BT_OBJECT_LOGGING_ENABLED
325 BT_LOGT("Decrementing object's reference count: %llu -> %llu: "
326 "addr=%p, cur-count=%llu, new-count=%llu",
327 obj->ref_count, obj->ref_count - 1,
328 obj, obj->ref_count, obj->ref_count - 1);
329 #endif
330
331 obj->ref_count--;
332
333 if (obj->ref_count == 0) {
334 BT_ASSERT(obj->release_func);
335 obj->release_func(obj);
336 }
337 }
338
339 static inline
340 void bt_object_get_ref(const void *ptr)
341 {
342 struct bt_object *obj = (void *) ptr;
343
344 if (G_UNLIKELY(!obj)) {
345 return;
346 }
347
348 #ifdef BT_ASSERT_PRE
349 BT_ASSERT_PRE(obj->is_shared, "Object is not shared: %!+O", obj);
350 #endif
351
352 bt_object_get_no_null_check(obj);
353 }
354
355 static inline
356 void bt_object_put_ref(const void *ptr)
357 {
358 struct bt_object *obj = (void *) ptr;
359
360 if (G_UNLIKELY(!obj)) {
361 return;
362 }
363
364 #ifdef BT_ASSERT_PRE
365 BT_ASSERT_PRE(obj->is_shared, "Object is not shared: %!+O", obj);
366 BT_ASSERT_PRE(bt_object_get_ref_count(obj) > 0,
367 "Decrementing a reference count set to 0: %!+O", ptr);
368 #endif
369
370 bt_object_put_no_null_check(obj);
371 }
372
373 #define BT_OBJECT_PUT_REF_AND_RESET(_var) \
374 do { \
375 bt_object_put_ref(_var); \
376 (_var) = NULL; \
377 } while (0)
378
379 #define BT_OBJECT_MOVE_REF(_var_dst, _var_src) \
380 do { \
381 bt_object_put_ref(_var_dst); \
382 (_var_dst) = (_var_src); \
383 (_var_src) = NULL; \
384 } while (0)
385
386 #endif /* BABELTRACE_OBJECT_INTERNAL_H */
This page took 0.037452 seconds and 4 git commands to generate.