From: Soeren Apel Date: Wed, 8 Feb 2017 19:33:48 +0000 (+0100) Subject: Free unused segment memory after acquisition X-Git-Tag: pulseview-0.4.0~198 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=5e6967cb2bcacbfb9e5b627becb6752621949998;p=pulseview.git Free unused segment memory after acquisition 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. --- diff --git a/pv/data/segment.cpp b/pv/data/segment.cpp index f635fc38..483d97b1 100644 --- a/pv/data/segment.cpp +++ b/pv/data/segment.cpp @@ -88,6 +88,21 @@ unsigned int Segment::unit_size() const return unit_size_; } +void Segment::free_unused_memory() +{ + lock_guard 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 lock(mutex_); diff --git a/pv/data/segment.hpp b/pv/data/segment.hpp index 513b7dbd..ba1db8e4 100644 --- a/pv/data/segment.hpp +++ b/pv/data/segment.hpp @@ -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); diff --git a/pv/session.cpp b/pv/session.cpp index 97741ebf..42c581d4 100644 --- a/pv/session.cpp +++ b/pv/session.cpp @@ -857,6 +857,9 @@ void Session::sample_thread_proc(function error_handler) assert(0); } + // Optimize memory usage + free_unused_memory(); + // We now have unsaved data unless we just "captured" from a file shared_ptr file_device = dynamic_pointer_cast(device_); @@ -868,6 +871,17 @@ void Session::sample_thread_proc(function error_handler) error_handler(tr("Out of memory, acquisition stopped.")); } +void Session::free_unused_memory() +{ + for (shared_ptr data : all_signal_data_) { + const vector< shared_ptr > segments = data->segments(); + + for (shared_ptr segment : segments) { + segment->free_unused_memory(); + } + } +} + void Session::feed_in_header() { cur_samplerate_ = device_->read_config(ConfigKey::SAMPLERATE); diff --git a/pv/session.hpp b/pv/session.hpp index 0f067a47..bd403605 100644 --- a/pv/session.hpp +++ b/pv/session.hpp @@ -182,6 +182,8 @@ private: private: void sample_thread_proc(std::function error_handler); + void free_unused_memory(); + void feed_in_header(); void feed_in_meta(std::shared_ptr meta);