diff options
author | Thomas Habets <thomas@habets.se> | 2020-04-10 17:40:11 +0100 |
---|---|---|
committer | Marcus Müller <marcus@hostalia.de> | 2020-04-11 01:41:39 +0200 |
commit | b5e8a552c09a1b9a1397e731cc6f54d427df9a67 (patch) | |
tree | dc60d92c7f53d5d15cbf42d2c5a2623a557eb7ca /gnuradio-runtime/lib | |
parent | 616879745ce5d61e6acd54ad84d60359a739a27d (diff) |
runtime: Remove most manual memory management
The remaining ones:
* `pmt_pool.cc`, which is a memory allocator so that makes sense
* the tricky and aptly named `sptr_magic.cc`.
Diffstat (limited to 'gnuradio-runtime/lib')
-rw-r--r-- | gnuradio-runtime/lib/block_executor.cc | 8 | ||||
-rw-r--r-- | gnuradio-runtime/lib/block_executor.h | 3 | ||||
-rw-r--r-- | gnuradio-runtime/lib/buffer.cc | 4 | ||||
-rw-r--r-- | gnuradio-runtime/lib/hier_block2.cc | 6 | ||||
-rw-r--r-- | gnuradio-runtime/lib/logger.cc | 10 | ||||
-rw-r--r-- | gnuradio-runtime/lib/math/qa_fxpt_nco.cc | 10 | ||||
-rw-r--r-- | gnuradio-runtime/lib/math/qa_fxpt_vco.cc | 30 | ||||
-rw-r--r-- | gnuradio-runtime/lib/message.cc | 11 | ||||
-rw-r--r-- | gnuradio-runtime/lib/thread/thread_group.cc | 47 | ||||
-rw-r--r-- | gnuradio-runtime/lib/top_block.cc | 7 | ||||
-rw-r--r-- | gnuradio-runtime/lib/vmcircbuf.cc | 24 |
11 files changed, 66 insertions, 94 deletions
diff --git a/gnuradio-runtime/lib/block_executor.cc b/gnuradio-runtime/lib/block_executor.cc index 9d6d33bb9b..d4c98e1225 100644 --- a/gnuradio-runtime/lib/block_executor.cc +++ b/gnuradio-runtime/lib/block_executor.cc @@ -20,6 +20,7 @@ #include <block_executor.h> #include <stdio.h> #include <boost/format.hpp> +#include <boost/make_unique.hpp> #include <boost/thread.hpp> #include <iostream> #include <limits> @@ -213,11 +214,11 @@ static bool propagate_tags(block::tag_propagation_policy_t policy, } block_executor::block_executor(block_sptr block, int max_noutput_items) - : d_block(block), d_log(0), d_max_noutput_items(max_noutput_items) + : d_block(block), d_max_noutput_items(max_noutput_items) { if (ENABLE_LOGGING) { std::string name = str(boost::format("sst-%03d.log") % which_scheduler++); - d_log = new std::ofstream(name.c_str()); + d_log = boost::make_unique<std::ofstream>(name.c_str()); std::unitbuf(*d_log); // make it unbuffered... *d_log << "block_executor: " << d_block << std::endl; } @@ -232,9 +233,6 @@ block_executor::block_executor(block_sptr block, int max_noutput_items) block_executor::~block_executor() { - if (ENABLE_LOGGING) - delete d_log; - d_block->stop(); // stop any drivers, etc. } diff --git a/gnuradio-runtime/lib/block_executor.h b/gnuradio-runtime/lib/block_executor.h index b2eb2cab83..3b7089419b 100644 --- a/gnuradio-runtime/lib/block_executor.h +++ b/gnuradio-runtime/lib/block_executor.h @@ -15,6 +15,7 @@ #include <gnuradio/runtime_types.h> #include <gnuradio/tags.h> #include <fstream> +#include <memory> namespace gr { @@ -26,7 +27,7 @@ class GR_RUNTIME_API block_executor { protected: block_sptr d_block; // The block we're trying to run - std::ofstream* d_log; + std::unique_ptr<std::ofstream> d_log; // These are allocated here so we don't have to on each iteration diff --git a/gnuradio-runtime/lib/buffer.cc b/gnuradio-runtime/lib/buffer.cc index 91cc43818b..720c72c4ee 100644 --- a/gnuradio-runtime/lib/buffer.cc +++ b/gnuradio-runtime/lib/buffer.cc @@ -82,7 +82,6 @@ buffer::buffer(int nitems, size_t sizeof_item, block_sptr link) : d_base(0), d_bufsize(0), d_max_reader_delay(0), - d_vmcircbuf(0), d_sizeof_item(sizeof_item), d_link(link), d_write_index(0), @@ -103,7 +102,6 @@ buffer_sptr make_buffer(int nitems, size_t sizeof_item, block_sptr link) buffer::~buffer() { - delete d_vmcircbuf; assert(d_readers.size() == 0); s_buffer_count--; } @@ -139,7 +137,7 @@ bool buffer::allocate_buffer(int nitems, size_t sizeof_item) } d_bufsize = nitems; - d_vmcircbuf = gr::vmcircbuf_sysconfig::make(d_bufsize * d_sizeof_item); + d_vmcircbuf.reset(gr::vmcircbuf_sysconfig::make(d_bufsize * d_sizeof_item)); if (d_vmcircbuf == 0) { std::cerr << "gr::buffer::allocate_buffer: failed to allocate buffer of size " << d_bufsize * d_sizeof_item / 1024 << " KB\n"; diff --git a/gnuradio-runtime/lib/hier_block2.cc b/gnuradio-runtime/lib/hier_block2.cc index 58c27b3ef3..035152051c 100644 --- a/gnuradio-runtime/lib/hier_block2.cc +++ b/gnuradio-runtime/lib/hier_block2.cc @@ -16,6 +16,7 @@ #include <gnuradio/flowgraph.h> #include <gnuradio/hier_block2.h> #include <gnuradio/io_signature.h> +#include <boost/make_unique.hpp> #include <iostream> namespace gr { @@ -30,11 +31,13 @@ hier_block2_sptr make_hier_block2(const std::string& name, new hier_block2(name, input_signature, output_signature)); } +hier_block2::hier_block2() {} + hier_block2::hier_block2(const std::string& name, gr::io_signature::sptr input_signature, gr::io_signature::sptr output_signature) : basic_block(name, input_signature, output_signature), - d_detail(new hier_block2_detail(this)), + d_detail(boost::make_unique<hier_block2_detail>(this)), hier_message_ports_in(pmt::PMT_NIL), hier_message_ports_out(pmt::PMT_NIL) { @@ -46,7 +49,6 @@ hier_block2::~hier_block2() { disconnect_all(); gnuradio::detail::sptr_magic::cancel_initial_sptr(this); - delete d_detail; } hier_block2::opaque_self hier_block2::self() { return shared_from_this(); } diff --git a/gnuradio-runtime/lib/logger.cc b/gnuradio-runtime/lib/logger.cc index f281f82e69..f282ca9276 100644 --- a/gnuradio-runtime/lib/logger.cc +++ b/gnuradio-runtime/lib/logger.cc @@ -20,6 +20,7 @@ #include <gnuradio/logger.h> #include <gnuradio/prefs.h> +#include <boost/make_unique.hpp> #include <algorithm> #include <stdexcept> @@ -87,7 +88,7 @@ void logger_config::load_config(std::string filename, unsigned int watch_period) instance.filename = filename; instance.watch_period = watch_period; // Stop any file watching thread - if (instance.watch_thread != NULL) + if (instance.watch_thread) stop_watch(); // Load configuration // std::cout<<"GNURadio Loading logger @@ -95,8 +96,8 @@ void logger_config::load_config(std::string filename, unsigned int watch_period) logger_configured = logger_load_config(instance.filename); // Start watch if required if (instance.watch_period > 0) { - instance.watch_thread = - new boost::thread(watch_file, instance.filename, instance.watch_period); + instance.watch_thread = boost::make_unique<boost::thread>( + watch_file, instance.filename, instance.watch_period); } } } @@ -108,8 +109,7 @@ void logger_config::stop_watch() if (instance.watch_thread) { instance.watch_thread->interrupt(); instance.watch_thread->join(); - delete (instance.watch_thread); - instance.watch_thread = NULL; + instance.watch_thread.reset(); } } diff --git a/gnuradio-runtime/lib/math/qa_fxpt_nco.cc b/gnuradio-runtime/lib/math/qa_fxpt_nco.cc index dd6bc2cb9f..1259dd6f9a 100644 --- a/gnuradio-runtime/lib/math/qa_fxpt_nco.cc +++ b/gnuradio-runtime/lib/math/qa_fxpt_nco.cc @@ -72,8 +72,8 @@ BOOST_AUTO_TEST_CASE(t1) { gr::nco<float, float> ref_nco; gr::fxpt_nco new_nco; - gr_complex* ref_block = new gr_complex[SIN_COS_BLOCK_SIZE]; - gr_complex* new_block = new gr_complex[SIN_COS_BLOCK_SIZE]; + std::vector<gr_complex> ref_block(SIN_COS_BLOCK_SIZE); + std::vector<gr_complex> new_block(SIN_COS_BLOCK_SIZE); double max_error = 0; ref_nco.set_freq((float)(2 * GR_M_PI / SIN_COS_FREQ)); @@ -81,8 +81,8 @@ BOOST_AUTO_TEST_CASE(t1) BOOST_CHECK(std::abs(ref_nco.get_freq() - new_nco.get_freq()) <= SIN_COS_TOLERANCE); - ref_nco.sincos((gr_complex*)ref_block, SIN_COS_BLOCK_SIZE); - new_nco.sincos((gr_complex*)new_block, SIN_COS_BLOCK_SIZE); + ref_nco.sincos((gr_complex*)ref_block.data(), SIN_COS_BLOCK_SIZE); + new_nco.sincos((gr_complex*)new_block.data(), SIN_COS_BLOCK_SIZE); for (int i = 0; i < SIN_COS_BLOCK_SIZE; i++) { BOOST_CHECK(std::abs(ref_block[i].real() - new_block[i].real()) <= @@ -96,6 +96,4 @@ BOOST_AUTO_TEST_CASE(t1) BOOST_CHECK(std::abs(ref_nco.get_phase() - new_nco.get_phase()) <= SIN_COS_TOLERANCE); // printf ("Fxpt max error %.9f, max phase error %.9f\n", max_error, // max_phase_error); - delete[] ref_block; - delete[] new_block; } diff --git a/gnuradio-runtime/lib/math/qa_fxpt_vco.cc b/gnuradio-runtime/lib/math/qa_fxpt_vco.cc index 49af65609d..2cd8a4733b 100644 --- a/gnuradio-runtime/lib/math/qa_fxpt_vco.cc +++ b/gnuradio-runtime/lib/math/qa_fxpt_vco.cc @@ -64,17 +64,19 @@ BOOST_AUTO_TEST_CASE(t1) { gr::vco<float, float> ref_vco; gr::fxpt_vco new_vco; - float* ref_block = new float[SIN_COS_BLOCK_SIZE]; - float* new_block = new float[SIN_COS_BLOCK_SIZE]; - float* input = new float[SIN_COS_BLOCK_SIZE]; + std::vector<float> ref_block(SIN_COS_BLOCK_SIZE); + std::vector<float> new_block(SIN_COS_BLOCK_SIZE); + std::vector<float> input(SIN_COS_BLOCK_SIZE); double max_error = 0; for (int i = 0; i < SIN_COS_BLOCK_SIZE; i++) { input[i] = sin(double(i)); } - ref_vco.cos(ref_block, input, SIN_COS_BLOCK_SIZE, SIN_COS_K, SIN_COS_AMPL); - new_vco.cos(new_block, input, SIN_COS_BLOCK_SIZE, SIN_COS_K, SIN_COS_AMPL); + ref_vco.cos( + ref_block.data(), input.data(), SIN_COS_BLOCK_SIZE, SIN_COS_K, SIN_COS_AMPL); + new_vco.cos( + new_block.data(), input.data(), SIN_COS_BLOCK_SIZE, SIN_COS_K, SIN_COS_AMPL); for (int i = 0; i < SIN_COS_BLOCK_SIZE; i++) { BOOST_CHECK(std::abs(ref_block[i] - new_block[i]) <= SIN_COS_TOLERANCE); @@ -83,9 +85,6 @@ BOOST_AUTO_TEST_CASE(t1) BOOST_CHECK(std::abs(ref_vco.get_phase() - new_vco.get_phase()) <= SIN_COS_TOLERANCE); // printf ("Fxpt max error %.9f, max phase error %.9f\n", max_error, // ref_vco.get_phase()-new_vco.get_phase()); - delete[] ref_block; - delete[] new_block; - delete[] input; } @@ -93,17 +92,19 @@ BOOST_AUTO_TEST_CASE(t2) { gr::vco<gr_complex, float> ref_vco; gr::fxpt_vco new_vco; - gr_complex* ref_block = new gr_complex[SIN_COS_BLOCK_SIZE]; - gr_complex* new_block = new gr_complex[SIN_COS_BLOCK_SIZE]; - float* input = new float[SIN_COS_BLOCK_SIZE]; + std::vector<gr_complex> ref_block(SIN_COS_BLOCK_SIZE); + std::vector<gr_complex> new_block(SIN_COS_BLOCK_SIZE); + std::vector<float> input(SIN_COS_BLOCK_SIZE); double max_error = 0; for (int i = 0; i < SIN_COS_BLOCK_SIZE; i++) { input[i] = sin(double(i)); } - ref_vco.sincos(ref_block, input, SIN_COS_BLOCK_SIZE, SIN_COS_K, SIN_COS_AMPL); - new_vco.sincos(new_block, input, SIN_COS_BLOCK_SIZE, SIN_COS_K, SIN_COS_AMPL); + ref_vco.sincos( + ref_block.data(), input.data(), SIN_COS_BLOCK_SIZE, SIN_COS_K, SIN_COS_AMPL); + new_vco.sincos( + new_block.data(), input.data(), SIN_COS_BLOCK_SIZE, SIN_COS_K, SIN_COS_AMPL); for (int i = 0; i < SIN_COS_BLOCK_SIZE; i++) { BOOST_CHECK(std::abs(ref_block[i] - new_block[i]) <= SIN_COS_TOLERANCE); @@ -112,7 +113,4 @@ BOOST_AUTO_TEST_CASE(t2) BOOST_CHECK(std::abs(ref_vco.get_phase() - new_vco.get_phase()) <= SIN_COS_TOLERANCE); // printf ("Fxpt max error %.9f, max phase error %.9f\n", max_error, // ref_vco.get_phase()-new_vco.get_phase()); - delete[] ref_block; - delete[] new_block; - delete[] input; } diff --git a/gnuradio-runtime/lib/message.cc b/gnuradio-runtime/lib/message.cc index fa8b1e3e85..ab5130a2d0 100644 --- a/gnuradio-runtime/lib/message.cc +++ b/gnuradio-runtime/lib/message.cc @@ -34,14 +34,13 @@ message::make_from_string(const std::string s, long type, double arg1, double ar } message::message(long type, double arg1, double arg2, size_t length) - : d_type(type), d_arg1(arg1), d_arg2(arg2) + : d_type(type), d_arg1(arg1), d_arg2(arg2), d_buf(length) { if (length == 0) - d_buf_start = d_msg_start = d_msg_end = d_buf_end = 0; + d_msg_start = d_msg_end = nullptr; else { - d_buf_start = new unsigned char[length]; - d_msg_start = d_buf_start; - d_msg_end = d_buf_end = d_buf_start + length; + d_msg_start = d_buf.data(); + d_msg_end = d_msg_start + length; } s_ncurrently_allocated++; } @@ -49,8 +48,6 @@ message::message(long type, double arg1, double arg2, size_t length) message::~message() { assert(d_next == 0); - delete[] d_buf_start; - d_msg_start = d_msg_end = d_buf_end = 0; s_ncurrently_allocated--; } diff --git a/gnuradio-runtime/lib/thread/thread_group.cc b/gnuradio-runtime/lib/thread/thread_group.cc index 1d9b8c846c..6c7512ab28 100644 --- a/gnuradio-runtime/lib/thread/thread_group.cc +++ b/gnuradio-runtime/lib/thread/thread_group.cc @@ -13,46 +13,36 @@ */ #include <gnuradio/thread/thread_group.h> +#include <boost/make_unique.hpp> namespace gr { namespace thread { thread_group::thread_group() {} -thread_group::~thread_group() -{ - // We shouldn't have to scoped_lock here, since referencing this - // object from another thread while we're deleting it in the - // current thread is going to lead to undefined behavior any - // way. - for (std::list<boost::thread*>::iterator it = m_threads.begin(); - it != m_threads.end(); - ++it) { - delete (*it); - } -} +thread_group::~thread_group() {} boost::thread* thread_group::create_thread(const boost::function0<void>& threadfunc) { // No scoped_lock required here since the only "shared data" that's // modified here occurs inside add_thread which does scoped_lock. - std::unique_ptr<boost::thread> thrd(new boost::thread(threadfunc)); - add_thread(thrd.get()); - return thrd.release(); + auto thrd = boost::make_unique<boost::thread>(threadfunc); + auto thrdp = thrd.get(); + add_thread(std::move(thrd)); + return thrdp; } -void thread_group::add_thread(boost::thread* thrd) +void thread_group::add_thread(std::unique_ptr<boost::thread> thrd) { boost::lock_guard<boost::shared_mutex> guard(m_mutex); // For now we'll simply ignore requests to add a thread object // multiple times. Should we consider this an error and either // throw or return an error value? - std::list<boost::thread*>::iterator it = - std::find(m_threads.begin(), m_threads.end(), thrd); + auto it = std::find(m_threads.begin(), m_threads.end(), thrd); BOOST_ASSERT(it == m_threads.end()); if (it == m_threads.end()) - m_threads.push_back(thrd); + m_threads.push_back(std::move(thrd)); } void thread_group::remove_thread(boost::thread* thrd) @@ -62,8 +52,10 @@ void thread_group::remove_thread(boost::thread* thrd) // For now we'll simply ignore requests to remove a thread // object that's not in the group. Should we consider this an // error and either throw or return an error value? - std::list<boost::thread*>::iterator it = - std::find(m_threads.begin(), m_threads.end(), thrd); + auto it = std::find_if( + m_threads.begin(), + m_threads.end(), + [&thrd](std::unique_ptr<boost::thread>& it) -> bool { return thrd == it.get(); }); BOOST_ASSERT(it != m_threads.end()); if (it != m_threads.end()) m_threads.erase(it); @@ -72,21 +64,16 @@ void thread_group::remove_thread(boost::thread* thrd) void thread_group::join_all() { boost::shared_lock<boost::shared_mutex> guard(m_mutex); - for (std::list<boost::thread*>::iterator it = m_threads.begin(); - it != m_threads.end(); - ++it) { - (*it)->join(); + for (auto& thrd : m_threads) { + thrd->join(); } } void thread_group::interrupt_all() { boost::shared_lock<boost::shared_mutex> guard(m_mutex); - for (std::list<boost::thread*>::iterator it = m_threads.begin(), - end = m_threads.end(); - it != end; - ++it) { - (*it)->interrupt(); + for (auto& thrd : m_threads) { + thrd->interrupt(); } } diff --git a/gnuradio-runtime/lib/top_block.cc b/gnuradio-runtime/lib/top_block.cc index 97aa36b22a..aad25f005a 100644 --- a/gnuradio-runtime/lib/top_block.cc +++ b/gnuradio-runtime/lib/top_block.cc @@ -18,6 +18,7 @@ #include <gnuradio/prefs.h> #include <gnuradio/top_block.h> #include <unistd.h> +#include <boost/make_unique.hpp> #include <iostream> namespace gr { @@ -27,17 +28,15 @@ top_block_sptr make_top_block(const std::string& name, bool catch_exceptions) } top_block::top_block(const std::string& name, bool catch_exceptions) - : hier_block2(name, io_signature::make(0, 0, 0), io_signature::make(0, 0, 0)) + : hier_block2(name, io_signature::make(0, 0, 0), io_signature::make(0, 0, 0)), + d_impl(boost::make_unique<top_block_impl>(this, catch_exceptions)) { - d_impl = new top_block_impl(this, catch_exceptions); } top_block::~top_block() { stop(); wait(); - - delete d_impl; } void top_block::start(int max_noutput_items) diff --git a/gnuradio-runtime/lib/vmcircbuf.cc b/gnuradio-runtime/lib/vmcircbuf.cc index e4821b0d78..4af5262671 100644 --- a/gnuradio-runtime/lib/vmcircbuf.cc +++ b/gnuradio-runtime/lib/vmcircbuf.cc @@ -108,23 +108,23 @@ void vmcircbuf_sysconfig::set_default_factory(vmcircbuf_factory* f) // test code for vmcircbuf factories // ------------------------------------------------------------------------ -static void init_buffer(vmcircbuf* c, int counter, int size) +static void init_buffer(const vmcircbuf& c, int counter, int size) { - unsigned int* p = (unsigned int*)c->pointer_to_first_copy(); + unsigned int* p = (unsigned int*)c.pointer_to_first_copy(); for (unsigned int i = 0; i < size / sizeof(int); i++) p[i] = counter + i; } static bool -check_mapping(vmcircbuf* c, int counter, int size, const char* msg, bool verbose) +check_mapping(const vmcircbuf& c, int counter, int size, const char* msg, bool verbose) { bool ok = true; if (verbose) fprintf(stderr, "... %s", msg); - unsigned int* p1 = (unsigned int*)c->pointer_to_first_copy(); - unsigned int* p2 = (unsigned int*)c->pointer_to_second_copy(); + unsigned int* p1 = (unsigned int*)c.pointer_to_first_copy(); + unsigned int* p2 = (unsigned int*)c.pointer_to_second_copy(); // fprintf(stderr, "p1 = %p, p2 = %p\n", p1, p2); @@ -167,13 +167,13 @@ test_a_bunch(vmcircbuf_factory* factory, int n, int size, int* start_ptr, bool v { bool ok = true; std::vector<int> counter(n); - std::vector<vmcircbuf*> c(n); + std::vector<std::unique_ptr<vmcircbuf>> c(n); int cum_size = 0; for (int i = 0; i < n; i++) { counter[i] = *start_ptr; *start_ptr += size; - if ((c[i] = factory->make(size)) == 0) { + if ((c[i] = std::unique_ptr<vmcircbuf>(factory->make(size))) == 0) { if (verbose) fprintf( stderr, @@ -183,21 +183,15 @@ test_a_bunch(vmcircbuf_factory* factory, int n, int size, int* start_ptr, bool v memsize(cum_size)); return false; } - init_buffer(c[i], counter[i], size); + init_buffer(*c[i], counter[i], size); cum_size += size; } for (int i = 0; i < n; i++) { std::string msg = str(boost::format("test_a_bunch_%dx%s[%d]") % n % memsize(size) % i); - ok &= check_mapping(c[i], counter[i], size, msg.c_str(), verbose); + ok &= check_mapping(*c[i], counter[i], size, msg.c_str(), verbose); } - - for (int i = 0; i < n; i++) { - delete c[i]; - c[i] = 0; - } - return ok; } |