lib: make BT_ASSERT_{PRE,POST}() always on; add BT_ASSERT_{PRE,POST}_DEV()
[babeltrace.git] / src / lib / object.h
index c3afd73f744459f6ec4beb5e2dd67b14c6a6741d..75c13e1571e00e96d6056ca870329518f3d8aabc 100644 (file)
  * SOFTWARE.
  */
 
-#include "common/babeltrace.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_LOGT() statements here won't find the log level
+ * (`BT_LOG_OUTPUT_LEVEL`).
+ */
+#if defined(BT_LOGT) && !defined(BT_OBJECT_DONT_LOG)
+# define _BT_OBJECT_LOGGING_ENABLED
+#endif
+
 struct bt_object;
 
 typedef void (*bt_object_release_func)(struct bt_object *);
@@ -126,8 +138,8 @@ void bt_object_set_parent(struct bt_object *child, struct bt_object *parent)
        BT_ASSERT(child);
        BT_ASSERT(child->is_shared);
 
-#ifdef BT_LOGV
-       BT_LOGV("Setting object's parent: addr=%p, parent-addr=%p",
+#ifdef _BT_OBJECT_LOGGING_ENABLED
+       BT_LOGT("Setting object's parent: addr=%p, parent-addr=%p",
                child, parent);
 #endif
 
@@ -173,8 +185,8 @@ void bt_object_with_parent_release_func(struct bt_object *obj)
                 */
                struct bt_object *parent = obj->parent;
 
-#ifdef BT_LOGV
-               BT_LOGV("Releasing parented object: addr=%p, ref-count=%llu, "
+#ifdef _BT_OBJECT_LOGGING_ENABLED
+               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);
@@ -263,8 +275,8 @@ void bt_object_get_no_null_check_no_parent_check(const struct bt_object *c_obj)
        BT_ASSERT(obj);
        BT_ASSERT(obj->is_shared);
 
-#ifdef BT_LOGV
-       BT_LOGV("Incrementing object's reference count: %llu -> %llu: "
+#ifdef _BT_OBJECT_LOGGING_ENABLED
+       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);
@@ -281,17 +293,17 @@ void bt_object_get_no_null_check(const void *c_obj)
        BT_ASSERT(obj);
        BT_ASSERT(obj->is_shared);
 
-       if (unlikely(obj->parent && bt_object_get_ref_count(obj) == 0)) {
-#ifdef BT_LOGV
-               BT_LOGV("Incrementing object's parent's reference count: "
+       if (G_UNLIKELY(obj->parent && bt_object_get_ref_count(obj) == 0)) {
+#ifdef _BT_OBJECT_LOGGING_ENABLED
+               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);
        }
 
-#ifdef BT_LOGV
-       BT_LOGV("Incrementing object's reference count: %llu -> %llu: "
+#ifdef _BT_OBJECT_LOGGING_ENABLED
+       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);
@@ -309,8 +321,8 @@ void bt_object_put_no_null_check(const void *c_obj)
        BT_ASSERT(obj->is_shared);
        BT_ASSERT(obj->ref_count > 0);
 
-#ifdef BT_LOGV
-       BT_LOGV("Decrementing object's reference count: %llu -> %llu: "
+#ifdef _BT_OBJECT_LOGGING_ENABLED
+       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);
@@ -329,12 +341,12 @@ void bt_object_get_ref(const void *ptr)
 {
        struct bt_object *obj = (void *) ptr;
 
-       if (unlikely(!obj)) {
+       if (G_UNLIKELY(!obj)) {
                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);
@@ -345,13 +357,13 @@ void bt_object_put_ref(const void *ptr)
 {
        struct bt_object *obj = (void *) ptr;
 
-       if (unlikely(!obj)) {
+       if (G_UNLIKELY(!obj)) {
                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
 
This page took 0.02488 seconds and 4 git commands to generate.