USRP Hardware Driver and USRP Manual  Version: 3.15.0.HEAD-0-gaea0e2de
UHD and USRP Manual
zero_copy.hpp
Go to the documentation of this file.
1 //
2 // Copyright 2010-2012,2015 Ettus Research LLC
3 // Copyright 2018 Ettus Research, a National Instruments Company
4 //
5 // SPDX-License-Identifier: GPL-3.0-or-later
6 //
7 
8 #ifndef INCLUDED_UHD_TRANSPORT_ZERO_COPY_HPP
9 #define INCLUDED_UHD_TRANSPORT_ZERO_COPY_HPP
10 
11 #include <uhd/config.hpp>
13 #include <boost/detail/atomic_count.hpp>
14 #include <boost/intrusive_ptr.hpp>
15 #include <boost/shared_ptr.hpp>
16 #include <boost/utility.hpp>
17 
18 namespace uhd { namespace transport {
19 
22 {
23 public:
24  managed_buffer(void) : _ref_count(0), _buffer(NULL), _length(0)
25  {
26 #ifdef UHD_TXRX_DEBUG_PRINTS
27  _mb_num = s_buffer_count;
28  // From Boost website: atomic_count seems only to have precrement operator.
29  ++s_buffer_count;
30 #endif
31  }
32 
33  virtual ~managed_buffer(void) {}
34 
40  virtual void release(void) = 0;
41 
46  UHD_INLINE void commit(size_t num_bytes)
47  {
48  _length = num_bytes;
49  }
50 
55  template <class T> UHD_INLINE T cast(void) const
56  {
57  return static_cast<T>(_buffer);
58  }
59 
64  UHD_INLINE size_t size(void) const
65  {
66  return _length;
67  }
68 
70  template <typename T>
71  UHD_INLINE boost::intrusive_ptr<T> make(T* p, void* buffer, size_t length)
72  {
73  _buffer = buffer;
74  _length = length;
75  return boost::intrusive_ptr<T>(p);
76  }
77 
78  boost::detail::atomic_count _ref_count;
79  typedef boost::intrusive_ptr<managed_buffer> sptr;
80 
81  int ref_count()
82  {
83  return (int)_ref_count;
84  }
85 
86 #ifdef UHD_TXRX_DEBUG_PRINTS
87  int num() const
88  {
89  return _mb_num;
90  }
91 #endif
92 
93 protected:
94  void* _buffer;
95  size_t _length;
96 #ifdef UHD_TXRX_DEBUG_PRINTS
97  int _mb_num;
98 #endif
99 
100 private:
101 #ifdef UHD_TXRX_DEBUG_PRINTS
102  static boost::detail::atomic_count s_buffer_count;
103 #endif
104 };
105 
107 {
108  ++(p->_ref_count);
109 }
110 
112 {
113  if (--(p->_ref_count) == 0)
114  p->release();
115 }
116 
123 {
124 public:
125  typedef boost::intrusive_ptr<managed_recv_buffer> sptr;
126 };
127 
134 {
135 public:
136  typedef boost::intrusive_ptr<managed_send_buffer> sptr;
137 };
138 
143 {
145  : recv_frame_size(0)
146  , send_frame_size(0)
147  , num_recv_frames(0)
148  , num_send_frames(0)
149  , recv_buff_size(0)
150  , send_buff_size(0)
151  { /* NOP */
152  }
159 };
160 
167 {
168 public:
169  typedef boost::shared_ptr<zero_copy_if> sptr;
170 
174  virtual ~zero_copy_if(){};
175 
181  virtual managed_recv_buffer::sptr get_recv_buff(double timeout = 0.1) = 0;
182 
188  virtual size_t get_num_recv_frames(void) const = 0;
189 
195  virtual size_t get_recv_frame_size(void) const = 0;
196 
202  virtual managed_send_buffer::sptr get_send_buff(double timeout = 0.1) = 0;
203 
209  virtual size_t get_num_send_frames(void) const = 0;
210 
216  virtual size_t get_send_frame_size(void) const = 0;
217 };
218 
219 }} // namespace uhd::transport
220 
221 #endif /* INCLUDED_UHD_TRANSPORT_ZERO_COPY_HPP */
Simple managed buffer with release interface.
Definition: zero_copy.hpp:21
UHD_INLINE T cast(void) const
Definition: zero_copy.hpp:55
size_t recv_buff_size
Definition: zero_copy.hpp:157
boost::intrusive_ptr< managed_buffer > sptr
Definition: zero_copy.hpp:79
size_t recv_frame_size
Definition: zero_copy.hpp:153
UHD_INLINE void intrusive_ptr_release(managed_buffer *p)
Definition: zero_copy.hpp:111
boost::noncopyable noncopyable
Definition: noncopyable.hpp:46
UHD_INLINE void commit(size_t num_bytes)
Definition: zero_copy.hpp:46
virtual void release(void)=0
size_t send_frame_size
Definition: zero_copy.hpp:154
Definition: zero_copy.hpp:142
size_t num_recv_frames
Definition: zero_copy.hpp:155
boost::shared_ptr< zero_copy_if > sptr
Definition: zero_copy.hpp:169
Definition: zero_copy.hpp:133
zero_copy_xport_params()
Definition: zero_copy.hpp:144
Definition: build_info.hpp:13
Definition: zero_copy.hpp:122
UHD_INLINE size_t size(void) const
Definition: zero_copy.hpp:64
boost::detail::atomic_count _ref_count
Definition: zero_copy.hpp:78
UHD_INLINE void intrusive_ptr_add_ref(managed_buffer *p)
Definition: zero_copy.hpp:106
#define UHD_INLINE
Definition: config.h:53
#define UHD_API
Definition: config.h:68
size_t send_buff_size
Definition: zero_copy.hpp:158
boost::intrusive_ptr< managed_recv_buffer > sptr
Definition: zero_copy.hpp:125
UHD_INLINE boost::intrusive_ptr< T > make(T *p, void *buffer, size_t length)
Create smart pointer to a reusable managed buffer.
Definition: zero_copy.hpp:71
Definition: zero_copy.hpp:166
virtual ~zero_copy_if()
Definition: zero_copy.hpp:174
virtual ~managed_buffer(void)
Definition: zero_copy.hpp:33
boost::intrusive_ptr< managed_send_buffer > sptr
Definition: zero_copy.hpp:136
managed_buffer(void)
Definition: zero_copy.hpp:24
int ref_count()
Definition: zero_copy.hpp:81
size_t _length
Definition: zero_copy.hpp:95
void * _buffer
Definition: zero_copy.hpp:94
size_t num_send_frames
Definition: zero_copy.hpp:156