UHD  003.006.002-0-g09bd61cc
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
zero_copy.hpp
Go to the documentation of this file.
1 //
2 // Copyright 2010-2012 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  virtual ~managed_buffer(void) {}
35 
41  virtual void release(void) = 0;
42 
47  UHD_INLINE void commit(size_t num_bytes){
48  _length = num_bytes;
49  }
50 
55  template <class T> UHD_INLINE T cast(void) const{
56  return static_cast<T>(_buffer);
57  }
58 
63  UHD_INLINE size_t size(void) const{
64  return _length;
65  }
66 
68  template <typename T> UHD_INLINE boost::intrusive_ptr<T> make(
69  T *p, void *buffer, size_t length
70  ){
71  _buffer = buffer;
72  _length = length;
73  return boost::intrusive_ptr<T>(p);
74  }
75 
76  boost::detail::atomic_count _ref_count;
77  typedef boost::intrusive_ptr<managed_buffer> sptr;
78 
79  protected:
80  void *_buffer;
81  size_t _length;
82  };
83 
85  ++(p->_ref_count);
86  }
87 
89  if (--(p->_ref_count) == 0) p->release();
90  }
91 
98  public:
99  typedef boost::intrusive_ptr<managed_recv_buffer> sptr;
100  };
101 
108  public:
109  typedef boost::intrusive_ptr<managed_send_buffer> sptr;
110  };
111 
117  class UHD_API zero_copy_if : boost::noncopyable{
118  public:
119  typedef boost::shared_ptr<zero_copy_if> sptr;
120 
126  virtual managed_recv_buffer::sptr get_recv_buff(double timeout = 0.1) = 0;
127 
133  virtual size_t get_num_recv_frames(void) const = 0;
134 
140  virtual size_t get_recv_frame_size(void) const = 0;
141 
147  virtual managed_send_buffer::sptr get_send_buff(double timeout = 0.1) = 0;
148 
154  virtual size_t get_num_send_frames(void) const = 0;
155 
161  virtual size_t get_send_frame_size(void) const = 0;
162 
163  };
164 
165 }} //namespace
166 
167 #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:55
boost::intrusive_ptr< managed_buffer > sptr
Definition: zero_copy.hpp:77
UHD_INLINE void intrusive_ptr_release(managed_buffer *p)
Definition: zero_copy.hpp:88
UHD_INLINE void commit(size_t num_bytes)
Definition: zero_copy.hpp:47
virtual void release(void)=0
boost::shared_ptr< zero_copy_if > sptr
Definition: zero_copy.hpp:119
Definition: zero_copy.hpp:107
#define UHD_API
Definition: config.hpp:79
Definition: convert.hpp:28
Definition: zero_copy.hpp:97
#define UHD_INLINE
Definition: config.hpp:69
UHD_INLINE size_t size(void) const
Definition: zero_copy.hpp:63
boost::detail::atomic_count _ref_count
Definition: zero_copy.hpp:76
UHD_INLINE void intrusive_ptr_add_ref(managed_buffer *p)
Definition: zero_copy.hpp:84
boost::intrusive_ptr< managed_recv_buffer > sptr
Definition: zero_copy.hpp:99
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:68
Definition: zero_copy.hpp:117
virtual ~managed_buffer(void)
Definition: zero_copy.hpp:34
boost::intrusive_ptr< managed_send_buffer > sptr
Definition: zero_copy.hpp:109
managed_buffer(void)
Definition: zero_copy.hpp:32
size_t _length
Definition: zero_copy.hpp:81
void * _buffer
Definition: zero_copy.hpp:80