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.
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_);
unsigned int unit_size() const;
+ void free_unused_memory();
+
protected:
void append_single_sample(void *data);
void append_samples(void *data, uint64_t samples);
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_);
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);
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);