Implement bt_ctf_trace stream class accessors
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Mon, 16 Mar 2015 19:43:20 +0000 (15:43 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Mon, 16 Mar 2015 19:43:35 +0000 (15:43 -0400)
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
formats/ctf/ir/trace.c
include/babeltrace/ctf-ir/trace.h

index fb897dabeea5e91ab804c9c1c4a06dcfde75dc21..0cf3f173a254dc8ab3ba16fce3b360d9d5c0cc6c 100644 (file)
@@ -155,47 +155,15 @@ struct bt_ctf_stream *bt_ctf_trace_create_stream(struct bt_ctf_trace *trace,
        }
 
        if (!stream_class_found) {
-               int64_t stream_id = bt_ctf_stream_class_get_id(stream_class);
-
-               if (stream_id < 0) {
-                       /* Try to assign a new stream id */
-                       if (_bt_ctf_stream_class_set_id(stream->stream_class,
-                               trace->next_stream_id++)) {
-                               goto error;
-                       }
-               }
-
-               for (i = 0; i < trace->stream_classes->len; i++) {
-                       if (stream_id == bt_ctf_stream_class_get_id(
-                                   trace->stream_classes->pdata[i])) {
-                               /* Duplicate stream id found */
-                               goto error;
-                       }
+               ret = bt_ctf_trace_add_stream_class(trace, stream_class);
+               if (ret) {
+                       goto error;
                }
-               bt_ctf_stream_class_get(stream->stream_class);
-               g_ptr_array_add(trace->stream_classes, stream->stream_class);
        }
 
        bt_ctf_stream_get(stream);
        g_ptr_array_add(trace->streams, stream);
 
-       /*
-        * Freeze the trace and its packet header.
-        *
-        * All field type byte orders set as "native" byte ordering can now be
-        * safely set to trace's own endianness, including the stream class'.
-        */
-       bt_ctf_field_type_set_native_byte_order(trace->packet_header_type,
-               trace->byte_order);
-       ret = bt_ctf_stream_class_set_byte_order(stream_class,
-               trace->byte_order == LITTLE_ENDIAN ?
-               BT_CTF_BYTE_ORDER_LITTLE_ENDIAN : BT_CTF_BYTE_ORDER_BIG_ENDIAN);
-       if (ret) {
-               goto error;
-       }
-
-       bt_ctf_stream_class_freeze(stream_class);
-       trace->frozen = 1;
        return stream;
 error:
        bt_ctf_stream_put(stream);
@@ -436,6 +404,99 @@ end:
        return clock;
 }
 
+int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
+               struct bt_ctf_stream_class *stream_class)
+{
+       int ret, i;
+       int64_t stream_id;
+
+       if (!trace || !stream_class) {
+               ret = -1;
+               goto end;
+       }
+
+       for (i = 0; i < trace->stream_classes->len; i++) {
+               if (trace->stream_classes->pdata[i] == stream_class) {
+                       ret = -1;
+                       goto end;
+               }
+       }
+
+       stream_id = bt_ctf_stream_class_get_id(stream_class);
+       if (stream_id < 0) {
+               stream_id = trace->next_stream_id++;
+
+               /* Try to assign a new stream id */
+               for (i = 0; i < trace->stream_classes->len; i++) {
+                       if (stream_id == bt_ctf_stream_class_get_id(
+                               trace->stream_classes->pdata[i])) {
+                               /* Duplicate stream id found */
+                               ret = -1;
+                               goto end;
+                       }
+               }
+
+               if (_bt_ctf_stream_class_set_id(stream_class,
+                       trace->next_stream_id++)) {
+                       /* TODO Should retry with a different stream id */
+                       ret = -1;
+                       goto end;
+               }
+       }
+
+       bt_ctf_stream_class_get(stream_class);
+       g_ptr_array_add(trace->stream_classes, stream_class);
+
+       /*
+        * Freeze the trace and its packet header.
+        *
+        * All field type byte orders set as "native" byte ordering can now be
+        * safely set to trace's own endianness, including the stream class'.
+        */
+       bt_ctf_field_type_set_native_byte_order(trace->packet_header_type,
+               trace->byte_order);
+       ret = bt_ctf_stream_class_set_byte_order(stream_class,
+               trace->byte_order == LITTLE_ENDIAN ?
+               BT_CTF_BYTE_ORDER_LITTLE_ENDIAN : BT_CTF_BYTE_ORDER_BIG_ENDIAN);
+       if (ret) {
+               goto end;
+       }
+       bt_ctf_stream_class_freeze(stream_class);
+       trace->frozen = 1;
+
+end:
+       return ret;
+}
+
+int bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace *trace)
+{
+       int ret;
+
+       if (!trace) {
+               ret = -1;
+               goto end;
+       }
+
+       ret = trace->stream_classes->len;
+end:
+       return ret;
+}
+
+struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class(
+               struct bt_ctf_trace *trace, int index)
+{
+       struct bt_ctf_stream_class *stream_class = NULL;
+
+       if (!trace || index < 0 || index >= trace->stream_classes->len) {
+               goto end;
+       }
+
+       stream_class = g_ptr_array_index(trace->stream_classes, index);
+       bt_ctf_stream_class_get(stream_class);
+end:
+       return stream_class;
+}
+
 BT_HIDDEN
 const char *get_byte_order_string(int byte_order)
 {
index c5a05147ab61deaa338c0bd9942f8c0b91b36706..03c281bcb21729b6074f3ce73ba7efd03c5e67bc 100644 (file)
@@ -200,7 +200,7 @@ extern int bt_ctf_trace_add_clock(struct bt_ctf_trace *trace,
 
 /*
  * bt_ctf_trace_get_clock_count: get the number of clocks
- *     associated to the trace.
+ *     associated with the trace.
  *
  * @param trace Trace instance.
  *
@@ -219,6 +219,40 @@ extern int bt_ctf_trace_get_clock_count(struct bt_ctf_trace *trace);
 extern struct bt_ctf_clock *bt_ctf_trace_get_clock(
                struct bt_ctf_trace *trace, int index);
 
+/*
+ * bt_ctf_trace_add_stream_class: add a stream_class to the trace.
+ *
+ * Add a stream class to the trace.
+ *
+ * @param trace Trace instance.
+ * @param stream_class Stream class to add to the trace.
+ *
+ * Returns 0 on success, a negative value on error.
+ */
+extern int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
+               struct bt_ctf_stream_class *stream_class);
+
+/*
+ * bt_ctf_trace_get_stream_class_count: get the number of stream classes
+ *     associated with the trace.
+ *
+ * @param trace Trace instance.
+ *
+ * Returns the stream class count on success, a negative value on error.
+ */
+extern int bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace *trace);
+
+/*
+ * bt_ctf_trace_get_stream_class: get a trace's stream class at index.
+ *
+ * @param trace Trace instance.
+ * @param index Index of the stream class in the given trace.
+ *
+ * Return a stream class on success, NULL on error.
+ */
+extern struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class(
+               struct bt_ctf_trace *trace, int index);
+
 /*
  * bt_ctf_trace_get_metadata_string: get metadata string.
  *
This page took 0.02723 seconds and 4 git commands to generate.