USRP Hardware Driver and USRP Manual  Version: 3.15.0.0-4+b1satnogs1
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>
56  UHD_INLINE T cast(void) const
57  {
58  return static_cast<T>(_buffer);
59  }
60 
65  UHD_INLINE size_t size(void) const
66  {
67  return _length;
68  }
69 
71  template <typename T>
72  UHD_INLINE boost::intrusive_ptr<T> make(T* p, void* buffer, size_t length)
73  {
74  _buffer = buffer;
75  _length = length;
76  return boost::intrusive_ptr<T>(p);
77  }
78 
79  boost::detail::atomic_count _ref_count;
80  typedef boost::intrusive_ptr<managed_buffer> sptr;
81 
82  int ref_count()
83  {
84  return (int)_ref_count;
85  }
86 
87 #ifdef UHD_TXRX_DEBUG_PRINTS
88  int num() const
89  {
90  return _mb_num;
91  }
92 #endif
93 
94 protected:
95  void* _buffer;
96  size_t _length;
97 #ifdef UHD_TXRX_DEBUG_PRINTS
98  int _mb_num;
99 #endif
100 
101 private:
102 #ifdef UHD_TXRX_DEBUG_PRINTS
103  static boost::detail::atomic_count s_buffer_count;
104 #endif
105 };
106 
108 {
109  ++(p->_ref_count);
110 }
111 
113 {
114  if (--(p->_ref_count) == 0)
115  p->release();
116 }
117 
124 {
125 public:
126  typedef boost::intrusive_ptr<managed_recv_buffer> sptr;
127 };
128 
135 {
136 public:
137  typedef boost::intrusive_ptr<managed_send_buffer> sptr;
138 };
139 
144 {
146  : recv_frame_size(0)
147  , send_frame_size(0)
148  , num_recv_frames(0)
149  , num_send_frames(0)
150  , recv_buff_size(0)
151  , send_buff_size(0)
152  { /* NOP */
153  }
160 };
161 
168 {
169 public:
170  typedef boost::shared_ptr<zero_copy_if> sptr;
171 
175  virtual ~zero_copy_if(){};
176 
182  virtual managed_recv_buffer::sptr get_recv_buff(double timeout = 0.1) = 0;
183 
189  virtual size_t get_num_recv_frames(void) const = 0;
190 
196  virtual size_t get_recv_frame_size(void) const = 0;
197 
203  virtual managed_send_buffer::sptr get_send_buff(double timeout = 0.1) = 0;
204 
210  virtual size_t get_num_send_frames(void) const = 0;
211 
217  virtual size_t get_send_frame_size(void) const = 0;
218 };
219 
220 }} // namespace uhd::transport
221 
222 #endif /* INCLUDED_UHD_TRANSPORT_ZERO_COPY_HPP */
Simple managed buffer with release interface.
Definition: zero_copy.hpp:22
int ref_count()
Definition: zero_copy.hpp:82
UHD_INLINE void commit(size_t num_bytes)
Definition: zero_copy.hpp:46
boost::detail::atomic_count _ref_count
Definition: zero_copy.hpp:79
managed_buffer(void)
Definition: zero_copy.hpp:24
size_t _length
Definition: zero_copy.hpp:96
virtual ~managed_buffer(void)
Definition: zero_copy.hpp:33
UHD_INLINE size_t size(void) const
Definition: zero_copy.hpp:65
boost::intrusive_ptr< managed_buffer > sptr
Definition: zero_copy.hpp:80
void * _buffer
Definition: zero_copy.hpp:95
UHD_INLINE T cast(void) const
Definition: zero_copy.hpp:56
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:72
virtual void release(void)=0
Definition: zero_copy.hpp:124
boost::intrusive_ptr< managed_recv_buffer > sptr
Definition: zero_copy.hpp:126
Definition: zero_copy.hpp:135
boost::intrusive_ptr< managed_send_buffer > sptr
Definition: zero_copy.hpp:137
Definition: zero_copy.hpp:168
virtual managed_send_buffer::sptr get_send_buff(double timeout=0.1)=0
virtual size_t get_num_send_frames(void) const =0
virtual managed_recv_buffer::sptr get_recv_buff(double timeout=0.1)=0
virtual size_t get_recv_frame_size(void) const =0
virtual size_t get_send_frame_size(void) const =0
virtual ~zero_copy_if()
Definition: zero_copy.hpp:175
virtual size_t get_num_recv_frames(void) const =0
boost::shared_ptr< zero_copy_if > sptr
Definition: zero_copy.hpp:170
#define UHD_INLINE
Definition: config.h:53
#define UHD_API
Definition: config.h:68
UHD_INLINE void intrusive_ptr_add_ref(managed_buffer *p)
Definition: zero_copy.hpp:107
UHD_INLINE void intrusive_ptr_release(managed_buffer *p)
Definition: zero_copy.hpp:112
Definition: build_info.hpp:13
boost::noncopyable noncopyable
Definition: noncopyable.hpp:46
Definition: zero_copy.hpp:144
size_t num_recv_frames
Definition: zero_copy.hpp:156
size_t recv_frame_size
Definition: zero_copy.hpp:154
size_t send_buff_size
Definition: zero_copy.hpp:159
size_t send_frame_size
Definition: zero_copy.hpp:155
size_t recv_buff_size
Definition: zero_copy.hpp:158
zero_copy_xport_params()
Definition: zero_copy.hpp:145
size_t num_send_frames
Definition: zero_copy.hpp:157