ir: consolidate reference counting functions
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Thu, 16 Jul 2015 17:04:50 +0000 (13:04 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Fri, 17 Jul 2015 16:33:58 +0000 (12:33 -0400)
This patch adds the bt_ctf_get() and bt_ctf_put() functions
which resp. get and put references of any CTF IR object.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
30 files changed:
formats/ctf/ir/clock.c
formats/ctf/ir/event-fields.c
formats/ctf/ir/event-types.c
formats/ctf/ir/event.c
formats/ctf/ir/stream-class.c
formats/ctf/ir/stream.c
formats/ctf/ir/trace.c
formats/ctf/writer/writer.c
include/Makefile.am
include/babeltrace/ctf-ir/clock-internal.h
include/babeltrace/ctf-ir/clock.h
include/babeltrace/ctf-ir/common-internal.h [new file with mode: 0644]
include/babeltrace/ctf-ir/event-fields-internal.h
include/babeltrace/ctf-ir/event-fields.h
include/babeltrace/ctf-ir/event-internal.h
include/babeltrace/ctf-ir/event-types-internal.h
include/babeltrace/ctf-ir/event-types.h
include/babeltrace/ctf-ir/event.h
include/babeltrace/ctf-ir/ref.h [new file with mode: 0644]
include/babeltrace/ctf-ir/stream-class-internal.h
include/babeltrace/ctf-ir/stream-class.h
include/babeltrace/ctf-ir/stream-internal.h
include/babeltrace/ctf-ir/stream.h
include/babeltrace/ctf-ir/trace-internal.h
include/babeltrace/ctf-ir/trace.h
include/babeltrace/ctf-writer/ref-internal.h [deleted file]
include/babeltrace/ctf-writer/writer-internal.h
include/babeltrace/ctf-writer/writer.h
include/babeltrace/ref-internal.h [new file with mode: 0644]
lib/objects.c

index d1f435d50ae567ed6d879b32ce8647ca962bb2ac..22a5d7475e30083b90dd9ae741f7718a99cbe4b4 100644 (file)
 
 #include <babeltrace/ctf-ir/clock-internal.h>
 #include <babeltrace/ctf-ir/utils.h>
+#include <babeltrace/ctf-ir/common-internal.h>
+#include <babeltrace/ctf-ir/ref.h>
 #include <babeltrace/ctf-writer/writer-internal.h>
 #include <babeltrace/compiler.h>
 #include <inttypes.h>
 
 static
-void bt_ctf_clock_destroy(struct bt_ctf_ref *ref);
+void bt_ctf_clock_destroy(struct bt_ref *ref);
 
 BT_HIDDEN
 struct bt_ctf_clock *_bt_ctf_clock_create(void)
@@ -47,7 +49,7 @@ struct bt_ctf_clock *_bt_ctf_clock_create(void)
 
        clock->precision = 1;
        clock->frequency = 1000000000;
-       bt_ctf_ref_init(&clock->ref_count);
+       bt_ctf_base_init(clock, bt_ctf_clock_destroy);
 end:
        return clock;
 }
@@ -100,7 +102,7 @@ struct bt_ctf_clock *bt_ctf_clock_create(const char *name)
        clock->uuid_set = 1;
        return clock;
 error_destroy:
-       bt_ctf_clock_destroy(&clock->ref_count);
+       bt_ctf_clock_destroy(&clock->base.ref_count);
 error:
        return NULL;
 }
@@ -345,20 +347,12 @@ end:
 
 void bt_ctf_clock_get(struct bt_ctf_clock *clock)
 {
-       if (!clock) {
-               return;
-       }
-
-       bt_ctf_ref_get(&clock->ref_count);
+       bt_ctf_get(clock);
 }
 
 void bt_ctf_clock_put(struct bt_ctf_clock *clock)
 {
-       if (!clock) {
-               return;
-       }
-
-       bt_ctf_ref_put(&clock->ref_count, bt_ctf_clock_destroy);
+       bt_ctf_put(clock);
 }
 
 BT_HIDDEN
@@ -410,15 +404,17 @@ void bt_ctf_clock_serialize(struct bt_ctf_clock *clock,
 }
 
 static
-void bt_ctf_clock_destroy(struct bt_ctf_ref *ref)
+void bt_ctf_clock_destroy(struct bt_ref *ref)
 {
        struct bt_ctf_clock *clock;
+       struct bt_ctf_base *base;
 
        if (!ref) {
                return;
        }
 
-       clock = container_of(ref, struct bt_ctf_clock, ref_count);
+       base = container_of(ref, struct bt_ctf_base, ref_count);
+       clock = container_of(base, struct bt_ctf_clock, base);
        if (clock->name) {
                g_string_free(clock->name, TRUE);
        }
index 2ca21f10059b05bc1a459a79ef7334d8749a8afc..2fb783c31ccbd51dacd0ff027f365501c5a3acf2 100644 (file)
@@ -29,6 +29,8 @@
 #include <babeltrace/ctf-writer/event-fields.h>
 #include <babeltrace/ctf-ir/event-fields-internal.h>
 #include <babeltrace/ctf-ir/event-types-internal.h>
+#include <babeltrace/ctf-ir/common-internal.h>
+#include <babeltrace/ctf-ir/ref.h>
 #include <babeltrace/compiler.h>
 
 #define PACKET_LEN_INCREMENT   (getpagesize() * 8 * CHAR_BIT)
@@ -57,7 +59,7 @@ static
 struct bt_ctf_field *bt_ctf_field_string_create(struct bt_ctf_field_type *);
 
 static
-void bt_ctf_field_destroy(struct bt_ctf_ref *);
+void bt_ctf_field_destroy(struct bt_ref *);
 static
 void bt_ctf_field_integer_destroy(struct bt_ctf_field *);
 static
@@ -250,7 +252,7 @@ struct bt_ctf_field *bt_ctf_field_create(struct bt_ctf_field_type *type)
        /* The type's declaration can't change after this point */
        bt_ctf_field_type_freeze(type);
        bt_ctf_field_type_get(type);
-       bt_ctf_ref_init(&field->ref_count);
+       bt_ctf_base_init(field, bt_ctf_field_destroy);
        field->type = type;
 error:
        return field;
@@ -258,16 +260,12 @@ error:
 
 void bt_ctf_field_get(struct bt_ctf_field *field)
 {
-       if (field) {
-               bt_ctf_ref_get(&field->ref_count);
-       }
+       bt_ctf_get(field);
 }
 
 void bt_ctf_field_put(struct bt_ctf_field *field)
 {
-       if (field) {
-               bt_ctf_ref_put(&field->ref_count, bt_ctf_field_destroy);
-       }
+       bt_ctf_put(field);
 }
 
 struct bt_ctf_field_type *bt_ctf_field_get_type(struct bt_ctf_field *field)
@@ -1275,9 +1273,10 @@ struct bt_ctf_field *bt_ctf_field_string_create(struct bt_ctf_field_type *type)
 }
 
 static
-void bt_ctf_field_destroy(struct bt_ctf_ref *ref)
+void bt_ctf_field_destroy(struct bt_ref *ref)
 {
        struct bt_ctf_field *field;
+       struct bt_ctf_base *base;
        struct bt_ctf_field_type *type;
        enum ctf_type_id type_id;
 
@@ -1285,7 +1284,8 @@ void bt_ctf_field_destroy(struct bt_ctf_ref *ref)
                return;
        }
 
-       field = container_of(ref, struct bt_ctf_field, ref_count);
+       base = container_of(ref, struct bt_ctf_base, ref_count);
+       field = container_of(base, struct bt_ctf_field, base);
        type = field->type;
        type_id = bt_ctf_field_type_get_type_id(type);
        if (type_id <= CTF_TYPE_UNKNOWN ||
index 5079d6982d97dc72e476853fad18f8716e0576e2..450f8d58d219f8e3e6f083c32c20363feadc46d6 100644 (file)
@@ -29,6 +29,8 @@
 #include <babeltrace/ctf-writer/event-types.h>
 #include <babeltrace/ctf-ir/event-types-internal.h>
 #include <babeltrace/ctf-ir/utils.h>
+#include <babeltrace/ctf-ir/ref.h>
+#include <babeltrace/ctf-ir/common-internal.h>
 #include <babeltrace/ctf-ir/clock.h>
 #include <babeltrace/ctf-writer/writer-internal.h>
 #include <babeltrace/compiler.h>
@@ -52,26 +54,26 @@ struct range_overlap_query {
 };
 
 static
-void bt_ctf_field_type_destroy(struct bt_ctf_ref *);
+void bt_ctf_field_type_destroy(struct bt_ref *);
 static
-void bt_ctf_field_type_integer_destroy(struct bt_ctf_ref *);
+void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type *);
 static
-void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_ref *);
+void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type *);
 static
-void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_ref *);
+void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type *);
 static
-void bt_ctf_field_type_structure_destroy(struct bt_ctf_ref *);
+void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type *);
 static
-void bt_ctf_field_type_variant_destroy(struct bt_ctf_ref *);
+void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type *);
 static
-void bt_ctf_field_type_array_destroy(struct bt_ctf_ref *);
+void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type *);
 static
-void bt_ctf_field_type_sequence_destroy(struct bt_ctf_ref *);
+void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type *);
 static
-void bt_ctf_field_type_string_destroy(struct bt_ctf_ref *);
+void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type *);
 
 static
-void (* const type_destroy_funcs[])(struct bt_ctf_ref *) = {
+void (* const type_destroy_funcs[])(struct bt_ctf_field_type *) = {
        [CTF_TYPE_INTEGER] = bt_ctf_field_type_integer_destroy,
        [CTF_TYPE_ENUM] =
                bt_ctf_field_type_enumeration_destroy,
@@ -298,7 +300,7 @@ void bt_ctf_field_type_init(struct bt_ctf_field_type *type, int init_bo)
        assert(type && (type_id > CTF_TYPE_UNKNOWN) &&
                (type_id < NR_CTF_TYPES));
 
-       bt_ctf_ref_init(&type->ref_count);
+       bt_ctf_base_init(type, bt_ctf_field_type_destroy);
        type->freeze = type_freeze_funcs[type_id];
        type->serialize = type_serialize_funcs[type_id];
 
@@ -346,23 +348,25 @@ end:
 }
 
 static
-void bt_ctf_field_type_destroy(struct bt_ctf_ref *ref)
+void bt_ctf_field_type_destroy(struct bt_ref *ref)
 {
        struct bt_ctf_field_type *type;
+       struct bt_ctf_base *base;
        enum ctf_type_id type_id;
 
        if (!ref) {
                return;
        }
 
-       type = container_of(ref, struct bt_ctf_field_type, ref_count);
+       base = container_of(ref, struct bt_ctf_base, ref_count);
+       type = container_of(base, struct bt_ctf_field_type, base);
        type_id = type->declaration->id;
        if (type_id <= CTF_TYPE_UNKNOWN ||
                type_id >= NR_CTF_TYPES) {
                return;
        }
 
-       type_destroy_funcs[type_id](ref);
+       type_destroy_funcs[type_id](type);
 }
 
 BT_HIDDEN
@@ -1920,20 +1924,12 @@ enum ctf_type_id bt_ctf_field_type_get_type_id(
 
 void bt_ctf_field_type_get(struct bt_ctf_field_type *type)
 {
-       if (!type) {
-               return;
-       }
-
-       bt_ctf_ref_get(&type->ref_count);
+       bt_ctf_get(type);
 }
 
 void bt_ctf_field_type_put(struct bt_ctf_field_type *type)
 {
-       if (!type) {
-               return;
-       }
-
-       bt_ctf_ref_put(&type->ref_count, bt_ctf_field_type_destroy);
+       bt_ctf_put(type);
 }
 
 BT_HIDDEN
@@ -2302,17 +2298,15 @@ end:
 }
 
 static
-void bt_ctf_field_type_integer_destroy(struct bt_ctf_ref *ref)
+void bt_ctf_field_type_integer_destroy(struct bt_ctf_field_type *type)
 {
-       struct bt_ctf_field_type_integer *integer;
+       struct bt_ctf_field_type_integer *integer =
+               (struct bt_ctf_field_type_integer *) type;
 
-       if (!ref) {
+       if (!type) {
                return;
        }
 
-       integer = container_of(
-               container_of(ref, struct bt_ctf_field_type, ref_count),
-               struct bt_ctf_field_type_integer, parent);
        if (integer->mapped_clock) {
                bt_ctf_clock_put(integer->mapped_clock);
        }
@@ -2320,66 +2314,58 @@ void bt_ctf_field_type_integer_destroy(struct bt_ctf_ref *ref)
 }
 
 static
-void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_ref *ref)
+void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_field_type *type)
 {
-       struct bt_ctf_field_type_enumeration *enumeration;
+       struct bt_ctf_field_type_enumeration *enumeration =
+               (struct bt_ctf_field_type_enumeration *) type;
 
-       if (!ref) {
+       if (!type) {
                return;
        }
 
-       enumeration = container_of(
-               container_of(ref, struct bt_ctf_field_type, ref_count),
-               struct bt_ctf_field_type_enumeration, parent);
        g_ptr_array_free(enumeration->entries, TRUE);
        bt_ctf_field_type_put(enumeration->container);
        g_free(enumeration);
 }
 
 static
-void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_ref *ref)
+void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_field_type *type)
 {
-       struct bt_ctf_field_type_floating_point *floating_point;
+       struct bt_ctf_field_type_floating_point *floating_point =
+               (struct bt_ctf_field_type_floating_point *) type;
 
-       if (!ref) {
+       if (!type) {
                return;
        }
 
-       floating_point = container_of(
-               container_of(ref, struct bt_ctf_field_type, ref_count),
-               struct bt_ctf_field_type_floating_point, parent);
        g_free(floating_point);
 }
 
 static
-void bt_ctf_field_type_structure_destroy(struct bt_ctf_ref *ref)
+void bt_ctf_field_type_structure_destroy(struct bt_ctf_field_type *type)
 {
-       struct bt_ctf_field_type_structure *structure;
+       struct bt_ctf_field_type_structure *structure =
+               (struct bt_ctf_field_type_structure *) type;
 
-       if (!ref) {
+       if (!type) {
                return;
        }
 
-       structure = container_of(
-               container_of(ref, struct bt_ctf_field_type, ref_count),
-               struct bt_ctf_field_type_structure, parent);
        g_ptr_array_free(structure->fields, TRUE);
        g_hash_table_destroy(structure->field_name_to_index);
        g_free(structure);
 }
 
 static
-void bt_ctf_field_type_variant_destroy(struct bt_ctf_ref *ref)
+void bt_ctf_field_type_variant_destroy(struct bt_ctf_field_type *type)
 {
-       struct bt_ctf_field_type_variant *variant;
+       struct bt_ctf_field_type_variant *variant =
+               (struct bt_ctf_field_type_variant *) type;
 
-       if (!ref) {
+       if (!type) {
                return;
        }
 
-       variant = container_of(
-               container_of(ref, struct bt_ctf_field_type, ref_count),
-               struct bt_ctf_field_type_variant, parent);
        g_ptr_array_free(variant->fields, TRUE);
        g_hash_table_destroy(variant->field_name_to_index);
        g_string_free(variant->tag_name, TRUE);
@@ -2389,33 +2375,29 @@ void bt_ctf_field_type_variant_destroy(struct bt_ctf_ref *ref)
 }
 
 static
-void bt_ctf_field_type_array_destroy(struct bt_ctf_ref *ref)
+void bt_ctf_field_type_array_destroy(struct bt_ctf_field_type *type)
 {
-       struct bt_ctf_field_type_array *array;
+       struct bt_ctf_field_type_array *array =
+               (struct bt_ctf_field_type_array *) type;
 
-       if (!ref) {
+       if (!type) {
                return;
        }
 
-       array = container_of(
-               container_of(ref, struct bt_ctf_field_type, ref_count),
-               struct bt_ctf_field_type_array, parent);
        bt_ctf_field_type_put(array->element_type);
        g_free(array);
 }
 
 static
-void bt_ctf_field_type_sequence_destroy(struct bt_ctf_ref *ref)
+void bt_ctf_field_type_sequence_destroy(struct bt_ctf_field_type *type)
 {
-       struct bt_ctf_field_type_sequence *sequence;
+       struct bt_ctf_field_type_sequence *sequence =
+               (struct bt_ctf_field_type_sequence *) type;
 
-       if (!ref) {
+       if (!type) {
                return;
        }
 
-       sequence = container_of(
-               container_of(ref, struct bt_ctf_field_type, ref_count),
-               struct bt_ctf_field_type_sequence, parent);
        bt_ctf_field_type_put(sequence->element_type);
        g_string_free(sequence->length_field_name, TRUE);
        bt_ctf_field_path_destroy(sequence->length_field_path);
@@ -2423,17 +2405,15 @@ void bt_ctf_field_type_sequence_destroy(struct bt_ctf_ref *ref)
 }
 
 static
-void bt_ctf_field_type_string_destroy(struct bt_ctf_ref *ref)
+void bt_ctf_field_type_string_destroy(struct bt_ctf_field_type *type)
 {
-       struct bt_ctf_field_type_string *string;
+       struct bt_ctf_field_type_string *string =
+               (struct bt_ctf_field_type_string *) type;
 
-       if (!ref) {
+       if (!type) {
                return;
        }
 
-       string = container_of(
-               container_of(ref, struct bt_ctf_field_type, ref_count),
-               struct bt_ctf_field_type_string, parent);
        g_free(string);
 }
 
index 586b69b6ba8f316bde944cb15f7831c53bc80278..6cb9f2d5c33685b3d08c90fe12b299bcf2d44d4a 100644 (file)
 #include <babeltrace/ctf-ir/stream-class-internal.h>
 #include <babeltrace/ctf-ir/trace-internal.h>
 #include <babeltrace/ctf-ir/utils.h>
+#include <babeltrace/ctf-ir/common-internal.h>
+#include <babeltrace/ctf-ir/ref.h>
 #include <babeltrace/ctf-ir/attributes-internal.h>
 #include <babeltrace/compiler.h>
 
 static
-void bt_ctf_event_class_destroy(struct bt_ctf_ref *ref);
+void bt_ctf_event_class_destroy(struct bt_ref *ref);
 static
-void bt_ctf_event_destroy(struct bt_ctf_ref *ref);
+void bt_ctf_event_destroy(struct bt_ref *ref);
 static
 int set_integer_field_value(struct bt_ctf_field *field, uint64_t value);
 
@@ -61,7 +63,7 @@ struct bt_ctf_event_class *bt_ctf_event_class_create(const char *name)
                goto error;
        }
 
-       bt_ctf_ref_init(&event_class->ref_count);
+       bt_ctf_base_init(event_class, bt_ctf_event_class_destroy);
        event_class->fields = bt_ctf_field_type_structure_create();
        if (!event_class->fields) {
                goto error;
@@ -510,20 +512,12 @@ end:
 
 void bt_ctf_event_class_get(struct bt_ctf_event_class *event_class)
 {
-       if (!event_class) {
-               return;
-       }
-
-       bt_ctf_ref_get(&event_class->ref_count);
+       bt_ctf_get(event_class);
 }
 
 void bt_ctf_event_class_put(struct bt_ctf_event_class *event_class)
 {
-       if (!event_class) {
-               return;
-       }
-
-       bt_ctf_ref_put(&event_class->ref_count, bt_ctf_event_class_destroy);
+       bt_ctf_put(event_class);
 }
 
 BT_HIDDEN
@@ -572,7 +566,7 @@ struct bt_ctf_event *bt_ctf_event_create(struct bt_ctf_event_class *event_class)
                goto end;
        }
 
-       bt_ctf_ref_init(&event->ref_count);
+       bt_ctf_base_init(event, bt_ctf_event_destroy);
        bt_ctf_event_class_get(event_class);
        bt_ctf_event_class_freeze(event_class);
        event->event_class = event_class;
@@ -603,7 +597,7 @@ end:
        return event;
 error_destroy:
        if (event) {
-               bt_ctf_event_destroy(&event->ref_count);
+               bt_ctf_event_destroy(&event->base.ref_count);
        }
 
        return NULL;
@@ -884,26 +878,19 @@ end:
 
 void bt_ctf_event_get(struct bt_ctf_event *event)
 {
-       if (!event) {
-               return;
-       }
-
-       bt_ctf_ref_get(&event->ref_count);
+       bt_ctf_get(event);
 }
 
 void bt_ctf_event_put(struct bt_ctf_event *event)
 {
-       if (!event) {
-               return;
-       }
-
-       bt_ctf_ref_put(&event->ref_count, bt_ctf_event_destroy);
+       bt_ctf_put(event);
 }
 
 static
-void bt_ctf_event_class_destroy(struct bt_ctf_ref *ref)
+void bt_ctf_event_class_destroy(struct bt_ref *ref)
 {
        struct bt_ctf_event_class *event_class;
+       struct bt_ctf_base *base;
 
        if (!ref) {
                return;
@@ -913,7 +900,8 @@ void bt_ctf_event_class_destroy(struct bt_ctf_ref *ref)
         * Don't call put() on the stream class. See comment in
         * bt_ctf_event_class_set_stream_class for explanation.
         */
-       event_class = container_of(ref, struct bt_ctf_event_class, ref_count);
+       base = container_of(ref, struct bt_ctf_base, ref_count);
+       event_class = container_of(base, struct bt_ctf_event_class, base);
        if (event_class->attributes) {
                bt_ctf_attributes_destroy(event_class->attributes);
        }
@@ -927,16 +915,17 @@ void bt_ctf_event_class_destroy(struct bt_ctf_ref *ref)
 }
 
 static
-void bt_ctf_event_destroy(struct bt_ctf_ref *ref)
+void bt_ctf_event_destroy(struct bt_ref *ref)
 {
        struct bt_ctf_event *event;
+       struct bt_ctf_base *base;
 
        if (!ref) {
                return;
        }
 
-       event = container_of(ref, struct bt_ctf_event,
-               ref_count);
+       base = container_of(ref, struct bt_ctf_base, ref_count);
+       event = container_of(base, struct bt_ctf_event, base);
        if (event->event_class) {
                bt_ctf_event_class_put(event->event_class);
        }
@@ -1292,7 +1281,7 @@ struct bt_ctf_event *bt_ctf_event_copy(struct bt_ctf_event *event)
                goto error;
        }
 
-       bt_ctf_ref_init(&copy->ref_count);
+       bt_ctf_base_init(copy, bt_ctf_event_destroy);
        copy->event_class = event->event_class;
        bt_ctf_event_class_get(copy->event_class);
        copy->stream = event->stream;
index 72b07b84c14cb26b98d983e908f4e88c18635dd3..1b4e9eb5f03822e202137ca170affd32cad84419 100644 (file)
 #include <babeltrace/ctf-ir/visitor-internal.h>
 #include <babeltrace/ctf-writer/functor-internal.h>
 #include <babeltrace/ctf-ir/utils.h>
+#include <babeltrace/ctf-ir/ref.h>
+#include <babeltrace/ctf-ir/common-internal.h>
 #include <babeltrace/compiler.h>
 #include <babeltrace/align.h>
 
 static
-void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref);
+void bt_ctf_stream_class_destroy(struct bt_ref *ref);
 static
 int init_event_header(struct bt_ctf_stream_class *stream_class);
 static
@@ -78,11 +80,11 @@ struct bt_ctf_stream_class *bt_ctf_stream_class_create(const char *name)
                goto error_destroy;
        }
 
-       bt_ctf_ref_init(&stream_class->ref_count);
+       bt_ctf_base_init(stream_class, bt_ctf_stream_class_destroy);
        return stream_class;
 
 error_destroy:
-       bt_ctf_stream_class_destroy(&stream_class->ref_count);
+       bt_ctf_stream_class_destroy(&stream_class->base.ref_count);
        stream_class = NULL;
 error:
        return stream_class;
@@ -608,20 +610,12 @@ end:
 
 void bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class)
 {
-       if (!stream_class) {
-               return;
-       }
-
-       bt_ctf_ref_get(&stream_class->ref_count);
+       bt_ctf_get(stream_class);
 }
 
 void bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class)
 {
-       if (!stream_class) {
-               return;
-       }
-
-       bt_ctf_ref_put(&stream_class->ref_count, bt_ctf_stream_class_destroy);
+       bt_ctf_put(stream_class);
 }
 
 BT_HIDDEN
@@ -764,15 +758,17 @@ end:
 }
 
 static
-void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref)
+void bt_ctf_stream_class_destroy(struct bt_ref *ref)
 {
        struct bt_ctf_stream_class *stream_class;
+       struct bt_ctf_base *base;
 
        if (!ref) {
                return;
        }
 
-       stream_class = container_of(ref, struct bt_ctf_stream_class, ref_count);
+       base = container_of(ref, struct bt_ctf_base, ref_count);
+       stream_class = container_of(base, struct bt_ctf_stream_class, base);
        bt_ctf_clock_put(stream_class->clock);
 
        if (stream_class->event_classes) {
index 047fc2925e19e3e79af185389f9b9fa22833125a..298749240dbefa052eb2b6bd5a104512db1ac6bd 100644 (file)
 #include <babeltrace/ctf-ir/stream.h>
 #include <babeltrace/ctf-ir/stream-internal.h>
 #include <babeltrace/ctf-ir/stream-class-internal.h>
+#include <babeltrace/ctf-ir/ref.h>
+#include <babeltrace/ctf-ir/common-internal.h>
 #include <babeltrace/ctf-writer/functor-internal.h>
 #include <babeltrace/compiler.h>
 #include <babeltrace/align.h>
 #include <babeltrace/ctf/ctf-index.h>
 
 static
-void bt_ctf_stream_destroy(struct bt_ctf_ref *ref);
+void bt_ctf_stream_destroy(struct bt_ref *ref);
 static
 int set_structure_field_integer(struct bt_ctf_field *, char *, uint64_t);
 
@@ -272,7 +274,7 @@ struct bt_ctf_stream *bt_ctf_stream_create(
 
        /* A stream has no ownership of its trace (weak ptr) */
        stream->trace = trace;
-       bt_ctf_ref_init(&stream->ref_count);
+       bt_ctf_base_init(stream, bt_ctf_stream_destroy);
        stream->packet_context = bt_ctf_field_create(
                stream_class->packet_context_type);
        if (!stream->packet_context) {
@@ -338,7 +340,7 @@ struct bt_ctf_stream *bt_ctf_stream_create(
 end:
        return stream;
 error_destroy:
-       bt_ctf_stream_destroy(&stream->ref_count);
+       bt_ctf_stream_destroy(&stream->base.ref_count);
        return NULL;
 }
 
@@ -897,32 +899,26 @@ end:
 
 void bt_ctf_stream_get(struct bt_ctf_stream *stream)
 {
-       if (!stream) {
-               return;
-       }
-
-       bt_ctf_ref_get(&stream->ref_count);
+       bt_ctf_get(stream);
 }
 
 void bt_ctf_stream_put(struct bt_ctf_stream *stream)
 {
-       if (!stream) {
-               return;
-       }
-
-       bt_ctf_ref_put(&stream->ref_count, bt_ctf_stream_destroy);
+       bt_ctf_put(stream);
 }
 
 static
-void bt_ctf_stream_destroy(struct bt_ctf_ref *ref)
+void bt_ctf_stream_destroy(struct bt_ref *ref)
 {
        struct bt_ctf_stream *stream;
+       struct bt_ctf_base *base;
 
        if (!ref) {
                return;
        }
 
-       stream = container_of(ref, struct bt_ctf_stream, ref_count);
+       base = container_of(ref, struct bt_ctf_base, ref_count);
+       stream = container_of(base, struct bt_ctf_stream, base);
        ctf_fini_pos(&stream->pos);
        if (stream->pos.fd >= 0 && close(stream->pos.fd)) {
                perror("close");
index d911dfd69946029e8078814a4a94ac30f2ca1af1..d5786d337251dff7c0e2f660ee5a5e7edad89ad2 100644 (file)
@@ -35,6 +35,8 @@
 #include <babeltrace/ctf-ir/attributes-internal.h>
 #include <babeltrace/ctf-ir/visitor-internal.h>
 #include <babeltrace/ctf-ir/utils.h>
+#include <babeltrace/ctf-ir/ref.h>
+#include <babeltrace/ctf-ir/common-internal.h>
 #include <babeltrace/compiler.h>
 #include <babeltrace/objects.h>
 
@@ -42,7 +44,7 @@
 #define DEFAULT_METADATA_STRING_SIZE 4096
 
 static
-void bt_ctf_trace_destroy(struct bt_ctf_ref *ref);
+void bt_ctf_trace_destroy(struct bt_ref *ref);
 static
 int init_trace_packet_header(struct bt_ctf_trace *trace);
 static
@@ -83,7 +85,7 @@ struct bt_ctf_trace *bt_ctf_trace_create(void)
        }
 
        bt_ctf_trace_set_byte_order(trace, BT_CTF_BYTE_ORDER_NATIVE);
-       bt_ctf_ref_init(&trace->ref_count);
+       bt_ctf_base_init(trace, bt_ctf_trace_destroy);
        trace->clocks = g_ptr_array_new_with_free_func(
                (GDestroyNotify) bt_ctf_clock_put);
        trace->streams = g_ptr_array_new_with_free_func(
@@ -109,21 +111,23 @@ struct bt_ctf_trace *bt_ctf_trace_create(void)
        return trace;
 
 error_destroy:
-       bt_ctf_trace_destroy(&trace->ref_count);
+       bt_ctf_trace_destroy(&trace->base.ref_count);
        trace = NULL;
 error:
        return trace;
 }
 
-void bt_ctf_trace_destroy(struct bt_ctf_ref *ref)
+void bt_ctf_trace_destroy(struct bt_ref *ref)
 {
        struct bt_ctf_trace *trace;
+       struct bt_ctf_base *base;
 
        if (!ref) {
                return;
        }
 
-       trace = container_of(ref, struct bt_ctf_trace, ref_count);
+       base = container_of(ref, struct bt_ctf_base, ref_count);
+       trace = container_of(base, struct bt_ctf_trace, base);
        if (trace->environment) {
                bt_ctf_attributes_destroy(trace->environment);
        }
@@ -859,20 +863,13 @@ end:
 
 void bt_ctf_trace_get(struct bt_ctf_trace *trace)
 {
-       if (!trace) {
-               return;
-       }
-
-       bt_ctf_ref_get(&trace->ref_count);
+       bt_ctf_get(trace);
 }
 
 void bt_ctf_trace_put(struct bt_ctf_trace *trace)
 {
-       if (!trace) {
-               return;
-       }
+       bt_ctf_put(trace);
 
-       bt_ctf_ref_put(&trace->ref_count, bt_ctf_trace_destroy);
 }
 
 BT_HIDDEN
index 51e545f6bb9ec7eea98f5250f779cb14d372dfe4..9adfcd381f22f09e5fdea1fbe41316ab6b74958b 100644 (file)
@@ -33,6 +33,8 @@
 #include <babeltrace/ctf-writer/functor-internal.h>
 #include <babeltrace/ctf-ir/stream-class-internal.h>
 #include <babeltrace/ctf-ir/stream-internal.h>
+#include <babeltrace/ctf-ir/ref.h>
+#include <babeltrace/ctf-ir/common-internal.h>
 #include <babeltrace/compiler.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -43,7 +45,7 @@
 #include <inttypes.h>
 
 static
-void bt_ctf_writer_destroy(struct bt_ctf_ref *ref);
+void bt_ctf_writer_destroy(struct bt_ref *ref);
 static
 int create_stream_file(struct bt_ctf_writer *writer,
                struct bt_ctf_stream *stream);
@@ -61,7 +63,7 @@ struct bt_ctf_writer *bt_ctf_writer_create(const char *path)
                goto error;
        }
 
-       bt_ctf_ref_init(&writer->ref_count);
+       bt_ctf_base_init(writer, bt_ctf_writer_destroy);
        writer->path = g_string_new(path);
        if (!writer->path) {
                goto error_destroy;
@@ -92,21 +94,23 @@ struct bt_ctf_writer *bt_ctf_writer_create(const char *path)
 
 error_destroy:
        unlinkat(writer->trace_dir_fd, "metadata", 0);
-       bt_ctf_writer_destroy(&writer->ref_count);
+       bt_ctf_writer_destroy(&writer->base.ref_count);
        writer = NULL;
 error:
        return writer;
 }
 
-void bt_ctf_writer_destroy(struct bt_ctf_ref *ref)
+void bt_ctf_writer_destroy(struct bt_ref *ref)
 {
        struct bt_ctf_writer *writer;
+       struct bt_ctf_base *base;
 
        if (!ref) {
                return;
        }
 
-       writer = container_of(ref, struct bt_ctf_writer, ref_count);
+       base = container_of(ref, struct bt_ctf_base, ref_count);
+       writer = container_of(base, struct bt_ctf_writer, base);
        bt_ctf_writer_flush_metadata(writer);
        if (writer->path) {
                g_string_free(writer->path, TRUE);
@@ -267,20 +271,12 @@ end:
 
 void bt_ctf_writer_get(struct bt_ctf_writer *writer)
 {
-       if (!writer) {
-               return;
-       }
-
-       bt_ctf_ref_get(&writer->ref_count);
+       bt_ctf_get(writer);
 }
 
 void bt_ctf_writer_put(struct bt_ctf_writer *writer)
 {
-       if (!writer) {
-               return;
-       }
-
-       bt_ctf_ref_put(&writer->ref_count, bt_ctf_writer_destroy);
+       bt_ctf_put(writer);
 }
 
 static
index 35a52f96ce68fcaacb80c593e5c7dcb4730e58c8..b70ee7e63c7462a634b32f8ed135d4fddebf5124 100644 (file)
@@ -27,6 +27,7 @@ babeltracectfirinclude_HEADERS = \
        babeltrace/ctf-ir/event-fields.h \
        babeltrace/ctf-ir/event-types.h \
        babeltrace/ctf-ir/event.h \
+       babeltrace/ctf-ir/ref.h \
        babeltrace/ctf-ir/stream.h \
        babeltrace/ctf-ir/stream-class.h \
        babeltrace/ctf-ir/trace.h \
@@ -43,6 +44,7 @@ noinst_HEADERS = \
        babeltrace/iterator-internal.h \
        babeltrace/trace-collection.h \
        babeltrace/prio_heap.h \
+       babeltrace/ref-internal.h \
        babeltrace/types.h \
        babeltrace/ctf-ir/metadata.h \
        babeltrace/ctf/events-internal.h \
@@ -51,9 +53,9 @@ noinst_HEADERS = \
        babeltrace/ctf/types.h \
        babeltrace/ctf/callbacks-internal.h \
        babeltrace/ctf/ctf-index.h \
-       babeltrace/ctf-writer/ref-internal.h \
        babeltrace/ctf-writer/writer-internal.h \
        babeltrace/ctf-ir/attributes-internal.h \
+       babeltrace/ctf-ir/common-internal.h \
        babeltrace/ctf-ir/event-types-internal.h \
        babeltrace/ctf-ir/event-fields-internal.h \
        babeltrace/ctf-ir/event-internal.h \
index f876cd3034fa81e204f2adc0369d35c0ae910b11..7428126e42c62cfcedb76dfbeb685314595c8f79 100644 (file)
  * SOFTWARE.
  */
 
-#include <babeltrace/ctf-writer/ref-internal.h>
 #include <babeltrace/ctf-writer/clock.h>
 #include <babeltrace/ctf-ir/trace-internal.h>
+#include <babeltrace/ctf-ir/common-internal.h>
 #include <babeltrace/babeltrace-internal.h>
 #include <glib.h>
 #include <babeltrace/compat/uuid.h>
 
 struct bt_ctf_clock {
-       struct bt_ctf_ref ref_count;
+       struct bt_ctf_base base;
        GString *name;
        GString *description;
        uint64_t frequency;
index cad2eec92df116bb4d936994dc439651eedc127d..1ac8ca9e36d99693d312914ec82e6dc80a767cb0 100644 (file)
@@ -258,6 +258,8 @@ extern int bt_ctf_clock_set_time(struct bt_ctf_clock *clock,
  * bt_ctf_clock_get and bt_ctf_clock_put: increment and decrement the
  * refcount of the clock
  *
+ * You may also use bt_ctf_get() and bt_ctf_put() with clock objects.
+ *
  * These functions ensure that the clock won't be destroyed when it
  * is in use. The same number of get and put (plus one extra put to
  * release the initial reference done at creation) has to be done to
diff --git a/include/babeltrace/ctf-ir/common-internal.h b/include/babeltrace/ctf-ir/common-internal.h
new file mode 100644 (file)
index 0000000..5922780
--- /dev/null
@@ -0,0 +1,47 @@
+#ifndef BABELTRACE_CTF_IR_COMMON_INTERNAL_H
+#define BABELTRACE_CTF_IR_COMMON_INTERNAL_H
+
+/*
+ * Babeltrace - CTF IR: common data structures
+ *
+ * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation
+ * Copyright (c) 2015 Philippe Proulx <pproulx@efficios.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <babeltrace/ref-internal.h>
+
+/*
+ * bt_ctf_trace  must be the base class of _all_ CTF IR classes and is assumed
+ * to be the first member of all bt_ctf_* structures.
+ */
+struct bt_ctf_base {
+       struct bt_ref ref_count;
+};
+
+static inline
+void bt_ctf_base_init(void *obj, bt_ref_release_func_t release_func)
+{
+       struct bt_ctf_base *base = obj;
+
+       bt_ref_init(&base->ref_count, release_func);
+}
+
+#endif /* BABELTRACE_CTF_IR_COMMON_INTERNAL_H */
index 34f8fad4476656805ca4d575e9f293e1d0fa0eea..e0214e82be3e0d1cc606027254075f7797e7197a 100644 (file)
  * SOFTWARE.
  */
 
-#include <babeltrace/ctf-writer/ref-internal.h>
 #include <babeltrace/ctf-writer/event-fields.h>
+#include <babeltrace/ctf-ir/common-internal.h>
 #include <babeltrace/babeltrace-internal.h>
 #include <babeltrace/ctf/types.h>
 #include <glib.h>
 
 struct bt_ctf_field {
-       struct bt_ctf_ref ref_count;
+       struct bt_ctf_base base;
        struct bt_ctf_field_type *type;
        int payload_set;
 };
index 4a55b449af3491a25cb71d263758dd2a9f78a85e..2b682bb649d90c28e53f7c21d507b49628cbb31a 100644 (file)
@@ -370,6 +370,8 @@ extern struct bt_ctf_field *bt_ctf_field_copy(struct bt_ctf_field *field);
  * bt_ctf_field_get and bt_ctf_field_put: increment and decrement the
  * field's reference count.
  *
+ * You may also use bt_ctf_get() and bt_ctf_put() with field objects.
+ *
  * These functions ensure that the field won't be destroyed when it
  * is in use. The same number of get and put (plus one extra put to
  * release the initial reference done at creation) have to be done to
index 5201ee53b7c54e353cd24789d97a4dc93054c125..9b2e2075e77cb708313aa9d9926c5a259b8829a2 100644 (file)
@@ -27,7 +27,6 @@
  * SOFTWARE.
  */
 
-#include <babeltrace/ctf-writer/ref-internal.h>
 #include <babeltrace/ctf-writer/event-types.h>
 #include <babeltrace/ctf-writer/event-fields.h>
 #include <babeltrace/babeltrace-internal.h>
 #include <babeltrace/ctf/types.h>
 #include <babeltrace/ctf-ir/stream-class.h>
 #include <babeltrace/ctf-ir/stream.h>
+#include <babeltrace/ctf-ir/common-internal.h>
 #include <glib.h>
 
 #define BT_CTF_EVENT_CLASS_ATTR_ID_INDEX       0
 #define BT_CTF_EVENT_CLASS_ATTR_NAME_INDEX     1
 
 struct bt_ctf_event_class {
-       struct bt_ctf_ref ref_count;
+       struct bt_ctf_base base;
        struct bt_object *attributes;
        /*
         * Weak reference; an event class does not have ownership of a
@@ -56,7 +56,7 @@ struct bt_ctf_event_class {
 };
 
 struct bt_ctf_event {
-       struct bt_ctf_ref ref_count;
+       struct bt_ctf_base base;
        struct bt_ctf_event_class *event_class;
        /* Weak reference; an event does not have ownership of a stream */
        struct bt_ctf_stream *stream;
index 8475198be676bc0f1860059c68807f98140c668f..d5290f575cda586d10cefdb1e44670e891610b97 100644 (file)
  */
 
 #include <babeltrace/ctf-writer/event-types.h>
-#include <babeltrace/ctf-writer/ref-internal.h>
 #include <babeltrace/ctf-writer/event-fields.h>
 #include <babeltrace/ctf-writer/writer.h>
 #include <babeltrace/ctf-ir/trace-internal.h>
+#include <babeltrace/ctf-ir/common-internal.h>
 #include <babeltrace/babeltrace-internal.h>
 #include <babeltrace/types.h>
 #include <babeltrace/ctf/events.h>
@@ -62,7 +62,7 @@ struct bt_ctf_field_path {
 };
 
 struct bt_ctf_field_type {
-       struct bt_ctf_ref ref_count;
+       struct bt_ctf_base base;
        struct bt_declaration *declaration;
        type_freeze_func freeze;
        type_serialize_func serialize;
index fdec2e07bb6bc1f92ea2ac8df751f7246b707e69..d777ce6b40603c23ff438ea28ec38e45fd561c2b 100644 (file)
@@ -755,6 +755,8 @@ extern enum ctf_type_id bt_ctf_field_type_get_type_id(
  * bt_ctf_field_type_get and bt_ctf_field_type_put: increment and decrement
  * the field type's reference count.
  *
+ * You may also use bt_ctf_get() and bt_ctf_put() with field type objects.
+ *
  * These functions ensure that the field type won't be destroyed while it
  * is in use. The same number of get and put (plus one extra put to
  * release the initial reference done at creation) have to be done to
index 09ada907f8673ef19bb9080594030e1921f86de2..4c90a2eb5759c6bb2952de67bbe38dbcb03df4c2 100644 (file)
@@ -307,6 +307,8 @@ extern int bt_ctf_event_class_set_context_type(
  * bt_ctf_event_class_get and bt_ctf_event_class_put: increment and decrement
  * the event class' reference count.
  *
+ * You may also use bt_ctf_get() and bt_ctf_put() with event class objects.
+ *
  * These functions ensure that the event class won't be destroyed while it
  * is in use. The same number of get and put (plus one extra put to
  * release the initial reference done at creation) have to be done to
@@ -512,6 +514,8 @@ extern struct bt_ctf_event *bt_ctf_event_copy(struct bt_ctf_event *event);
  * bt_ctf_event_get and bt_ctf_event_put: increment and decrement
  * the event's reference count.
  *
+ * You may also use bt_ctf_get() and bt_ctf_put() with event objects.
+ *
  * These functions ensure that the event won't be destroyed while it
  * is in use. The same number of get and put (plus one extra put to
  * release the initial reference done at creation) have to be done to
diff --git a/include/babeltrace/ctf-ir/ref.h b/include/babeltrace/ctf-ir/ref.h
new file mode 100644 (file)
index 0000000..fa612ca
--- /dev/null
@@ -0,0 +1,98 @@
+#ifndef BABELTRACE_CTF_IR_REF_H
+#define BABELTRACE_CTF_IR_REF_H
+
+/*
+ * BabelTrace - CTF IR: common reference counting
+ *
+ * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation
+ * Copyright (c) 2015 Philippe Proulx <pproulx@efficios.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <assert.h>
+#include <babeltrace/ref-internal.h>
+#include <babeltrace/ctf-ir/common-internal.h>
+
+/*
+ * BT_CTF_PUT: calls bt_ctf_put() with a variable, then sets this
+ *     variable to NULL.
+ *
+ * A common action with CTF IR objects is to create or get one, do
+ * something with it, and then put it. To avoid putting it a second time
+ * later (if an error occurs, for example), the variable is often reset
+ * to NULL after putting the object it points to. Since this is so
+ * common, you can use the BT_CTF_PUT() macro, which does just that.
+ *
+ * It is safe to call this function with a NULL object.
+ *
+ * @param obj CTF IR object.
+ */
+#define BT_CTF_PUT(_obj)               \
+       do {                            \
+               bt_ctf_put(_obj);       \
+               (_obj) = NULL;          \
+       } while (0)
+
+/*
+ * bt_ctf_get: increments the reference count of a CTF IR object.
+ *
+ * The same number of bt_ctf_get() and bt_ctf_put() (plus one extra
+ * bt_ctf_put() to release the initial reference done at creation) have
+ * to be done to destroy a CTF IR object.
+ *
+ * It is safe to call this function with a NULL object.
+ *
+ * @param obj CTF IR object.
+ */
+static inline
+void bt_ctf_get(void *obj)
+{
+       if (obj) {
+               struct bt_ctf_base *base = obj;
+
+               bt_ref_get(&base->ref_count);
+       }
+}
+
+/*
+ * bt_ctf_put: decrements the reference count of a CTF IR object.
+ *
+ * The same number of bt_ctf_get() and bt_ctf_put() (plus one extra
+ * bt_ctf_put() to release the initial reference done at creation) have
+ * to be done to destroy a CTF IR object.
+ *
+ * When the object's reference count is decremented to 0 by a call to
+ * bt_ctf_put(), the object is freed.
+ *
+ * It is safe to call this function with a NULL object.
+ *
+ * @param obj CTF IR object.
+ */
+static inline
+void bt_ctf_put(void *obj)
+{
+       if (obj) {
+               struct bt_ctf_base *base = obj;
+
+               bt_ref_put(&base->ref_count);
+       }
+}
+
+#endif /* BABELTRACE_CTF_IR_REF_H */
index c88be91c02fa0ffe129938b3c941e60c38c56155..4338742e7688e21a20210236ca1fe287549a87b2 100644 (file)
  * SOFTWARE.
  */
 
-#include <babeltrace/ctf-writer/ref-internal.h>
 #include <babeltrace/ctf-writer/clock.h>
 #include <babeltrace/ctf-writer/event-fields.h>
 #include <babeltrace/ctf-writer/event-types.h>
 #include <babeltrace/ctf-ir/trace.h>
+#include <babeltrace/ctf-ir/common-internal.h>
 #include <babeltrace/babeltrace-internal.h>
 #include <babeltrace/ctf/types.h>
 #include <glib.h>
 
 struct bt_ctf_stream_class {
-       struct bt_ctf_ref ref_count;
+       struct bt_ctf_base base;
        GString *name;
        struct bt_ctf_clock *clock;
        GPtrArray *event_classes; /* Array of pointers to bt_ctf_event_class */
index 200a1fd1b4494340e267725d6766e6f85957c01b..75be6b678f7f37b07aa19d5f14491abf45b5c5ad 100644 (file)
@@ -290,6 +290,8 @@ extern int bt_ctf_stream_class_set_event_context_type(
  * bt_ctf_stream_class_get and bt_ctf_stream_class_put: increment and
  * decrement the stream class' reference count.
  *
+ * You may also use bt_ctf_get() and bt_ctf_put() with stream class objects.
+ *
  * These functions ensure that the stream class won't be destroyed while it
  * is in use. The same number of get and put (plus one extra put to
  * release the initial reference done at creation) have to be done to
index 44ff774ac4db18ccd85d8a0d7e978df8e242a726..b0011ddfdbe91f2e721bc9aeb0d7d652f69b2f14 100644 (file)
@@ -28,7 +28,7 @@
  */
 
 #include <babeltrace/ctf-ir/stream.h>
-#include <babeltrace/ctf-writer/ref-internal.h>
+#include <babeltrace/ctf-ir/common-internal.h>
 #include <babeltrace/ctf-writer/clock.h>
 #include <babeltrace/ctf-writer/event-fields.h>
 #include <babeltrace/ctf-writer/event-types.h>
@@ -37,7 +37,7 @@
 #include <glib.h>
 
 struct bt_ctf_stream {
-       struct bt_ctf_ref ref_count;
+       struct bt_ctf_base base;
        /* Trace owning this stream. A stream does not own a trace. */
        struct bt_ctf_trace *trace;
        uint32_t id;
index 9e61d763bb9f9bf5eae50b886132e9aec8ce3e46..18f86a7c0949ca10e550d29a8443c616723b3aaa 100644 (file)
@@ -219,6 +219,8 @@ extern int bt_ctf_stream_flush(struct bt_ctf_stream *stream);
  * bt_ctf_stream_get and bt_ctf_stream_put: increment and decrement the
  * stream's reference count.
  *
+ * You may also use bt_ctf_get() and bt_ctf_put() with stream objects.
+ *
  * These functions ensure that the stream won't be destroyed while it
  * is in use. The same number of get and put (plus one extra put to
  * release the initial reference done at creation) have to be done to
index 03433a4be873442e3f134f117fc4b32822bb2948..4a76c1a187819342ea04268a6db8b69a37dc0c61 100644 (file)
  * SOFTWARE.
  */
 
-#include <babeltrace/ctf-writer/ref-internal.h>
 #include <babeltrace/ctf-ir/trace.h>
 #include <babeltrace/ctf-ir/event-types.h>
 #include <babeltrace/ctf-ir/event-fields.h>
+#include <babeltrace/ctf-ir/common-internal.h>
 #include <babeltrace/babeltrace-internal.h>
 #include <babeltrace/objects.h>
 #include <glib.h>
@@ -48,7 +48,7 @@ enum field_type_alias {
 };
 
 struct bt_ctf_trace {
-       struct bt_ctf_ref ref_count;
+       struct bt_ctf_base base;
        int frozen;
        uuid_t uuid;
        int byte_order; /* A value defined in Babeltrace's "endian.h" */
index 953810342b10896b4c85364bb8d1e589be8ce6f0..cc628a5b5240f804ab26666cabe0d7f2b938e72b 100644 (file)
@@ -346,6 +346,8 @@ extern int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace *trace,
  * bt_ctf_trace_get and bt_ctf_trace_put: increment and decrement the
  * trace's reference count.
  *
+ * You may also use bt_ctf_get() and bt_ctf_put() with trace objects.
+ *
  * These functions ensure that the trace won't be destroyed while it
  * is in use. The same number of get and put (plus one extra put to
  * release the initial reference done at creation) have to be done to
diff --git a/include/babeltrace/ctf-writer/ref-internal.h b/include/babeltrace/ctf-writer/ref-internal.h
deleted file mode 100644 (file)
index 1db4974..0000000
+++ /dev/null
@@ -1,61 +0,0 @@
-#ifndef BABELTRACE_CTF_WRITER_REF_INTERNAL_H
-#define BABELTRACE_CTF_WRITER_REF_INTERNAL_H
-
-/*
- * BabelTrace - CTF Writer: Reference count
- *
- * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
- *
- * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-#include <assert.h>
-
-struct bt_ctf_ref {
-       long refcount;
-};
-
-static inline
-void bt_ctf_ref_init(struct bt_ctf_ref *ref)
-{
-       assert(ref);
-       ref->refcount = 1;
-}
-
-static inline
-void bt_ctf_ref_get(struct bt_ctf_ref *ref)
-{
-       assert(ref);
-       ref->refcount++;
-}
-
-static inline
-void bt_ctf_ref_put(struct bt_ctf_ref *ref,
-               void (*release)(struct bt_ctf_ref *))
-{
-       assert(ref);
-       assert(release);
-       if ((--ref->refcount) == 0) {
-               release(ref);
-       }
-}
-
-#endif /* BABELTRACE_CTF_WRITER_REF_INTERNAL_H */
index 388ff0d1a9893cc5b7a853ed33d64cb813c03f80..6b1677ae22ef445c047ae0b85148802441a2679b 100644 (file)
  * SOFTWARE.
  */
 
-#include <babeltrace/ctf-writer/ref-internal.h>
 #include <babeltrace/ctf-writer/writer.h>
 #include <babeltrace/babeltrace-internal.h>
 #include <glib.h>
 #include <dirent.h>
 #include <sys/types.h>
 #include <babeltrace/ctf-ir/trace.h>
+#include <babeltrace/ctf-ir/common-internal.h>
 
 struct bt_ctf_writer {
-       struct bt_ctf_ref ref_count;
+       struct bt_ctf_base base;
        int frozen; /* Protects attributes that can't be changed mid-trace */
        struct bt_ctf_trace *trace;
        GString *path;
index f189ab5cb2656d5b586ea31b717a51ea2af346cb..1c94bccacf51d37f959bd6dc38cfe3b7f0329139 100644 (file)
@@ -153,6 +153,8 @@ extern int bt_ctf_writer_set_byte_order(struct bt_ctf_writer *writer,
  * bt_ctf_writer_get and bt_ctf_writer_put: increment and decrement the
  * writer's reference count.
  *
+ * You may also use bt_ctf_get() and bt_ctf_put() with writer objects.
+ *
  * These functions ensure that the writer won't be destroyed while it
  * is in use. The same number of get and put (plus one extra put to
  * release the initial reference done at creation) have to be done to
diff --git a/include/babeltrace/ref-internal.h b/include/babeltrace/ref-internal.h
new file mode 100644 (file)
index 0000000..a8eec33
--- /dev/null
@@ -0,0 +1,67 @@
+#ifndef BABELTRACE_REF_INTERNAL_H
+#define BABELTRACE_REF_INTERNAL_H
+
+/*
+ * Babeltrace - reference counting
+ *
+ * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <assert.h>
+
+struct bt_ref;
+
+typedef void (*bt_ref_release_func_t)(struct bt_ref *);
+
+struct bt_ref {
+       long refcount;
+       bt_ref_release_func_t release_func;
+};
+
+static inline
+void bt_ref_init(struct bt_ref *ref,
+       bt_ref_release_func_t release_func)
+{
+       assert(ref);
+       ref->refcount = 1;
+       ref->release_func = release_func;
+}
+
+static inline
+void bt_ref_get(struct bt_ref *ref)
+{
+       assert(ref);
+       ref->refcount++;
+}
+
+static inline
+void bt_ref_put(struct bt_ref *ref)
+{
+       assert(ref);
+       assert(ref->release_func);
+       if ((--ref->refcount) == 0) {
+               ref->release_func(ref);
+       }
+}
+
+#endif /* BABELTRACE_REF_INTERNAL_H */
index 4d31d2ee0c9057e9c4e1a1675897708c66157803..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)
 {
@@ -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.050142 seconds and 4 git commands to generate.