lib: add internal object pool API and use it; adapt plugins/tests
[babeltrace.git] / lib / ref.c
index ac0580137a5ad959d25fbfa29b3a3dc0a7ffae19..e59187c6d706c0ef49d96bcd8a09d366b489e217 100644 (file)
--- a/lib/ref.c
+++ b/lib/ref.c
  * SOFTWARE.
  */
 
+#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>
 
@@ -31,14 +35,28 @@ void *bt_get(void *ptr)
 {
        struct bt_object *obj = ptr;
 
-       if (!obj) {
+       if (unlikely(!obj)) {
+               goto end;
+       }
+
+       BT_ASSERT_PRE(obj->is_shared, "Object is not shared: addr=%p", obj);
+
+       if (unlikely(!obj->ref_count.release)) {
                goto end;
        }
 
-       if (obj->parent && bt_object_get_ref_count(obj) == 0) {
+       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);
+
 end:
        return obj;
 }
@@ -47,9 +65,25 @@ void bt_put(void *ptr)
 {
        struct bt_object *obj = ptr;
 
-       if (!obj) {
+       if (unlikely(!obj)) {
                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, 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.02436 seconds and 4 git commands to generate.