diff options
author | Thomas Habets <thomas@habets.se> | 2020-08-12 13:18:30 +0100 |
---|---|---|
committer | Martin Braun <martin@gnuradio.org> | 2020-08-14 04:08:48 -0700 |
commit | 84e007d9be2a8fe1b3dd050f53fde35f77860a62 (patch) | |
tree | fb28d6ab3eccdf958474446b136f44adc686c06a | |
parent | fe436e255dc378189bc414a8bbb9cdfc44e52783 (diff) |
digital/qa_header: Remove manual memory management
-rw-r--r-- | gr-digital/include/gnuradio/digital/header_buffer.h | 5 | ||||
-rw-r--r-- | gr-digital/lib/qa_header_buffer.cc | 30 | ||||
-rw-r--r-- | gr-digital/lib/qa_header_format.cc | 53 | ||||
-rw-r--r-- | gr-digital/python/digital/bindings/header_buffer_python.cc | 2 |
4 files changed, 32 insertions, 58 deletions
diff --git a/gr-digital/include/gnuradio/digital/header_buffer.h b/gr-digital/include/gnuradio/digital/header_buffer.h index b2d6b56c32..ba6e4ee3cc 100644 --- a/gr-digital/include/gnuradio/digital/header_buffer.h +++ b/gr-digital/include/gnuradio/digital/header_buffer.h @@ -71,8 +71,7 @@ namespace digital { * As simple example of using this class in transmit mode: * * \verbatim - uint8_t* buffer = (uint8_t*)volk_malloc(header_nbytes(), - volk_get_alignment()); + volk::vector<uint8_t> buffer(header_nbytes()); header_buffer hdr(buffer); hdr.add_field64(sync_word, sync_word_len); @@ -81,8 +80,6 @@ namespace digital { hdr.add_field8(header_options); // Do something with the header - - volk_free(buffer); \endverbatim * * In this example, the header contains four fields: diff --git a/gr-digital/lib/qa_header_buffer.cc b/gr-digital/lib/qa_header_buffer.cc index 4b1690f9ac..3fa22e5883 100644 --- a/gr-digital/lib/qa_header_buffer.cc +++ b/gr-digital/lib/qa_header_buffer.cc @@ -15,15 +15,15 @@ #include <gnuradio/attributes.h> #include <gnuradio/digital/header_buffer.h> #include <stdio.h> -#include <volk/volk.h> +#include <volk/volk_alloc.hh> #include <boost/test/unit_test.hpp> BOOST_AUTO_TEST_CASE(test_add8) { size_t len = sizeof(uint8_t); - uint8_t* buf = (uint8_t*)volk_malloc(len, volk_get_alignment()); + volk::vector<uint8_t> buf(len); - gr::digital::header_buffer header(buf); + gr::digital::header_buffer header(buf.data()); header.add_field8(0xAF); BOOST_REQUIRE_EQUAL(len, header.length()); @@ -31,18 +31,16 @@ BOOST_AUTO_TEST_CASE(test_add8) header.clear(); BOOST_REQUIRE_EQUAL((size_t)0, header.length()); - - volk_free(buf); } BOOST_AUTO_TEST_CASE(test_add16) { size_t len = sizeof(uint16_t); - uint8_t* buf = (uint8_t*)volk_malloc(len, volk_get_alignment()); + volk::vector<uint8_t> buf(len); uint16_t data = 0xAF5C; - gr::digital::header_buffer header(buf); + gr::digital::header_buffer header(buf.data()); header.add_field16(data); // Test standard add of a uint16 @@ -72,18 +70,16 @@ BOOST_AUTO_TEST_CASE(test_add16) BOOST_REQUIRE_EQUAL((size_t)1, header.length()); BOOST_REQUIRE_EQUAL((uint8_t)0x5C, header.header()[0]); header.clear(); - - volk_free(buf); } BOOST_AUTO_TEST_CASE(test_add32) { size_t len = sizeof(uint32_t); - uint8_t* buf = (uint8_t*)volk_malloc(len, volk_get_alignment()); + volk::vector<uint8_t> buf(len); uint32_t data = 0xAF5C7654; - gr::digital::header_buffer header(buf); + gr::digital::header_buffer header(buf.data()); header.add_field32(data); // Test standard add of a uint32 @@ -119,18 +115,16 @@ BOOST_AUTO_TEST_CASE(test_add32) BOOST_REQUIRE_EQUAL((uint8_t)0x76, header.header()[1]); BOOST_REQUIRE_EQUAL((uint8_t)0x5C, header.header()[2]); header.clear(); - - volk_free(buf); } BOOST_AUTO_TEST_CASE(test_add64) { size_t len = sizeof(uint64_t); - uint8_t* buf = (uint8_t*)volk_malloc(len, volk_get_alignment()); + volk::vector<uint8_t> buf(len); uint64_t data = 0xAF5C765432104567; - gr::digital::header_buffer header(buf); + gr::digital::header_buffer header(buf.data()); header.add_field64(data); // Test standard add of a uint64 @@ -181,16 +175,14 @@ BOOST_AUTO_TEST_CASE(test_add64) BOOST_REQUIRE_EQUAL((uint8_t)0x32, header.header()[3]); BOOST_REQUIRE_EQUAL((uint8_t)0x54, header.header()[4]); header.clear(); - - volk_free(buf); } BOOST_AUTO_TEST_CASE(test_add_many) { size_t len = (32 + 64 + 8 + 16 + 32) / 8; - uint8_t* buf = (uint8_t*)volk_malloc(len, volk_get_alignment()); + volk::vector<uint8_t> buf(len); - gr::digital::header_buffer header(buf); + gr::digital::header_buffer header(buf.data()); header.add_field32(0x01234567); header.add_field64(0x89ABCDEFFEDCBA98); header.add_field8(0x76); diff --git a/gr-digital/lib/qa_header_format.cc b/gr-digital/lib/qa_header_format.cc index 19d84fb65c..072f241062 100644 --- a/gr-digital/lib/qa_header_format.cc +++ b/gr-digital/lib/qa_header_format.cc @@ -19,7 +19,7 @@ #include <gnuradio/expj.h> #include <gnuradio/xoroshiro128p.h> #include <stdio.h> -#include <volk/volk.h> +#include <volk/volk_alloc.hh> #include <boost/test/unit_test.hpp> #include <cmath> @@ -50,16 +50,15 @@ BOOST_AUTO_TEST_CASE(test_default_format) int lower8 = N & 0xFF; std::string ac = "1010101010101010"; // 0xAAAA - unsigned char* data = - (unsigned char*)volk_malloc(N * sizeof(unsigned char), volk_get_alignment()); - xoroshiro_fill_buffer(data, N); + volk::vector<unsigned char> data(N); + xoroshiro_fill_buffer(data.data(), N); gr::digital::header_format_default::sptr hdr_format; hdr_format = gr::digital::header_format_default::make(ac, 0); pmt::pmt_t output; pmt::pmt_t info = pmt::make_dict(); - bool ret = hdr_format->format(N, data, output, info); + bool ret = hdr_format->format(N, data.data(), output, info); size_t length = pmt::length(output); BOOST_REQUIRE(ret); @@ -81,8 +80,6 @@ BOOST_AUTO_TEST_CASE(test_default_format) BOOST_REQUIRE_EQUAL(lower8, (int)h3); BOOST_REQUIRE_EQUAL(upper8, (int)h4); BOOST_REQUIRE_EQUAL(lower8, (int)h5); - - volk_free(data); } @@ -90,12 +87,11 @@ BOOST_AUTO_TEST_CASE(test_default_parse) { static const int nbytes = 106; static const int nbits = 8 * nbytes; - unsigned char* bytes = - (unsigned char*)volk_malloc(nbytes * sizeof(unsigned char), volk_get_alignment()); - unsigned char* bits = - (unsigned char*)volk_malloc(nbits * sizeof(unsigned char), volk_get_alignment()); - xoroshiro_fill_buffer(bytes, nbytes); + volk::vector<unsigned char> bytes(nbytes); + volk::vector<unsigned char> bits(nbits); + + xoroshiro_fill_buffer(bytes.data(), nbytes); int index = 0; bytes[index + 0] = 0xAA; @@ -106,7 +102,7 @@ BOOST_AUTO_TEST_CASE(test_default_parse) bytes[index + 5] = 0x64; gr::blocks::kernel::unpack_k_bits unpacker = gr::blocks::kernel::unpack_k_bits(8); - unpacker.unpack(bits, bytes, nbytes); + unpacker.unpack(bits.data(), bytes.data(), nbytes); std::string ac = "1010101010101010"; // 0xAAAA gr::digital::header_format_default::sptr hdr_format; @@ -114,7 +110,7 @@ BOOST_AUTO_TEST_CASE(test_default_parse) int count = 0; std::vector<pmt::pmt_t> info; - bool ret = hdr_format->parse(nbits, bits, info, count); + bool ret = hdr_format->parse(nbits, bits.data(), info, count); BOOST_REQUIRE(ret); BOOST_REQUIRE_EQUAL((size_t)1, info.size()); @@ -126,9 +122,6 @@ BOOST_AUTO_TEST_CASE(test_default_parse) int hdr_bits = (int)hdr_format->header_nbits(); int expected_bits = nbits - hdr_bits; BOOST_REQUIRE_EQUAL(expected_bits, payload_bits); - - volk_free(bytes); - volk_free(bits); } BOOST_AUTO_TEST_CASE(test_counter_format) @@ -138,9 +131,8 @@ BOOST_AUTO_TEST_CASE(test_counter_format) int lower8 = N & 0xFF; std::string ac = "1010101010101010"; // 0xAAAA - unsigned char* data = - (unsigned char*)volk_malloc(N * sizeof(unsigned char), volk_get_alignment()); - xoroshiro_fill_buffer(data, N); + volk::vector<unsigned char> data(N); + xoroshiro_fill_buffer(data.data(), N); uint16_t expected_bps = 2; gr::digital::header_format_counter::sptr hdr_format; @@ -149,7 +141,7 @@ BOOST_AUTO_TEST_CASE(test_counter_format) pmt::pmt_t output; pmt::pmt_t info = pmt::make_dict(); - bool ret = hdr_format->format(N, data, output, info); + bool ret = hdr_format->format(N, data.data(), output, info); size_t length = pmt::length(output); BOOST_REQUIRE(ret); @@ -183,13 +175,11 @@ BOOST_AUTO_TEST_CASE(test_counter_format) BOOST_REQUIRE_EQUAL((uint16_t)0, counter); // Run another format to increment the counter to 1 and test. - ret = hdr_format->format(N, data, output, info); + ret = hdr_format->format(N, data.data(), output, info); h8 = pmt::u8vector_ref(output, 8); h9 = pmt::u8vector_ref(output, 9); counter = ((h8 << 8) & 0xFF00) | (h9 & 0x00FF); BOOST_REQUIRE_EQUAL((uint16_t)1, counter); - - volk_free(data); } @@ -197,11 +187,9 @@ BOOST_AUTO_TEST_CASE(test_counter_parse) { static const int nbytes = 110; static const int nbits = 8 * nbytes; - unsigned char* bytes = - (unsigned char*)volk_malloc(nbytes * sizeof(unsigned char), volk_get_alignment()); - unsigned char* bits = - (unsigned char*)volk_malloc(nbits * sizeof(unsigned char), volk_get_alignment()); - xoroshiro_fill_buffer(bytes, nbytes); + volk::vector<unsigned char> bytes(nbytes); + volk::vector<unsigned char> bits(nbits); + xoroshiro_fill_buffer(bytes.data(), nbytes); int index = 0; bytes[index + 0] = 0xAA; bytes[index + 1] = 0xAA; @@ -215,7 +203,7 @@ BOOST_AUTO_TEST_CASE(test_counter_parse) bytes[index + 9] = 0x00; gr::blocks::kernel::unpack_k_bits unpacker = gr::blocks::kernel::unpack_k_bits(8); - unpacker.unpack(bits, bytes, nbytes); + unpacker.unpack(bits.data(), bytes.data(), nbytes); uint16_t expected_bps = 2; std::string ac = "1010101010101010"; // 0xAAAA @@ -224,7 +212,7 @@ BOOST_AUTO_TEST_CASE(test_counter_parse) int count = 0; std::vector<pmt::pmt_t> info; - bool ret = hdr_format->parse(nbits, bits, info, count); + bool ret = hdr_format->parse(nbits, bits.data(), info, count); BOOST_REQUIRE(ret); BOOST_REQUIRE_EQUAL((size_t)1, info.size()); @@ -241,7 +229,4 @@ BOOST_AUTO_TEST_CASE(test_counter_parse) BOOST_REQUIRE_EQUAL(expected_bits, payload_syms * bps); BOOST_REQUIRE_EQUAL(expected_bps, (uint16_t)bps); BOOST_REQUIRE_EQUAL(0, counter); - - volk_free(bytes); - volk_free(bits); } diff --git a/gr-digital/python/digital/bindings/header_buffer_python.cc b/gr-digital/python/digital/bindings/header_buffer_python.cc index 2676a2ecc6..5e07bac14f 100644 --- a/gr-digital/python/digital/bindings/header_buffer_python.cc +++ b/gr-digital/python/digital/bindings/header_buffer_python.cc @@ -14,7 +14,7 @@ /* BINDTOOL_GEN_AUTOMATIC(0) */ /* BINDTOOL_USE_PYGCCXML(0) */ /* BINDTOOL_HEADER_FILE(header_buffer.h) */ -/* BINDTOOL_HEADER_FILE_HASH(cb0f4b24d7fa8f9c23b864584c361e11) */ +/* BINDTOOL_HEADER_FILE_HASH(fa6a18f60473f6981e632c4cf3005793) */ /***********************************************************************************/ #include <pybind11/complex.h> |