]> sigrok.org Git - pulseview.git/blame - pv/data/segment.cpp
Free unused segment memory after acquisition
[pulseview.git] / pv / data / segment.cpp
CommitLineData
28a4c9c5 1/*
b3f22de0 2 * This file is part of the PulseView project.
28a4c9c5 3 *
26a883ed 4 * Copyright (C) 2017 Soeren Apel <soeren@apelpie.net>
28a4c9c5
JH
5 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
efdec55a 18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
28a4c9c5
JH
19 */
20
f3d66e52 21#include "segment.hpp"
28a4c9c5 22
f556bc6a
JH
23#include <assert.h>
24#include <stdlib.h>
25#include <string.h>
26
26a883ed
SA
27#include <vector>
28
3b68d03d
JH
29using std::lock_guard;
30using std::recursive_mutex;
26a883ed 31using std::vector;
7d29656f 32
51e77110 33namespace pv {
1b1ec774 34namespace data {
51e77110 35
f3d66e52 36Segment::Segment(uint64_t samplerate, unsigned int unit_size) :
8dbbc7f0 37 sample_count_(0),
7f4038d6 38 start_time_(0),
ff008de6 39 samplerate_(samplerate),
8dbbc7f0 40 unit_size_(unit_size)
f556bc6a 41{
8dbbc7f0
JH
42 lock_guard<recursive_mutex> lock(mutex_);
43 assert(unit_size_ > 0);
26a883ed
SA
44
45 // Determine the number of samples we can fit in one chunk
46 // without exceeding MaxChunkSize
47 chunk_size_ = std::min(MaxChunkSize,
48 (MaxChunkSize / unit_size_) * unit_size_);
49
50 // Create the initial chunk
51 current_chunk_ = new uint8_t[chunk_size_];
52 data_chunks_.push_back(current_chunk_);
53 used_samples_ = 0;
54 unused_samples_ = chunk_size_ / unit_size_;
f556bc6a
JH
55}
56
f3d66e52 57Segment::~Segment()
28a4c9c5 58{
8dbbc7f0 59 lock_guard<recursive_mutex> lock(mutex_);
26a883ed
SA
60
61 for (uint8_t* chunk : data_chunks_)
62 delete[] chunk;
28a4c9c5
JH
63}
64
f3d66e52 65uint64_t Segment::get_sample_count() const
28a4c9c5 66{
8dbbc7f0
JH
67 lock_guard<recursive_mutex> lock(mutex_);
68 return sample_count_;
f556bc6a
JH
69}
70
60d9b99a 71const pv::util::Timestamp& Segment::start_time() const
7f4038d6
JH
72{
73 return start_time_;
74}
75
f3d66e52 76double Segment::samplerate() const
ff008de6
JH
77{
78 return samplerate_;
79}
80
f3d66e52 81void Segment::set_samplerate(double samplerate)
ff008de6
JH
82{
83 samplerate_ = samplerate;
84}
85
f3d66e52 86unsigned int Segment::unit_size() const
6fd14a32 87{
8dbbc7f0 88 return unit_size_;
6fd14a32
JH
89}
90
5e6967cb
SA
91void Segment::free_unused_memory()
92{
93 lock_guard<recursive_mutex> lock(mutex_);
94
95 // No more data will come in, so re-create the last chunk accordingly
96 uint8_t* resized_chunk = new uint8_t[used_samples_ * unit_size_];
97 memcpy(resized_chunk, current_chunk_, used_samples_ * unit_size_);
98
99 delete[] current_chunk_;
100 current_chunk_ = resized_chunk;
101
102 data_chunks_.pop_back();
103 data_chunks_.push_back(resized_chunk);
104}
105
26a883ed
SA
106void Segment::append_single_sample(void *data)
107{
108 lock_guard<recursive_mutex> lock(mutex_);
109
110 // There will always be space for at least one sample in
111 // the current chunk, so we do not need to test for space
112
113 memcpy(current_chunk_ + (used_samples_ * unit_size_),
114 data, unit_size_);
115 used_samples_++;
116 unused_samples_--;
117
118 if (unused_samples_ == 0) {
119 current_chunk_ = new uint8_t[chunk_size_];
120 data_chunks_.push_back(current_chunk_);
121 used_samples_ = 0;
122 unused_samples_ = chunk_size_ / unit_size_;
123 }
124
125 sample_count_++;
126}
127
128void Segment::append_samples(void* data, uint64_t samples)
27d7c96b 129{
8dbbc7f0 130 lock_guard<recursive_mutex> lock(mutex_);
27d7c96b 131
26a883ed
SA
132 if (unused_samples_ >= samples) {
133 // All samples fit into the current chunk
134 memcpy(current_chunk_ + (used_samples_ * unit_size_),
135 data, (samples * unit_size_));
136 used_samples_ += samples;
137 unused_samples_ -= samples;
138 } else {
139 // Only a part of the samples fit, split data up between chunks
140 memcpy(current_chunk_ + (used_samples_ * unit_size_),
141 data, (unused_samples_ * unit_size_));
142 const uint64_t remaining_samples = samples - unused_samples_;
143
e7216ae0 144 // If we're out of memory, this will throw std::bad_alloc
26a883ed
SA
145 current_chunk_ = new uint8_t[chunk_size_];
146 data_chunks_.push_back(current_chunk_);
147 memcpy(current_chunk_, (uint8_t*)data + (unused_samples_ * unit_size_),
148 (remaining_samples * unit_size_));
149
150 used_samples_ = remaining_samples;
151 unused_samples_ = (chunk_size_ / unit_size_) - remaining_samples;
27d7c96b 152 }
26a883ed
SA
153
154 if (unused_samples_ == 0) {
155 // If we're out of memory, this will throw std::bad_alloc
156 current_chunk_ = new uint8_t[chunk_size_];
157 data_chunks_.push_back(current_chunk_);
158 used_samples_ = 0;
159 unused_samples_ = chunk_size_ / unit_size_;
160 }
161
162 sample_count_ += samples;
27d7c96b
DK
163}
164
26a883ed 165uint8_t* Segment::get_raw_samples(uint64_t start, uint64_t count) const
27d7c96b 166{
26a883ed
SA
167 assert(start < sample_count_);
168 assert(start + count <= sample_count_);
169 assert(count > 0);
170
8dbbc7f0 171 lock_guard<recursive_mutex> lock(mutex_);
26a883ed
SA
172
173 uint8_t* dest = new uint8_t[count * unit_size_];
174 uint8_t* dest_ptr = dest;
175
176 uint64_t chunk_num = (start * unit_size_) / chunk_size_;
177 uint64_t chunk_offs = (start * unit_size_) % chunk_size_;
178
179 while (count > 0) {
180 const uint8_t* chunk = data_chunks_[chunk_num];
181
182 uint64_t copy_size = std::min(count * unit_size_,
183 chunk_size_ - chunk_offs);
184
185 memcpy(dest_ptr, chunk + chunk_offs, copy_size);
186
187 dest_ptr += copy_size;
188 count -= (copy_size / unit_size_);
189
190 chunk_num++;
191 chunk_offs = 0;
192 }
193
194 return dest;
27d7c96b
DK
195}
196
26a883ed
SA
197SegmentRawDataIterator* Segment::begin_raw_sample_iteration(uint64_t start) const
198{
199 SegmentRawDataIterator* it = new SegmentRawDataIterator;
200
201 assert(start < sample_count_);
202
203 it->sample_index = start;
204 it->chunk_num = (start * unit_size_) / chunk_size_;
205 it->chunk_offs = (start * unit_size_) % chunk_size_;
206 it->chunk = data_chunks_[it->chunk_num];
207 it->value = it->chunk + it->chunk_offs;
208
209 return it;
210}
211
212void Segment::continue_raw_sample_iteration(SegmentRawDataIterator* it, uint64_t increase) const
f556bc6a 213{
8dbbc7f0 214 lock_guard<recursive_mutex> lock(mutex_);
27d7c96b 215
26a883ed
SA
216 if (it->sample_index > sample_count_)
217 {
218 // Fail gracefully if we are asked to deliver data we don't have
219 return;
220 } else {
221 it->sample_index += increase;
222 it->chunk_offs += (increase * unit_size_);
223 }
224
225 if (it->chunk_offs > (chunk_size_ - 1)) {
226 it->chunk_num++;
227 it->chunk_offs -= chunk_size_;
228 it->chunk = data_chunks_[it->chunk_num];
229 }
27d7c96b 230
26a883ed
SA
231 it->value = it->chunk + it->chunk_offs;
232}
27d7c96b 233
26a883ed
SA
234void Segment::end_raw_sample_iteration(SegmentRawDataIterator* it) const
235{
236 delete it;
28a4c9c5 237}
51e77110 238
26a883ed 239
1b1ec774 240} // namespace data
51e77110 241} // namespace pv