lib: add internal object pool API and use it; adapt plugins/tests
[babeltrace.git] / lib / ref.c
index d39a24d0c1bc5121f4082b5f3eb7ed039a4faae1..e59187c6d706c0ef49d96bcd8a09d366b489e217 100644 (file)
--- a/lib/ref.c
+++ b/lib/ref.c
@@ -27,6 +27,7 @@
 #define BT_LOG_TAG "REF"
 #include <babeltrace/lib-logging-internal.h>
 
+#include <babeltrace/assert-pre-internal.h>
 #include <babeltrace/ref-internal.h>
 #include <babeltrace/object-internal.h>
 
@@ -38,15 +39,24 @@ void *bt_get(void *ptr)
                goto end;
        }
 
+       BT_ASSERT_PRE(obj->is_shared, "Object is not shared: addr=%p", obj);
+
+       if (unlikely(!obj->ref_count.release)) {
+               goto end;
+       }
+
        if (unlikely(obj->parent && bt_object_get_ref_count(obj) == 0)) {
                BT_LOGV("Incrementing object's parent's reference count: "
                        "addr=%p, parent-addr=%p", ptr, obj->parent);
                bt_get(obj->parent);
        }
+       BT_LOGV("Incrementing object's reference count: %lu -> %lu: "
+               "addr=%p, cur-count=%lu, new-count=%lu",
+               obj->ref_count.count, obj->ref_count.count + 1,
+               ptr,
+               obj->ref_count.count, obj->ref_count.count + 1);
        bt_ref_get(&obj->ref_count);
-       BT_LOGV("Incremented object's reference count: %lu -> %lu: "
-               "addr=%p, new-count=%lu", obj->ref_count.count - 1,
-               obj->ref_count.count, ptr, obj->ref_count.count);
+
 end:
        return obj;
 }
@@ -59,8 +69,21 @@ void bt_put(void *ptr)
                return;
        }
 
+       BT_ASSERT_PRE(obj->is_shared, "Object is not shared: addr=%p", obj);
+
+       if (unlikely(!obj->ref_count.release)) {
+               return;
+       }
+
+       if (BT_LOG_ON_WARN && unlikely(bt_object_get_ref_count(obj) == 0)) {
+               BT_LOGW("Decrementing a reference count set to 0: addr=%p",
+                       ptr);
+       }
+
        BT_LOGV("Decrementing object's reference count: %lu -> %lu: "
-               "addr=%p, cur-count=%lu", obj->ref_count.count,
-               obj->ref_count.count - 1, ptr, obj->ref_count.count);
+               "addr=%p, cur-count=%lu, new-count=%lu",
+               obj->ref_count.count, obj->ref_count.count - 1,
+               ptr,
+               obj->ref_count.count, obj->ref_count.count - 1);
        bt_ref_put(&obj->ref_count);
 }
This page took 0.024394 seconds and 4 git commands to generate.