Pothos  0.7.0-gf7fbae99
The Pothos dataflow programming software suite
ManagedBuffer.hpp
Go to the documentation of this file.
1 
11 #pragma once
12 #include <Pothos/Config.hpp>
14 #include <memory> //shared_ptr
15 #include <atomic>
16 #include <cassert>
17 
18 namespace Pothos {
19 
21 class BufferManager;
22 class BufferChunk;
23 class BufferAccumulator;
24 
32 {
33 public:
34 
39  ManagedBuffer(void);
40 
44  ManagedBuffer(const SharedBuffer &buff);
45 
50  explicit operator bool(void) const;
51 
56  void reset(void);
57 
64  void reset(std::shared_ptr<BufferManager> manager, const SharedBuffer &buff, const size_t slabIndex = 0);
65 
71  const SharedBuffer &getBuffer(void) const;
72 
79  size_t getSlabIndex(void) const;
80 
82  ~ManagedBuffer(void);
83 
85  ManagedBuffer(const ManagedBuffer &obj);
86 
88  ManagedBuffer(ManagedBuffer &&obj) noexcept;
89 
91  ManagedBuffer &operator=(const ManagedBuffer &obj);
92 
94  ManagedBuffer &operator=(ManagedBuffer &&obj);
95 
97  std::shared_ptr<BufferManager> getBufferManager(void) const;
98 
100  bool operator<(const ManagedBuffer &) const;
101 
106  bool unique(void) const;
107 
111  size_t useCount(void) const;
112 
116  void setNextBuffer(const ManagedBuffer &next);
117 
122  ManagedBuffer getNextBuffer(void) const;
123 
124 private:
125  friend BufferChunk;
126  friend BufferAccumulator;
127  struct Impl; Impl *_impl;
128  ManagedBuffer(Impl *impl);
129  POTHOS_API friend bool operator==(const ManagedBuffer &lhs, const ManagedBuffer &rhs);
130 };
131 
136 inline bool operator==(const ManagedBuffer &lhs, const ManagedBuffer &rhs);
137 
138 } //namespace Pothos
139 
141 {
142  Impl(void);
143 
144  std::atomic<int> counter;
145  std::weak_ptr<BufferManager> weakManager;
147  size_t slabIndex;
149 
150  void incr(void)
151  {
152  counter.fetch_add(1, std::memory_order_relaxed);
153  }
154 
155  void decr(void)
156  {
157  //decrement the counter, and handle the last ref case
158  if (counter.fetch_sub(1, std::memory_order_release) == 1)
159  {
160  std::atomic_thread_fence(std::memory_order_acquire);
161  this->cleanup();
162  }
163  }
164 
165  void cleanup(void);
166 };
167 
169  _impl(nullptr)
170 {
171  return;
172 }
173 
174 inline Pothos::ManagedBuffer::operator bool(void) const
175 {
176  return _impl != nullptr;
177 }
178 
180 {
181  if (_impl != nullptr) _impl->decr();
182  _impl = nullptr;
183 }
184 
186 {
187  if (_impl == nullptr) return SharedBuffer::null();
188  return _impl->buffer;
189 }
190 
191 inline size_t Pothos::ManagedBuffer::getSlabIndex(void) const
192 {
193  assert(*this);
194  return _impl->slabIndex;
195 }
196 
198 {
199  if (_impl != nullptr) _impl->decr();
200 }
201 
203  _impl(obj._impl)
204 {
205  if (_impl != nullptr) _impl->incr();
206 }
207 
209  _impl(obj._impl)
210 {
211  obj._impl = nullptr;
212 }
213 
215 {
216  if (_impl != nullptr) _impl->decr();
217  this->_impl = obj._impl;
218  if (_impl != nullptr) _impl->incr();
219  return *this;
220 }
221 
223 {
224  if (_impl != nullptr) _impl->decr();
225  this->_impl = obj._impl;
226  obj._impl = nullptr;
227  return *this;
228 }
229 
230 inline std::shared_ptr<Pothos::BufferManager> Pothos::ManagedBuffer::getBufferManager(void) const
231 {
232  assert(*this);
233  return _impl->weakManager.lock();
234 }
235 
236 inline bool Pothos::ManagedBuffer::operator<(const ManagedBuffer &rhs) const
237 {
238  return _impl < rhs._impl;
239 }
240 
241 inline bool Pothos::ManagedBuffer::unique(void) const
242 {
243  if (_impl == nullptr) return false;
244  return _impl->counter.load(std::memory_order_relaxed) == 1;
245 }
246 
247 inline size_t Pothos::ManagedBuffer::useCount(void) const
248 {
249  if (_impl == nullptr) return 0;
250  return _impl->counter.load(std::memory_order_relaxed);
251 }
252 
254 {
255  assert(*this);
256  _impl->nextBuffer = next._impl;
257 }
258 
260 {
261  assert(*this);
262  return Pothos::ManagedBuffer(_impl->nextBuffer);
263 }
264 
266  _impl(impl)
267 {
268  if (_impl != nullptr) _impl->incr();
269 }
270 
271 inline bool Pothos::operator==(const ManagedBuffer &lhs, const ManagedBuffer &rhs)
272 {
273  return lhs._impl == rhs._impl;
274 }
Definition: BufferAccumulator.hpp:31
ManagedBuffer(void)
Definition: ManagedBuffer.hpp:168
bool unique(void) const
Definition: ManagedBuffer.hpp:241
#define POTHOS_API
Definition: Config.hpp:41
void setNextBuffer(const ManagedBuffer &next)
Definition: ManagedBuffer.hpp:253
POTHOS_API bool operator==(const Callable &lhs, const Callable &rhs)
std::shared_ptr< BufferManager > getBufferManager(void) const
Get the associated buffer manager.
Definition: ManagedBuffer.hpp:230
Definition: ArchiveEntry.hpp:20
void incr(void)
Definition: ManagedBuffer.hpp:150
void reset(void)
Definition: ManagedBuffer.hpp:179
SharedBuffer buffer
Definition: ManagedBuffer.hpp:146
~ManagedBuffer(void)
ManagedBuffer destructor.
Definition: ManagedBuffer.hpp:197
Definition: ManagedBuffer.hpp:31
std::atomic< int > counter
Definition: ManagedBuffer.hpp:144
ManagedBuffer getNextBuffer(void) const
Definition: ManagedBuffer.hpp:259
size_t useCount(void) const
Definition: ManagedBuffer.hpp:247
const SharedBuffer & getBuffer(void) const
Definition: ManagedBuffer.hpp:185
Definition: SharedBuffer.hpp:21
void decr(void)
Definition: ManagedBuffer.hpp:155
size_t getSlabIndex(void) const
Definition: ManagedBuffer.hpp:191
std::weak_ptr< BufferManager > weakManager
Definition: ManagedBuffer.hpp:145
static const SharedBuffer & null(void)
Get a const reference to a null/empty SharedBuffer.
Definition: BufferChunk.hpp:30
ManagedBuffer & operator=(const ManagedBuffer &obj)
ManagedBuffer copy assignment.
Definition: ManagedBuffer.hpp:214
bool operator<(const ManagedBuffer &) const
sortable operator for ManagedBuffer
Definition: ManagedBuffer.hpp:236
Definition: ManagedBuffer.hpp:140
size_t slabIndex
Definition: ManagedBuffer.hpp:147
Pothos::ManagedBuffer::Impl * nextBuffer
Definition: ManagedBuffer.hpp:148