]> sigrok.org Git - pulseview.git/blame - pv/data/segment.cpp
LogicSegment: Remove constructor requiring sigrok::Logic
[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
eb8269e3
UH
23#include <cassert>
24#include <cstdlib>
25#include <cstring>
26a883ed
SA
26#include <vector>
27
3b68d03d 28using std::lock_guard;
6f925ba9 29using std::min;
3b68d03d 30using std::recursive_mutex;
26a883ed 31using std::vector;
7d29656f 32
51e77110 33namespace pv {
1b1ec774 34namespace data {
51e77110 35
dd3f9a41
SA
36const uint64_t Segment::MaxChunkSize = 10*1024*1024; /* 10MiB */
37
f3d66e52 38Segment::Segment(uint64_t samplerate, unsigned int unit_size) :
8dbbc7f0 39 sample_count_(0),
7f4038d6 40 start_time_(0),
ff008de6 41 samplerate_(samplerate),
c70e3464
SA
42 unit_size_(unit_size),
43 iterator_count_(0),
44 mem_optimization_requested_(false)
f556bc6a 45{
8dbbc7f0
JH
46 lock_guard<recursive_mutex> lock(mutex_);
47 assert(unit_size_ > 0);
26a883ed
SA
48
49 // Determine the number of samples we can fit in one chunk
50 // without exceeding MaxChunkSize
6f925ba9 51 chunk_size_ = min(MaxChunkSize, (MaxChunkSize / unit_size_) * unit_size_);
26a883ed
SA
52
53 // Create the initial chunk
54 current_chunk_ = new uint8_t[chunk_size_];
55 data_chunks_.push_back(current_chunk_);
56 used_samples_ = 0;
57 unused_samples_ = chunk_size_ / unit_size_;
f556bc6a
JH
58}
59
f3d66e52 60Segment::~Segment()
28a4c9c5 61{
8dbbc7f0 62 lock_guard<recursive_mutex> lock(mutex_);
26a883ed
SA
63
64 for (uint8_t* chunk : data_chunks_)
65 delete[] chunk;
28a4c9c5
JH
66}
67
f3d66e52 68uint64_t Segment::get_sample_count() const
28a4c9c5 69{
8dbbc7f0
JH
70 lock_guard<recursive_mutex> lock(mutex_);
71 return sample_count_;
f556bc6a
JH
72}
73
60d9b99a 74const pv::util::Timestamp& Segment::start_time() const
7f4038d6
JH
75{
76 return start_time_;
77}
78
f3d66e52 79double Segment::samplerate() const
ff008de6
JH
80{
81 return samplerate_;
82}
83
f3d66e52 84void Segment::set_samplerate(double samplerate)
ff008de6
JH
85{
86 samplerate_ = samplerate;
87}
88
f3d66e52 89unsigned int Segment::unit_size() const
6fd14a32 90{
8dbbc7f0 91 return unit_size_;
6fd14a32
JH
92}
93
5e6967cb
SA
94void Segment::free_unused_memory()
95{
96 lock_guard<recursive_mutex> lock(mutex_);
97
c70e3464
SA
98 // Do not mess with the data chunks if we have iterators pointing at them
99 if (iterator_count_ > 0) {
100 mem_optimization_requested_ = true;
101 return;
102 }
103
5e6967cb
SA
104 // No more data will come in, so re-create the last chunk accordingly
105 uint8_t* resized_chunk = new uint8_t[used_samples_ * unit_size_];
106 memcpy(resized_chunk, current_chunk_, used_samples_ * unit_size_);
107
108 delete[] current_chunk_;
109 current_chunk_ = resized_chunk;
110
111 data_chunks_.pop_back();
112 data_chunks_.push_back(resized_chunk);
113}
114
26a883ed
SA
115void Segment::append_single_sample(void *data)
116{
117 lock_guard<recursive_mutex> lock(mutex_);
118
119 // There will always be space for at least one sample in
120 // the current chunk, so we do not need to test for space
121
122 memcpy(current_chunk_ + (used_samples_ * unit_size_),
123 data, unit_size_);
124 used_samples_++;
125 unused_samples_--;
126
127 if (unused_samples_ == 0) {
128 current_chunk_ = new uint8_t[chunk_size_];
129 data_chunks_.push_back(current_chunk_);
130 used_samples_ = 0;
131 unused_samples_ = chunk_size_ / unit_size_;
132 }
133
134 sample_count_++;
135}
136
137void Segment::append_samples(void* data, uint64_t samples)
27d7c96b 138{
8dbbc7f0 139 lock_guard<recursive_mutex> lock(mutex_);
27d7c96b 140
26a883ed
SA
141 if (unused_samples_ >= samples) {
142 // All samples fit into the current chunk
143 memcpy(current_chunk_ + (used_samples_ * unit_size_),
144 data, (samples * unit_size_));
145 used_samples_ += samples;
146 unused_samples_ -= samples;
147 } else {
148 // Only a part of the samples fit, split data up between chunks
149 memcpy(current_chunk_ + (used_samples_ * unit_size_),
150 data, (unused_samples_ * unit_size_));
151 const uint64_t remaining_samples = samples - unused_samples_;
152
e7216ae0 153 // If we're out of memory, this will throw std::bad_alloc
26a883ed
SA
154 current_chunk_ = new uint8_t[chunk_size_];
155 data_chunks_.push_back(current_chunk_);
156 memcpy(current_chunk_, (uint8_t*)data + (unused_samples_ * unit_size_),
157 (remaining_samples * unit_size_));
158
159 used_samples_ = remaining_samples;
160 unused_samples_ = (chunk_size_ / unit_size_) - remaining_samples;
27d7c96b 161 }
26a883ed
SA
162
163 if (unused_samples_ == 0) {
164 // If we're out of memory, this will throw std::bad_alloc
165 current_chunk_ = new uint8_t[chunk_size_];
166 data_chunks_.push_back(current_chunk_);
167 used_samples_ = 0;
168 unused_samples_ = chunk_size_ / unit_size_;
169 }
170
171 sample_count_ += samples;
27d7c96b
DK
172}
173
26a883ed 174uint8_t* Segment::get_raw_samples(uint64_t start, uint64_t count) const
27d7c96b 175{
26a883ed
SA
176 assert(start < sample_count_);
177 assert(start + count <= sample_count_);
178 assert(count > 0);
179
8dbbc7f0 180 lock_guard<recursive_mutex> lock(mutex_);
26a883ed
SA
181
182 uint8_t* dest = new uint8_t[count * unit_size_];
183 uint8_t* dest_ptr = dest;
184
185 uint64_t chunk_num = (start * unit_size_) / chunk_size_;
186 uint64_t chunk_offs = (start * unit_size_) % chunk_size_;
187
188 while (count > 0) {
189 const uint8_t* chunk = data_chunks_[chunk_num];
190
6f925ba9 191 uint64_t copy_size = min(count * unit_size_,
26a883ed
SA
192 chunk_size_ - chunk_offs);
193
194 memcpy(dest_ptr, chunk + chunk_offs, copy_size);
195
196 dest_ptr += copy_size;
197 count -= (copy_size / unit_size_);
198
199 chunk_num++;
200 chunk_offs = 0;
201 }
202
203 return dest;
27d7c96b
DK
204}
205
c70e3464 206SegmentRawDataIterator* Segment::begin_raw_sample_iteration(uint64_t start)
26a883ed
SA
207{
208 SegmentRawDataIterator* it = new SegmentRawDataIterator;
209
210 assert(start < sample_count_);
211
c70e3464
SA
212 iterator_count_++;
213
26a883ed
SA
214 it->sample_index = start;
215 it->chunk_num = (start * unit_size_) / chunk_size_;
216 it->chunk_offs = (start * unit_size_) % chunk_size_;
217 it->chunk = data_chunks_[it->chunk_num];
218 it->value = it->chunk + it->chunk_offs;
219
220 return it;
221}
222
c70e3464 223void Segment::continue_raw_sample_iteration(SegmentRawDataIterator* it, uint64_t increase)
f556bc6a 224{
8dbbc7f0 225 lock_guard<recursive_mutex> lock(mutex_);
27d7c96b 226
26a883ed
SA
227 if (it->sample_index > sample_count_)
228 {
229 // Fail gracefully if we are asked to deliver data we don't have
230 return;
231 } else {
232 it->sample_index += increase;
233 it->chunk_offs += (increase * unit_size_);
234 }
235
236 if (it->chunk_offs > (chunk_size_ - 1)) {
237 it->chunk_num++;
238 it->chunk_offs -= chunk_size_;
239 it->chunk = data_chunks_[it->chunk_num];
240 }
27d7c96b 241
26a883ed
SA
242 it->value = it->chunk + it->chunk_offs;
243}
27d7c96b 244
c70e3464 245void Segment::end_raw_sample_iteration(SegmentRawDataIterator* it)
26a883ed
SA
246{
247 delete it;
c70e3464
SA
248
249 iterator_count_--;
250
251 if ((iterator_count_ == 0) && mem_optimization_requested_) {
252 mem_optimization_requested_ = false;
253 free_unused_memory();
254 }
28a4c9c5 255}
51e77110 256
26a883ed 257
1b1ec774 258} // namespace data
51e77110 259} // namespace pv