From: Soeren Apel Date: Wed, 8 Feb 2017 19:33:48 +0000 (+0100) Subject: Free unused segment memory after acquisition X-Git-Url: http://git.code-monkey.de/?p=pulseview.git;a=commitdiff_plain;h=5e6967cb2bcacbfb9e5b627becb6752621949998;hp=332c6dd8ebbbd24a5efdf3761f94a48deb9d721a 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 f635fc3..483d97b 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 513b7db..ba1db8e 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 97741eb..42c581d 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 0f067a4..bd40360 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);