]> sigrok.org Git - pulseview.git/commitdiff
Make get_raw_samples() use provided mem instead of allocating
authorSoeren Apel <redacted>
Sat, 1 Jul 2017 20:24:06 +0000 (22:24 +0200)
committerUwe Hermann <redacted>
Wed, 5 Jul 2017 22:37:08 +0000 (00:37 +0200)
Until now, Segment::get_raw_samples() was allocating the required
amount of memory and returned it to the caller to use. This way,
there was always enough memory allocated for the data that was
written to that memory location.

However, in SignalBase::conversion_thread_proc() we want to use
one memory location multiple times because we will create several
layers of libsigrok wrapper objects around it. This only works
if Segment::get_raw_samples() uses a given memory location instead
of allocating it by itself.

pv/data/analogsegment.cpp
pv/data/analogsegment.hpp
pv/data/decodesignal.cpp
pv/data/logicsegment.cpp
pv/data/logicsegment.hpp
pv/data/segment.cpp
pv/data/segment.hpp
pv/data/signalbase.cpp
pv/storesession.cpp
pv/views/trace/analogsignal.cpp

index 5e42556ca8c3f01341283f909f2ffc81ed2d394a..0860177e6d9f6139b1ad15053527feeff0ca877b 100644 (file)
@@ -96,18 +96,19 @@ void AnalogSegment::append_interleaved_samples(const float *data,
                        prev_sample_count + 1);
 }
 
-const float* AnalogSegment::get_samples(
-       int64_t start_sample, int64_t end_sample) const
+void AnalogSegment::get_samples(int64_t start_sample, int64_t end_sample,
+       float* dest) const
 {
        assert(start_sample >= 0);
        assert(start_sample < (int64_t)sample_count_);
        assert(end_sample >= 0);
        assert(end_sample < (int64_t)sample_count_);
        assert(start_sample <= end_sample);
+       assert(dest != nullptr);
 
        lock_guard<recursive_mutex> lock(mutex_);
 
-       return (float*)get_raw_samples(start_sample, (end_sample - start_sample));
+       get_raw_samples(start_sample, (end_sample - start_sample), (uint8_t*)dest);
 }
 
 const pair<float, float> AnalogSegment::get_min_max() const
index 8dd6f5f189746ec903157c7563892ad2073f1170..8cb4d25b9d78648133a470a8eb9974951b3b4ddc 100644 (file)
@@ -86,8 +86,7 @@ public:
        void append_interleaved_samples(const float *data,
                size_t sample_count, size_t stride);
 
-       const float* get_samples(int64_t start_sample,
-               int64_t end_sample) const;
+       void get_samples(int64_t start_sample, int64_t end_sample, float* dest) const;
 
        const pair<float, float> get_min_max() const;
 
index 6ac66a1ab2af6cbd9e25e5e5f1bac00bf98f2389..1d9f3c8ffb0dababec91fd579de2eef4b17e22e7 100644 (file)
@@ -653,7 +653,10 @@ void DecodeSignal::mux_logic_samples(const int64_t start, const int64_t end)
                        const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data();
                        const shared_ptr<LogicSegment> segment = logic_data->logic_segments().front();
                        segments.push_back(segment);
-                       signal_data.push_back(segment->get_samples(start, end));
+
+                       uint8_t* data = new uint8_t[(end - start) * segment->unit_size()];
+                       segment->get_samples(start, end, data);
+                       signal_data.push_back(data);
 
                        const int bitpos = ch.assigned_signal->logic_bit_index();
                        signal_in_bytepos.push_back(bitpos / 8);
@@ -784,10 +787,12 @@ void DecodeSignal::decode_data(
                const int64_t chunk_end = min(i + chunk_sample_count,
                        abs_start_samplenum + sample_count);
 
-               const uint8_t* chunk = segment_->get_samples(i, chunk_end);
+               int64_t data_size = (chunk_end - i) * unit_size;
+               uint8_t* chunk = new uint8_t[data_size];
+               segment_->get_samples(i, chunk_end, chunk);
 
                if (srd_session_send(srd_session_, i, chunk_end, chunk,
-                               (chunk_end - i) * unit_size, unit_size) != SRD_OK) {
+                               data_size, unit_size) != SRD_OK) {
                        error_message_ = tr("Decoder reported an error");
                        delete[] chunk;
                        break;
index b648f5d1c302af35c22c7c1dec35eaf08dee923e..dff99c01742239665d7575029f34e7b2ab765918 100644 (file)
@@ -166,18 +166,19 @@ void LogicSegment::append_payload(void *data, uint64_t data_size)
                        prev_sample_count + 1);
 }
 
-const uint8_t* LogicSegment::get_samples(int64_t start_sample,
-       int64_t end_sample) const
+void LogicSegment::get_samples(int64_t start_sample,
+       int64_t end_sample,     uint8_t* dest) const
 {
        assert(start_sample >= 0);
        assert(start_sample <= (int64_t)sample_count_);
        assert(end_sample >= 0);
        assert(end_sample <= (int64_t)sample_count_);
        assert(start_sample <= end_sample);
+       assert(dest != nullptr);
 
        lock_guard<recursive_mutex> lock(mutex_);
 
-       return get_raw_samples(start_sample, (end_sample - start_sample));
+       get_raw_samples(start_sample, (end_sample - start_sample), dest);
 }
 
 SegmentLogicDataIterator* LogicSegment::begin_sample_iteration(uint64_t start)
@@ -295,7 +296,8 @@ uint64_t LogicSegment::get_unpacked_sample(uint64_t index) const
 {
        assert(index < sample_count_);
 
-       const uint8_t* data = get_raw_samples(index, 1);
+       uint8_t* data = new uint8_t[unit_size_];
+       get_raw_samples(index, 1, data);
        uint64_t sample = unpack_sample(data);
        delete[] data;
 
index 3c012a45c3257bcf4c20be27256b3a243ba0cf2b..3621e7f03a6944944a6f07533a55192e406b7c33 100644 (file)
@@ -84,7 +84,7 @@ public:
        void append_payload(shared_ptr<sigrok::Logic> logic);
        void append_payload(void *data, uint64_t data_size);
 
-       const uint8_t* get_samples(int64_t start_sample, int64_t end_sample) const;
+       void get_samples(int64_t start_sample, int64_t end_sample, uint8_t* dest) const;
 
        SegmentLogicDataIterator* begin_sample_iteration(uint64_t start);
        void continue_sample_iteration(SegmentLogicDataIterator* it, uint64_t increase);
index 905b79274efeb6ee5ddebd7045348c5da85d9db3..ae9788fc676724d06166f8ffd851767f5abe6362 100644 (file)
@@ -171,15 +171,16 @@ void Segment::append_samples(void* data, uint64_t samples)
        sample_count_ += samples;
 }
 
-uint8_t* Segment::get_raw_samples(uint64_t start, uint64_t count) const
+void Segment::get_raw_samples(uint64_t start, uint64_t count,
+       uint8_t* dest) const
 {
        assert(start < sample_count_);
        assert(start + count <= sample_count_);
        assert(count > 0);
+       assert(dest != nullptr);
 
        lock_guard<recursive_mutex> lock(mutex_);
 
-       uint8_t* dest = new uint8_t[count * unit_size_];
        uint8_t* dest_ptr = dest;
 
        uint64_t chunk_num = (start * unit_size_) / chunk_size_;
@@ -199,8 +200,6 @@ uint8_t* Segment::get_raw_samples(uint64_t start, uint64_t count) const
                chunk_num++;
                chunk_offs = 0;
        }
-
-       return dest;
 }
 
 SegmentRawDataIterator* Segment::begin_raw_sample_iteration(uint64_t start)
index 7338bd3896f5a055bbe5575b3f06ba514b9cdb90..db75ef710a285ceb75f795667a0826d34f4b8e1b 100644 (file)
@@ -76,7 +76,7 @@ public:
 protected:
        void append_single_sample(void *data);
        void append_samples(void *data, uint64_t samples);
-       uint8_t* get_raw_samples(uint64_t start, uint64_t count) const;
+       void get_raw_samples(uint64_t start, uint64_t count, uint8_t *dest) const;
 
        SegmentRawDataIterator* begin_raw_sample_iteration(uint64_t start);
        void continue_raw_sample_iteration(SegmentRawDataIterator* it, uint64_t increase);
index 3c0305a533b343f79c13627eecb52a35403119af..0f3c8f1be1d32b6beb316488c2067d77b70011f4 100644 (file)
@@ -274,6 +274,7 @@ void SignalBase::conversion_thread_proc(QObject* segment)
                                float min_v, max_v;
                                tie(min_v, max_v) = asegment->get_min_max();
 
+                               float* asamples = new float[ConversionBlockSize];
                                vector<uint8_t> lsamples;
                                lsamples.reserve(ConversionBlockSize);
 
@@ -284,23 +285,21 @@ void SignalBase::conversion_thread_proc(QObject* segment)
 
                                        // Convert as many sample blocks as we can
                                        while ((end_sample - i) > ConversionBlockSize) {
-                                               const float* asamples = asegment->get_samples(i, i + ConversionBlockSize);
+                                               asegment->get_samples(i, i + ConversionBlockSize, asamples);
                                                for (uint32_t j = 0; j < ConversionBlockSize; j++)
                                                        lsamples.push_back(convert_a2l_threshold(threshold, asamples[j]));
                                                lsegment->append_payload(lsamples.data(), lsamples.size());
                                                samples_added(lsegment, i, i + ConversionBlockSize);
                                                i += ConversionBlockSize;
                                                lsamples.clear();
-                                               delete[] asamples;
                                        }
 
                                        // Convert remaining samples
-                                       const float* asamples = asegment->get_samples(i, end_sample);
+                                       asegment->get_samples(i, end_sample, asamples);
                                        for (uint32_t j = 0; j < (end_sample - i); j++)
                                                lsamples.push_back(convert_a2l_threshold(threshold, asamples[j]));
                                        lsegment->append_payload(lsamples.data(), lsamples.size());
                                        samples_added(lsegment, i, end_sample);
-                                       delete[] asamples;
                                }
 
                                if (conversion_type_ == A2LConversionBySchmittTrigger) {
@@ -311,27 +310,27 @@ void SignalBase::conversion_thread_proc(QObject* segment)
 
                                        // Convert as many sample blocks as we can
                                        while ((end_sample - i) > ConversionBlockSize) {
-                                               const float* asamples = asegment->get_samples(i, i + ConversionBlockSize);
+                                               asegment->get_samples(i, i + ConversionBlockSize, asamples);
                                                for (uint32_t j = 0; j < ConversionBlockSize; j++)
                                                        lsamples.push_back(convert_a2l_schmitt_trigger(lo_thr, hi_thr, asamples[j], state));
                                                lsegment->append_payload(lsamples.data(), lsamples.size());
                                                samples_added(lsegment, i, i + ConversionBlockSize);
                                                i += ConversionBlockSize;
                                                lsamples.clear();
-                                               delete[] asamples;
                                        }
 
                                        // Convert remaining samples
-                                       const float* asamples = asegment->get_samples(i, end_sample);
+                                       asegment->get_samples(i, end_sample, asamples);
                                        for (uint32_t j = 0; j < (end_sample - i); j++)
                                                lsamples.push_back(convert_a2l_schmitt_trigger(lo_thr, hi_thr, asamples[j], state));
                                        lsegment->append_payload(lsamples.data(), lsamples.size());
                                        samples_added(lsegment, i, end_sample);
-                                       delete[] asamples;
                                }
 
                                // If acquisition is ongoing, start-/endsample may have changed
                                end_sample = asegment->get_sample_count();
+
+                               delete[] asamples;
                        }
                }
 
index cb643682d57023462e38cc8dbd228bcd2182a353..50815c26dcedfcaff7716f115b2eb376c371960a 100644 (file)
@@ -240,8 +240,8 @@ void StoreSession::store_proc(vector< shared_ptr<data::SignalBase> > achannel_li
                                shared_ptr<sigrok::Channel> achannel = (achannel_list.at(i))->channel();
                                shared_ptr<data::AnalogSegment> asegment = asegment_list.at(i);
 
-                               const float *adata =
-                                       asegment->get_samples(start_sample_, start_sample_ + packet_len);
+                               float *adata = new float[packet_len];
+                               asegment->get_samples(start_sample_, start_sample_ + packet_len, adata);
 
                                auto analog = context->create_analog_packet(
                                        vector<shared_ptr<sigrok::Channel> >{achannel},
@@ -257,11 +257,11 @@ void StoreSession::store_proc(vector< shared_ptr<data::SignalBase> > achannel_li
                        }
 
                        if (lsegment) {
-                               const uint8_t* ldata =
-                                       lsegment->get_samples(start_sample_, start_sample_ + packet_len);
+                               const size_t data_size = packet_len * lunit_size;
+                               uint8_t* ldata = new uint8_t[data_size];
+                               lsegment->get_samples(start_sample_, start_sample_ + packet_len, ldata);
 
-                               const size_t length = packet_len * lunit_size;
-                               auto logic = context->create_logic_packet((void*)ldata, length, lunit_size);
+                               auto logic = context->create_logic_packet((void*)ldata, data_size, lunit_size);
                                const string ldata_str = output_->receive(logic);
 
                                if (output_stream_.is_open())
index b65eba569d54baad700f802238bc225b593ce0b6..f94a70f80f92e6322996969324d149016a8bc06d 100644 (file)
@@ -341,16 +341,16 @@ void AnalogSignal::paint_trace(QPainter &p,
 
        int64_t sample_count = min(points_count, TracePaintBlockSize);
        int64_t block_sample = 0;
-       const float *sample_block = segment->get_samples(start, start + sample_count);
+       float *sample_block = new float[TracePaintBlockSize];
+       segment->get_samples(start, start + sample_count, sample_block);
 
        const int w = 2;
        for (int64_t sample = start; sample != end; sample++, block_sample++) {
 
                if (block_sample == TracePaintBlockSize) {
                        block_sample = 0;
-                       delete[] sample_block;
                        sample_count = min(points_count - sample, TracePaintBlockSize);
-                       sample_block = segment->get_samples(sample, sample + sample_count);
+                       segment->get_samples(sample, sample + sample_count, sample_block);
                }
 
                const float x = (sample / samples_per_pixel -