]> sigrok.org Git - pulseview.git/blame_incremental - pv/data/logicsegment.cpp
Free unused segment memory after acquisition
[pulseview.git] / pv / data / logicsegment.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <extdef.h>
21
22#include <assert.h>
23#include <string.h>
24#include <stdlib.h>
25#include <cmath>
26
27#include "logicsegment.hpp"
28
29#include <libsigrokcxx/libsigrokcxx.hpp>
30
31using std::lock_guard;
32using std::recursive_mutex;
33using std::max;
34using std::min;
35using std::pair;
36using std::shared_ptr;
37
38using sigrok::Logic;
39
40namespace pv {
41namespace data {
42
43const int LogicSegment::MipMapScalePower = 4;
44const int LogicSegment::MipMapScaleFactor = 1 << MipMapScalePower;
45const float LogicSegment::LogMipMapScaleFactor = logf(MipMapScaleFactor);
46const uint64_t LogicSegment::MipMapDataUnit = 64*1024; // bytes
47
48LogicSegment::LogicSegment(shared_ptr<Logic> logic, uint64_t samplerate) :
49 Segment(samplerate, logic->unit_size()),
50 last_append_sample_(0)
51{
52 lock_guard<recursive_mutex> lock(mutex_);
53 memset(mip_map_, 0, sizeof(mip_map_));
54 append_payload(logic);
55}
56
57LogicSegment::~LogicSegment()
58{
59 lock_guard<recursive_mutex> lock(mutex_);
60 for (MipMapLevel &l : mip_map_)
61 free(l.data);
62}
63
64uint64_t LogicSegment::unpack_sample(const uint8_t *ptr) const
65{
66#ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
67 return *(uint64_t*)ptr;
68#else
69 uint64_t value = 0;
70 switch (unit_size_) {
71 default:
72 value |= ((uint64_t)ptr[7]) << 56;
73 /* FALLTHRU */
74 case 7:
75 value |= ((uint64_t)ptr[6]) << 48;
76 /* FALLTHRU */
77 case 6:
78 value |= ((uint64_t)ptr[5]) << 40;
79 /* FALLTHRU */
80 case 5:
81 value |= ((uint64_t)ptr[4]) << 32;
82 /* FALLTHRU */
83 case 4:
84 value |= ((uint32_t)ptr[3]) << 24;
85 /* FALLTHRU */
86 case 3:
87 value |= ((uint32_t)ptr[2]) << 16;
88 /* FALLTHRU */
89 case 2:
90 value |= ptr[1] << 8;
91 /* FALLTHRU */
92 case 1:
93 value |= ptr[0];
94 /* FALLTHRU */
95 case 0:
96 break;
97 }
98 return value;
99#endif
100}
101
102void LogicSegment::pack_sample(uint8_t *ptr, uint64_t value)
103{
104#ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
105 *(uint64_t*)ptr = value;
106#else
107 switch (unit_size_) {
108 default:
109 ptr[7] = value >> 56;
110 /* FALLTHRU */
111 case 7:
112 ptr[6] = value >> 48;
113 /* FALLTHRU */
114 case 6:
115 ptr[5] = value >> 40;
116 /* FALLTHRU */
117 case 5:
118 ptr[4] = value >> 32;
119 /* FALLTHRU */
120 case 4:
121 ptr[3] = value >> 24;
122 /* FALLTHRU */
123 case 3:
124 ptr[2] = value >> 16;
125 /* FALLTHRU */
126 case 2:
127 ptr[1] = value >> 8;
128 /* FALLTHRU */
129 case 1:
130 ptr[0] = value;
131 /* FALLTHRU */
132 case 0:
133 break;
134 }
135#endif
136}
137
138void LogicSegment::append_payload(shared_ptr<Logic> logic)
139{
140 assert(unit_size_ == logic->unit_size());
141 assert((logic->data_length() % unit_size_) == 0);
142
143 lock_guard<recursive_mutex> lock(mutex_);
144
145 append_samples(logic->data_pointer(),
146 logic->data_length() / unit_size_);
147
148 // Generate the first mip-map from the data
149 append_payload_to_mipmap();
150}
151
152const uint8_t* LogicSegment::get_samples(int64_t start_sample,
153 int64_t end_sample) const
154{
155 assert(start_sample >= 0);
156 assert(start_sample <= (int64_t)sample_count_);
157 assert(end_sample >= 0);
158 assert(end_sample <= (int64_t)sample_count_);
159 assert(start_sample <= end_sample);
160
161 lock_guard<recursive_mutex> lock(mutex_);
162
163 return get_raw_samples(start_sample, (end_sample-start_sample));
164}
165
166SegmentLogicDataIterator* LogicSegment::begin_sample_iteration(uint64_t start) const
167{
168 return (SegmentLogicDataIterator*)begin_raw_sample_iteration(start);
169}
170
171void LogicSegment::continue_sample_iteration(SegmentLogicDataIterator* it, uint64_t increase) const
172{
173 Segment::continue_raw_sample_iteration((SegmentRawDataIterator*)it, increase);
174}
175
176void LogicSegment::end_sample_iteration(SegmentLogicDataIterator* it) const
177{
178 Segment::end_raw_sample_iteration((SegmentRawDataIterator*)it);
179}
180
181void LogicSegment::reallocate_mipmap_level(MipMapLevel &m)
182{
183 lock_guard<recursive_mutex> lock(mutex_);
184
185 const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) /
186 MipMapDataUnit) * MipMapDataUnit;
187
188 if (new_data_length > m.data_length) {
189 m.data_length = new_data_length;
190
191 // Padding is added to allow for the uint64_t write word
192 m.data = realloc(m.data, new_data_length * unit_size_ +
193 sizeof(uint64_t));
194 }
195}
196
197void LogicSegment::append_payload_to_mipmap()
198{
199 MipMapLevel &m0 = mip_map_[0];
200 uint64_t prev_length;
201 uint8_t *dest_ptr;
202 SegmentRawDataIterator* it;
203 uint64_t accumulator;
204 unsigned int diff_counter;
205
206 // Expand the data buffer to fit the new samples
207 prev_length = m0.length;
208 m0.length = sample_count_ / MipMapScaleFactor;
209
210 // Break off if there are no new samples to compute
211 if (m0.length == prev_length)
212 return;
213
214 reallocate_mipmap_level(m0);
215
216 dest_ptr = (uint8_t*)m0.data + prev_length * unit_size_;
217
218 // Iterate through the samples to populate the first level mipmap
219 uint64_t start_sample = prev_length * MipMapScaleFactor;
220 uint64_t end_sample = m0.length * MipMapScaleFactor;
221
222 it = begin_raw_sample_iteration(start_sample);
223 for (uint64_t i = start_sample; i < end_sample;) {
224 // Accumulate transitions which have occurred in this sample
225 accumulator = 0;
226 diff_counter = MipMapScaleFactor;
227 while (diff_counter-- > 0) {
228 const uint64_t sample = unpack_sample(it->value);
229 accumulator |= last_append_sample_ ^ sample;
230 last_append_sample_ = sample;
231 continue_raw_sample_iteration(it, 1);
232 i++;
233 }
234
235 pack_sample(dest_ptr, accumulator);
236 dest_ptr += unit_size_;
237 }
238 end_raw_sample_iteration(it);
239
240 // Compute higher level mipmaps
241 for (unsigned int level = 1; level < ScaleStepCount; level++) {
242 MipMapLevel &m = mip_map_[level];
243 const MipMapLevel &ml = mip_map_[level-1];
244
245 // Expand the data buffer to fit the new samples
246 prev_length = m.length;
247 m.length = ml.length / MipMapScaleFactor;
248
249 // Break off if there are no more samples to be computed
250 if (m.length == prev_length)
251 break;
252
253 reallocate_mipmap_level(m);
254
255 // Subsample the lower level
256 const uint8_t* src_ptr = (uint8_t*)ml.data +
257 unit_size_ * prev_length * MipMapScaleFactor;
258 const uint8_t *const end_dest_ptr =
259 (uint8_t*)m.data + unit_size_ * m.length;
260
261 for (dest_ptr = (uint8_t*)m.data +
262 unit_size_ * prev_length;
263 dest_ptr < end_dest_ptr;
264 dest_ptr += unit_size_) {
265 accumulator = 0;
266 diff_counter = MipMapScaleFactor;
267 while (diff_counter-- > 0) {
268 accumulator |= unpack_sample(src_ptr);
269 src_ptr += unit_size_;
270 }
271
272 pack_sample(dest_ptr, accumulator);
273 }
274 }
275}
276
277uint64_t LogicSegment::get_unpacked_sample(uint64_t index) const
278{
279 assert(index < sample_count_);
280
281 const uint8_t* data = get_raw_samples(index, 1);
282 uint64_t sample = unpack_sample(data);
283 delete[] data;
284
285 return sample;
286}
287
288void LogicSegment::get_subsampled_edges(
289 std::vector<EdgePair> &edges,
290 uint64_t start, uint64_t end,
291 float min_length, int sig_index)
292{
293 uint64_t index = start;
294 unsigned int level;
295 bool last_sample;
296 bool fast_forward;
297
298 assert(end <= get_sample_count());
299 assert(start <= end);
300 assert(min_length > 0);
301 assert(sig_index >= 0);
302 assert(sig_index < 64);
303
304 lock_guard<recursive_mutex> lock(mutex_);
305
306 const uint64_t block_length = (uint64_t)max(min_length, 1.0f);
307 const unsigned int min_level = max((int)floorf(logf(min_length) /
308 LogMipMapScaleFactor) - 1, 0);
309 const uint64_t sig_mask = 1ULL << sig_index;
310
311 // Store the initial state
312 last_sample = (get_unpacked_sample(start) & sig_mask) != 0;
313 edges.push_back(pair<int64_t, bool>(index++, last_sample));
314
315 while (index + block_length <= end) {
316 //----- Continue to search -----//
317 level = min_level;
318
319 // We cannot fast-forward if there is no mip-map data at
320 // at the minimum level.
321 fast_forward = (mip_map_[level].data != nullptr);
322
323 if (min_length < MipMapScaleFactor) {
324 // Search individual samples up to the beginning of
325 // the next first level mip map block
326 const uint64_t final_index = min(end,
327 pow2_ceil(index, MipMapScalePower));
328
329 for (; index < final_index &&
330 (index & ~((uint64_t)(~0) << MipMapScalePower)) != 0;
331 index++) {
332 const bool sample =
333 (get_unpacked_sample(index) & sig_mask) != 0;
334
335 // If there was a change we cannot fast forward
336 if (sample != last_sample) {
337 fast_forward = false;
338 break;
339 }
340 }
341 } else {
342 // If resolution is less than a mip map block,
343 // round up to the beginning of the mip-map block
344 // for this level of detail
345 const int min_level_scale_power =
346 (level + 1) * MipMapScalePower;
347 index = pow2_ceil(index, min_level_scale_power);
348 if (index >= end)
349 break;
350
351 // We can fast forward only if there was no change
352 const bool sample =
353 (get_unpacked_sample(index) & sig_mask) != 0;
354 if (last_sample != sample)
355 fast_forward = false;
356 }
357
358 if (fast_forward) {
359
360 // Fast forward: This involves zooming out to higher
361 // levels of the mip map searching for changes, then
362 // zooming in on them to find the point where the edge
363 // begins.
364
365 // Slide right and zoom out at the beginnings of mip-map
366 // blocks until we encounter a change
367 while (1) {
368 const int level_scale_power =
369 (level + 1) * MipMapScalePower;
370 const uint64_t offset =
371 index >> level_scale_power;
372
373 // Check if we reached the last block at this
374 // level, or if there was a change in this block
375 if (offset >= mip_map_[level].length ||
376 (get_subsample(level, offset) &
377 sig_mask))
378 break;
379
380 if ((offset & ~((uint64_t)(~0) << MipMapScalePower)) == 0) {
381 // If we are now at the beginning of a
382 // higher level mip-map block ascend one
383 // level
384 if (level + 1 >= ScaleStepCount ||
385 !mip_map_[level + 1].data)
386 break;
387
388 level++;
389 } else {
390 // Slide right to the beginning of the
391 // next mip map block
392 index = pow2_ceil(index + 1,
393 level_scale_power);
394 }
395 }
396
397 // Zoom in, and slide right until we encounter a change,
398 // and repeat until we reach min_level
399 while (1) {
400 assert(mip_map_[level].data);
401
402 const int level_scale_power =
403 (level + 1) * MipMapScalePower;
404 const uint64_t offset =
405 index >> level_scale_power;
406
407 // Check if we reached the last block at this
408 // level, or if there was a change in this block
409 if (offset >= mip_map_[level].length ||
410 (get_subsample(level, offset) &
411 sig_mask)) {
412 // Zoom in unless we reached the minimum
413 // zoom
414 if (level == min_level)
415 break;
416
417 level--;
418 } else {
419 // Slide right to the beginning of the
420 // next mip map block
421 index = pow2_ceil(index + 1,
422 level_scale_power);
423 }
424 }
425
426 // If individual samples within the limit of resolution,
427 // do a linear search for the next transition within the
428 // block
429 if (min_length < MipMapScaleFactor) {
430 for (; index < end; index++) {
431 const bool sample = (get_unpacked_sample(index) &
432 sig_mask) != 0;
433 if (sample != last_sample)
434 break;
435 }
436 }
437 }
438
439 //----- Store the edge -----//
440
441 // Take the last sample of the quanization block
442 const int64_t final_index = index + block_length;
443 if (index + block_length > end)
444 break;
445
446 // Store the final state
447 const bool final_sample =
448 (get_unpacked_sample(final_index - 1) & sig_mask) != 0;
449 edges.push_back(pair<int64_t, bool>(index, final_sample));
450
451 index = final_index;
452 last_sample = final_sample;
453 }
454
455 // Add the final state
456 const bool end_sample = get_unpacked_sample(end) & sig_mask;
457 if (last_sample != end_sample)
458 edges.push_back(pair<int64_t, bool>(end, end_sample));
459 edges.push_back(pair<int64_t, bool>(end + 1, end_sample));
460}
461
462uint64_t LogicSegment::get_subsample(int level, uint64_t offset) const
463{
464 assert(level >= 0);
465 assert(mip_map_[level].data);
466 return unpack_sample((uint8_t*)mip_map_[level].data +
467 unit_size_ * offset);
468}
469
470uint64_t LogicSegment::pow2_ceil(uint64_t x, unsigned int power)
471{
472 const uint64_t p = 1 << power;
473 return (x + p - 1) / p * p;
474}
475
476} // namespace data
477} // namespace pv