summaryrefslogtreecommitdiff
path: root/gr-blocks
diff options
context:
space:
mode:
authorThomas Habets <thomas@habets.se>2020-03-22 17:57:59 +0000
committerMartin Braun <martin@gnuradio.org>2020-03-27 08:46:42 -0700
commit94878530c26606b5b1d83d9dfb154e3b5e6e823a (patch)
treea836a518ac48f5fd346d01ef5341586e1ffc3b5c /gr-blocks
parent4329df15bd927a355ec66ce55876ad2e5b11fcf8 (diff)
blocks/udp_source: Remove manual memory management for buffers
Diffstat (limited to 'gr-blocks')
-rw-r--r--gr-blocks/lib/udp_source_impl.cc19
-rw-r--r--gr-blocks/lib/udp_source_impl.h10
2 files changed, 13 insertions, 16 deletions
diff --git a/gr-blocks/lib/udp_source_impl.cc b/gr-blocks/lib/udp_source_impl.cc
index d280e33457..230ec907b3 100644
--- a/gr-blocks/lib/udp_source_impl.cc
+++ b/gr-blocks/lib/udp_source_impl.cc
@@ -43,12 +43,10 @@ udp_source_impl::udp_source_impl(
d_eof(eof),
d_connected(false),
d_residual(0),
- d_sent(0)
+ d_sent(0),
+ d_rxbuf(4 * payload_size),
+ d_residbuf(BUF_SIZE_PAYLOADS * payload_size)
{
- // Give us some more room to play.
- d_rxbuf = new char[4 * d_payload_size];
- d_residbuf = new char[BUF_SIZE_PAYLOADS * d_payload_size];
-
connect(host, port);
}
@@ -56,9 +54,6 @@ udp_source_impl::~udp_source_impl()
{
if (d_connected)
disconnect();
-
- delete[] d_rxbuf;
- delete[] d_residbuf;
}
void udp_source_impl::connect(const std::string& host, int port)
@@ -120,7 +115,7 @@ int udp_source_impl::get_port(void)
void udp_source_impl::start_receive()
{
d_socket->async_receive_from(
- boost::asio::buffer((void*)d_rxbuf, d_payload_size),
+ boost::asio::buffer((void*)d_rxbuf.data(), d_payload_size),
d_endpoint_rcvd,
boost::bind(&udp_source_impl::handle_read,
this,
@@ -150,7 +145,9 @@ void udp_source_impl::handle_read(const boost::system::error_code& error,
} else {
// otherwise, copy received data into local buffer for
// copying later.
- memcpy(d_residbuf + d_residual, d_rxbuf, bytes_transferred);
+ memcpy(d_residbuf.data() + d_residual,
+ d_rxbuf.data(),
+ bytes_transferred);
d_residual += bytes_transferred;
}
}
@@ -185,7 +182,7 @@ int udp_source_impl::work(int noutput_items,
int bytes_to_send = std::min<int>(d_itemsize * noutput_items, bytes_left_in_buffer);
// Copy the received data in the residual buffer to the output stream
- memcpy(out, d_residbuf + d_sent, bytes_to_send);
+ memcpy(out, d_residbuf.data() + d_sent, bytes_to_send);
int nitems = bytes_to_send / d_itemsize;
// Keep track of where we are if we don't have enough output
diff --git a/gr-blocks/lib/udp_source_impl.h b/gr-blocks/lib/udp_source_impl.h
index 36d4e2b199..418a19b37d 100644
--- a/gr-blocks/lib/udp_source_impl.h
+++ b/gr-blocks/lib/udp_source_impl.h
@@ -23,11 +23,11 @@ class udp_source_impl : public udp_source
{
private:
const size_t d_itemsize;
- int d_payload_size; // maximum transmission unit (packet length)
- const bool d_eof; // look for an EOF signal
- bool d_connected; // are we connected?
- char* d_rxbuf; // get UDP buffer items
- char* d_residbuf; // hold buffer between calls
+ int d_payload_size; // maximum transmission unit (packet length)
+ const bool d_eof; // look for an EOF signal
+ bool d_connected; // are we connected?
+ std::vector<char> d_rxbuf; // get UDP buffer items
+ std::vector<char> d_residbuf; // hold buffer between calls
ssize_t d_residual; // hold information about number of bytes stored in residbuf
ssize_t d_sent; // track how much of d_residbuf we've outputted