From bb1eef8cade6e39532919918ce1168c3e62a54df Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Tue, 1 Jun 2010 12:23:20 -0700 Subject: Moved the packet handler state stuff into a separate header (so we dont pull in all the includes). Use callback for getting buffers rather than zc interface pointer so its more modular. --- host/lib/usrp/usrp2/io_impl.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'host/lib/usrp/usrp2/io_impl.cpp') diff --git a/host/lib/usrp/usrp2/io_impl.cpp b/host/lib/usrp/usrp2/io_impl.cpp index 79b18fb63..5a082bf13 100644 --- a/host/lib/usrp/usrp2/io_impl.cpp +++ b/host/lib/usrp/usrp2/io_impl.cpp @@ -15,11 +15,13 @@ // along with this program. If not, see . // +#include "../../transport/vrt_packet_handler.hpp" #include "usrp2_impl.hpp" #include "usrp2_regs.hpp" #include #include #include //htonl and ntohl +#include #include using namespace uhd; @@ -65,6 +67,10 @@ void usrp2_impl::io_init(void){ /*********************************************************************** * Send Data **********************************************************************/ +static inline managed_send_buffer::sptr get_send_buff(zero_copy_if::sptr zc_if){ + return zc_if->get_send_buff(); +} + size_t usrp2_impl::send( const asio::const_buffer &buff, const tx_metadata_t &metadata, @@ -76,7 +82,7 @@ size_t usrp2_impl::send( buff, metadata, send_mode, //buffer to empty and samples metadata io_type, _tx_otw_type, //input and output types to convert get_master_clock_freq(), //master clock tick rate - _data_transport, //zero copy interface + boost::bind(get_send_buff, _data_transport), get_max_send_samps_per_packet() ); } @@ -84,6 +90,10 @@ size_t usrp2_impl::send( /*********************************************************************** * Receive Data **********************************************************************/ +static inline managed_recv_buffer::sptr get_recv_buff(zero_copy_if::sptr zc_if){ + return zc_if->get_recv_buff(); +} + size_t usrp2_impl::recv( const asio::mutable_buffer &buff, rx_metadata_t &metadata, @@ -95,6 +105,6 @@ size_t usrp2_impl::recv( buff, metadata, recv_mode, //buffer to fill and samples metadata io_type, _rx_otw_type, //input and output types to convert get_master_clock_freq(), //master clock tick rate - _data_transport //zero copy interface + boost::bind(get_recv_buff, _data_transport) ); } -- cgit v1.2.3 From 212159ca3bc00d233464cd6f9f454e5ac6e08f88 Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Tue, 1 Jun 2010 17:51:26 -0700 Subject: Implemented pirate thread, moved io impl details into io impl cpp file. Fixed bug in bounded buffer push with pop on full. --- host/include/uhd/transport/bounded_buffer.hpp | 4 ++ host/lib/transport/CMakeLists.txt | 1 - host/lib/transport/udp_zero_copy_asio.cpp | 2 +- host/lib/transport/vrt_packet_handler.hpp | 29 +++++++++- host/lib/transport/vrt_packet_handler_state.hpp | 56 ------------------- host/lib/usrp/usrp2/io_impl.cpp | 73 ++++++++++++++++++++++--- host/lib/usrp/usrp2/usrp2_impl.cpp | 3 +- host/lib/usrp/usrp2/usrp2_impl.hpp | 5 +- 8 files changed, 103 insertions(+), 70 deletions(-) delete mode 100644 host/lib/transport/vrt_packet_handler_state.hpp (limited to 'host/lib/usrp/usrp2/io_impl.cpp') diff --git a/host/include/uhd/transport/bounded_buffer.hpp b/host/include/uhd/transport/bounded_buffer.hpp index cdec05849..baecd6382 100644 --- a/host/include/uhd/transport/bounded_buffer.hpp +++ b/host/include/uhd/transport/bounded_buffer.hpp @@ -57,10 +57,14 @@ namespace uhd{ namespace transport{ if(_buffer.full()){ _buffer.pop_back(); _buffer.push_front(elem); + lock.unlock(); + _empty_cond.notify_one(); return false; } else{ _buffer.push_front(elem); + lock.unlock(); + _empty_cond.notify_one(); return true; } } diff --git a/host/lib/transport/CMakeLists.txt b/host/lib/transport/CMakeLists.txt index 32644a6c0..a74f7d527 100644 --- a/host/lib/transport/CMakeLists.txt +++ b/host/lib/transport/CMakeLists.txt @@ -50,6 +50,5 @@ LIBUHD_APPEND_SOURCES( ${CMAKE_SOURCE_DIR}/lib/transport/udp_simple.cpp ${CMAKE_SOURCE_DIR}/lib/transport/udp_zero_copy_asio.cpp ${CMAKE_SOURCE_DIR}/lib/transport/vrt_packet_handler.hpp - ${CMAKE_SOURCE_DIR}/lib/transport/vrt_packet_handler_state.hpp ${CMAKE_SOURCE_DIR}/lib/transport/zero_copy.cpp ) diff --git a/host/lib/transport/udp_zero_copy_asio.cpp b/host/lib/transport/udp_zero_copy_asio.cpp index 4402437f3..190e3f9ce 100644 --- a/host/lib/transport/udp_zero_copy_asio.cpp +++ b/host/lib/transport/udp_zero_copy_asio.cpp @@ -28,7 +28,7 @@ using namespace uhd::transport; * Constants **********************************************************************/ static const size_t MIN_SOCK_BUFF_SIZE = size_t(100e3); -static const size_t MAX_DGRAM_SIZE = 2048; //assume max size on send and recv +static const size_t MAX_DGRAM_SIZE = 1500; //assume max size on send and recv static const double RECV_TIMEOUT = 0.1; // 100 ms /*********************************************************************** diff --git a/host/lib/transport/vrt_packet_handler.hpp b/host/lib/transport/vrt_packet_handler.hpp index a179731ca..d6b863040 100644 --- a/host/lib/transport/vrt_packet_handler.hpp +++ b/host/lib/transport/vrt_packet_handler.hpp @@ -18,7 +18,6 @@ #ifndef INCLUDED_LIBUHD_TRANSPORT_VRT_PACKET_HANDLER_HPP #define INCLUDED_LIBUHD_TRANSPORT_VRT_PACKET_HANDLER_HPP -#include "vrt_packet_handler_state.hpp" #include #include #include @@ -37,6 +36,24 @@ namespace vrt_packet_handler{ /*********************************************************************** * vrt packet handler for recv **********************************************************************/ + struct recv_state{ + //init the expected seq number + size_t next_packet_seq; + + //state variables to handle fragments + uhd::transport::managed_recv_buffer::sptr managed_buff; + boost::asio::const_buffer copy_buff; + size_t fragment_offset_in_samps; + + recv_state(void){ + //first expected seq is zero + next_packet_seq = 0; + + //initially empty copy buffer + copy_buff = boost::asio::buffer("", 0); + } + }; + typedef boost::function get_recv_buff_t; typedef boost::function recv_cb_t; @@ -106,6 +123,7 @@ namespace vrt_packet_handler{ if (boost::asio::buffer_size(state.copy_buff) == 0){ state.fragment_offset_in_samps = 0; state.managed_buff = get_recv_buff(); + if (state.managed_buff.get() == NULL) return 0; recv_cb(state.managed_buff); //callback before vrt unpack try{ _recv1_helper( @@ -210,6 +228,15 @@ namespace vrt_packet_handler{ /*********************************************************************** * vrt packet handler for send **********************************************************************/ + struct send_state{ + //init the expected seq number + size_t next_packet_seq; + + send_state(void){ + next_packet_seq = 0; + } + }; + typedef boost::function get_send_buff_t; typedef boost::function send_cb_t; diff --git a/host/lib/transport/vrt_packet_handler_state.hpp b/host/lib/transport/vrt_packet_handler_state.hpp deleted file mode 100644 index 2320a3b8e..000000000 --- a/host/lib/transport/vrt_packet_handler_state.hpp +++ /dev/null @@ -1,56 +0,0 @@ -// -// Copyright 2010 Ettus Research LLC -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . -// - -#ifndef INCLUDED_LIBUHD_TRANSPORT_VRT_PACKET_HANDLER_STATE_HPP -#define INCLUDED_LIBUHD_TRANSPORT_VRT_PACKET_HANDLER_STATE_HPP - -#include -#include -#include - -namespace vrt_packet_handler{ - - struct recv_state{ - //init the expected seq number - size_t next_packet_seq; - - //state variables to handle fragments - uhd::transport::managed_recv_buffer::sptr managed_buff; - boost::asio::const_buffer copy_buff; - size_t fragment_offset_in_samps; - - recv_state(void){ - //first expected seq is zero - next_packet_seq = 0; - - //initially empty copy buffer - copy_buff = boost::asio::buffer("", 0); - } - }; - - struct send_state{ - //init the expected seq number - size_t next_packet_seq; - - send_state(void){ - next_packet_seq = 0; - } - }; - -} //namespace vrt_packet_handler - -#endif /* INCLUDED_LIBUHD_TRANSPORT_VRT_PACKET_HANDLER_STATE_HPP */ diff --git a/host/lib/usrp/usrp2/io_impl.cpp b/host/lib/usrp/usrp2/io_impl.cpp index 5a082bf13..efd64d4ab 100644 --- a/host/lib/usrp/usrp2/io_impl.cpp +++ b/host/lib/usrp/usrp2/io_impl.cpp @@ -19,9 +19,11 @@ #include "usrp2_impl.hpp" #include "usrp2_regs.hpp" #include +#include #include #include //htonl and ntohl #include +#include #include using namespace uhd; @@ -29,6 +31,60 @@ using namespace uhd::usrp; using namespace uhd::transport; namespace asio = boost::asio; +/*********************************************************************** + * io impl details (internal to this file) + **********************************************************************/ +struct usrp2_impl::io_impl{ + + io_impl(zero_copy_if::sptr zc_if); + ~io_impl(void); + + managed_recv_buffer::sptr get_recv_buff(void); + + //state management for the vrt packet handler code + vrt_packet_handler::recv_state packet_handler_recv_state; + vrt_packet_handler::send_state packet_handler_send_state; + + //methods and variables for the recv pirate + void recv_pirate_loop(zero_copy_if::sptr zc_if); + boost::thread *recv_pirate_thread; bool recv_pirate_running; + bounded_buffer::sptr recv_pirate_booty; +}; + +usrp2_impl::io_impl::io_impl(zero_copy_if::sptr zc_if){ + //create a large enough booty + size_t num_frames = zc_if->get_num_recv_frames(); + std::cout << "Recv pirate num frames: " << num_frames << std::endl; + recv_pirate_booty = bounded_buffer::make(num_frames); + + //create a new pirate thread (yarr!!) + recv_pirate_thread = new boost::thread( + boost::bind(&usrp2_impl::io_impl::recv_pirate_loop, this, zc_if) + ); +} + +usrp2_impl::io_impl::~io_impl(void){ + recv_pirate_running = false; + recv_pirate_thread->interrupt(); + recv_pirate_thread->join(); + delete recv_pirate_thread; +} + +managed_recv_buffer::sptr usrp2_impl::io_impl::get_recv_buff(void){ + managed_recv_buffer::sptr buff; + recv_pirate_booty->pop_with_timed_wait(buff, boost::posix_time::milliseconds(100)); + //timeout means a null sptr... + return buff; +} + +void usrp2_impl::io_impl::recv_pirate_loop(zero_copy_if::sptr zc_if){ + recv_pirate_running = true; + while(recv_pirate_running){ + managed_recv_buffer::sptr buff = zc_if->get_recv_buff(); + if (buff->size()) recv_pirate_booty->push_with_pop_on_full(buff); + } +} + /*********************************************************************** * Helper Functions **********************************************************************/ @@ -62,6 +118,13 @@ void usrp2_impl::io_init(void){ ); _iface->poke32(FR_RX_CTRL_VRT_STREAM_ID, 0); _iface->poke32(FR_RX_CTRL_VRT_TRAILER, 0); + + //create new io impl + _io_impl = new io_impl(_data_transport); +} + +void usrp2_impl::io_done(void){ + delete _io_impl; } /*********************************************************************** @@ -78,7 +141,7 @@ size_t usrp2_impl::send( send_mode_t send_mode ){ return vrt_packet_handler::send( - _packet_handler_send_state, //last state of the send handler + _io_impl->packet_handler_send_state, //last state of the send handler buff, metadata, send_mode, //buffer to empty and samples metadata io_type, _tx_otw_type, //input and output types to convert get_master_clock_freq(), //master clock tick rate @@ -90,10 +153,6 @@ size_t usrp2_impl::send( /*********************************************************************** * Receive Data **********************************************************************/ -static inline managed_recv_buffer::sptr get_recv_buff(zero_copy_if::sptr zc_if){ - return zc_if->get_recv_buff(); -} - size_t usrp2_impl::recv( const asio::mutable_buffer &buff, rx_metadata_t &metadata, @@ -101,10 +160,10 @@ size_t usrp2_impl::recv( recv_mode_t recv_mode ){ return vrt_packet_handler::recv( - _packet_handler_recv_state, //last state of the recv handler + _io_impl->packet_handler_recv_state, //last state of the recv handler buff, metadata, recv_mode, //buffer to fill and samples metadata io_type, _rx_otw_type, //input and output types to convert get_master_clock_freq(), //master clock tick rate - boost::bind(get_recv_buff, _data_transport) + boost::bind(&usrp2_impl::io_impl::get_recv_buff, _io_impl) ); } diff --git a/host/lib/usrp/usrp2/usrp2_impl.cpp b/host/lib/usrp/usrp2/usrp2_impl.cpp index af3ec216a..7f79c483b 100644 --- a/host/lib/usrp/usrp2/usrp2_impl.cpp +++ b/host/lib/usrp/usrp2/usrp2_impl.cpp @@ -185,7 +185,8 @@ usrp2_impl::usrp2_impl( } usrp2_impl::~usrp2_impl(void){ - /* NOP */ + //cleanup the send and recv io + io_done(); } /*********************************************************************** diff --git a/host/lib/usrp/usrp2/usrp2_impl.hpp b/host/lib/usrp/usrp2/usrp2_impl.hpp index 6a2a09349..4b6805217 100644 --- a/host/lib/usrp/usrp2/usrp2_impl.hpp +++ b/host/lib/usrp/usrp2/usrp2_impl.hpp @@ -33,7 +33,6 @@ #include #include #include -#include "../../transport/vrt_packet_handler_state.hpp" /*! * Make a usrp2 dboard interface. @@ -153,10 +152,10 @@ private: uhd::transport::vrt::max_header_words32*sizeof(boost::uint32_t) ; - vrt_packet_handler::recv_state _packet_handler_recv_state; - vrt_packet_handler::send_state _packet_handler_send_state; uhd::otw_type_t _rx_otw_type, _tx_otw_type; + struct io_impl; io_impl *_io_impl; void io_init(void); + void io_done(void); //udp transports for control and data uhd::transport::udp_zero_copy::sptr _data_transport; -- cgit v1.2.3 From 92fd3a514b6808e7b94f172c75afb2026b849fce Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Wed, 2 Jun 2010 14:49:32 -0700 Subject: disable boost thread interrupt when doing pop with timed wait, fixed error on exit --- host/lib/usrp/usrp2/io_impl.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'host/lib/usrp/usrp2/io_impl.cpp') diff --git a/host/lib/usrp/usrp2/io_impl.cpp b/host/lib/usrp/usrp2/io_impl.cpp index efd64d4ab..43d6e814a 100644 --- a/host/lib/usrp/usrp2/io_impl.cpp +++ b/host/lib/usrp/usrp2/io_impl.cpp @@ -72,9 +72,9 @@ usrp2_impl::io_impl::~io_impl(void){ managed_recv_buffer::sptr usrp2_impl::io_impl::get_recv_buff(void){ managed_recv_buffer::sptr buff; + boost::this_thread::disable_interruption di; //disable because the wait can throw recv_pirate_booty->pop_with_timed_wait(buff, boost::posix_time::milliseconds(100)); - //timeout means a null sptr... - return buff; + return buff; //a timeout means that we return a null sptr... } void usrp2_impl::io_impl::recv_pirate_loop(zero_copy_if::sptr zc_if){ @@ -119,6 +119,8 @@ void usrp2_impl::io_init(void){ _iface->poke32(FR_RX_CTRL_VRT_STREAM_ID, 0); _iface->poke32(FR_RX_CTRL_VRT_TRAILER, 0); + std::cout << "TX samples per packet: " << get_max_send_samps_per_packet() << std::endl; + //create new io impl _io_impl = new io_impl(_data_transport); } -- cgit v1.2.3 From a4494bc2c985919aee7119ed401c72f41cde206e Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Wed, 2 Jun 2010 15:21:56 -0700 Subject: use smart pointer for io impl, simplify send buffer callback --- host/lib/usrp/usrp2/io_impl.cpp | 12 ++---------- host/lib/usrp/usrp2/usrp2_impl.cpp | 3 +-- host/lib/usrp/usrp2/usrp2_impl.hpp | 3 +-- 3 files changed, 4 insertions(+), 14 deletions(-) (limited to 'host/lib/usrp/usrp2/io_impl.cpp') diff --git a/host/lib/usrp/usrp2/io_impl.cpp b/host/lib/usrp/usrp2/io_impl.cpp index 43d6e814a..5d425bd8a 100644 --- a/host/lib/usrp/usrp2/io_impl.cpp +++ b/host/lib/usrp/usrp2/io_impl.cpp @@ -122,20 +122,12 @@ void usrp2_impl::io_init(void){ std::cout << "TX samples per packet: " << get_max_send_samps_per_packet() << std::endl; //create new io impl - _io_impl = new io_impl(_data_transport); -} - -void usrp2_impl::io_done(void){ - delete _io_impl; + _io_impl = boost::shared_ptr(new io_impl(_data_transport)); } /*********************************************************************** * Send Data **********************************************************************/ -static inline managed_send_buffer::sptr get_send_buff(zero_copy_if::sptr zc_if){ - return zc_if->get_send_buff(); -} - size_t usrp2_impl::send( const asio::const_buffer &buff, const tx_metadata_t &metadata, @@ -147,7 +139,7 @@ size_t usrp2_impl::send( buff, metadata, send_mode, //buffer to empty and samples metadata io_type, _tx_otw_type, //input and output types to convert get_master_clock_freq(), //master clock tick rate - boost::bind(get_send_buff, _data_transport), + boost::bind(&zero_copy_if::get_send_buff, _data_transport), get_max_send_samps_per_packet() ); } diff --git a/host/lib/usrp/usrp2/usrp2_impl.cpp b/host/lib/usrp/usrp2/usrp2_impl.cpp index 7f79c483b..af3ec216a 100644 --- a/host/lib/usrp/usrp2/usrp2_impl.cpp +++ b/host/lib/usrp/usrp2/usrp2_impl.cpp @@ -185,8 +185,7 @@ usrp2_impl::usrp2_impl( } usrp2_impl::~usrp2_impl(void){ - //cleanup the send and recv io - io_done(); + /* NOP */ } /*********************************************************************** diff --git a/host/lib/usrp/usrp2/usrp2_impl.hpp b/host/lib/usrp/usrp2/usrp2_impl.hpp index 4b6805217..bb4554e8d 100644 --- a/host/lib/usrp/usrp2/usrp2_impl.hpp +++ b/host/lib/usrp/usrp2/usrp2_impl.hpp @@ -153,9 +153,8 @@ private: ; uhd::otw_type_t _rx_otw_type, _tx_otw_type; - struct io_impl; io_impl *_io_impl; + struct io_impl; boost::shared_ptr _io_impl; void io_init(void); - void io_done(void); //udp transports for control and data uhd::transport::udp_zero_copy::sptr _data_transport; -- cgit v1.2.3 From b2054a45d45ba85e30ff601159b18f5ebd15dd76 Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Wed, 2 Jun 2010 17:44:20 -0700 Subject: Created macros for dealing with pimpls and implemented in code. --- host/include/uhd/transport/zero_copy.hpp | 27 ++++++++++------ host/include/uhd/usrp/dboard_base.hpp | 4 +-- host/include/uhd/utils/CMakeLists.txt | 1 + host/include/uhd/utils/pimpl.hpp | 55 ++++++++++++++++++++++++++++++++ host/lib/transport/zero_copy.cpp | 7 ++-- host/lib/usrp/dboard_base.cpp | 8 ++--- host/lib/usrp/usrp2/io_impl.cpp | 2 +- host/lib/usrp/usrp2/usrp2_impl.hpp | 3 +- 8 files changed, 85 insertions(+), 22 deletions(-) create mode 100644 host/include/uhd/utils/pimpl.hpp (limited to 'host/lib/usrp/usrp2/io_impl.cpp') diff --git a/host/include/uhd/transport/zero_copy.hpp b/host/include/uhd/transport/zero_copy.hpp index d6eb89a91..2815e3189 100644 --- a/host/include/uhd/transport/zero_copy.hpp +++ b/host/include/uhd/transport/zero_copy.hpp @@ -19,6 +19,7 @@ #define INCLUDED_UHD_TRANSPORT_ZERO_COPY_HPP #include +#include #include #include #include @@ -156,10 +157,14 @@ namespace uhd{ namespace transport{ */ class UHD_API phony_zero_copy_recv_if : public virtual zero_copy_if{ public: - - //! structors + /*! + * Create a phony zero copy recv interface. + * \param max_buff_size max buffer size in bytes + */ phony_zero_copy_recv_if(size_t max_buff_size); - ~phony_zero_copy_recv_if(void); + + //! destructor + virtual ~phony_zero_copy_recv_if(void); /*! * Get a new receive buffer from this transport object. @@ -167,7 +172,6 @@ namespace uhd{ namespace transport{ managed_recv_buffer::sptr get_recv_buff(void); private: - /*! * Perform a private copying recv. * \param buff the buffer to write data into @@ -175,7 +179,7 @@ namespace uhd{ namespace transport{ */ virtual size_t recv(const boost::asio::mutable_buffer &buff) = 0; - struct impl; impl *_impl; //private implementation details + UHD_PIMPL_DECL(impl) _impl; }; /*! @@ -186,10 +190,14 @@ namespace uhd{ namespace transport{ */ class UHD_API phony_zero_copy_send_if : public virtual zero_copy_if{ public: - - //! structors + /*! + * Create a phony zero copy send interface. + * \param max_buff_size max buffer size in bytes + */ phony_zero_copy_send_if(size_t max_buff_size); - ~phony_zero_copy_send_if(void); + + //! destructor + virtual ~phony_zero_copy_send_if(void); /*! * Get a new send buffer from this transport object. @@ -197,7 +205,6 @@ namespace uhd{ namespace transport{ managed_send_buffer::sptr get_send_buff(void); private: - /*! * Perform a private copying send. * \param buff the buffer to read data from @@ -205,7 +212,7 @@ namespace uhd{ namespace transport{ */ virtual size_t send(const boost::asio::const_buffer &buff) = 0; - struct impl; impl *_impl; //private implementation details + UHD_PIMPL_DECL(impl) _impl; }; }} //namespace diff --git a/host/include/uhd/usrp/dboard_base.hpp b/host/include/uhd/usrp/dboard_base.hpp index 28bf2ae66..e88d39876 100644 --- a/host/include/uhd/usrp/dboard_base.hpp +++ b/host/include/uhd/usrp/dboard_base.hpp @@ -20,6 +20,7 @@ #include #include +#include #include #include #include @@ -58,8 +59,7 @@ protected: dboard_id_t get_tx_id(void); private: - struct dboard_base_impl; - dboard_base_impl *_impl; + UHD_PIMPL_DECL(impl) _impl; }; /*! diff --git a/host/include/uhd/utils/CMakeLists.txt b/host/include/uhd/utils/CMakeLists.txt index f588c6310..391e684c8 100644 --- a/host/include/uhd/utils/CMakeLists.txt +++ b/host/include/uhd/utils/CMakeLists.txt @@ -20,6 +20,7 @@ INSTALL(FILES assert.hpp exception.hpp gain_handler.hpp + pimpl.hpp props.hpp safe_main.hpp static.hpp diff --git a/host/include/uhd/utils/pimpl.hpp b/host/include/uhd/utils/pimpl.hpp new file mode 100644 index 000000000..09bf0c0a2 --- /dev/null +++ b/host/include/uhd/utils/pimpl.hpp @@ -0,0 +1,55 @@ +// +// Copyright 2010 Ettus Research LLC +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// + +#ifndef INCLUDED_UHD_UTILS_PIMPL_HPP +#define INCLUDED_UHD_UTILS_PIMPL_HPP + +#include +#include + +/*! \file pimpl.hpp + * "Pimpl idiom" (pointer to implementation idiom). + * The UHD_PIMPL_* macros simplify code overhead for declaring and making pimpls. + * + * Each pimpl is implemented as a shared pointer to the implementation: + * - The container class will not have to deallocate the pimpl. + * - The container class will use the pimpl as a regular pointer. + * - Usage: _impl->method(arg0, arg1) + * - Usage: _impl->member = value; + * + * \see http://en.wikipedia.org/wiki/Opaque_pointer + */ + +/*! + * Make a declaration for a pimpl in a header file. + * - Usage: UHD_PIMPL_DECL(impl) _impl; + * \param _name the name of the pimpl class + */ +#define UHD_PIMPL_DECL(_name) \ + struct _name; boost::shared_ptr<_name> + +/*! + * Make an instance of a pimpl in a source file. + * - Usage: _impl = UHD_PIMPL_MAKE(impl, ()); + * - Usage: _impl = UHD_PIMPL_MAKE(impl, (a0, a1)); + * \param _name the name of the pimpl class + * \param _args the constructor args for the pimpl + */ +#define UHD_PIMPL_MAKE(_name, _args) \ + boost::shared_ptr<_name>(new _name _args) + +#endif /* INCLUDED_UHD_UTILS_PIMPL_HPP */ diff --git a/host/lib/transport/zero_copy.cpp b/host/lib/transport/zero_copy.cpp index f69fd2774..27f41329b 100644 --- a/host/lib/transport/zero_copy.cpp +++ b/host/lib/transport/zero_copy.cpp @@ -58,12 +58,12 @@ struct phony_zero_copy_recv_if::impl{ }; phony_zero_copy_recv_if::phony_zero_copy_recv_if(size_t max_buff_size){ - _impl = new impl; + _impl = UHD_PIMPL_MAKE(impl, ()); _impl->max_buff_size = max_buff_size; } phony_zero_copy_recv_if::~phony_zero_copy_recv_if(void){ - delete _impl; + /* NOP */ } managed_recv_buffer::sptr phony_zero_copy_recv_if::get_recv_buff(void){ @@ -122,7 +122,7 @@ struct phony_zero_copy_send_if::impl{ }; phony_zero_copy_send_if::phony_zero_copy_send_if(size_t max_buff_size){ - _impl = new impl; + _impl = UHD_PIMPL_MAKE(impl, ()); _impl->send_mem = new boost::uint8_t[max_buff_size]; _impl->send_buff = managed_send_buffer::sptr(new managed_send_buffer_impl( boost::asio::buffer(_impl->send_mem, max_buff_size), @@ -132,7 +132,6 @@ phony_zero_copy_send_if::phony_zero_copy_send_if(size_t max_buff_size){ phony_zero_copy_send_if::~phony_zero_copy_send_if(void){ delete [] _impl->send_mem; - delete _impl; } managed_send_buffer::sptr phony_zero_copy_send_if::get_send_buff(void){ diff --git a/host/lib/usrp/dboard_base.cpp b/host/lib/usrp/dboard_base.cpp index bd4b37ef3..eafb8897f 100644 --- a/host/lib/usrp/dboard_base.cpp +++ b/host/lib/usrp/dboard_base.cpp @@ -25,17 +25,17 @@ using namespace uhd::usrp; /*********************************************************************** * dboard_base dboard dboard_base class **********************************************************************/ -struct dboard_base::dboard_base_impl{ +struct dboard_base::impl{ ctor_args_impl args; - dboard_base_impl(ctor_args_t args) : args(*args){} + impl(ctor_args_t args) : args(*args){} }; dboard_base::dboard_base(ctor_args_t args){ - _impl = new dboard_base_impl(args); + _impl = UHD_PIMPL_MAKE(impl, (args)); } dboard_base::~dboard_base(void){ - delete _impl; + /* NOP */ } std::string dboard_base::get_subdev_name(void){ diff --git a/host/lib/usrp/usrp2/io_impl.cpp b/host/lib/usrp/usrp2/io_impl.cpp index 5d425bd8a..18f2d013f 100644 --- a/host/lib/usrp/usrp2/io_impl.cpp +++ b/host/lib/usrp/usrp2/io_impl.cpp @@ -122,7 +122,7 @@ void usrp2_impl::io_init(void){ std::cout << "TX samples per packet: " << get_max_send_samps_per_packet() << std::endl; //create new io impl - _io_impl = boost::shared_ptr(new io_impl(_data_transport)); + _io_impl = UHD_PIMPL_MAKE(io_impl, (_data_transport)); } /*********************************************************************** diff --git a/host/lib/usrp/usrp2/usrp2_impl.hpp b/host/lib/usrp/usrp2/usrp2_impl.hpp index bb4554e8d..40c193866 100644 --- a/host/lib/usrp/usrp2/usrp2_impl.hpp +++ b/host/lib/usrp/usrp2/usrp2_impl.hpp @@ -23,6 +23,7 @@ #include "codec_ctrl.hpp" #include "serdes_ctrl.hpp" #include +#include #include #include #include @@ -153,7 +154,7 @@ private: ; uhd::otw_type_t _rx_otw_type, _tx_otw_type; - struct io_impl; boost::shared_ptr _io_impl; + UHD_PIMPL_DECL(io_impl) _io_impl; void io_init(void); //udp transports for control and data -- cgit v1.2.3