ir: add user byte order to integer/float types
[babeltrace.git] / formats / ctf / ir / stream-class.c
index 72b07b84c14cb26b98d983e908f4e88c18635dd3..14f7a6189573a748651780d68d21b3e5dbd9d8e7 100644 (file)
 #include <babeltrace/ctf-ir/visitor-internal.h>
 #include <babeltrace/ctf-writer/functor-internal.h>
 #include <babeltrace/ctf-ir/utils.h>
+#include <babeltrace/ref.h>
 #include <babeltrace/compiler.h>
 #include <babeltrace/align.h>
+#include <babeltrace/endian.h>
 
 static
-void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref);
+void bt_ctf_stream_class_destroy(struct bt_object *obj);
 static
 int init_event_header(struct bt_ctf_stream_class *stream_class);
 static
@@ -63,46 +65,34 @@ struct bt_ctf_stream_class *bt_ctf_stream_class_create(const char *name)
 
        stream_class->name = g_string_new(name);
        stream_class->event_classes = g_ptr_array_new_with_free_func(
-               (GDestroyNotify)bt_ctf_event_class_put);
+               (GDestroyNotify) bt_object_release);
        if (!stream_class->event_classes) {
-               goto error_destroy;
+               goto error;
        }
 
        ret = init_event_header(stream_class);
        if (ret) {
-               goto error_destroy;
+               goto error;
        }
 
        ret = init_packet_context(stream_class);
        if (ret) {
-               goto error_destroy;
+               goto error;
        }
 
-       bt_ctf_ref_init(&stream_class->ref_count);
+       bt_object_init(stream_class, bt_ctf_stream_class_destroy);
        return stream_class;
 
-error_destroy:
-       bt_ctf_stream_class_destroy(&stream_class->ref_count);
-       stream_class = NULL;
 error:
+        BT_PUT(stream_class);
        return stream_class;
 }
 
 struct bt_ctf_trace *bt_ctf_stream_class_get_trace(
                struct bt_ctf_stream_class *stream_class)
 {
-       struct bt_ctf_trace *trace = NULL;
-
-       if (!stream_class) {
-               goto end;
-       }
-
-       trace = stream_class->trace;
-       if (trace) {
-               bt_ctf_trace_get(trace);
-       }
-end:
-       return trace;
+       return (struct bt_ctf_trace *) bt_object_get_parent(
+               stream_class);
 }
 
 const char *bt_ctf_stream_class_get_name(
@@ -144,7 +134,7 @@ struct bt_ctf_clock *bt_ctf_stream_class_get_clock(
        }
 
        clock = stream_class->clock;
-       bt_ctf_clock_get(clock);
+       bt_get(clock);
 end:
        return clock;
 }
@@ -173,7 +163,7 @@ int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class *stream_class,
                mapped_clock = bt_ctf_field_type_integer_get_mapped_clock(
                        timestamp_field);
                if (mapped_clock) {
-                       bt_ctf_clock_put(mapped_clock);
+                       bt_put(mapped_clock);
                        goto end;
                }
 
@@ -185,14 +175,14 @@ int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class *stream_class,
        }
 
        if (stream_class->clock) {
-               bt_ctf_clock_put(stream_class->clock);
+               bt_put(stream_class->clock);
        }
 
        stream_class->clock = clock;
-       bt_ctf_clock_get(clock);
+       bt_get(clock);
 end:
        if (timestamp_field) {
-               bt_ctf_field_type_put(timestamp_field);
+               bt_put(timestamp_field);
        }
        return ret;
 }
@@ -326,6 +316,8 @@ int bt_ctf_stream_class_add_event_class(
 {
        int ret = 0;
        int64_t event_id;
+       struct bt_ctf_trace *trace = NULL;
+       struct bt_ctf_stream_class *old_stream_class = NULL;
 
        if (!stream_class || !event_class) {
                ret = -1;
@@ -341,15 +333,25 @@ int bt_ctf_stream_class_add_event_class(
                goto end;
        }
 
+       old_stream_class = (struct bt_ctf_stream_class *) bt_object_get_parent(
+               event_class);
+       if (old_stream_class) {
+               /* Event class is already associated to a stream class. */
+               ret = -1;
+               goto end;
+       }
+
        /*
         * Resolve the event's sequence length and variant tags if the
         * stream is already associated with a trace. Otherwise, this
         * validation will be performed once the stream is registered
         * to a trace.
         */
-       if (stream_class->trace) {
+       trace = (struct bt_ctf_trace *) bt_object_get_parent(
+               stream_class);
+       if (trace) {
                ret = bt_ctf_event_class_resolve_types(event_class,
-                       stream_class->trace, stream_class);
+                       trace, stream_class);
                if (ret) {
                        goto end;
                }
@@ -365,17 +367,12 @@ int bt_ctf_stream_class_add_event_class(
                }
        }
 
-       ret = bt_ctf_event_class_set_stream_class(event_class, stream_class);
-       if (ret) {
-               goto end;
-       }
-
        ret = bt_ctf_event_class_set_stream_id(event_class, stream_class->id);
        if (ret) {
                goto end;
        }
 
-       bt_ctf_event_class_get(event_class);
+       bt_object_set_parent(event_class, stream_class);
        g_ptr_array_add(stream_class->event_classes, event_class);
        bt_ctf_event_class_freeze(event_class);
 
@@ -391,6 +388,8 @@ int bt_ctf_stream_class_add_event_class(
                        stream_class->byte_order);
        }
 end:
+       BT_PUT(trace);
+       BT_PUT(old_stream_class);
        return ret;
 }
 
@@ -420,7 +419,7 @@ struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class(
        }
 
        event_class = g_ptr_array_index(stream_class->event_classes, index);
-       bt_ctf_event_class_get(event_class);
+       bt_get(event_class);
 end:
        return event_class;
 }
@@ -443,7 +442,7 @@ struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_name(
 
                if (!strcmp(name, cur_event_class_name)) {
                        event_class = cur_event_class;
-                       bt_ctf_event_class_get(event_class);
+                       bt_get(event_class);
                        goto end;
                }
        }
@@ -467,7 +466,7 @@ struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_id(
 
                if (bt_ctf_event_class_get_id(current_event_class) == id) {
                        event_class = current_event_class;
-                       bt_ctf_event_class_get(event_class);
+                       bt_get(event_class);
                        goto end;
                }
        }
@@ -485,7 +484,7 @@ struct bt_ctf_field_type *bt_ctf_stream_class_get_packet_context_type(
        }
 
        assert(stream_class->packet_context_type);
-       bt_ctf_field_type_get(stream_class->packet_context_type);
+       bt_get(stream_class->packet_context_type);
        ret = stream_class->packet_context_type;
 end:
        return ret;
@@ -513,8 +512,8 @@ int bt_ctf_stream_class_set_packet_context_type(
                goto end;
        }
 
-       bt_ctf_field_type_put(stream_class->packet_context_type);
-       bt_ctf_field_type_get(packet_context_type);
+       bt_put(stream_class->packet_context_type);
+       bt_get(packet_context_type);
        stream_class->packet_context_type = packet_context_type;
 end:
        return ret;
@@ -530,7 +529,7 @@ struct bt_ctf_field_type *bt_ctf_stream_class_get_event_header_type(
        }
 
        assert(stream_class->event_header_type);
-       bt_ctf_field_type_get(stream_class->event_header_type);
+       bt_get(stream_class->event_header_type);
        ret = stream_class->event_header_type;
 end:
        return ret;
@@ -558,8 +557,8 @@ int bt_ctf_stream_class_set_event_header_type(
                goto end;
        }
 
-       bt_ctf_field_type_put(stream_class->event_header_type);
-       bt_ctf_field_type_get(event_header_type);
+       bt_put(stream_class->event_header_type);
+       bt_get(event_header_type);
        stream_class->event_header_type = event_header_type;
 end:
        return ret;
@@ -575,7 +574,7 @@ struct bt_ctf_field_type *bt_ctf_stream_class_get_event_context_type(
        }
 
        assert(stream_class->event_context_type);
-       bt_ctf_field_type_get(stream_class->event_context_type);
+       bt_get(stream_class->event_context_type);
        ret = stream_class->event_context_type;
 end:
        return ret;
@@ -599,8 +598,8 @@ int bt_ctf_stream_class_set_event_context_type(
                goto end;
        }
 
-       bt_ctf_field_type_put(stream_class->event_context_type);
-       bt_ctf_field_type_get(event_context_type);
+       bt_put(stream_class->event_context_type);
+       bt_get(event_context_type);
        stream_class->event_context_type = event_context_type;
 end:
        return ret;
@@ -608,20 +607,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_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_put(stream_class);
 }
 
 BT_HIDDEN
@@ -639,52 +630,31 @@ void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class *stream_class)
 }
 
 BT_HIDDEN
-int bt_ctf_stream_class_set_byte_order(struct bt_ctf_stream_class *stream_class,
-       enum bt_ctf_byte_order byte_order)
+void bt_ctf_stream_class_set_byte_order(
+       struct bt_ctf_stream_class *stream_class, int byte_order)
 {
-       int i, ret = 0;
-       int internal_byte_order;
-
-       /* Note that "NATIVE" means the trace's endianness, not the host's. */
-       if (!stream_class || byte_order <= BT_CTF_BYTE_ORDER_UNKNOWN ||
-               byte_order > BT_CTF_BYTE_ORDER_NETWORK) {
-               ret = -1;
-               goto end;
-       }
-
-       switch (byte_order) {
-       case BT_CTF_BYTE_ORDER_NETWORK:
-       case BT_CTF_BYTE_ORDER_BIG_ENDIAN:
-               internal_byte_order = BIG_ENDIAN;
-               break;
-       case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN:
-               internal_byte_order = LITTLE_ENDIAN;
-               break;
-       default:
-               ret = -1;
-               goto end;
-       }
+       int i;
 
-       stream_class->byte_order = internal_byte_order;
+       assert(stream_class);
+       assert(byte_order == LITTLE_ENDIAN || byte_order == BIG_ENDIAN);
+       stream_class->byte_order = byte_order;
 
        /* Set native byte order to little or big endian */
        bt_ctf_field_type_set_native_byte_order(
-               stream_class->event_header_type, stream_class->byte_order);
+               stream_class->event_header_type, byte_order);
        bt_ctf_field_type_set_native_byte_order(
-               stream_class->packet_context_type, stream_class->byte_order);
+               stream_class->packet_context_type, byte_order);
        bt_ctf_field_type_set_native_byte_order(
-               stream_class->event_context_type, stream_class->byte_order);
+               stream_class->event_context_type, byte_order);
 
        /* Set all events' native byte order */
        for (i = 0; i < stream_class->event_classes->len; i++) {
-               bt_ctf_event_class_set_native_byte_order(
-                       g_ptr_array_index(stream_class->event_classes, i),
-                       stream_class->byte_order);
-               bt_ctf_event_class_freeze(
-                       g_ptr_array_index(stream_class->event_classes, i));
+               struct bt_ctf_event_class *event_class =
+                       g_ptr_array_index(stream_class->event_classes, i);
+
+               bt_ctf_event_class_set_native_byte_order(event_class,
+                       byte_order);
        }
-end:
-       return ret;
 }
 
 BT_HIDDEN
@@ -741,52 +711,15 @@ end:
        return ret;
 }
 
-BT_HIDDEN
-int bt_ctf_stream_class_set_trace(struct bt_ctf_stream_class *stream_class,
-               struct bt_ctf_trace *trace)
-{
-       int ret = 0;
-
-       if (!stream_class) {
-               ret = -1;
-               goto end;
-       }
-
-       if (stream_class->trace && trace) {
-               /* Already attached to a trace */
-               ret = -1;
-               goto end;
-       }
-
-       stream_class->trace = trace;
-end:
-       return ret;
-}
-
 static
-void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref)
+void bt_ctf_stream_class_destroy(struct bt_object *obj)
 {
        struct bt_ctf_stream_class *stream_class;
 
-       if (!ref) {
-               return;
-       }
-
-       stream_class = container_of(ref, struct bt_ctf_stream_class, ref_count);
-       bt_ctf_clock_put(stream_class->clock);
+       stream_class = container_of(obj, struct bt_ctf_stream_class, base);
+       bt_put(stream_class->clock);
 
        if (stream_class->event_classes) {
-               size_t i;
-
-               /* Unregister this stream class from the event classes */
-               for (i = 0; i < stream_class->event_classes->len; i++) {
-                       struct bt_ctf_event_class *event_class =
-                               g_ptr_array_index(stream_class->event_classes,
-                               i);
-
-                       bt_ctf_event_class_set_stream_class(event_class, NULL);
-               }
-
                g_ptr_array_free(stream_class->event_classes, TRUE);
        }
 
@@ -794,11 +727,9 @@ void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref)
                g_string_free(stream_class->name, TRUE);
        }
 
-       bt_ctf_field_type_put(stream_class->event_header_type);
-       bt_ctf_field_type_put(stream_class->packet_context_type);
-       if (stream_class->event_context_type) {
-               bt_ctf_field_type_put(stream_class->event_context_type);
-       }
+       bt_put(stream_class->event_header_type);
+       bt_put(stream_class->packet_context_type);
+       bt_put(stream_class->event_context_type);
        g_free(stream_class);
 }
 
@@ -831,16 +762,16 @@ int init_event_header(struct bt_ctf_stream_class *stream_class)
        }
 
        if (stream_class->event_header_type) {
-               bt_ctf_field_type_put(stream_class->event_header_type);
+               bt_put(stream_class->event_header_type);
        }
        stream_class->event_header_type = event_header_type;
 end:
        if (ret) {
-               bt_ctf_field_type_put(event_header_type);
+               bt_put(event_header_type);
        }
 
-       bt_ctf_field_type_put(_uint32_t);
-       bt_ctf_field_type_put(_uint64_t);
+       bt_put(_uint32_t);
+       bt_put(_uint64_t);
        return ret;
 }
 
@@ -892,16 +823,14 @@ int init_packet_context(struct bt_ctf_stream_class *stream_class)
                goto end;
        }
 
-       if (stream_class->packet_context_type) {
-               bt_ctf_field_type_put(stream_class->packet_context_type);
-       }
+       bt_put(stream_class->packet_context_type);
        stream_class->packet_context_type = packet_context_type;
 end:
        if (ret) {
-               bt_ctf_field_type_put(packet_context_type);
+               bt_put(packet_context_type);
                goto end;
        }
 
-       bt_ctf_field_type_put(_uint64_t);
+       bt_put(_uint64_t);
        return ret;
 }
This page took 0.029206 seconds and 4 git commands to generate.