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);
}
const std::pair<float, float> 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;
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);
}
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;
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<recursive_mutex> lock(mutex_);
assert(unit_size_ > 0);
{
lock_guard<recursive_mutex> 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_);
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_;
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<recursive_mutex> lock(mutex_);
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();
+ }
}
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<uint8_t*> data_chunks_;
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;