]> sigrok.org Git - pulseview.git/blob - pv/data/logicsegment.hpp
Fix #1596 by making memory management more robust
[pulseview.git] / pv / data / logicsegment.hpp
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, see <http://www.gnu.org/licenses/>.
18  */
19
20 #ifndef PULSEVIEW_PV_DATA_LOGICSEGMENT_HPP
21 #define PULSEVIEW_PV_DATA_LOGICSEGMENT_HPP
22
23 #include "segment.hpp"
24
25 #include <vector>
26
27 #include <QObject>
28
29 using std::enable_shared_from_this;
30 using std::pair;
31 using std::shared_ptr;
32 using std::vector;
33
34 namespace sigrok {
35         class Logic;
36 }
37
38 namespace LogicSegmentTest {
39 struct Pow2;
40 struct Basic;
41 struct LargeData;
42 struct Pulses;
43 struct LongPulses;
44 }
45
46 namespace pv {
47 namespace data {
48
49 class Logic;
50
51 class LogicSegment : public Segment, public enable_shared_from_this<Segment>
52 {
53         Q_OBJECT
54
55 public:
56         typedef pair<int64_t, bool> EdgePair;
57
58         static const unsigned int ScaleStepCount = 10;
59         static const int MipMapScalePower;
60         static const int MipMapScaleFactor;
61         static const float LogMipMapScaleFactor;
62         static const uint64_t MipMapDataUnit;
63
64 private:
65         struct MipMapLevel
66         {
67                 uint64_t length;
68                 uint64_t data_length;
69                 void *data;
70         };
71
72 public:
73         LogicSegment(pv::data::Logic& owner, uint32_t segment_id,
74                 unsigned int unit_size, uint64_t samplerate);
75
76         virtual ~LogicSegment();
77
78         /**
79          * Using enable_shared_from_this prevents the normal use of shared_ptr
80          * instances by users of LogicSegment instances. Instead, shared_ptrs may
81          * only be created by the instance itself.
82          * See https://en.cppreference.com/w/cpp/memory/enable_shared_from_this
83          */
84         shared_ptr<const LogicSegment> get_shared_ptr() const;
85
86         void append_payload(shared_ptr<sigrok::Logic> logic);
87         void append_payload(void *data, uint64_t data_size);
88
89         void get_samples(int64_t start_sample, int64_t end_sample, uint8_t* dest) const;
90
91         /**
92          * Parses a logic data segment to generate a list of transitions
93          * in a time interval to a given level of detail.
94          * @param[out] edges The vector to place the edges into.
95          * @param[in] start The start sample index.
96          * @param[in] end The end sample index.
97          * @param[in] min_length The minimum number of samples that
98          * can be resolved at this level of detail.
99          * @param[in] sig_index The index of the signal.
100          */
101         void get_subsampled_edges(vector<EdgePair> &edges,
102                 uint64_t start, uint64_t end,
103                 float min_length, int sig_index, bool first_change_only = false);
104
105         void get_surrounding_edges(vector<EdgePair> &dest,
106                 uint64_t origin_sample, float min_length, int sig_index);
107
108 private:
109         uint64_t unpack_sample(const uint8_t *ptr) const;
110         void pack_sample(uint8_t *ptr, uint64_t value);
111
112         void reallocate_mipmap_level(MipMapLevel &m);
113
114         void append_payload_to_mipmap();
115
116         uint64_t get_unpacked_sample(uint64_t index) const;
117
118         template <class T> void downsampleTmain(const T*&in, T &acc, T &prev);
119         template <class T> void downsampleT(const uint8_t *in, uint8_t *&out, uint64_t len);
120         void downsampleGeneric(const uint8_t *in, uint8_t *&out, uint64_t len);
121
122 private:
123         uint64_t get_subsample(int level, uint64_t offset) const;
124
125         static uint64_t pow2_ceil(uint64_t x, unsigned int power);
126
127 private:
128         Logic& owner_;
129
130         struct MipMapLevel mip_map_[ScaleStepCount];
131         uint64_t last_append_sample_;
132         uint64_t last_append_accumulator_;
133         uint64_t last_append_extra_;
134
135         friend struct LogicSegmentTest::Pow2;
136         friend struct LogicSegmentTest::Basic;
137         friend struct LogicSegmentTest::LargeData;
138         friend struct LogicSegmentTest::Pulses;
139         friend struct LogicSegmentTest::LongPulses;
140 };
141
142 } // namespace data
143 } // namespace pv
144
145 #endif // PULSEVIEW_PV_DATA_LOGICSEGMENT_HPP