diff options
author | Thomas Habets <thomas@habets.se> | 2020-08-29 22:10:49 +0100 |
---|---|---|
committer | mormj <34754695+mormj@users.noreply.github.com> | 2020-09-09 12:45:08 -0400 |
commit | 3e40f271c4fadbcc2a3d5f67895e64f18117dfd5 (patch) | |
tree | a5ff2b82a81dca93910098b404b418e5202a0d1e | |
parent | 0ba6f7a230c25f3467a01f5be65916c948678e4a (diff) |
blocks/correctiq: Remove manual memory management & add const
-rw-r--r-- | gr-blocks/include/gnuradio/blocks/correctiq_auto.h | 4 | ||||
-rw-r--r-- | gr-blocks/include/gnuradio/blocks/correctiq_man.h | 4 | ||||
-rw-r--r-- | gr-blocks/lib/correctiq_auto_impl.cc | 44 | ||||
-rw-r--r-- | gr-blocks/lib/correctiq_auto_impl.h | 14 | ||||
-rw-r--r-- | gr-blocks/lib/correctiq_man_impl.cc | 49 | ||||
-rw-r--r-- | gr-blocks/lib/correctiq_man_impl.h | 11 |
6 files changed, 41 insertions, 85 deletions
diff --git a/gr-blocks/include/gnuradio/blocks/correctiq_auto.h b/gr-blocks/include/gnuradio/blocks/correctiq_auto.h index 2ae9864b58..b8469e49fd 100644 --- a/gr-blocks/include/gnuradio/blocks/correctiq_auto.h +++ b/gr-blocks/include/gnuradio/blocks/correctiq_auto.h @@ -40,8 +40,8 @@ public: * creating new instances. */ - virtual double get_freq() = 0; - virtual float get_gain() = 0; + virtual double get_freq() const = 0; + virtual float get_gain() const = 0; virtual void set_freq(double newValue) = 0; virtual void set_gain(float newValue) = 0; diff --git a/gr-blocks/include/gnuradio/blocks/correctiq_man.h b/gr-blocks/include/gnuradio/blocks/correctiq_man.h index a9cf6f85aa..c45da098af 100644 --- a/gr-blocks/include/gnuradio/blocks/correctiq_man.h +++ b/gr-blocks/include/gnuradio/blocks/correctiq_man.h @@ -38,8 +38,8 @@ public: * creating new instances. */ - virtual float get_real() = 0; - virtual float get_imag() = 0; + virtual float get_real() const = 0; + virtual float get_imag() const = 0; virtual void set_real(float newValue) = 0; virtual void set_imag(float newValue) = 0; diff --git a/gr-blocks/lib/correctiq_auto_impl.cc b/gr-blocks/lib/correctiq_auto_impl.cc index 31fab551fc..346106a545 100644 --- a/gr-blocks/lib/correctiq_auto_impl.cc +++ b/gr-blocks/lib/correctiq_auto_impl.cc @@ -40,19 +40,15 @@ correctiq_auto_impl::correctiq_auto_impl(double samp_rate, d_avg_real(0.0), d_avg_img(0.0), d_ratio(1e-05f), + d_k(d_avg_real, d_avg_img), d_samp_rate(samp_rate), d_freq(freq), d_gain(gain), d_sync_window(sync_window), d_synchronized(false), - d_buffer_size(8192), - d_volk_const_buffer(NULL) + d_max_sync_samples((long)(d_samp_rate * (double)d_sync_window)) { - d_max_sync_samples = (long)(d_samp_rate * (double)d_sync_window); - - set_const_buffer(d_buffer_size); - d_k = gr_complex(0.0, 0.0); - fill_const_buffer(); + set_const_buffer(8192); message_port_register_in(pmt::mp("rsync")); set_msg_handler(pmt::mp("rsync"), @@ -108,38 +104,22 @@ void correctiq_auto_impl::handle_resync(pmt::pmt_t msg) /* * Our virtual destructor. */ -correctiq_auto_impl::~correctiq_auto_impl() -{ - if (d_volk_const_buffer) - volk_free(d_volk_const_buffer); -} +correctiq_auto_impl::~correctiq_auto_impl() {} void correctiq_auto_impl::set_const_buffer(int new_size) { - d_buffer_size = new_size; - - if (d_volk_const_buffer) { - volk_free(d_volk_const_buffer); - } - - d_volk_const_buffer = reinterpret_cast<gr_complex*>( - volk_malloc(sizeof(gr_complex) * d_buffer_size, volk_get_alignment())); - + d_volk_const_buffer.resize(new_size); fill_const_buffer(); } void correctiq_auto_impl::fill_const_buffer() { - gr_complex* tmp_ptr = d_volk_const_buffer; - - for (int i = 0; i < d_buffer_size; i++) { - *tmp_ptr++ = d_k; - } + std::fill(std::begin(d_volk_const_buffer), std::end(d_volk_const_buffer), d_k); } -double correctiq_auto_impl::get_freq() { return d_freq; } +double correctiq_auto_impl::get_freq() const { return d_freq; } -float correctiq_auto_impl::get_gain() { return d_gain; } +float correctiq_auto_impl::get_gain() const { return d_gain; } void correctiq_auto_impl::set_freq(double new_value) { @@ -171,15 +151,17 @@ int correctiq_auto_impl::work(int noutput_items, out[i].imag(in[i].imag() - d_avg_img); } } else { - if (noutput_items > d_buffer_size) + if (noutput_items > static_cast<int>(d_volk_const_buffer.size())) set_const_buffer(noutput_items); // Inputs are complex but we're casting as floats to leverage volk const float* in = (const float*)input_items[0]; float* out = (float*)output_items[0]; - volk_32f_x2_add_32f( - out, in, reinterpret_cast<float*>(d_volk_const_buffer), 2 * noutput_items); + volk_32f_x2_add_32f(out, + in, + reinterpret_cast<float*>(d_volk_const_buffer.data()), + 2 * noutput_items); } if (!d_synchronized && (d_sync_counter >= d_max_sync_samples)) { diff --git a/gr-blocks/lib/correctiq_auto_impl.h b/gr-blocks/lib/correctiq_auto_impl.h index 24e56fc380..a6cc87f154 100644 --- a/gr-blocks/lib/correctiq_auto_impl.h +++ b/gr-blocks/lib/correctiq_auto_impl.h @@ -12,6 +12,7 @@ #define INCLUDED_CORRECTIQ_CORRECTIQ_AUTO_IMPL_H #include <gnuradio/blocks/correctiq_auto.h> +#include <volk/volk_alloc.hh> namespace gr { namespace blocks { @@ -32,15 +33,14 @@ private: long d_sync_counter; bool d_synchronized; - long d_max_sync_samples; + const long d_max_sync_samples; void send_sync_values(); void send_syncing(); void trigger_resync(std::string reason); - int d_buffer_size; - gr_complex* d_volk_const_buffer; + volk::vector<gr_complex> d_volk_const_buffer; void set_const_buffer(int new_size); void fill_const_buffer(); @@ -49,11 +49,11 @@ public: correctiq_auto_impl(double samp_rate, double freq, float gain, float sync_window); ~correctiq_auto_impl(); - virtual double get_freq(); - virtual float get_gain(); + double get_freq() const override; + float get_gain() const override; - virtual void set_freq(double new_value); - virtual void set_gain(float new_value); + void set_freq(double new_value) override; + void set_gain(float new_value) override; void handle_resync(pmt::pmt_t msg); diff --git a/gr-blocks/lib/correctiq_man_impl.cc b/gr-blocks/lib/correctiq_man_impl.cc index f9df4a496e..47a6c970a0 100644 --- a/gr-blocks/lib/correctiq_man_impl.cc +++ b/gr-blocks/lib/correctiq_man_impl.cc @@ -31,14 +31,9 @@ correctiq_man_impl::correctiq_man_impl(float real, float imag) : gr::sync_block("correctiq_man", gr::io_signature::make(1, 1, sizeof(gr_complex)), gr::io_signature::make(1, 1, sizeof(gr_complex))), - d_avg_real(real), - d_avg_img(imag), - d_buffer_size(8192), - d_volk_const_buffer(NULL) + d_k(real, imag) { - d_k = gr_complex(d_avg_real, d_avg_img); - - set_const_buffer(d_buffer_size); + set_const_buffer(8192); message_port_register_in(pmt::mp("set_real")); set_msg_handler(pmt::mp("set_real"), @@ -51,53 +46,35 @@ correctiq_man_impl::correctiq_man_impl(float real, float imag) /* * Our virtual destructor. */ -correctiq_man_impl::~correctiq_man_impl() -{ - if (d_volk_const_buffer) - volk_free(d_volk_const_buffer); -} +correctiq_man_impl::~correctiq_man_impl() {} void correctiq_man_impl::set_const_buffer(int new_size) { - d_buffer_size = new_size; - - if (d_volk_const_buffer) { - volk_free(d_volk_const_buffer); - } - - d_volk_const_buffer = reinterpret_cast<gr_complex*>( - volk_malloc(sizeof(gr_complex) * d_buffer_size, volk_get_alignment())); - + d_volk_const_buffer.resize(new_size); fill_const_buffer(); } void correctiq_man_impl::fill_const_buffer() { - gr_complex* tmp_ptr = d_volk_const_buffer; - - for (int i = 0; i < d_buffer_size; i++) { - *tmp_ptr++ = d_k; - } + std::fill(std::begin(d_volk_const_buffer), std::end(d_volk_const_buffer), d_k); } -float correctiq_man_impl::get_real() { return d_avg_real; } -float correctiq_man_impl::get_imag() { return d_avg_img; } +float correctiq_man_impl::get_real() const { return d_k.real(); } +float correctiq_man_impl::get_imag() const { return d_k.imag(); } -void correctiq_man_impl::set_real(float new_value) +void correctiq_man_impl::set_real(float real) { gr::thread::scoped_lock guard(d_setlock); - d_avg_real = new_value; - d_k = gr_complex(d_avg_real, d_avg_img); + d_k = gr_complex(real, d_k.imag()); fill_const_buffer(); } -void correctiq_man_impl::set_imag(float new_value) +void correctiq_man_impl::set_imag(float imag) { gr::thread::scoped_lock guard(d_setlock); - d_avg_img = new_value; - d_k = gr_complex(d_avg_real, d_avg_img); + d_k = gr_complex(d_k.real(), imag); fill_const_buffer(); } @@ -151,7 +128,7 @@ int correctiq_man_impl::work(int noutput_items, { gr::thread::scoped_lock guard(d_setlock); - if (noutput_items > d_buffer_size) + if (noutput_items > static_cast<int>(d_volk_const_buffer.size())) set_const_buffer(noutput_items); // Inputs are complex but we're casting as floats to leverage volk @@ -159,7 +136,7 @@ int correctiq_man_impl::work(int noutput_items, float* out = (float*)output_items[0]; volk_32f_x2_add_32f( - out, in, reinterpret_cast<float*>(d_volk_const_buffer), 2 * noutput_items); + out, in, reinterpret_cast<float*>(d_volk_const_buffer.data()), 2 * noutput_items); // Tell runtime system how many output items we produced. return noutput_items; diff --git a/gr-blocks/lib/correctiq_man_impl.h b/gr-blocks/lib/correctiq_man_impl.h index edaaf2ff0e..313961ebac 100644 --- a/gr-blocks/lib/correctiq_man_impl.h +++ b/gr-blocks/lib/correctiq_man_impl.h @@ -12,6 +12,7 @@ #define INCLUDED_CORRECTIQ_CORRECTIQ_MAN_IMPL_H #include <gnuradio/blocks/correctiq_man.h> +#include <volk/volk_alloc.hh> namespace gr { namespace blocks { @@ -19,13 +20,9 @@ namespace blocks { class correctiq_man_impl : public correctiq_man { private: - float d_avg_real; - float d_avg_img; - gr_complex d_k; - int d_buffer_size; - gr_complex* d_volk_const_buffer; + volk::vector<gr_complex> d_volk_const_buffer; void set_const_buffer(int new_size); void fill_const_buffer(); @@ -34,8 +31,8 @@ public: correctiq_man_impl(float real, float imag); ~correctiq_man_impl(); - virtual float get_real(); - virtual float get_imag(); + float get_real() const override; + float get_imag() const override; virtual void set_real(float new_value); virtual void set_imag(float new_value); |