From 067bb62415847791709f4c3cad8bb252a63f45f8 Mon Sep 17 00:00:00 2001 From: Uwe Hermann Date: Fri, 10 Mar 2017 20:26:31 +0100 Subject: [PATCH 1/1] Prefer std::make_shared(). This patch was generated using clang-tidy: clang-tidy -checks="-*,modernize-make-shared" -fix (with some additional manual fixups) Using make_shared() over manual construction has multiple advantages: - It's shorter to write and easier to read. V1: auto sb = shared_ptr(new Foo()); V2: auto sb = make_shared(); - The type "Foo" is repeated less often (less code duplication, lower risk of forgetting to update one of the "Foo"s upon copy-paste etc.) - Manual construction leads to two individual allocations (actual data and the control block of the shared_ptr). Using make_shared() will only lead to one allocation, which has performance, cache-locality and memory consumption benefits. - It's exception-safe, whereas manual construction is not necessarily: V1: func(shared_ptr(new Foo()), shared_ptr(new Foo())); V2: func(make_shared(), make_shared()); In "V1", one of the "new" invocations could throw, potentially causing memory leaks. No leaks will happen with make_shared(). --- pv/data/decoderstack.cpp | 4 ++-- pv/popups/channels.cpp | 3 ++- pv/session.cpp | 22 ++++++++++------------ pv/view/decodetrace.cpp | 4 ++-- pv/view/view.cpp | 7 +++---- 5 files changed, 19 insertions(+), 21 deletions(-) diff --git a/pv/data/decoderstack.cpp b/pv/data/decoderstack.cpp index c216d8d2..11b2b70b 100644 --- a/pv/data/decoderstack.cpp +++ b/pv/data/decoderstack.cpp @@ -44,6 +44,7 @@ using std::list; using std::map; using std::pair; using std::shared_ptr; +using std::make_shared; using std::vector; using namespace pv::data::decode; @@ -74,8 +75,7 @@ DecoderStack::DecoderStack(pv::Session &session, connect(&session_, SIGNAL(frame_ended()), this, SLOT(on_frame_ended())); - stack_.push_back(shared_ptr( - new decode::Decoder(dec))); + stack_.push_back(make_shared(dec)); } DecoderStack::~DecoderStack() diff --git a/pv/popups/channels.cpp b/pv/popups/channels.cpp index 89080c4f..9c200b9d 100644 --- a/pv/popups/channels.cpp +++ b/pv/popups/channels.cpp @@ -51,6 +51,7 @@ using std::map; using std::mutex; using std::set; using std::shared_ptr; +using std::make_shared; using std::unordered_set; using std::vector; @@ -165,7 +166,7 @@ void Channels::populate_group(shared_ptr group, // popup. shared_ptr binding; if (group) - binding = shared_ptr(new Device(group)); + binding = make_shared(group); // Create a title if the group is going to have any content if ((!sigs.empty() || (binding && !binding->properties().empty())) && diff --git a/pv/session.cpp b/pv/session.cpp index 31cbb73c..ef2f39bb 100644 --- a/pv/session.cpp +++ b/pv/session.cpp @@ -76,6 +76,7 @@ using std::pair; using std::recursive_mutex; using std::set; using std::shared_ptr; +using std::make_shared; using std::string; using std::unordered_set; using std::vector; @@ -307,7 +308,7 @@ void Session::restore_settings(QSettings &settings) settings.endGroup(); if (QFileInfo(filename).isReadable()) { - device = std::make_shared(device_manager_.context(), + device = make_shared(device_manager_.context(), filename.toStdString()); set_device(device); @@ -617,8 +618,7 @@ bool Session::add_decoder(srd_decoder *const dec) try { // Create the decoder - decoder_stack = shared_ptr( - new data::DecoderStack(*this, dec)); + decoder_stack = make_shared(*this, dec); // Make a list of all the channels std::vector all_channels; @@ -644,7 +644,7 @@ bool Session::add_decoder(srd_decoder *const dec) // Create the decode signal shared_ptr signalbase = - shared_ptr(new data::SignalBase(nullptr)); + make_shared(nullptr); signalbase->set_decoder_stack(decoder_stack); signalbases_.insert(signalbase); @@ -772,8 +772,7 @@ void Session::update_signals() switch(channel->type()->id()) { case SR_CHANNEL_LOGIC: if (!signalbase) { - signalbase = shared_ptr( - new data::SignalBase(channel)); + signalbase = make_shared(channel); signalbases_.insert(signalbase); all_signal_data_.insert(logic_data_); @@ -789,8 +788,7 @@ void Session::update_signals() case SR_CHANNEL_ANALOG: { if (!signalbase) { - signalbase = shared_ptr( - new data::SignalBase(channel)); + signalbase = make_shared(channel); signalbases_.insert(signalbase); shared_ptr data(new data::Analog()); @@ -954,8 +952,8 @@ void Session::feed_in_logic(shared_ptr logic) set_capture_state(Running); // Create a new data segment - cur_logic_segment_ = shared_ptr( - new data::LogicSegment(*logic_data_, logic, cur_samplerate_)); + cur_logic_segment_ = make_shared( + *logic_data_, logic, cur_samplerate_); logic_data_->push_segment(cur_logic_segment_); // @todo Putting this here means that only listeners querying @@ -1006,8 +1004,8 @@ void Session::feed_in_analog(shared_ptr analog) assert(data); // Create a segment, keep it in the maps of channels - segment = shared_ptr( - new data::AnalogSegment(*data, cur_samplerate_)); + segment = make_shared( + *data, cur_samplerate_); cur_analog_segments_[channel] = segment; // Push the segment into the analog data. diff --git a/pv/view/decodetrace.cpp b/pv/view/decodetrace.cpp index c5fa6e91..164c7e5a 100644 --- a/pv/view/decodetrace.cpp +++ b/pv/view/decodetrace.cpp @@ -66,6 +66,7 @@ using std::map; using std::min; using std::pair; using std::shared_ptr; +using std::make_shared; using std::tie; using std::unordered_set; using std::vector; @@ -961,8 +962,7 @@ void DecodeTrace::on_stack_decoder(srd_decoder *decoder) assert(decoder); assert(decoder_stack); - decoder_stack->push(shared_ptr( - new data::decode::Decoder(decoder))); + decoder_stack->push(make_shared(decoder)); decoder_stack->begin_decode(); create_popup_form(); diff --git a/pv/view/view.cpp b/pv/view/view.cpp index 23de2087..f46045ad 100644 --- a/pv/view/view.cpp +++ b/pv/view/view.cpp @@ -627,8 +627,8 @@ std::shared_ptr View::cursors() const void View::add_flag(const Timestamp& time) { - flags_.push_back(shared_ptr(new Flag(*this, time, - QString("%1").arg(next_flag_text_)))); + flags_.push_back(make_shared(*this, time, + QString("%1").arg(next_flag_text_))); next_flag_text_ = (next_flag_text_ >= 'Z') ? 'A' : (next_flag_text_ + 1); @@ -688,8 +688,7 @@ void View::restack_all_trace_tree_items() void View::trigger_event(util::Timestamp location) { - trigger_markers_.push_back(shared_ptr( - new TriggerMarker(*this, location))); + trigger_markers_.push_back(make_shared(*this, location)); } void View::get_scroll_layout(double &length, Timestamp &offset) const -- 2.30.2