ir: consolidate reference counting functions
[babeltrace.git] / lib / objects.c
index 4f4670d18c3ea65d42b5a7585e5bf0fd27eba0d3..8a0bc650c53c7cef03d22563fdaffd8be0cbe10d 100644 (file)
 #include <string.h>
 #include <assert.h>
 #include <string.h>
-#include <babeltrace/ctf-writer/ref-internal.h>
+#include <babeltrace/ref-internal.h>
 #include <babeltrace/compiler.h>
-#include <glib.h>
 #include <babeltrace/objects.h>
+#include <glib.h>
 
 #define BT_OBJECT_FROM_CONCRETE(_concrete) ((struct bt_object *) (_concrete))
 #define BT_OBJECT_TO_BOOL(_base) ((struct bt_object_bool *) (_base))
@@ -44,7 +44,7 @@
 
 struct bt_object {
        enum bt_object_type type;
-       struct bt_ctf_ref ref_count;
+       struct bt_ref ref_count;
        bool is_frozen;
 };
 
@@ -86,6 +86,9 @@ struct bt_object_map {
        GHashTable *ght;
 };
 
+static
+void bt_object_destroy(struct bt_ref *ref_count);
+
 static
 void bt_object_string_destroy(struct bt_object *object)
 {
@@ -160,7 +163,7 @@ struct bt_object *bt_object_string_copy(const struct bt_object *string_obj)
 static
 struct bt_object *bt_object_array_copy(const struct bt_object *array_obj)
 {
-       int x;
+       int i;
        int ret;
        struct bt_object *copy_obj;
        struct bt_object_array *typed_array_obj;
@@ -172,10 +175,10 @@ struct bt_object *bt_object_array_copy(const struct bt_object *array_obj)
                goto end;
        }
 
-       for (x = 0; x < typed_array_obj->garray->len; ++x) {
+       for (i = 0; i < typed_array_obj->garray->len; ++i) {
                struct bt_object *element_obj_copy;
                struct bt_object *element_obj =
-                       bt_object_array_get(array_obj, x);
+                       bt_object_array_get(array_obj, i);
 
                if (!element_obj) {
                        BT_OBJECT_PUT(copy_obj);
@@ -304,7 +307,7 @@ static
 bool bt_object_array_compare(const struct bt_object *object_a,
                const struct bt_object *object_b)
 {
-       int x;
+       int i;
        bool ret = true;
        const struct bt_object_array *array_obj_a =
                BT_OBJECT_TO_ARRAY(object_a);
@@ -314,12 +317,12 @@ bool bt_object_array_compare(const struct bt_object *object_a,
                goto end;
        }
 
-       for (x = 0; x < array_obj_a->garray->len; ++x) {
+       for (i = 0; i < array_obj_a->garray->len; ++i) {
                struct bt_object *element_obj_a;
                struct bt_object *element_obj_b;
 
-               element_obj_a = bt_object_array_get(object_a, x);
-               element_obj_b = bt_object_array_get(object_b, x);
+               element_obj_a = bt_object_array_get(object_a, i);
+               element_obj_b = bt_object_array_get(object_b, i);
 
                if (!bt_object_compare(element_obj_a, element_obj_b)) {
                        BT_OBJECT_PUT(element_obj_a);
@@ -394,13 +397,13 @@ void bt_object_generic_freeze(struct bt_object *object)
 
 void bt_object_array_freeze(struct bt_object *object)
 {
-       int x;
+       int i;
        struct bt_object_array *typed_array_obj =
                BT_OBJECT_TO_ARRAY(object);
 
-       for (x = 0; x < typed_array_obj->garray->len; ++x) {
+       for (i = 0; i < typed_array_obj->garray->len; ++i) {
                struct bt_object *element_obj =
-                       g_ptr_array_index(typed_array_obj->garray, x);
+                       g_ptr_array_index(typed_array_obj->garray, i);
 
                bt_object_freeze(element_obj);
        }
@@ -435,7 +438,7 @@ void (* const freeze_funcs[])(struct bt_object *) = {
 };
 
 static
-void bt_object_destroy(struct bt_ctf_ref *ref_count)
+void bt_object_destroy(struct bt_ref *ref_count)
 {
        struct bt_object *object;
 
@@ -455,26 +458,18 @@ void bt_object_destroy(struct bt_ctf_ref *ref_count)
 
 void bt_object_get(struct bt_object *object)
 {
-       if (!object) {
-               goto skip;
+       if (object && !bt_object_is_null(object)) {
+               bt_ref_get(&object->ref_count);
        }
 
-       bt_ctf_ref_get(&object->ref_count);
-
-skip:
        return;
 }
 
 void bt_object_put(struct bt_object *object)
 {
-       if (!object) {
-               goto skip;
+       if (object && !bt_object_is_null(object)) {
+               bt_ref_put(&object->ref_count);
        }
-
-       bt_ctf_ref_put(&object->ref_count, bt_object_destroy);
-
-skip:
-       return;
 }
 
 enum bt_object_status bt_object_freeze(struct bt_object *object)
@@ -513,7 +508,7 @@ struct bt_object bt_object_create_base(enum bt_object_type type)
 
        base.type = type;
        base.is_frozen = false;
-       bt_ctf_ref_init(&base.ref_count);
+       bt_ref_init(&base.ref_count, bt_object_destroy);
 
        return base;
 }
This page took 0.040208 seconds and 4 git commands to generate.