]> sigrok.org Git - pulseview.git/commitdiff
Free unused segment memory after acquisition
authorSoeren Apel <redacted>
Wed, 8 Feb 2017 19:33:48 +0000 (20:33 +0100)
committerSoeren Apel <redacted>
Wed, 8 Feb 2017 19:33:48 +0000 (20:33 +0100)
Segments allocate chunks of MaxChunkSize bytes each.
Most likely, the last allocated chunk isn't fully used,
so there's memory going to waste. This patch fixes this
by allocating a chunk of the required size that replaces
the last standard chunk.

pv/data/segment.cpp
pv/data/segment.hpp
pv/session.cpp
pv/session.hpp

index f635fc387a59dc4156273ca0dd4d88602ca35a27..483d97b13304b337801066ad84a0ae5dcdb57e62 100644 (file)
@@ -88,6 +88,21 @@ unsigned int Segment::unit_size() const
        return unit_size_;
 }
 
+void Segment::free_unused_memory()
+{
+       lock_guard<recursive_mutex> lock(mutex_);
+
+       // No more data will come in, so re-create the last chunk accordingly
+       uint8_t* resized_chunk = new uint8_t[used_samples_ * unit_size_];
+       memcpy(resized_chunk, current_chunk_, used_samples_ * unit_size_);
+
+       delete[] current_chunk_;
+       current_chunk_ = resized_chunk;
+
+       data_chunks_.pop_back();
+       data_chunks_.push_back(resized_chunk);
+}
+
 void Segment::append_single_sample(void *data)
 {
        lock_guard<recursive_mutex> lock(mutex_);
index 513b7dbd9412fdc03883e7c4991083551923df60..ba1db8e42cee554bc5778dba843dfd63713e9db1 100644 (file)
@@ -67,6 +67,8 @@ public:
 
        unsigned int unit_size() const;
 
+       void free_unused_memory();
+
 protected:
        void append_single_sample(void *data);
        void append_samples(void *data, uint64_t samples);
index 97741ebf7b944a4c6507bd1a4e64c1d8128a5d5b..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);
index 0f067a47be1f8e2875467c976cd4424e616c046b..bd403605eae33e80b120894419aa6359495f76de 100644 (file)
@@ -182,6 +182,8 @@ private:
 private:
        void sample_thread_proc(std::function<void (const QString)> error_handler);
 
+       void free_unused_memory();
+
        void feed_in_header();
 
        void feed_in_meta(std::shared_ptr<sigrok::Meta> meta);