Add support for custom event headers
[babeltrace.git] / formats / ctf / ir / stream-class.c
index 48c3d4c5c10970a37b8219a862c555185d45115b..cd87dffa8624c4493042be22bef19f544c0b3202 100644 (file)
 #include <babeltrace/ctf-writer/stream.h>
 #include <babeltrace/ctf-ir/stream-class-internal.h>
 #include <babeltrace/ctf-writer/functor-internal.h>
+#include <babeltrace/ctf-ir/utils.h>
 #include <babeltrace/compiler.h>
 #include <babeltrace/align.h>
 
 static
 void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref);
 static
-int init_event_header(struct bt_ctf_stream_class *stream_class,
-               enum bt_ctf_byte_order byte_order);
+int init_event_header(struct bt_ctf_stream_class *stream_class);
 static
-int init_packet_context(struct bt_ctf_stream_class *stream_class,
-               enum bt_ctf_byte_order byte_order);
+int init_packet_context(struct bt_ctf_stream_class *stream_class);
 
 struct bt_ctf_stream_class *bt_ctf_stream_class_create(const char *name)
 {
        int ret;
        struct bt_ctf_stream_class *stream_class = NULL;
 
-       if (!name || !strlen(name)) {
+       if (!name || !strlen(name) || bt_ctf_validate_identifier(name)) {
                goto error;
        }
 
@@ -68,7 +67,12 @@ struct bt_ctf_stream_class *bt_ctf_stream_class_create(const char *name)
                goto error_destroy;
        }
 
-       ret = init_packet_context(stream_class, BT_CTF_BYTE_ORDER_NATIVE);
+       ret = init_event_header(stream_class);
+       if (ret) {
+               goto error_destroy;
+       }
+
+       ret = init_packet_context(stream_class);
        if (ret) {
                goto error_destroy;
        }
@@ -151,7 +155,7 @@ int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class,
 {
        int ret = 0;
 
-       if (!stream_class) {
+       if (!stream_class || stream_class->frozen) {
                ret = -1;
                goto end;
        }
@@ -203,27 +207,28 @@ end:
        return ret;
 }
 
-int64_t bt_ctf_stream_class_get_event_class_count(
+int bt_ctf_stream_class_get_event_class_count(
                struct bt_ctf_stream_class *stream_class)
 {
-       int64_t ret;
+       int ret;
 
        if (!stream_class) {
                ret = -1;
                goto end;
        }
 
-       ret = (int64_t) stream_class->event_classes->len;
+       ret = (int) stream_class->event_classes->len;
 end:
        return ret;
 }
 
 struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class(
-               struct bt_ctf_stream_class *stream_class, size_t index)
+               struct bt_ctf_stream_class *stream_class, int index)
 {
        struct bt_ctf_event_class *event_class = NULL;
 
-       if (!stream_class || index >= stream_class->event_classes->len) {
+       if (!stream_class || index < 0 ||
+               index >= stream_class->event_classes->len) {
                goto end;
        }
 
@@ -285,13 +290,16 @@ int bt_ctf_stream_class_set_packet_context_type(
 {
        int ret = 0;
 
-       if (!stream_class || !packet_context_type) {
+       if (!stream_class || !packet_context_type || stream_class->frozen) {
                ret = -1;
                goto end;
        }
 
        assert(stream_class->packet_context_type);
-       if (bt_ctf_field_type_get_type_id(stream_class->packet_context_type) !=
+       if (stream_class->packet_context_type == packet_context_type) {
+               goto end;
+       }
+       if (bt_ctf_field_type_get_type_id(packet_context_type) !=
                CTF_TYPE_STRUCT) {
                /* A packet context must be a structure */
                ret = -1;
@@ -305,6 +313,92 @@ end:
        return ret;
 }
 
+struct bt_ctf_field_type *bt_ctf_stream_class_get_event_header_type(
+               struct bt_ctf_stream_class *stream_class)
+{
+       struct bt_ctf_field_type *ret = NULL;
+
+       if (!stream_class || !stream_class->event_header_type) {
+               goto end;
+       }
+
+       assert(stream_class->event_header_type);
+       bt_ctf_field_type_get(stream_class->event_header_type);
+       ret = stream_class->event_header_type;
+end:
+       return ret;
+}
+
+int bt_ctf_stream_class_set_event_header_type(
+               struct bt_ctf_stream_class *stream_class,
+               struct bt_ctf_field_type *event_header_type)
+{
+       int ret = 0;
+
+       if (!stream_class || !event_header_type || stream_class->frozen) {
+               ret = -1;
+               goto end;
+       }
+
+       assert(stream_class->event_header_type);
+       if (stream_class->event_header_type == event_header_type) {
+               goto end;
+       }
+       if (bt_ctf_field_type_get_type_id(event_header_type) !=
+               CTF_TYPE_STRUCT) {
+               /* An event header must be a structure */
+               ret = -1;
+               goto end;
+       }
+
+       bt_ctf_field_type_put(stream_class->event_header_type);
+       bt_ctf_field_type_get(event_header_type);
+       stream_class->event_header_type = event_header_type;
+end:
+       return ret;
+}
+
+struct bt_ctf_field_type *bt_ctf_stream_class_get_event_context_type(
+               struct bt_ctf_stream_class *stream_class)
+{
+       struct bt_ctf_field_type *ret = NULL;
+
+       if (!stream_class || !stream_class->event_context_type) {
+               goto end;
+       }
+
+       assert(stream_class->event_context_type);
+       bt_ctf_field_type_get(stream_class->event_context_type);
+       ret = stream_class->event_context_type;
+end:
+       return ret;
+}
+
+int bt_ctf_stream_class_set_event_context_type(
+               struct bt_ctf_stream_class *stream_class,
+               struct bt_ctf_field_type *event_context_type)
+{
+       int ret = 0;
+
+       if (!stream_class || !event_context_type || stream_class->frozen) {
+               ret = -1;
+               goto end;
+       }
+
+       if (bt_ctf_field_type_get_type_id(event_context_type) !=
+               CTF_TYPE_STRUCT) {
+               /* A packet context must be a structure */
+               ret = -1;
+               goto end;
+       }
+
+       bt_ctf_field_type_put(stream_class->event_context_type);
+       bt_ctf_field_type_get(event_context_type);
+       stream_class->event_context_type = event_context_type;
+end:
+       return ret;
+}
+
 void bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class)
 {
        if (!stream_class) {
@@ -326,15 +420,31 @@ void bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class)
 BT_HIDDEN
 void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class *stream_class)
 {
+       size_t i;
+
        if (!stream_class) {
                return;
        }
 
        stream_class->frozen = 1;
+       bt_ctf_field_type_freeze(stream_class->event_header_type);
        bt_ctf_field_type_freeze(stream_class->packet_context_type);
+       bt_ctf_field_type_freeze(stream_class->event_context_type);
        bt_ctf_clock_freeze(stream_class->clock);
-       g_ptr_array_foreach(stream_class->event_classes,
-               (GFunc)bt_ctf_event_class_freeze, NULL);
+
+       bt_ctf_field_type_set_native_byte_order(
+               stream_class->event_header_type, stream_class->byte_order);
+       bt_ctf_field_type_set_native_byte_order(
+               stream_class->packet_context_type, stream_class->byte_order);
+       bt_ctf_field_type_set_native_byte_order(
+               stream_class->event_context_type, stream_class->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));
+       }
 }
 
 BT_HIDDEN
@@ -342,11 +452,30 @@ int bt_ctf_stream_class_set_byte_order(struct bt_ctf_stream_class *stream_class,
        enum bt_ctf_byte_order byte_order)
 {
        int ret = 0;
+       int internal_byte_order;
 
-       ret = init_event_header(stream_class, byte_order);
-       if (ret) {
+       /* 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 ||
+               stream_class->frozen) {
+               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;
+       }
+
+       stream_class->byte_order = internal_byte_order;
 end:
        return ret;
 }
@@ -437,16 +566,15 @@ void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref)
        }
 
        bt_ctf_field_type_put(stream_class->event_header_type);
-       bt_ctf_field_put(stream_class->event_header);
        bt_ctf_field_type_put(stream_class->packet_context_type);
-       bt_ctf_field_type_put(stream_class->event_context_type);
-       bt_ctf_field_put(stream_class->event_context);
+       if (stream_class->event_context_type) {
+               bt_ctf_field_type_put(stream_class->event_context_type);
+       }
        g_free(stream_class);
 }
 
 static
-int init_event_header(struct bt_ctf_stream_class *stream_class,
-               enum bt_ctf_byte_order byte_order)
+int init_event_header(struct bt_ctf_stream_class *stream_class)
 {
        int ret = 0;
        struct bt_ctf_field_type *event_header_type =
@@ -461,16 +589,6 @@ int init_event_header(struct bt_ctf_stream_class *stream_class,
                goto end;
        }
 
-       ret = bt_ctf_field_type_set_byte_order(_uint32_t, byte_order);
-       if (ret) {
-               goto end;
-       }
-
-       ret = bt_ctf_field_type_set_byte_order(_uint64_t, byte_order);
-       if (ret) {
-               goto end;
-       }
-
        ret = bt_ctf_field_type_structure_add_field(event_header_type,
                _uint32_t, "id");
        if (ret) {
@@ -483,12 +601,10 @@ int init_event_header(struct bt_ctf_stream_class *stream_class,
                goto end;
        }
 
-       stream_class->event_header_type = event_header_type;
-       stream_class->event_header = bt_ctf_field_create(
-               stream_class->event_header_type);
-       if (!stream_class->event_header) {
-               ret = -1;
+       if (stream_class->event_header_type) {
+               bt_ctf_field_type_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);
@@ -500,8 +616,7 @@ end:
 }
 
 static
-int init_packet_context(struct bt_ctf_stream_class *stream_class,
-               enum bt_ctf_byte_order byte_order)
+int init_packet_context(struct bt_ctf_stream_class *stream_class)
 {
        int ret = 0;
        struct bt_ctf_field_type *packet_context_type =
@@ -518,11 +633,6 @@ int init_packet_context(struct bt_ctf_stream_class *stream_class,
         * We create a stream packet context as proposed in the CTF
         * specification.
         */
-       ret = bt_ctf_field_type_set_byte_order(_uint64_t, byte_order);
-       if (ret) {
-               goto end;
-       }
-
        ret = bt_ctf_field_type_structure_add_field(packet_context_type,
                _uint64_t, "timestamp_begin");
        if (ret) {
@@ -553,6 +663,9 @@ 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);
+       }
        stream_class->packet_context_type = packet_context_type;
 end:
        if (ret) {
This page took 0.030405 seconds and 4 git commands to generate.