2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
21 #include "segment.hpp"
27 using std::lock_guard;
28 using std::recursive_mutex;
33 Segment::Segment(uint64_t samplerate, unsigned int unit_size) :
36 samplerate_(samplerate),
40 lock_guard<recursive_mutex> lock(mutex_);
41 assert(unit_size_ > 0);
46 lock_guard<recursive_mutex> lock(mutex_);
49 uint64_t Segment::get_sample_count() const
51 lock_guard<recursive_mutex> lock(mutex_);
55 const pv::util::Timestamp& Segment::start_time() const
60 double Segment::samplerate() const
65 void Segment::set_samplerate(double samplerate)
67 samplerate_ = samplerate;
70 unsigned int Segment::unit_size() const
75 void Segment::set_capacity(const uint64_t new_capacity)
77 lock_guard<recursive_mutex> lock(mutex_);
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;
87 uint64_t Segment::capacity() const
89 lock_guard<recursive_mutex> lock(mutex_);
93 void Segment::append_data(void *data, uint64_t samples)
95 lock_guard<recursive_mutex> lock(mutex_);
97 assert(capacity_ >= sample_count_);
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);
104 memcpy((uint8_t*)data_.data() + sample_count_ * unit_size_,
105 data, samples * unit_size_);
106 sample_count_ += samples;