PulseView  0.3.0
A Qt-based sigrok GUI
segment.cpp
Go to the documentation of this file.
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, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "segment.hpp"
22 
23 #include <assert.h>
24 #include <stdlib.h>
25 #include <string.h>
26 
27 using std::lock_guard;
28 using std::recursive_mutex;
29 
30 namespace pv {
31 namespace data {
32 
33 Segment::Segment(uint64_t samplerate, unsigned int unit_size) :
34  sample_count_(0),
35  start_time_(0),
36  samplerate_(samplerate),
37  capacity_(0),
38  unit_size_(unit_size)
39 {
40  lock_guard<recursive_mutex> lock(mutex_);
41  assert(unit_size_ > 0);
42 }
43 
45 {
46  lock_guard<recursive_mutex> lock(mutex_);
47 }
48 
49 uint64_t Segment::get_sample_count() const
50 {
51  lock_guard<recursive_mutex> lock(mutex_);
52  return sample_count_;
53 }
54 
56 {
57  return start_time_;
58 }
59 
60 double Segment::samplerate() const
61 {
62  return samplerate_;
63 }
64 
65 void Segment::set_samplerate(double samplerate)
66 {
68 }
69 
70 unsigned int Segment::unit_size() const
71 {
72  return unit_size_;
73 }
74 
75 void Segment::set_capacity(const uint64_t new_capacity)
76 {
77  lock_guard<recursive_mutex> lock(mutex_);
78 
79  assert(capacity_ >= sample_count_);
80  if (new_capacity > capacity_) {
81  // If we're out of memory, this will throw std::bad_alloc
82  data_.resize((new_capacity * unit_size_) + sizeof(uint64_t));
83  capacity_ = new_capacity;
84  }
85 }
86 
87 uint64_t Segment::capacity() const
88 {
89  lock_guard<recursive_mutex> lock(mutex_);
90  return data_.size();
91 }
92 
93 void Segment::append_data(void *data, uint64_t samples)
94 {
95  lock_guard<recursive_mutex> lock(mutex_);
96 
97  assert(capacity_ >= sample_count_);
98 
99  // Ensure there's enough capacity to copy.
100  const uint64_t free_space = capacity_ - sample_count_;
101  if (free_space < samples)
102  set_capacity(sample_count_ + samples);
103 
104  memcpy((uint8_t*)data_.data() + sample_count_ * unit_size_,
105  data, samples * unit_size_);
106  sample_count_ += samples;
107 }
108 
109 } // namespace data
110 } // namespace pv
unsigned int unit_size() const
Definition: segment.cpp:70
Segment(uint64_t samplerate, unsigned int unit_size)
Definition: segment.cpp:33
uint64_t sample_count_
Definition: segment.hpp:82
void set_capacity(uint64_t new_capacity)
Increase the capacity of the segment.
Definition: segment.cpp:75
uint64_t capacity() const
Get the current capacity of the segment.
Definition: segment.cpp:87
std::vector< uint8_t > data_
Definition: segment.hpp:81
void append_data(void *data, uint64_t samples)
Definition: segment.cpp:93
double samplerate() const
Definition: segment.cpp:60
const pv::util::Timestamp & start_time() const
Definition: segment.cpp:55
pv::util::Timestamp start_time_
Definition: segment.hpp:83
uint64_t get_sample_count() const
Definition: segment.cpp:49
double samplerate_
Definition: segment.hpp:84
void set_samplerate(double samplerate)
Definition: segment.cpp:65
std::recursive_mutex mutex_
Definition: segment.hpp:80
boost::multiprecision::number< boost::multiprecision::cpp_dec_float< 24 >, boost::multiprecision::et_off > Timestamp
Timestamp type providing yoctosecond resolution.
Definition: util.hpp:58
uint64_t capacity_
Definition: segment.hpp:85
virtual ~Segment()
Definition: segment.cpp:44
unsigned int unit_size_
Definition: segment.hpp:86