]> sigrok.org Git - pulseview.git/blame - pv/data/segment.cpp
Device: add support for list of discrete values for probe factor
[pulseview.git] / pv / data / segment.cpp
CommitLineData
28a4c9c5 1/*
b3f22de0 2 * This file is part of the PulseView project.
28a4c9c5
JH
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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
f3d66e52 21#include "segment.hpp"
28a4c9c5 22
f556bc6a
JH
23#include <assert.h>
24#include <stdlib.h>
25#include <string.h>
26
3b68d03d
JH
27using std::lock_guard;
28using std::recursive_mutex;
7d29656f 29
51e77110 30namespace pv {
1b1ec774 31namespace data {
51e77110 32
f3d66e52 33Segment::Segment(uint64_t samplerate, unsigned int unit_size) :
8dbbc7f0 34 sample_count_(0),
7f4038d6 35 start_time_(0),
ff008de6 36 samplerate_(samplerate),
8dbbc7f0
JH
37 capacity_(0),
38 unit_size_(unit_size)
f556bc6a 39{
8dbbc7f0
JH
40 lock_guard<recursive_mutex> lock(mutex_);
41 assert(unit_size_ > 0);
f556bc6a
JH
42}
43
f3d66e52 44Segment::~Segment()
28a4c9c5 45{
8dbbc7f0 46 lock_guard<recursive_mutex> lock(mutex_);
28a4c9c5
JH
47}
48
f3d66e52 49uint64_t Segment::get_sample_count() const
28a4c9c5 50{
8dbbc7f0
JH
51 lock_guard<recursive_mutex> lock(mutex_);
52 return sample_count_;
f556bc6a
JH
53}
54
60d9b99a 55const pv::util::Timestamp& Segment::start_time() const
7f4038d6
JH
56{
57 return start_time_;
58}
59
f3d66e52 60double Segment::samplerate() const
ff008de6
JH
61{
62 return samplerate_;
63}
64
f3d66e52 65void Segment::set_samplerate(double samplerate)
ff008de6
JH
66{
67 samplerate_ = samplerate;
68}
69
f3d66e52 70unsigned int Segment::unit_size() const
6fd14a32 71{
8dbbc7f0 72 return unit_size_;
6fd14a32
JH
73}
74
f3d66e52 75void Segment::set_capacity(const uint64_t new_capacity)
27d7c96b 76{
8dbbc7f0 77 lock_guard<recursive_mutex> lock(mutex_);
27d7c96b 78
8dbbc7f0
JH
79 assert(capacity_ >= sample_count_);
80 if (new_capacity > capacity_) {
e7216ae0 81 // If we're out of memory, this will throw std::bad_alloc
8dbbc7f0 82 data_.resize((new_capacity * unit_size_) + sizeof(uint64_t));
e7216ae0 83 capacity_ = new_capacity;
27d7c96b
DK
84 }
85}
86
f3d66e52 87uint64_t Segment::capacity() const
27d7c96b 88{
8dbbc7f0
JH
89 lock_guard<recursive_mutex> lock(mutex_);
90 return data_.size();
27d7c96b
DK
91}
92
f3d66e52 93void Segment::append_data(void *data, uint64_t samples)
f556bc6a 94{
8dbbc7f0 95 lock_guard<recursive_mutex> lock(mutex_);
27d7c96b 96
8dbbc7f0 97 assert(capacity_ >= sample_count_);
27d7c96b
DK
98
99 // Ensure there's enough capacity to copy.
8dbbc7f0 100 const uint64_t free_space = capacity_ - sample_count_;
2ad82c2e 101 if (free_space < samples)
8dbbc7f0 102 set_capacity(sample_count_ + samples);
27d7c96b 103
8dbbc7f0
JH
104 memcpy((uint8_t*)data_.data() + sample_count_ * unit_size_,
105 data, samples * unit_size_);
106 sample_count_ += samples;
28a4c9c5 107}
51e77110 108
1b1ec774 109} // namespace data
51e77110 110} // namespace pv