USRP Hardware Driver and USRP Manual  Version: 3.11.0.HEAD-0-gdca39145
UHD and USRP Manual
zero_copy.hpp
Go to the documentation of this file.
1 //
2 // Copyright 2010-2012,2015 Ettus Research LLC
3 //
4 // SPDX-License-Identifier: GPL-3.0
5 //
6 
7 #ifndef INCLUDED_UHD_TRANSPORT_ZERO_COPY_HPP
8 #define INCLUDED_UHD_TRANSPORT_ZERO_COPY_HPP
9 
10 #include <uhd/config.hpp>
11 #include <boost/utility.hpp>
12 #include <boost/shared_ptr.hpp>
13 #include <boost/intrusive_ptr.hpp>
14 #include <boost/detail/atomic_count.hpp>
15 
16 namespace uhd{ namespace transport{
17 
20  public:
21  managed_buffer(void):_ref_count(0),_buffer(NULL),_length(0)
22  {
23 #ifdef UHD_TXRX_DEBUG_PRINTS
24  _mb_num = s_buffer_count;
25  // From Boost website: atomic_count seems only to have precrement operator.
26  ++s_buffer_count;
27 #endif
28  }
29 
30  virtual ~managed_buffer(void) {}
31 
37  virtual void release(void) = 0;
38 
43  UHD_INLINE void commit(size_t num_bytes){
44  _length = num_bytes;
45  }
46 
51  template <class T> UHD_INLINE T cast(void) const{
52  return static_cast<T>(_buffer);
53  }
54 
59  UHD_INLINE size_t size(void) const{
60  return _length;
61  }
62 
64  template <typename T> UHD_INLINE boost::intrusive_ptr<T> make(
65  T *p, void *buffer, size_t length
66  ){
67  _buffer = buffer;
68  _length = length;
69  return boost::intrusive_ptr<T>(p);
70  }
71 
72  boost::detail::atomic_count _ref_count;
73  typedef boost::intrusive_ptr<managed_buffer> sptr;
74 
75  int ref_count(){
76  return (int) _ref_count;
77  }
78 
79 #ifdef UHD_TXRX_DEBUG_PRINTS
80  int num() const{
81  return _mb_num;
82  }
83 #endif
84 
85  protected:
86  void *_buffer;
87  size_t _length;
88 #ifdef UHD_TXRX_DEBUG_PRINTS
89  int _mb_num;
90 #endif
91 
92  private:
93 #ifdef UHD_TXRX_DEBUG_PRINTS
94  static boost::detail::atomic_count s_buffer_count;
95 #endif
96  };
97 
99  ++(p->_ref_count);
100  }
101 
103  if (--(p->_ref_count) == 0) p->release();
104  }
105 
112  public:
113  typedef boost::intrusive_ptr<managed_recv_buffer> sptr;
114  };
115 
122  public:
123  typedef boost::intrusive_ptr<managed_send_buffer> sptr;
124  };
125 
134  };
135 
141  class UHD_API zero_copy_if : boost::noncopyable{
142  public:
143  typedef boost::shared_ptr<zero_copy_if> sptr;
144 
148  virtual ~zero_copy_if() {};
149 
155  virtual managed_recv_buffer::sptr get_recv_buff(double timeout = 0.1) = 0;
156 
162  virtual size_t get_num_recv_frames(void) const = 0;
163 
169  virtual size_t get_recv_frame_size(void) const = 0;
170 
176  virtual managed_send_buffer::sptr get_send_buff(double timeout = 0.1) = 0;
177 
183  virtual size_t get_num_send_frames(void) const = 0;
184 
190  virtual size_t get_send_frame_size(void) const = 0;
191 
192  };
193 
194 }} //namespace
195 
196 #endif /* INCLUDED_UHD_TRANSPORT_ZERO_COPY_HPP */
Simple managed buffer with release interface.
Definition: zero_copy.hpp:19
UHD_INLINE T cast(void) const
Definition: zero_copy.hpp:51
boost::intrusive_ptr< managed_buffer > sptr
Definition: zero_copy.hpp:73
size_t recv_frame_size
Definition: zero_copy.hpp:130
UHD_INLINE void intrusive_ptr_release(managed_buffer *p)
Definition: zero_copy.hpp:102
UHD_INLINE void commit(size_t num_bytes)
Definition: zero_copy.hpp:43
virtual void release(void)=0
size_t send_frame_size
Definition: zero_copy.hpp:131
Definition: zero_copy.hpp:129
size_t num_recv_frames
Definition: zero_copy.hpp:132
boost::shared_ptr< zero_copy_if > sptr
Definition: zero_copy.hpp:143
Definition: zero_copy.hpp:121
Definition: build_info.hpp:14
Definition: zero_copy.hpp:111
UHD_INLINE size_t size(void) const
Definition: zero_copy.hpp:59
boost::detail::atomic_count _ref_count
Definition: zero_copy.hpp:72
UHD_INLINE void intrusive_ptr_add_ref(managed_buffer *p)
Definition: zero_copy.hpp:98
#define UHD_INLINE
Definition: config.h:52
#define UHD_API
Definition: config.h:62
boost::intrusive_ptr< managed_recv_buffer > sptr
Definition: zero_copy.hpp:113
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:64
Definition: zero_copy.hpp:141
virtual ~zero_copy_if()
Definition: zero_copy.hpp:148
virtual ~managed_buffer(void)
Definition: zero_copy.hpp:30
boost::intrusive_ptr< managed_send_buffer > sptr
Definition: zero_copy.hpp:123
managed_buffer(void)
Definition: zero_copy.hpp:21
int ref_count()
Definition: zero_copy.hpp:75
size_t _length
Definition: zero_copy.hpp:87
void * _buffer
Definition: zero_copy.hpp:86
size_t num_send_frames
Definition: zero_copy.hpp:133