]> sigrok.org Git - pulseview.git/blob - pv/data/snapshot.cpp
6e1235d24218fafce46f95c8c99a93c759d87109
[pulseview.git] / pv / data / snapshot.cpp
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 "snapshot.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 Snapshot::Snapshot(unsigned int unit_size) :
34         sample_count_(0),
35         start_time_(0),
36         capacity_(0),
37         unit_size_(unit_size)
38 {
39         lock_guard<recursive_mutex> lock(mutex_);
40         assert(unit_size_ > 0);
41 }
42
43 Snapshot::~Snapshot()
44 {
45         lock_guard<recursive_mutex> lock(mutex_);
46 }
47
48 uint64_t Snapshot::get_sample_count() const
49 {
50         lock_guard<recursive_mutex> lock(mutex_);
51         return sample_count_;
52 }
53
54 double Snapshot::start_time() const
55 {
56         return start_time_;
57 }
58
59 unsigned int Snapshot::unit_size() const
60 {
61         return unit_size_;
62 }
63
64 void Snapshot::set_capacity(const uint64_t new_capacity)
65 {
66         lock_guard<recursive_mutex> lock(mutex_);
67
68         assert(capacity_ >= sample_count_);
69         if (new_capacity > capacity_) {
70                 capacity_ = new_capacity;
71                 data_.resize((new_capacity * unit_size_) + sizeof(uint64_t));
72         }
73 }
74
75 uint64_t Snapshot::capacity() const
76 {
77         lock_guard<recursive_mutex> lock(mutex_);
78         return data_.size();
79 }
80
81 void Snapshot::append_data(void *data, uint64_t samples)
82 {
83         lock_guard<recursive_mutex> lock(mutex_);
84
85         assert(capacity_ >= sample_count_);
86
87         // Ensure there's enough capacity to copy.
88         const uint64_t free_space = capacity_ - sample_count_;
89         if (free_space < samples) {
90                 set_capacity(sample_count_ + samples);
91         }
92
93         memcpy((uint8_t*)data_.data() + sample_count_ * unit_size_,
94                 data, samples * unit_size_);
95         sample_count_ += samples;
96 }
97
98 } // namespace data
99 } // namespace pv