Fix: bt_component_create is not hidden
[babeltrace.git] / lib / ref.c
index 97460e93522a84505637d3ba80d69fb85c416405..02d2c4e9bf914fe3467600d81ea4d5abd13058d9 100644 (file)
--- a/lib/ref.c
+++ b/lib/ref.c
@@ -24,6 +24,9 @@
  * SOFTWARE.
  */
 
+#define BT_LOG_TAG "REF"
+#include <babeltrace/lib-logging-internal.h>
+
 #include <babeltrace/ref-internal.h>
 #include <babeltrace/object-internal.h>
 
@@ -31,23 +34,51 @@ void *bt_get(void *ptr)
 {
        struct bt_object *obj = ptr;
 
-       if (!obj) {
+       if (unlikely(!obj)) {
+               goto end;
+       }
+
+       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;
 }
 
-void bt_put(void *obj)
+void bt_put(void *ptr)
 {
-       if (obj) {
-               struct bt_object *base = obj;
+       struct bt_object *obj = ptr;
 
-               bt_ref_put(&base->ref_count);
+       if (unlikely(!obj)) {
+               return;
        }
+
+       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.033957 seconds and 4 git commands to generate.