From: Soeren Apel Date: Sun, 19 Feb 2017 20:19:11 +0000 (+0100) Subject: Segment: Do not alter chunks when there are active iterators X-Git-Tag: pulseview-0.4.0~183 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=c70e34649be658e7a443d5e68abe16dd55d53bf2;p=pulseview.git Segment: Do not alter chunks when there are active iterators --- diff --git a/pv/data/analogsegment.cpp b/pv/data/analogsegment.cpp index 38db0f12..b3f310a1 100644 --- a/pv/data/analogsegment.cpp +++ b/pv/data/analogsegment.cpp @@ -106,17 +106,17 @@ const std::pair AnalogSegment::get_min_max() const return std::make_pair(min_value_, max_value_); } -SegmentAnalogDataIterator* AnalogSegment::begin_sample_iteration(uint64_t start) const +SegmentAnalogDataIterator* AnalogSegment::begin_sample_iteration(uint64_t start) { return (SegmentAnalogDataIterator*)begin_raw_sample_iteration(start); } -void AnalogSegment::continue_sample_iteration(SegmentAnalogDataIterator* it, uint64_t increase) const +void AnalogSegment::continue_sample_iteration(SegmentAnalogDataIterator* it, uint64_t increase) { Segment::continue_raw_sample_iteration((SegmentRawDataIterator*)it, increase); } -void AnalogSegment::end_sample_iteration(SegmentAnalogDataIterator* it) const +void AnalogSegment::end_sample_iteration(SegmentAnalogDataIterator* it) { Segment::end_raw_sample_iteration((SegmentRawDataIterator*)it); } diff --git a/pv/data/analogsegment.hpp b/pv/data/analogsegment.hpp index e6c312ad..27c9863b 100644 --- a/pv/data/analogsegment.hpp +++ b/pv/data/analogsegment.hpp @@ -89,9 +89,9 @@ public: const std::pair get_min_max() const; - SegmentAnalogDataIterator* begin_sample_iteration(uint64_t start) const; - void continue_sample_iteration(SegmentAnalogDataIterator* it, uint64_t increase) const; - void end_sample_iteration(SegmentAnalogDataIterator* it) const; + SegmentAnalogDataIterator* begin_sample_iteration(uint64_t start); + void continue_sample_iteration(SegmentAnalogDataIterator* it, uint64_t increase); + void end_sample_iteration(SegmentAnalogDataIterator* it); void get_envelope_section(EnvelopeSection &s, uint64_t start, uint64_t end, float min_length) const; diff --git a/pv/data/logicsegment.cpp b/pv/data/logicsegment.cpp index ddcf308d..61382e6f 100644 --- a/pv/data/logicsegment.cpp +++ b/pv/data/logicsegment.cpp @@ -175,17 +175,17 @@ const uint8_t* LogicSegment::get_samples(int64_t start_sample, return get_raw_samples(start_sample, (end_sample-start_sample)); } -SegmentLogicDataIterator* LogicSegment::begin_sample_iteration(uint64_t start) const +SegmentLogicDataIterator* LogicSegment::begin_sample_iteration(uint64_t start) { return (SegmentLogicDataIterator*)begin_raw_sample_iteration(start); } -void LogicSegment::continue_sample_iteration(SegmentLogicDataIterator* it, uint64_t increase) const +void LogicSegment::continue_sample_iteration(SegmentLogicDataIterator* it, uint64_t increase) { Segment::continue_raw_sample_iteration((SegmentRawDataIterator*)it, increase); } -void LogicSegment::end_sample_iteration(SegmentLogicDataIterator* it) const +void LogicSegment::end_sample_iteration(SegmentLogicDataIterator* it) { Segment::end_raw_sample_iteration((SegmentRawDataIterator*)it); } diff --git a/pv/data/logicsegment.hpp b/pv/data/logicsegment.hpp index a7a38ebe..c450f0a9 100644 --- a/pv/data/logicsegment.hpp +++ b/pv/data/logicsegment.hpp @@ -81,9 +81,9 @@ public: const uint8_t* get_samples(int64_t start_sample, int64_t end_sample) const; - SegmentLogicDataIterator* begin_sample_iteration(uint64_t start) const; - void continue_sample_iteration(SegmentLogicDataIterator* it, uint64_t increase) const; - void end_sample_iteration(SegmentLogicDataIterator* it) const; + SegmentLogicDataIterator* begin_sample_iteration(uint64_t start); + void continue_sample_iteration(SegmentLogicDataIterator* it, uint64_t increase); + void end_sample_iteration(SegmentLogicDataIterator* it); private: uint64_t unpack_sample(const uint8_t *ptr) const; diff --git a/pv/data/segment.cpp b/pv/data/segment.cpp index cfe9aafa..64d9cd66 100644 --- a/pv/data/segment.cpp +++ b/pv/data/segment.cpp @@ -39,7 +39,9 @@ Segment::Segment(uint64_t samplerate, unsigned int unit_size) : sample_count_(0), start_time_(0), samplerate_(samplerate), - unit_size_(unit_size) + unit_size_(unit_size), + iterator_count_(0), + mem_optimization_requested_(false) { lock_guard lock(mutex_); assert(unit_size_ > 0); @@ -94,6 +96,12 @@ void Segment::free_unused_memory() { lock_guard lock(mutex_); + // Do not mess with the data chunks if we have iterators pointing at them + if (iterator_count_ > 0) { + mem_optimization_requested_ = true; + return; + } + // 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_); @@ -196,12 +204,14 @@ uint8_t* Segment::get_raw_samples(uint64_t start, uint64_t count) const return dest; } -SegmentRawDataIterator* Segment::begin_raw_sample_iteration(uint64_t start) const +SegmentRawDataIterator* Segment::begin_raw_sample_iteration(uint64_t start) { SegmentRawDataIterator* it = new SegmentRawDataIterator; assert(start < sample_count_); + iterator_count_++; + it->sample_index = start; it->chunk_num = (start * unit_size_) / chunk_size_; it->chunk_offs = (start * unit_size_) % chunk_size_; @@ -211,7 +221,7 @@ SegmentRawDataIterator* Segment::begin_raw_sample_iteration(uint64_t start) cons return it; } -void Segment::continue_raw_sample_iteration(SegmentRawDataIterator* it, uint64_t increase) const +void Segment::continue_raw_sample_iteration(SegmentRawDataIterator* it, uint64_t increase) { lock_guard lock(mutex_); @@ -233,9 +243,16 @@ void Segment::continue_raw_sample_iteration(SegmentRawDataIterator* it, uint64_t it->value = it->chunk + it->chunk_offs; } -void Segment::end_raw_sample_iteration(SegmentRawDataIterator* it) const +void Segment::end_raw_sample_iteration(SegmentRawDataIterator* it) { delete it; + + iterator_count_--; + + if ((iterator_count_ == 0) && mem_optimization_requested_) { + mem_optimization_requested_ = false; + free_unused_memory(); + } } diff --git a/pv/data/segment.hpp b/pv/data/segment.hpp index 14122d6a..f1ff0f52 100644 --- a/pv/data/segment.hpp +++ b/pv/data/segment.hpp @@ -74,9 +74,9 @@ protected: void append_samples(void *data, uint64_t samples); uint8_t* get_raw_samples(uint64_t start, uint64_t count) const; - SegmentRawDataIterator* begin_raw_sample_iteration(uint64_t start) const; - void continue_raw_sample_iteration(SegmentRawDataIterator* it, uint64_t increase) const; - void end_raw_sample_iteration(SegmentRawDataIterator* it) const; + SegmentRawDataIterator* begin_raw_sample_iteration(uint64_t start); + void continue_raw_sample_iteration(SegmentRawDataIterator* it, uint64_t increase); + void end_raw_sample_iteration(SegmentRawDataIterator* it); mutable std::recursive_mutex mutex_; std::vector data_chunks_; @@ -87,6 +87,8 @@ protected: double samplerate_; uint64_t chunk_size_; unsigned int unit_size_; + int iterator_count_; + bool mem_optimization_requested_; friend struct SegmentTest::SmallSize8Single; friend struct SegmentTest::MediumSize8Single;