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