UHD  003.001.002
zero_copy.hpp
Go to the documentation of this file.
1 //
2 // Copyright 2010-2011 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 
26 namespace uhd{ namespace transport{
27 
29  template <typename T> UHD_INLINE boost::intrusive_ptr<T> make_managed_buffer(T *p){
30  p->_ref_count = 1; //reset the count to 1 reference
31  return boost::intrusive_ptr<T>(p, false);
32  }
33 
40  public:
41  typedef boost::intrusive_ptr<managed_recv_buffer> sptr;
42 
48  virtual void release(void) = 0;
49 
54  template <class T> inline T cast(void) const{
55  return static_cast<T>(this->get_buff());
56  }
57 
62  inline size_t size(void) const{
63  return this->get_size();
64  }
65 
66  private:
67  virtual const void *get_buff(void) const = 0;
68  virtual size_t get_size(void) const = 0;
69 
70  public: int _ref_count;
71  };
72 
74  ++(p->_ref_count);
75  }
76 
78  if (--(p->_ref_count) == 0) p->release();
79  }
80 
87  public:
88  typedef boost::intrusive_ptr<managed_send_buffer> sptr;
89 
96  virtual void commit(size_t num_bytes) = 0;
97 
102  template <class T> inline T cast(void) const{
103  return static_cast<T>(this->get_buff());
104  }
105 
110  inline size_t size(void) const{
111  return this->get_size();
112  }
113 
114  private:
115  virtual void *get_buff(void) const = 0;
116  virtual size_t get_size(void) const = 0;
117 
118  public: int _ref_count;
119  };
120 
122  ++(p->_ref_count);
123  }
124 
126  if (--(p->_ref_count) == 0) p->commit(0);
127  }
128 
134  class UHD_API zero_copy_if : boost::noncopyable{
135  public:
136  typedef boost::shared_ptr<zero_copy_if> sptr;
137 
143  virtual managed_recv_buffer::sptr get_recv_buff(double timeout = 0.1) = 0;
144 
150  virtual size_t get_num_recv_frames(void) const = 0;
151 
157  virtual size_t get_recv_frame_size(void) const = 0;
158 
164  virtual managed_send_buffer::sptr get_send_buff(double timeout = 0.1) = 0;
165 
171  virtual size_t get_num_send_frames(void) const = 0;
172 
178  virtual size_t get_send_frame_size(void) const = 0;
179 
180  };
181 
182 }} //namespace
183 
184 #endif /* INCLUDED_UHD_TRANSPORT_ZERO_COPY_HPP */
UHD_INLINE void intrusive_ptr_release(managed_recv_buffer *p)
Definition: zero_copy.hpp:77
size_t size(void) const
Definition: zero_copy.hpp:62
size_t size(void) const
Definition: zero_copy.hpp:110
boost::shared_ptr< zero_copy_if > sptr
Definition: zero_copy.hpp:136
Definition: zero_copy.hpp:86
#define UHD_API
Definition: config.hpp:76
Definition: convert.hpp:28
UHD_INLINE boost::intrusive_ptr< T > make_managed_buffer(T *p)
Create smart pointer to a reusable managed buffer.
Definition: zero_copy.hpp:29
int _ref_count
Definition: zero_copy.hpp:118
Definition: zero_copy.hpp:39
int _ref_count
Definition: zero_copy.hpp:70
#define UHD_INLINE
Definition: config.hpp:67
UHD_INLINE void intrusive_ptr_add_ref(managed_recv_buffer *p)
Definition: zero_copy.hpp:73
boost::intrusive_ptr< managed_recv_buffer > sptr
Definition: zero_copy.hpp:41
Definition: zero_copy.hpp:134
boost::intrusive_ptr< managed_send_buffer > sptr
Definition: zero_copy.hpp:88
T cast(void) const
Definition: zero_copy.hpp:102
T cast(void) const
Definition: zero_copy.hpp:54
virtual void commit(size_t num_bytes)=0