Free unused segment memory after acquisition
[pulseview.git] / pv / session.cpp
index fcd0e135b5a190972613e85ac79ada3f067b7023..42c581d420122fd126fc9e7fb7d51b11f3b34648 100644 (file)
@@ -857,6 +857,9 @@ void Session::sample_thread_proc(function<void (const QString)> error_handler)
                assert(0);
        }
 
+       // Optimize memory usage
+       free_unused_memory();
+
        // We now have unsaved data unless we just "captured" from a file
        shared_ptr<devices::File> file_device =
                dynamic_pointer_cast<devices::File>(device_);
@@ -868,6 +871,17 @@ void Session::sample_thread_proc(function<void (const QString)> error_handler)
                error_handler(tr("Out of memory, acquisition stopped."));
 }
 
+void Session::free_unused_memory()
+{
+       for (shared_ptr<data::SignalData> data : all_signal_data_) {
+               const vector< shared_ptr<data::Segment> > segments = data->segments();
+
+               for (shared_ptr<data::Segment> segment : segments) {
+                       segment->free_unused_memory();
+               }
+       }
+}
+
 void Session::feed_in_header()
 {
        cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE);
@@ -927,8 +941,6 @@ void Session::feed_in_logic(shared_ptr<Logic> logic)
 {
        lock_guard<recursive_mutex> lock(data_mutex_);
 
-       const size_t sample_count = logic->data_length() / logic->unit_size();
-
        if (!logic_data_) {
                // The only reason logic_data_ would not have been created is
                // if it was not possible to determine the signals when the
@@ -942,8 +954,7 @@ void Session::feed_in_logic(shared_ptr<Logic> logic)
 
                // Create a new data segment
                cur_logic_segment_ = shared_ptr<data::LogicSegment>(
-                       new data::LogicSegment(
-                               logic, cur_samplerate_, sample_count));
+                       new data::LogicSegment(logic, cur_samplerate_));
                logic_data_->push_segment(cur_logic_segment_);
 
                // @todo Putting this here means that only listeners querying
@@ -988,8 +999,7 @@ void Session::feed_in_analog(shared_ptr<Analog> analog)
 
                        // Create a segment, keep it in the maps of channels
                        segment = shared_ptr<data::AnalogSegment>(
-                               new data::AnalogSegment(
-                                       cur_samplerate_, sample_count));
+                               new data::AnalogSegment(cur_samplerate_));
                        cur_analog_segments_[channel] = segment;
 
                        // Find the analog data associated with the channel