sink.ctf.fs: initialize structure fields
authorSimon Marchi <simon.marchi@efficios.com>
Fri, 27 May 2022 15:38:44 +0000 (11:38 -0400)
committerSimon Marchi <simon.marchi@efficios.com>
Wed, 17 Apr 2024 17:57:53 +0000 (13:57 -0400)
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 <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/12239
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
src/plugins/ctf/fs-sink/fs-sink-stream.cpp
src/plugins/ctf/fs-sink/fs-sink-stream.hpp
src/plugins/ctf/fs-sink/fs-sink-trace.cpp
src/plugins/ctf/fs-sink/fs-sink-trace.hpp
src/plugins/ctf/fs-sink/fs-sink.cpp
src/plugins/ctf/fs-sink/fs-sink.hpp
src/plugins/ctf/fs-sink/translate-trace-ir-to-ctf-ir.cpp

index 6611744b5b9c1acec87b6140fcaa0a55e81a5681..9ab71b257aecd2c51d6a3845ba61482cc67a09c2 100644 (file)
@@ -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;
index 8d559c6a8bac95547cdacc06ed5316f5eaeae20d..8344666aa4bdc1e7ddeeecb57c0c3a28c4901f7b 100644 (file)
 
 #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;
 };
 
index 5466dd645a9b38274de43fcce3250d1f4e047226..295ec2bc936ee3326c022b7ecaac7b96c56ea87a 100644 (file)
@@ -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;
index 0da8b4b04955b2abd7df0f3216fd3009ae114b51..a85966ca84ee53d7b78f16bd580465d4acbc3b9e 100644 (file)
 
 #include <babeltrace2/babeltrace.h>
 
+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);
index 00084b796526e22527fd959651886565e9a782b8..39d4ff43848a0d7e2d9ddd3c94f0b66730e8bc3f 100644 (file)
@@ -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);
index 3c1e01282f323e2ef6cc397579afc76d0c734c92..70f9b5e9ae08d49cdb5758911e902b56fd414c42 100644 (file)
 
 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
index 96884a162af12a366ecafb8ea32f5e2ee238d059..0a8e28dfdefe41e9880549b39084ff12405e342d 100644 (file)
@@ -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;
This page took 0.031029 seconds and 4 git commands to generate.