USRP Hardware Driver and USRP Manual  Version: 003.009.000-0-gcd88f80f
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 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 //
17 
18 #ifndef INCLUDED_UHD_TRANSPORT_ZERO_COPY_HPP
19 #define INCLUDED_UHD_TRANSPORT_ZERO_COPY_HPP
20 
21 #include <uhd/config.hpp>
22 #include <boost/utility.hpp>
23 #include <boost/shared_ptr.hpp>
24 #include <boost/intrusive_ptr.hpp>
25 #include <boost/detail/atomic_count.hpp>
26 
27 namespace uhd{ namespace transport{
28 
31  public:
32  managed_buffer(void):_ref_count(0),_buffer(NULL),_length(0)
33  {
34 #ifdef UHD_TXRX_DEBUG_PRINTS
35  _mb_num = s_buffer_count;
36  // From Boost website: atomic_count seems only to have precrement operator.
37  ++s_buffer_count;
38 #endif
39  }
40 
41  virtual ~managed_buffer(void) {}
42 
48  virtual void release(void) = 0;
49 
54  UHD_INLINE void commit(size_t num_bytes){
55  _length = num_bytes;
56  }
57 
62  template <class T> UHD_INLINE T cast(void) const{
63  return static_cast<T>(_buffer);
64  }
65 
70  UHD_INLINE size_t size(void) const{
71  return _length;
72  }
73 
75  template <typename T> UHD_INLINE boost::intrusive_ptr<T> make(
76  T *p, void *buffer, size_t length
77  ){
78  _buffer = buffer;
79  _length = length;
80  return boost::intrusive_ptr<T>(p);
81  }
82 
83  boost::detail::atomic_count _ref_count;
84  typedef boost::intrusive_ptr<managed_buffer> sptr;
85 
86  int ref_count(){
87  return (int) _ref_count;
88  }
89 
90 #ifdef UHD_TXRX_DEBUG_PRINTS
91  int num() const{
92  return _mb_num;
93  }
94 #endif
95 
96  protected:
97  void *_buffer;
98  size_t _length;
99 #ifdef UHD_TXRX_DEBUG_PRINTS
100  int _mb_num;
101 #endif
102 
103  private:
104 #ifdef UHD_TXRX_DEBUG_PRINTS
105  static boost::detail::atomic_count s_buffer_count;
106 #endif
107  };
108 
110  ++(p->_ref_count);
111  }
112 
114  if (--(p->_ref_count) == 0) p->release();
115  }
116 
123  public:
124  typedef boost::intrusive_ptr<managed_recv_buffer> sptr;
125  };
126 
133  public:
134  typedef boost::intrusive_ptr<managed_send_buffer> sptr;
135  };
136 
145  };
146 
152  class UHD_API zero_copy_if : boost::noncopyable{
153  public:
154  typedef boost::shared_ptr<zero_copy_if> sptr;
155 
159  virtual ~zero_copy_if() {};
160 
166  virtual managed_recv_buffer::sptr get_recv_buff(double timeout = 0.1) = 0;
167 
173  virtual size_t get_num_recv_frames(void) const = 0;
174 
180  virtual size_t get_recv_frame_size(void) const = 0;
181 
187  virtual managed_send_buffer::sptr get_send_buff(double timeout = 0.1) = 0;
188 
194  virtual size_t get_num_send_frames(void) const = 0;
195 
201  virtual size_t get_send_frame_size(void) const = 0;
202 
203  };
204 
205 }} //namespace
206 
207 #endif /* INCLUDED_UHD_TRANSPORT_ZERO_COPY_HPP */
Simple managed buffer with release interface.
Definition: zero_copy.hpp:30
UHD_INLINE T cast(void) const
Definition: zero_copy.hpp:62
boost::intrusive_ptr< managed_buffer > sptr
Definition: zero_copy.hpp:84
size_t recv_frame_size
Definition: zero_copy.hpp:141
UHD_INLINE void intrusive_ptr_release(managed_buffer *p)
Definition: zero_copy.hpp:113
UHD_INLINE void commit(size_t num_bytes)
Definition: zero_copy.hpp:54
virtual void release(void)=0
size_t send_frame_size
Definition: zero_copy.hpp:142
Definition: zero_copy.hpp:140
size_t num_recv_frames
Definition: zero_copy.hpp:143
boost::shared_ptr< zero_copy_if > sptr
Definition: zero_copy.hpp:154
Definition: zero_copy.hpp:132
Definition: convert.hpp:28
Definition: zero_copy.hpp:122
UHD_INLINE size_t size(void) const
Definition: zero_copy.hpp:70
boost::detail::atomic_count _ref_count
Definition: zero_copy.hpp:83
UHD_INLINE void intrusive_ptr_add_ref(managed_buffer *p)
Definition: zero_copy.hpp:109
#define UHD_INLINE
Definition: config.h:56
#define UHD_API
Definition: config.h:66
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:75
Definition: zero_copy.hpp:152
virtual ~zero_copy_if()
Definition: zero_copy.hpp:159
virtual ~managed_buffer(void)
Definition: zero_copy.hpp:41
boost::intrusive_ptr< managed_send_buffer > sptr
Definition: zero_copy.hpp:134
managed_buffer(void)
Definition: zero_copy.hpp:32
int ref_count()
Definition: zero_copy.hpp:86
size_t _length
Definition: zero_copy.hpp:98
void * _buffer
Definition: zero_copy.hpp:97
size_t num_send_frames
Definition: zero_copy.hpp:144