From: Jens Steinhauser Date: Wed, 27 Aug 2014 09:12:40 +0000 (+0200) Subject: Snapshot classes: Use vector instead of raw pointer X-Git-Tag: pulseview-0.3.0~558 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=c82c7e4e330ce5aa04c4f3b8e6f8de85e3e288e1;p=pulseview.git Snapshot classes: Use vector instead of raw pointer As a side-effect this turns a segfault (see bug #397) into a segfault with a more usable message: terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc Aborted (core dumped) --- diff --git a/pv/data/analogsnapshot.cpp b/pv/data/analogsnapshot.cpp index 9e9b5a0f..2a14d1ea 100644 --- a/pv/data/analogsnapshot.cpp +++ b/pv/data/analogsnapshot.cpp @@ -68,9 +68,9 @@ void AnalogSnapshot::append_interleaved_samples(const float *data, lock_guard lock(_mutex); - _data = realloc(_data, (_sample_count + sample_count) * sizeof(float)); + _data.resize((_sample_count + sample_count) * sizeof(float)); - float *dst = (float*)_data + _sample_count; + float *dst = (float*)_data.data() + _sample_count; const float *dst_end = dst + sample_count; while (dst != dst_end) { @@ -96,7 +96,7 @@ const float* AnalogSnapshot::get_samples( lock_guard lock(_mutex); float *const data = new float[end_sample - start_sample]; - memcpy(data, (float*)_data + start_sample, sizeof(float) * + memcpy(data, (float*)_data.data() + start_sample, sizeof(float) * (end_sample - start_sample)); return data; } @@ -156,9 +156,9 @@ void AnalogSnapshot::append_payload_to_envelope_levels() dest_ptr = e0.samples + prev_length; // Iterate through the samples to populate the first level mipmap - const float *const end_src_ptr = (float*)_data + + const float *const end_src_ptr = (float*)_data.data() + e0.length * EnvelopeScaleFactor; - for (const float *src_ptr = (float*)_data + + for (const float *src_ptr = (float*)_data.data() + prev_length * EnvelopeScaleFactor; src_ptr < end_src_ptr; src_ptr += EnvelopeScaleFactor) { diff --git a/pv/data/logicsnapshot.cpp b/pv/data/logicsnapshot.cpp index 3bd43c92..f1ff3c3c 100644 --- a/pv/data/logicsnapshot.cpp +++ b/pv/data/logicsnapshot.cpp @@ -162,7 +162,7 @@ void LogicSnapshot::get_samples(uint8_t *const data, lock_guard lock(_mutex); const size_t size = (end_sample - start_sample) * _unit_size; - memcpy(data, (const uint8_t*)_data + start_sample * _unit_size, size); + memcpy(data, (const uint8_t*)_data.data() + start_sample * _unit_size, size); } void LogicSnapshot::reallocate_mipmap_level(MipMapLevel &m) @@ -201,9 +201,9 @@ void LogicSnapshot::append_payload_to_mipmap() dest_ptr = (uint8_t*)m0.data + prev_length * _unit_size; // Iterate through the samples to populate the first level mipmap - const uint8_t *const end_src_ptr = (uint8_t*)_data + + const uint8_t *const end_src_ptr = (uint8_t*)_data.data() + m0.length * _unit_size * MipMapScaleFactor; - for (src_ptr = (uint8_t*)_data + + for (src_ptr = (uint8_t*)_data.data() + prev_length * _unit_size * MipMapScaleFactor; src_ptr < end_src_ptr;) { @@ -266,7 +266,7 @@ uint64_t LogicSnapshot::get_sample(uint64_t index) const assert(_data); assert(index < _sample_count); - return unpack_sample((uint8_t*)_data + index * _unit_size); + return unpack_sample((uint8_t*)_data.data() + index * _unit_size); } void LogicSnapshot::get_subsampled_edges( diff --git a/pv/data/snapshot.cpp b/pv/data/snapshot.cpp index 2f3f0413..e8e285e7 100644 --- a/pv/data/snapshot.cpp +++ b/pv/data/snapshot.cpp @@ -31,7 +31,6 @@ namespace pv { namespace data { Snapshot::Snapshot(int unit_size) : - _data(NULL), _sample_count(0), _capacity(0), _unit_size(unit_size) @@ -43,7 +42,6 @@ Snapshot::Snapshot(int unit_size) : Snapshot::~Snapshot() { lock_guard lock(_mutex); - free(_data); } uint64_t Snapshot::get_sample_count() const @@ -64,14 +62,14 @@ void Snapshot::set_capacity(const uint64_t new_capacity) assert(_capacity >= _sample_count); if (new_capacity > _capacity) { _capacity = new_capacity; - _data = realloc(_data, (new_capacity * _unit_size) + sizeof(uint64_t)); + _data.resize((new_capacity * _unit_size) + sizeof(uint64_t)); } } uint64_t Snapshot::capacity() const { lock_guard lock(_mutex); - return _capacity; + return _data.size(); } void Snapshot::append_data(void *data, uint64_t samples) @@ -86,7 +84,7 @@ void Snapshot::append_data(void *data, uint64_t samples) set_capacity(_sample_count + samples); } - memcpy((uint8_t*)_data + _sample_count * _unit_size, + memcpy((uint8_t*)_data.data() + _sample_count * _unit_size, data, samples * _unit_size); _sample_count += samples; } diff --git a/pv/data/snapshot.h b/pv/data/snapshot.h index 49916440..e4b9c0e8 100644 --- a/pv/data/snapshot.h +++ b/pv/data/snapshot.h @@ -25,6 +25,7 @@ #include #include +#include namespace pv { namespace data { @@ -72,7 +73,7 @@ protected: protected: mutable std::recursive_mutex _mutex; - void *_data; + std::vector _data; uint64_t _sample_count; uint64_t _capacity; int _unit_size;