Move to kernel style SPDX license identifiers
[babeltrace.git] / src / lib / object.h
index b8cb33bfdf7c673450c604cf79832f2415f5aac0..c0180dce76cda3dc43ae9aea7b74a30fd814c6a4 100644 (file)
@@ -1,45 +1,17 @@
-#ifndef BABELTRACE_OBJECT_INTERNAL_H
-#define BABELTRACE_OBJECT_INTERNAL_H
-
 /*
+ * SPDX-License-Identifier: MIT
+ *
  * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
  * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
  */
 
+#ifndef BABELTRACE_OBJECT_INTERNAL_H
+#define BABELTRACE_OBJECT_INTERNAL_H
+
 #include "common/macros.h"
 #include "common/assert.h"
 #include <stdbool.h>
 
-/*
- * Some parts of the Babeltrace project use this internal library header
- * for internal reference counting. Until we make this header generic
- * for the whole project, make it possible to disable logging in this
- * file by defining `BT_OBJECT_DONT_LOG` because it's possible that the
- * BT_LOGV() statements here won't find the log level
- * (`BT_LOG_OUTPUT_LEVEL`).
- */
-#if defined(BT_LOGV) && !defined(BT_OBJECT_DONT_LOG)
-# define _BT_OBJECT_LOGGING_ENABLED
-#endif
-
 struct bt_object;
 
 typedef void (*bt_object_release_func)(struct bt_object *);
@@ -47,10 +19,10 @@ typedef void (*bt_object_parent_is_owner_listener_func)(
                struct bt_object *);
 
 static inline
-void bt_object_get_no_null_check(const void *obj);
+void bt_object_get_ref_no_null_check(const void *obj);
 
 static inline
-void bt_object_put_no_null_check(const void *obj);
+void bt_object_put_ref_no_null_check(const void *obj);
 
 /*
  * Babeltrace object base.
@@ -104,8 +76,8 @@ unsigned long long bt_object_get_ref_count(const struct bt_object *c_obj)
 {
        struct bt_object *obj = (void *) c_obj;
 
-       BT_ASSERT(obj);
-       BT_ASSERT(obj->is_shared);
+       BT_ASSERT_DBG(obj);
+       BT_ASSERT_DBG(obj->is_shared);
        return obj->ref_count;
 }
 
@@ -114,8 +86,8 @@ struct bt_object *bt_object_borrow_parent(const struct bt_object *c_obj)
 {
        struct bt_object *obj = (void *) c_obj;
 
-       BT_ASSERT(obj);
-       BT_ASSERT(obj->is_shared);
+       BT_ASSERT_DBG(obj);
+       BT_ASSERT_DBG(obj->is_shared);
        return obj->parent;
 }
 
@@ -126,7 +98,7 @@ struct bt_object *bt_object_get_parent(const struct bt_object *c_obj)
        struct bt_object *parent = bt_object_borrow_parent(obj);
 
        if (parent) {
-               bt_object_get_no_null_check(parent);
+               bt_object_get_ref_no_null_check(parent);
        }
 
        return parent;
@@ -135,11 +107,11 @@ struct bt_object *bt_object_get_parent(const struct bt_object *c_obj)
 static inline
 void bt_object_set_parent(struct bt_object *child, struct bt_object *parent)
 {
-       BT_ASSERT(child);
-       BT_ASSERT(child->is_shared);
+       BT_ASSERT_DBG(child);
+       BT_ASSERT_DBG(child->is_shared);
 
-#ifdef _BT_OBJECT_LOGGING_ENABLED
-       BT_LOGV("Setting object's parent: addr=%p, parent-addr=%p",
+#ifdef BT_LOGT
+       BT_LOGT("Setting object's parent: addr=%p, parent-addr=%p",
                child, parent);
 #endif
 
@@ -150,12 +122,12 @@ void bt_object_set_parent(struct bt_object *child, struct bt_object *parent)
         * object's reference count falls to zero.
         */
        if (parent) {
-               BT_ASSERT(!child->parent);
+               BT_ASSERT_DBG(!child->parent);
                child->parent = parent;
-               bt_object_get_no_null_check(parent);
+               bt_object_get_ref_no_null_check(parent);
        } else {
                if (child->parent) {
-                       bt_object_put_no_null_check(child->parent);
+                       bt_object_put_ref_no_null_check(child->parent);
                }
 
                child->parent = NULL;
@@ -165,9 +137,9 @@ void bt_object_set_parent(struct bt_object *child, struct bt_object *parent)
 static inline
 void bt_object_try_spec_release(struct bt_object *obj)
 {
-       BT_ASSERT(obj);
-       BT_ASSERT(obj->is_shared);
-       BT_ASSERT(obj->spec_release_func);
+       BT_ASSERT_DBG(obj);
+       BT_ASSERT_DBG(obj->is_shared);
+       BT_ASSERT_DBG(obj->spec_release_func);
 
        if (bt_object_get_ref_count(obj) == 0) {
                obj->spec_release_func(obj);
@@ -185,8 +157,8 @@ void bt_object_with_parent_release_func(struct bt_object *obj)
                 */
                struct bt_object *parent = obj->parent;
 
-#ifdef _BT_OBJECT_LOGGING_ENABLED
-               BT_LOGV("Releasing parented object: addr=%p, ref-count=%llu, "
+#ifdef BT_LOGT
+               BT_LOGT("Releasing parented object: addr=%p, ref-count=%llu, "
                        "parent-addr=%p, parent-ref-count=%llu",
                        obj, obj->ref_count,
                        parent, parent->ref_count);
@@ -203,7 +175,7 @@ void bt_object_with_parent_release_func(struct bt_object *obj)
                }
 
                /* The release function will be invoked by the parent. */
-               bt_object_put_no_null_check(parent);
+               bt_object_put_ref_no_null_check(parent);
        } else {
                bt_object_try_spec_release(obj);
        }
@@ -213,8 +185,8 @@ static inline
 void bt_object_init(struct bt_object *obj, bool is_shared,
                bt_object_release_func release_func)
 {
-       BT_ASSERT(obj);
-       BT_ASSERT(!is_shared || release_func);
+       BT_ASSERT_DBG(obj);
+       BT_ASSERT_DBG(!is_shared || release_func);
        obj->is_shared = is_shared;
        obj->release_func = release_func;
        obj->parent_is_owner_listener_func = NULL;
@@ -240,8 +212,8 @@ static inline
 void bt_object_init_shared_with_parent(struct bt_object *obj,
                bt_object_release_func spec_release_func)
 {
-       BT_ASSERT(obj);
-       BT_ASSERT(spec_release_func);
+       BT_ASSERT_DBG(obj);
+       BT_ASSERT_DBG(spec_release_func);
        bt_object_init_shared(obj, bt_object_with_parent_release_func);
        obj->spec_release_func = spec_release_func;
 }
@@ -250,9 +222,9 @@ static inline
 void bt_object_set_parent_is_owner_listener_func(struct bt_object *obj,
                bt_object_parent_is_owner_listener_func func)
 {
-       BT_ASSERT(obj);
-       BT_ASSERT(obj->is_shared);
-       BT_ASSERT(obj->spec_release_func);
+       BT_ASSERT_DBG(obj);
+       BT_ASSERT_DBG(obj->is_shared);
+       BT_ASSERT_DBG(obj->spec_release_func);
        ((struct bt_object *) obj)->parent_is_owner_listener_func = func;
 }
 
@@ -261,22 +233,22 @@ void bt_object_inc_ref_count(const struct bt_object *c_obj)
 {
        struct bt_object *obj = (void *) c_obj;
 
-       BT_ASSERT(obj);
-       BT_ASSERT(obj->is_shared);
+       BT_ASSERT_DBG(obj);
+       BT_ASSERT_DBG(obj->is_shared);
        obj->ref_count++;
-       BT_ASSERT(obj->ref_count != 0);
+       BT_ASSERT_DBG(obj->ref_count != 0);
 }
 
 static inline
-void bt_object_get_no_null_check_no_parent_check(const struct bt_object *c_obj)
+void bt_object_get_ref_no_null_check_no_parent_check(const struct bt_object *c_obj)
 {
        struct bt_object *obj = (void *) c_obj;
 
-       BT_ASSERT(obj);
-       BT_ASSERT(obj->is_shared);
+       BT_ASSERT_DBG(obj);
+       BT_ASSERT_DBG(obj->is_shared);
 
-#ifdef _BT_OBJECT_LOGGING_ENABLED
-       BT_LOGV("Incrementing object's reference count: %llu -> %llu: "
+#ifdef BT_LOGT
+       BT_LOGT("Incrementing object's reference count: %llu -> %llu: "
                "addr=%p, cur-count=%llu, new-count=%llu",
                obj->ref_count, obj->ref_count + 1,
                obj, obj->ref_count, obj->ref_count + 1);
@@ -286,24 +258,24 @@ void bt_object_get_no_null_check_no_parent_check(const struct bt_object *c_obj)
 }
 
 static inline
-void bt_object_get_no_null_check(const void *c_obj)
+void bt_object_get_ref_no_null_check(const void *c_obj)
 {
        struct bt_object *obj = (void *) c_obj;
 
-       BT_ASSERT(obj);
-       BT_ASSERT(obj->is_shared);
+       BT_ASSERT_DBG(obj);
+       BT_ASSERT_DBG(obj->is_shared);
 
        if (G_UNLIKELY(obj->parent && bt_object_get_ref_count(obj) == 0)) {
-#ifdef _BT_OBJECT_LOGGING_ENABLED
-               BT_LOGV("Incrementing object's parent's reference count: "
+#ifdef BT_LOGT
+               BT_LOGT("Incrementing object's parent's reference count: "
                        "addr=%p, parent-addr=%p", obj, obj->parent);
 #endif
 
-               bt_object_get_no_null_check(obj->parent);
+               bt_object_get_ref_no_null_check(obj->parent);
        }
 
-#ifdef _BT_OBJECT_LOGGING_ENABLED
-       BT_LOGV("Incrementing object's reference count: %llu -> %llu: "
+#ifdef BT_LOGT
+       BT_LOGT("Incrementing object's reference count: %llu -> %llu: "
                "addr=%p, cur-count=%llu, new-count=%llu",
                obj->ref_count, obj->ref_count + 1,
                obj, obj->ref_count, obj->ref_count + 1);
@@ -313,16 +285,16 @@ void bt_object_get_no_null_check(const void *c_obj)
 }
 
 static inline
-void bt_object_put_no_null_check(const void *c_obj)
+void bt_object_put_ref_no_null_check(const void *c_obj)
 {
        struct bt_object *obj = (void *) c_obj;
 
-       BT_ASSERT(obj);
-       BT_ASSERT(obj->is_shared);
-       BT_ASSERT(obj->ref_count > 0);
+       BT_ASSERT_DBG(obj);
+       BT_ASSERT_DBG(obj->is_shared);
+       BT_ASSERT_DBG(obj->ref_count > 0);
 
-#ifdef _BT_OBJECT_LOGGING_ENABLED
-       BT_LOGV("Decrementing object's reference count: %llu -> %llu: "
+#ifdef BT_LOGT
+       BT_LOGT("Decrementing object's reference count: %llu -> %llu: "
                "addr=%p, cur-count=%llu, new-count=%llu",
                obj->ref_count, obj->ref_count - 1,
                obj, obj->ref_count, obj->ref_count - 1);
@@ -331,7 +303,7 @@ void bt_object_put_no_null_check(const void *c_obj)
        obj->ref_count--;
 
        if (obj->ref_count == 0) {
-               BT_ASSERT(obj->release_func);
+               BT_ASSERT_DBG(obj->release_func);
                obj->release_func(obj);
        }
 }
@@ -345,11 +317,11 @@ void bt_object_get_ref(const void *ptr)
                return;
        }
 
-#ifdef BT_ASSERT_PRE
-       BT_ASSERT_PRE(obj->is_shared, "Object is not shared: %!+O", obj);
+#ifdef BT_ASSERT_PRE_DEV
+       BT_ASSERT_PRE_DEV(obj->is_shared, "Object is not shared: %!+O", obj);
 #endif
 
-       bt_object_get_no_null_check(obj);
+       bt_object_get_ref_no_null_check(obj);
 }
 
 static inline
@@ -361,13 +333,13 @@ void bt_object_put_ref(const void *ptr)
                return;
        }
 
-#ifdef BT_ASSERT_PRE
-       BT_ASSERT_PRE(obj->is_shared, "Object is not shared: %!+O", obj);
-       BT_ASSERT_PRE(bt_object_get_ref_count(obj) > 0,
+#ifdef BT_ASSERT_PRE_DEV
+       BT_ASSERT_PRE_DEV(obj->is_shared, "Object is not shared: %!+O", obj);
+       BT_ASSERT_PRE_DEV(bt_object_get_ref_count(obj) > 0,
                "Decrementing a reference count set to 0: %!+O", ptr);
 #endif
 
-       bt_object_put_no_null_check(obj);
+       bt_object_put_ref_no_null_check(obj);
 }
 
 #define BT_OBJECT_PUT_REF_AND_RESET(_var)      \
This page took 0.028052 seconds and 4 git commands to generate.