USRP Hardware Driver and USRP Manual  Version: 3.11.0.HEAD-0-ga1b5c4ae
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>
12 #include <boost/utility.hpp>
13 #include <boost/shared_ptr.hpp>
14 #include <boost/intrusive_ptr.hpp>
15 #include <boost/detail/atomic_count.hpp>
16 
17 namespace uhd{ namespace transport{
18 
21  public:
22  managed_buffer(void):_ref_count(0),_buffer(NULL),_length(0)
23  {
24 #ifdef UHD_TXRX_DEBUG_PRINTS
25  _mb_num = s_buffer_count;
26  // From Boost website: atomic_count seems only to have precrement operator.
27  ++s_buffer_count;
28 #endif
29  }
30 
31  virtual ~managed_buffer(void) {}
32 
38  virtual void release(void) = 0;
39 
44  UHD_INLINE void commit(size_t num_bytes){
45  _length = num_bytes;
46  }
47 
52  template <class T> UHD_INLINE T cast(void) const{
53  return static_cast<T>(_buffer);
54  }
55 
60  UHD_INLINE size_t size(void) const{
61  return _length;
62  }
63 
65  template <typename T> UHD_INLINE boost::intrusive_ptr<T> make(
66  T *p, void *buffer, size_t length
67  ){
68  _buffer = buffer;
69  _length = length;
70  return boost::intrusive_ptr<T>(p);
71  }
72 
73  boost::detail::atomic_count _ref_count;
74  typedef boost::intrusive_ptr<managed_buffer> sptr;
75 
76  int ref_count(){
77  return (int) _ref_count;
78  }
79 
80 #ifdef UHD_TXRX_DEBUG_PRINTS
81  int num() const{
82  return _mb_num;
83  }
84 #endif
85 
86  protected:
87  void *_buffer;
88  size_t _length;
89 #ifdef UHD_TXRX_DEBUG_PRINTS
90  int _mb_num;
91 #endif
92 
93  private:
94 #ifdef UHD_TXRX_DEBUG_PRINTS
95  static boost::detail::atomic_count s_buffer_count;
96 #endif
97  };
98 
100  ++(p->_ref_count);
101  }
102 
104  if (--(p->_ref_count) == 0) p->release();
105  }
106 
113  public:
114  typedef boost::intrusive_ptr<managed_recv_buffer> sptr;
115  };
116 
123  public:
124  typedef boost::intrusive_ptr<managed_send_buffer> sptr;
125  };
126 
135  };
136 
142  class UHD_API zero_copy_if : boost::noncopyable{
143  public:
144  typedef boost::shared_ptr<zero_copy_if> sptr;
145 
149  virtual ~zero_copy_if() {};
150 
156  virtual managed_recv_buffer::sptr get_recv_buff(double timeout = 0.1) = 0;
157 
163  virtual size_t get_num_recv_frames(void) const = 0;
164 
170  virtual size_t get_recv_frame_size(void) const = 0;
171 
177  virtual managed_send_buffer::sptr get_send_buff(double timeout = 0.1) = 0;
178 
184  virtual size_t get_num_send_frames(void) const = 0;
185 
191  virtual size_t get_send_frame_size(void) const = 0;
192 
193  };
194 
195 }} //namespace
196 
197 #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:52
boost::intrusive_ptr< managed_buffer > sptr
Definition: zero_copy.hpp:74
size_t recv_frame_size
Definition: zero_copy.hpp:131
UHD_INLINE void intrusive_ptr_release(managed_buffer *p)
Definition: zero_copy.hpp:103
UHD_INLINE void commit(size_t num_bytes)
Definition: zero_copy.hpp:44
virtual void release(void)=0
size_t send_frame_size
Definition: zero_copy.hpp:132
Definition: zero_copy.hpp:130
size_t num_recv_frames
Definition: zero_copy.hpp:133
boost::shared_ptr< zero_copy_if > sptr
Definition: zero_copy.hpp:144
Definition: zero_copy.hpp:122
Definition: build_info.hpp:14
Definition: zero_copy.hpp:112
UHD_INLINE size_t size(void) const
Definition: zero_copy.hpp:60
boost::detail::atomic_count _ref_count
Definition: zero_copy.hpp:73
UHD_INLINE void intrusive_ptr_add_ref(managed_buffer *p)
Definition: zero_copy.hpp:99
#define UHD_INLINE
Definition: config.h:53
#define UHD_API
Definition: config.h:63
boost::intrusive_ptr< managed_recv_buffer > sptr
Definition: zero_copy.hpp:114
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:65
Definition: zero_copy.hpp:142
virtual ~zero_copy_if()
Definition: zero_copy.hpp:149
virtual ~managed_buffer(void)
Definition: zero_copy.hpp:31
boost::intrusive_ptr< managed_send_buffer > sptr
Definition: zero_copy.hpp:124
managed_buffer(void)
Definition: zero_copy.hpp:22
int ref_count()
Definition: zero_copy.hpp:76
size_t _length
Definition: zero_copy.hpp:88
void * _buffer
Definition: zero_copy.hpp:87
size_t num_send_frames
Definition: zero_copy.hpp:134