USRP Hardware Driver and USRP Manual  Version: 3.14.1.HEAD-0-g0347a6d8
UHD and USRP Manual
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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>
12 #include <boost/detail/atomic_count.hpp>
13 #include <boost/intrusive_ptr.hpp>
14 #include <boost/shared_ptr.hpp>
15 #include <boost/utility.hpp>
16 
17 namespace uhd { namespace transport {
18 
21 {
22 public:
23  managed_buffer(void) : _ref_count(0), _buffer(NULL), _length(0)
24  {
25 #ifdef UHD_TXRX_DEBUG_PRINTS
26  _mb_num = s_buffer_count;
27  // From Boost website: atomic_count seems only to have precrement operator.
28  ++s_buffer_count;
29 #endif
30  }
31 
32  virtual ~managed_buffer(void) {}
33 
39  virtual void release(void) = 0;
40 
45  UHD_INLINE void commit(size_t num_bytes)
46  {
47  _length = num_bytes;
48  }
49 
54  template <class T> UHD_INLINE T cast(void) const
55  {
56  return static_cast<T>(_buffer);
57  }
58 
63  UHD_INLINE size_t size(void) const
64  {
65  return _length;
66  }
67 
69  template <typename T>
70  UHD_INLINE boost::intrusive_ptr<T> make(T* p, void* buffer, size_t length)
71  {
72  _buffer = buffer;
73  _length = length;
74  return boost::intrusive_ptr<T>(p);
75  }
76 
77  boost::detail::atomic_count _ref_count;
78  typedef boost::intrusive_ptr<managed_buffer> sptr;
79 
80  int ref_count()
81  {
82  return (int)_ref_count;
83  }
84 
85 #ifdef UHD_TXRX_DEBUG_PRINTS
86  int num() const
87  {
88  return _mb_num;
89  }
90 #endif
91 
92 protected:
93  void* _buffer;
94  size_t _length;
95 #ifdef UHD_TXRX_DEBUG_PRINTS
96  int _mb_num;
97 #endif
98 
99 private:
100 #ifdef UHD_TXRX_DEBUG_PRINTS
101  static boost::detail::atomic_count s_buffer_count;
102 #endif
103 };
104 
106 {
107  ++(p->_ref_count);
108 }
109 
111 {
112  if (--(p->_ref_count) == 0)
113  p->release();
114 }
115 
122 {
123 public:
124  typedef boost::intrusive_ptr<managed_recv_buffer> sptr;
125 };
126 
133 {
134 public:
135  typedef boost::intrusive_ptr<managed_send_buffer> sptr;
136 };
137 
142 {
144  : recv_frame_size(0)
145  , send_frame_size(0)
146  , num_recv_frames(0)
147  , num_send_frames(0)
148  , recv_buff_size(0)
149  , send_buff_size(0)
150  { /* NOP */
151  }
158 };
159 
165 class UHD_API zero_copy_if : boost::noncopyable
166 {
167 public:
168  typedef boost::shared_ptr<zero_copy_if> sptr;
169 
173  virtual ~zero_copy_if(){};
174 
180  virtual managed_recv_buffer::sptr get_recv_buff(double timeout = 0.1) = 0;
181 
187  virtual size_t get_num_recv_frames(void) const = 0;
188 
194  virtual size_t get_recv_frame_size(void) const = 0;
195 
201  virtual managed_send_buffer::sptr get_send_buff(double timeout = 0.1) = 0;
202 
208  virtual size_t get_num_send_frames(void) const = 0;
209 
215  virtual size_t get_send_frame_size(void) const = 0;
216 };
217 
218 }} // namespace uhd::transport
219 
220 #endif /* INCLUDED_UHD_TRANSPORT_ZERO_COPY_HPP */
Simple managed buffer with release interface.
Definition: zero_copy.hpp:20
UHD_INLINE T cast(void) const
Definition: zero_copy.hpp:54
size_t recv_buff_size
Definition: zero_copy.hpp:156
boost::intrusive_ptr< managed_buffer > sptr
Definition: zero_copy.hpp:78
size_t recv_frame_size
Definition: zero_copy.hpp:152
UHD_INLINE void intrusive_ptr_release(managed_buffer *p)
Definition: zero_copy.hpp:110
UHD_INLINE void commit(size_t num_bytes)
Definition: zero_copy.hpp:45
virtual void release(void)=0
size_t send_frame_size
Definition: zero_copy.hpp:153
Definition: zero_copy.hpp:141
size_t num_recv_frames
Definition: zero_copy.hpp:154
boost::shared_ptr< zero_copy_if > sptr
Definition: zero_copy.hpp:168
Definition: zero_copy.hpp:132
zero_copy_xport_params()
Definition: zero_copy.hpp:143
Definition: build_info.hpp:13
Definition: zero_copy.hpp:121
UHD_INLINE size_t size(void) const
Definition: zero_copy.hpp:63
boost::detail::atomic_count _ref_count
Definition: zero_copy.hpp:77
UHD_INLINE void intrusive_ptr_add_ref(managed_buffer *p)
Definition: zero_copy.hpp:105
#define UHD_INLINE
Definition: config.h:53
#define UHD_API
Definition: config.h:68
size_t send_buff_size
Definition: zero_copy.hpp:157
boost::intrusive_ptr< managed_recv_buffer > sptr
Definition: zero_copy.hpp:124
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:70
Definition: zero_copy.hpp:165
virtual ~zero_copy_if()
Definition: zero_copy.hpp:173
virtual ~managed_buffer(void)
Definition: zero_copy.hpp:32
boost::intrusive_ptr< managed_send_buffer > sptr
Definition: zero_copy.hpp:135
managed_buffer(void)
Definition: zero_copy.hpp:23
int ref_count()
Definition: zero_copy.hpp:80
size_t _length
Definition: zero_copy.hpp:94
void * _buffer
Definition: zero_copy.hpp:93
size_t num_send_frames
Definition: zero_copy.hpp:155