From b71d729878967a4c0fb839fdea8d034cf90a1d1f Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Wed, 17 Feb 2016 18:57:04 -0500 Subject: [PATCH] ir: add optional name property to stream MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Philippe Proulx Signed-off-by: Jérémie Galarneau --- formats/ctf/ir/stream.c | 35 ++++++++++++++++++--- formats/ctf/writer/writer.c | 2 +- include/babeltrace/ctf-ir/stream-internal.h | 1 + include/babeltrace/ctf-ir/stream.h | 5 ++- tests/lib/test_ctf_writer.c | 9 ++++-- 5 files changed, 42 insertions(+), 10 deletions(-) diff --git a/formats/ctf/ir/stream.c b/formats/ctf/ir/stream.c index b3081de4..f2706169 100644 --- a/formats/ctf/ir/stream.c +++ b/formats/ctf/ir/stream.c @@ -299,7 +299,8 @@ end: } struct bt_ctf_stream *bt_ctf_stream_create( - struct bt_ctf_stream_class *stream_class) + struct bt_ctf_stream_class *stream_class, + const char *name) { int ret; struct bt_ctf_stream *stream = NULL; @@ -307,17 +308,17 @@ struct bt_ctf_stream *bt_ctf_stream_create( struct bt_ctf_writer *writer = NULL; if (!stream_class) { - goto end; + goto error; } trace = bt_ctf_stream_class_get_trace(stream_class); if (!trace) { - goto end; + goto error; } stream = g_new0(struct bt_ctf_stream, 1); if (!stream) { - goto end; + goto error; } bt_object_init(stream, bt_ctf_stream_destroy); @@ -330,6 +331,13 @@ struct bt_ctf_stream *bt_ctf_stream_create( stream->stream_class = stream_class; stream->pos.fd = -1; + if (name) { + stream->name = g_string_new(name); + if (!stream->name) { + goto error; + } + } + if (trace->is_created_by_writer) { int fd; writer = (struct bt_ctf_writer *) @@ -400,7 +408,6 @@ struct bt_ctf_stream *bt_ctf_stream_create( /* Add this stream to the trace's streams */ g_ptr_array_add(trace->streams, stream); -end: BT_PUT(trace); BT_PUT(writer); return stream; @@ -906,6 +913,10 @@ void bt_ctf_stream_destroy(struct bt_object *obj) if (stream->events) { g_ptr_array_free(stream->events, TRUE); } + + if (stream->name) { + g_string_free(stream->name, TRUE); + } bt_put(stream->packet_header); bt_put(stream->packet_context); bt_put(stream->event_context); @@ -961,3 +972,17 @@ end: bt_put(field_type); return ret; } + +const char *bt_ctf_stream_get_name(struct bt_ctf_stream *stream) +{ + const char *name = NULL; + + if (!stream) { + goto end; + } + + name = stream->name ? stream->name->str : NULL; + +end: + return name; +} diff --git a/formats/ctf/writer/writer.c b/formats/ctf/writer/writer.c index fe6f52cb..6c294931 100644 --- a/formats/ctf/writer/writer.c +++ b/formats/ctf/writer/writer.c @@ -181,7 +181,7 @@ struct bt_ctf_stream *bt_ctf_writer_create_stream(struct bt_ctf_writer *writer, } } - stream = bt_ctf_stream_create(stream_class); + stream = bt_ctf_stream_create(stream_class, NULL); if (!stream) { goto error; } diff --git a/include/babeltrace/ctf-ir/stream-internal.h b/include/babeltrace/ctf-ir/stream-internal.h index 15579404..5ffe815e 100644 --- a/include/babeltrace/ctf-ir/stream-internal.h +++ b/include/babeltrace/ctf-ir/stream-internal.h @@ -44,6 +44,7 @@ struct bt_ctf_stream { GPtrArray *events; struct ctf_stream_pos pos; unsigned int flushed_packet_count; + GString *name; struct bt_ctf_field *packet_header; struct bt_ctf_field *packet_context; struct bt_ctf_field *event_header; diff --git a/include/babeltrace/ctf-ir/stream.h b/include/babeltrace/ctf-ir/stream.h index 53f02da6..a08e327f 100644 --- a/include/babeltrace/ctf-ir/stream.h +++ b/include/babeltrace/ctf-ir/stream.h @@ -41,7 +41,10 @@ struct bt_ctf_event; struct bt_ctf_stream; extern struct bt_ctf_stream *bt_ctf_stream_create( - struct bt_ctf_stream_class *stream_class); + struct bt_ctf_stream_class *stream_class, + const char *name); + +extern const char *bt_ctf_stream_get_name(struct bt_ctf_stream *stream); /* * bt_ctf_stream_get_stream_class: get a stream's class. diff --git a/tests/lib/test_ctf_writer.c b/tests/lib/test_ctf_writer.c index 53a5a41d..a2fbd2e4 100644 --- a/tests/lib/test_ctf_writer.c +++ b/tests/lib/test_ctf_writer.c @@ -58,7 +58,7 @@ #define DEFAULT_CLOCK_TIME 0 #define DEFAULT_CLOCK_VALUE 0 -#define NR_TESTS 589 +#define NR_TESTS 590 static int64_t current_time = 42; @@ -2670,6 +2670,7 @@ void test_create_writer_stream_from_stream_class(void) { int ret; char trace_path[] = "/tmp/ctfwriter_XXXXXX"; + const char *writer_stream_name = "writer stream instance"; struct bt_ctf_writer *writer = NULL; struct bt_ctf_trace *writer_trace = NULL; struct bt_ctf_stream_class *writer_sc = NULL; @@ -2702,8 +2703,10 @@ void test_create_writer_stream_from_stream_class(void) assert(!ret); ret = bt_ctf_trace_add_stream_class(writer_trace, writer_sc); assert(!ret); - writer_stream = bt_ctf_stream_create(writer_sc); + writer_stream = bt_ctf_stream_create(writer_sc, writer_stream_name); assert(writer_stream); + ok(!strcmp(bt_ctf_stream_get_name(writer_stream), writer_stream_name), + "bt_ctf_stream_get_name() returns the stream's name"); /* Create non-writer trace, stream class, and stream */ non_writer_trace = bt_ctf_trace_create(); @@ -2715,7 +2718,7 @@ void test_create_writer_stream_from_stream_class(void) assert(!ret); ret = bt_ctf_trace_add_stream_class(non_writer_trace, non_writer_sc); assert(!ret); - non_writer_stream = bt_ctf_stream_create(non_writer_sc); + non_writer_stream = bt_ctf_stream_create(non_writer_sc, NULL); assert(non_writer_stream); /* Create event class and event */ -- 2.34.1