X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=lib%2Fref.c;h=02d2c4e9bf914fe3467600d81ea4d5abd13058d9;hb=7401e2d1ffcf2e41031b3a7f3e3ba9ff158876c9;hp=e642e266de87543c24f9f3819e6c8b7a577c1b46;hpb=b82cd9f0ac23ce23386d4cf3d6b46aaf09293770;p=babeltrace.git diff --git a/lib/ref.c b/lib/ref.c index e642e266..02d2c4e9 100644 --- a/lib/ref.c +++ b/lib/ref.c @@ -24,24 +24,61 @@ * SOFTWARE. */ +#define BT_LOG_TAG "REF" +#include + #include #include -void *bt_get(void *obj) +void *bt_get(void *ptr) { - if (obj) { - struct bt_object *base = obj; + struct bt_object *obj = ptr; + + if (unlikely(!obj)) { + goto end; + } - bt_ref_get(&base->ref_count); + 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); + +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); }