From 150640e86f21b86c92aac2ac2d0cd8333a1005bd Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Fri, 27 May 2022 11:38:44 -0400 Subject: [PATCH] sink.ctf.fs: initialize structure fields In preparation to using some C++ objects as fields. Allocate with new and deallocate with delete where relevant. Change-Id: I924a6088b17e1d3ae4b8a239cf059fc3a7ecc8c6 Signed-off-by: Simon Marchi Reviewed-on: https://review.lttng.org/c/babeltrace/+/12239 Tested-by: jenkins Reviewed-by: Philippe Proulx --- src/plugins/ctf/fs-sink/fs-sink-stream.cpp | 8 +-- src/plugins/ctf/fs-sink/fs-sink-stream.hpp | 51 ++++++++++--------- src/plugins/ctf/fs-sink/fs-sink-trace.cpp | 8 +-- src/plugins/ctf/fs-sink/fs-sink-trace.hpp | 19 ++++--- src/plugins/ctf/fs-sink/fs-sink.cpp | 13 +---- src/plugins/ctf/fs-sink/fs-sink.hpp | 18 +++---- .../fs-sink/translate-trace-ir-to-ctf-ir.cpp | 13 +++-- 7 files changed, 59 insertions(+), 71 deletions(-) diff --git a/src/plugins/ctf/fs-sink/fs-sink-stream.cpp b/src/plugins/ctf/fs-sink/fs-sink-stream.cpp index 6611744b..9ab71b25 100644 --- a/src/plugins/ctf/fs-sink/fs-sink-stream.cpp +++ b/src/plugins/ctf/fs-sink/fs-sink-stream.cpp @@ -38,7 +38,7 @@ void fs_sink_stream_destroy(struct fs_sink_stream *stream) } bt_packet_put_ref(stream->packet_state.packet); - g_free(stream); + delete stream; end: return; @@ -124,14 +124,10 @@ static void set_stream_file_name(struct fs_sink_stream *stream) struct fs_sink_stream *fs_sink_stream_create(struct fs_sink_trace *trace, const bt_stream *ir_stream) { - struct fs_sink_stream *stream = g_new0(struct fs_sink_stream, 1); + fs_sink_stream *stream = new fs_sink_stream; int ret; GString *path = g_string_new(trace->path->str); - if (!stream) { - goto end; - } - stream->log_level = trace->log_level; stream->trace = trace; stream->ir_stream = ir_stream; diff --git a/src/plugins/ctf/fs-sink/fs-sink-stream.hpp b/src/plugins/ctf/fs-sink/fs-sink-stream.hpp index 8d559c6a..8344666a 100644 --- a/src/plugins/ctf/fs-sink/fs-sink-stream.hpp +++ b/src/plugins/ctf/fs-sink/fs-sink-stream.hpp @@ -14,19 +14,22 @@ #include "ctfser/ctfser.h" +struct fs_sink_trace; +struct fs_sink_ctf_stream_class; + struct fs_sink_stream { - bt_logging_level log_level; - struct fs_sink_trace *trace; - struct bt_ctfser ctfser; + bt_logging_level log_level = BT_LOGGING_LEVEL_NONE; + fs_sink_trace *trace = nullptr; + bt_ctfser ctfser {}; /* Stream's file name */ - GString *file_name; + GString *file_name = nullptr; /* Weak */ - const bt_stream *ir_stream; + const bt_stream *ir_stream = nullptr; - struct fs_sink_ctf_stream_class *sc; + fs_sink_ctf_stream_class *sc = nullptr; /* Current packet's state */ struct @@ -36,65 +39,65 @@ struct fs_sink_stream * packet (got a packet beginning message, but no * packet end message yet). */ - bool is_open; + bool is_open = false; /* * Current beginning default clock snapshot for the * current packet (`UINT64_C(-1)` if not set). */ - uint64_t beginning_cs; + uint64_t beginning_cs = 0; /* * Current end default clock snapshot for the current * packet (`UINT64_C(-1)` if not set). */ - uint64_t end_cs; + uint64_t end_cs = 0; /* * Current packet's content size (bits) for the current * packet. */ - uint64_t content_size; + uint64_t content_size = 0; /* * Current packet's total size (bits) for the current * packet. */ - uint64_t total_size; + uint64_t total_size = 0; /* * Discarded events (free running) counter for the * current packet. */ - uint64_t discarded_events_counter; + uint64_t discarded_events_counter = 0; /* Sequence number (free running) of the current packet */ - uint64_t seq_num; + uint64_t seq_num = 0; /* * Offset of the packet context structure within the * current packet (bits). */ - uint64_t context_offset_bits; + uint64_t context_offset_bits = 0; /* * Owned by this; `NULL` if the current packet is closed * or if the trace IR stream does not support packets. */ - const bt_packet *packet; + const bt_packet *packet = nullptr; } packet_state; /* Previous packet's state */ struct { /* End default clock snapshot (`UINT64_C(-1)` if not set) */ - uint64_t end_cs; + uint64_t end_cs = 0; /* Discarded events (free running) counter */ - uint64_t discarded_events_counter; + uint64_t discarded_events_counter = 0; /* Sequence number (free running) */ - uint64_t seq_num; + uint64_t seq_num = 0; } prev_packet_state; /* State to handle discarded events */ @@ -116,14 +119,14 @@ struct fs_sink_stream * * * Its end time is the current packet's end time. */ - bool in_range; + bool in_range = false; /* * Beginning and end times of the time range given by a * previously received discarded events message. */ - uint64_t beginning_cs; - uint64_t end_cs; + uint64_t beginning_cs = 0; + uint64_t end_cs = 0; } discarded_events_state; /* State to handle discarded packets */ @@ -146,14 +149,14 @@ struct fs_sink_stream * * Its end time is the current packet's beginning * time. */ - bool in_range; + bool in_range = false; /* * Beginning and end times of the time range given by a * previously received discarded packets message. */ - uint64_t beginning_cs; - uint64_t end_cs; + uint64_t beginning_cs = 0; + uint64_t end_cs = 0; } discarded_packets_state; }; diff --git a/src/plugins/ctf/fs-sink/fs-sink-trace.cpp b/src/plugins/ctf/fs-sink/fs-sink-trace.cpp index 5466dd64..295ec2bc 100644 --- a/src/plugins/ctf/fs-sink/fs-sink-trace.cpp +++ b/src/plugins/ctf/fs-sink/fs-sink-trace.cpp @@ -526,7 +526,7 @@ void fs_sink_trace_destroy(struct fs_sink_trace *trace) fs_sink_ctf_trace_destroy(trace->trace); trace->trace = NULL; - g_free(trace); + delete trace; g_string_free(tsdl, TRUE); @@ -550,13 +550,9 @@ static void ir_trace_destruction_listener(const bt_trace *ir_trace, void *data) struct fs_sink_trace *fs_sink_trace_create(struct fs_sink_comp *fs_sink, const bt_trace *ir_trace) { int ret; - struct fs_sink_trace *trace = g_new0(struct fs_sink_trace, 1); + fs_sink_trace *trace = new fs_sink_trace; bt_trace_add_listener_status trace_status; - if (!trace) { - goto end; - } - trace->log_level = fs_sink->log_level; trace->fs_sink = fs_sink; trace->ir_trace = ir_trace; diff --git a/src/plugins/ctf/fs-sink/fs-sink-trace.hpp b/src/plugins/ctf/fs-sink/fs-sink-trace.hpp index 0da8b4b0..a85966ca 100644 --- a/src/plugins/ctf/fs-sink/fs-sink-trace.hpp +++ b/src/plugins/ctf/fs-sink/fs-sink-trace.hpp @@ -11,13 +11,16 @@ #include +struct fs_sink_comp; +struct fs_sink_ctf_trace; + struct fs_sink_trace { - bt_logging_level log_level; - struct fs_sink_comp *fs_sink; + bt_logging_level log_level = BT_LOGGING_LEVEL_NONE; + fs_sink_comp *fs_sink = nullptr; /* Owned by this */ - struct fs_sink_ctf_trace *trace; + fs_sink_ctf_trace *trace = nullptr; /* * Weak reference: this object does not own it, and `trace` @@ -31,21 +34,21 @@ struct fs_sink_trace * could "leak" resources (memory, file descriptors) associated * to traces and streams which otherwise would not exist. */ - const bt_trace *ir_trace; + const bt_trace *ir_trace = nullptr; - bt_listener_id ir_trace_destruction_listener_id; + bt_listener_id ir_trace_destruction_listener_id = 0; /* Trace's directory */ - GString *path; + GString *path = nullptr; /* `metadata` file path */ - GString *metadata_path; + GString *metadata_path = nullptr; /* * Hash table of `const bt_stream *` (weak) to * `struct fs_sink_stream *` (owned by hash table). */ - GHashTable *streams; + GHashTable *streams = nullptr; }; struct fs_sink_trace *fs_sink_trace_create(struct fs_sink_comp *fs_sink, const bt_trace *ir_trace); diff --git a/src/plugins/ctf/fs-sink/fs-sink.cpp b/src/plugins/ctf/fs-sink/fs-sink.cpp index 00084b79..39d4ff43 100644 --- a/src/plugins/ctf/fs-sink/fs-sink.cpp +++ b/src/plugins/ctf/fs-sink/fs-sink.cpp @@ -126,7 +126,7 @@ static void destroy_fs_sink_comp(struct fs_sink_comp *fs_sink) } BT_MESSAGE_ITERATOR_PUT_REF_AND_RESET(fs_sink->upstream_iter); - g_free(fs_sink); + delete fs_sink; end: return; @@ -143,16 +143,7 @@ bt_component_class_initialize_method_status ctf_fs_sink_init(bt_self_component_s bt_logging_level log_level = bt_component_get_logging_level(bt_self_component_as_component(self_comp)); - fs_sink = g_new0(struct fs_sink_comp, 1); - if (!fs_sink) { - BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp, - "Failed to allocate one CTF FS sink structure."); - BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT( - self_comp, "Failed to allocate one CTF FS sink structure."); - status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; - goto end; - } - + fs_sink = new fs_sink_comp; fs_sink->log_level = log_level; fs_sink->self_comp = self_comp; fs_sink->output_dir_path = g_string_new(NULL); diff --git a/src/plugins/ctf/fs-sink/fs-sink.hpp b/src/plugins/ctf/fs-sink/fs-sink.hpp index 3c1e0128..70f9b5e9 100644 --- a/src/plugins/ctf/fs-sink/fs-sink.hpp +++ b/src/plugins/ctf/fs-sink/fs-sink.hpp @@ -13,14 +13,14 @@ struct fs_sink_comp { - bt_logging_level log_level; - bt_self_component *self_comp; + bt_logging_level log_level = BT_LOGGING_LEVEL_NONE; + bt_self_component *self_comp = nullptr; /* Owned by this */ - bt_message_iterator *upstream_iter; + bt_message_iterator *upstream_iter = nullptr; /* Base output directory path */ - GString *output_dir_path; + GString *output_dir_path = nullptr; /* * True if the component assumes that it will only write a @@ -28,25 +28,25 @@ struct fs_sink_comp * streams). This makes the component write the stream files * directly in the output directory (`output_dir_path` above). */ - bool assume_single_trace; + bool assume_single_trace = false; /* True to completely ignore discarded events messages */ - bool ignore_discarded_events; + bool ignore_discarded_events = false; /* True to completely ignore discarded packets messages */ - bool ignore_discarded_packets; + bool ignore_discarded_packets = false; /* * True to make the component quiet (nothing printed to the * standard output). */ - bool quiet; + bool quiet = false; /* * Hash table of `const bt_trace *` (weak) to * `struct fs_sink_trace *` (owned by hash table). */ - GHashTable *traces; + GHashTable *traces = nullptr; }; bt_component_class_initialize_method_status diff --git a/src/plugins/ctf/fs-sink/translate-trace-ir-to-ctf-ir.cpp b/src/plugins/ctf/fs-sink/translate-trace-ir-to-ctf-ir.cpp index 96884a16..0a8e28df 100644 --- a/src/plugins/ctf/fs-sink/translate-trace-ir-to-ctf-ir.cpp +++ b/src/plugins/ctf/fs-sink/translate-trace-ir-to-ctf-ir.cpp @@ -40,20 +40,20 @@ namespace sink { struct TraceIrToCtfIrCtx { - bt_logging_level log_level; - bt_self_component *self_comp; + bt_logging_level log_level = BT_LOGGING_LEVEL_NONE; + bt_self_component *self_comp = nullptr; /* Weak */ - struct fs_sink_ctf_stream_class *cur_sc; + struct fs_sink_ctf_stream_class *cur_sc = nullptr; /* Weak */ - struct fs_sink_ctf_event_class *cur_ec; + struct fs_sink_ctf_event_class *cur_ec = nullptr; - bt_field_path_scope cur_scope; + bt_field_path_scope cur_scope = BT_FIELD_PATH_SCOPE_PACKET_CONTEXT; /* * Array of `struct field_path_elem` */ - GArray *cur_path; + GArray *cur_path = nullptr; }; } /* namespace sink */ @@ -1472,7 +1472,6 @@ end: static inline void ctx_init(ctf::sink::TraceIrToCtfIrCtx *ctx, struct fs_sink_comp *fs_sink) { - memset(ctx, 0, sizeof(*ctx)); ctx->cur_path = g_array_new(FALSE, TRUE, sizeof(struct field_path_elem)); BT_ASSERT(ctx->cur_path); ctx->log_level = fs_sink->log_level; -- 2.34.1