From ef0c4a15a08fe244291de4ad483e936f1ae60d09 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Galarneau?= Date: Mon, 16 Mar 2015 15:43:20 -0400 Subject: [PATCH] Implement bt_ctf_trace stream class accessors MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérémie Galarneau --- formats/ctf/ir/trace.c | 131 ++++++++++++++++++++++-------- include/babeltrace/ctf-ir/trace.h | 36 +++++++- 2 files changed, 131 insertions(+), 36 deletions(-) diff --git a/formats/ctf/ir/trace.c b/formats/ctf/ir/trace.c index fb897dab..0cf3f173 100644 --- a/formats/ctf/ir/trace.c +++ b/formats/ctf/ir/trace.c @@ -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) { diff --git a/include/babeltrace/ctf-ir/trace.h b/include/babeltrace/ctf-ir/trace.h index c5a05147..03c281bc 100644 --- a/include/babeltrace/ctf-ir/trace.h +++ b/include/babeltrace/ctf-ir/trace.h @@ -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. * -- 2.34.1