Pothos  0.4.1-gb758ed46
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 
45  explicit operator bool(void) const;
46 
51  void reset(void);
52 
59  void reset(std::shared_ptr<BufferManager> manager, const SharedBuffer &buff, const size_t slabIndex = 0);
60 
66  const SharedBuffer &getBuffer(void) const;
67 
74  size_t getSlabIndex(void) const;
75 
77  ~ManagedBuffer(void);
78 
80  ManagedBuffer(const ManagedBuffer &obj);
81 
84 
86  ManagedBuffer &operator=(const ManagedBuffer &obj);
87 
89  ManagedBuffer &operator=(ManagedBuffer &&obj);
90 
92  std::shared_ptr<BufferManager> getBufferManager(void) const;
93 
95  bool operator<(const ManagedBuffer &) const;
96 
101  bool unique(void) const;
102 
106  size_t useCount(void) const;
107 
111  void setNextBuffer(const ManagedBuffer &next);
112 
117  ManagedBuffer getNextBuffer(void) const;
118 
119 private:
120  friend BufferChunk;
121  friend BufferAccumulator;
122  struct Impl; Impl *_impl;
123  ManagedBuffer(Impl *impl);
124  POTHOS_API friend bool operator==(const ManagedBuffer &lhs, const ManagedBuffer &rhs);
125 };
126 
131 inline bool operator==(const ManagedBuffer &lhs, const ManagedBuffer &rhs);
132 
133 } //namespace Pothos
134 
136 {
137  Impl(void);
138 
139  std::atomic<int> counter;
140  std::weak_ptr<BufferManager> weakManager;
142  size_t slabIndex;
144 
145  void incr(void)
146  {
147  counter++;
148  }
149 
150  void decr(void)
151  {
152  //decrement the counter, and handle the last ref case
153  if (counter.fetch_sub(1) == 0) this->cleanup();
154  }
155 
156  void cleanup(void);
157 };
158 
160  _impl(nullptr)
161 {
162  return;
163 }
164 
165 inline Pothos::ManagedBuffer::operator bool(void) const
166 {
167  return _impl != nullptr;
168 }
169 
171 {
172  if (_impl != nullptr) _impl->decr();
173  _impl = nullptr;
174 }
175 
177 {
178  assert(*this);
179  return _impl->buffer;
180 }
181 
182 inline size_t Pothos::ManagedBuffer::getSlabIndex(void) const
183 {
184  assert(*this);
185  return _impl->slabIndex;
186 }
187 
189 {
190  if (_impl != nullptr) _impl->decr();
191 }
192 
194  _impl(obj._impl)
195 {
196  if (_impl != nullptr) _impl->incr();
197 }
198 
200  _impl(obj._impl)
201 {
202  obj._impl = nullptr;
203 }
204 
206 {
207  if (_impl != nullptr) _impl->decr();
208  this->_impl = obj._impl;
209  if (_impl != nullptr) _impl->incr();
210  return *this;
211 }
212 
214 {
215  if (_impl != nullptr) _impl->decr();
216  this->_impl = obj._impl;
217  obj._impl = nullptr;
218  return *this;
219 }
220 
221 inline std::shared_ptr<Pothos::BufferManager> Pothos::ManagedBuffer::getBufferManager(void) const
222 {
223  assert(*this);
224  return _impl->weakManager.lock();
225 }
226 
227 inline bool Pothos::ManagedBuffer::operator<(const ManagedBuffer &rhs) const
228 {
229  return _impl < rhs._impl;
230 }
231 
232 inline bool Pothos::ManagedBuffer::unique(void) const
233 {
234  return _impl->counter == 0;
235 }
236 
237 inline size_t Pothos::ManagedBuffer::useCount(void) const
238 {
239  if (*this) return _impl->counter+1;
240  return 0;
241 }
242 
244 {
245  assert(*this);
246  _impl->nextBuffer = next._impl;
247 }
248 
250 {
251  assert(*this);
252  return Pothos::ManagedBuffer(_impl->nextBuffer);
253 }
254 
256  _impl(impl)
257 {
258  if (_impl != nullptr) _impl->incr();
259 }
260 
261 inline bool Pothos::operator==(const ManagedBuffer &lhs, const ManagedBuffer &rhs)
262 {
263  return lhs._impl == rhs._impl;
264 }
Definition: BufferAccumulator.hpp:31
ManagedBuffer(void)
Definition: ManagedBuffer.hpp:159
const SharedBuffer & getBuffer(void) const
Definition: ManagedBuffer.hpp:176
#define POTHOS_API
Definition: Config.hpp:41
ManagedBuffer getNextBuffer(void) const
Definition: ManagedBuffer.hpp:249
void setNextBuffer(const ManagedBuffer &next)
Definition: ManagedBuffer.hpp:243
std::shared_ptr< BufferManager > getBufferManager(void) const
Get the associated buffer manager.
Definition: ManagedBuffer.hpp:221
bool unique(void) const
Definition: ManagedBuffer.hpp:232
Definition: Testing.hpp:134
void incr(void)
Definition: ManagedBuffer.hpp:145
void reset(void)
Definition: ManagedBuffer.hpp:170
bool operator==(const ManagedBuffer &lhs, const ManagedBuffer &rhs)
Definition: ManagedBuffer.hpp:261
SharedBuffer buffer
Definition: ManagedBuffer.hpp:141
~ManagedBuffer(void)
ManagedBuffer destructor.
Definition: ManagedBuffer.hpp:188
Definition: ManagedBuffer.hpp:31
std::atomic< int > counter
Definition: ManagedBuffer.hpp:139
size_t getSlabIndex(void) const
Definition: ManagedBuffer.hpp:182
size_t useCount(void) const
Definition: ManagedBuffer.hpp:237
Definition: SharedBuffer.hpp:21
void decr(void)
Definition: ManagedBuffer.hpp:150
std::weak_ptr< BufferManager > weakManager
Definition: ManagedBuffer.hpp:140
Definition: BufferChunk.hpp:30
ManagedBuffer & operator=(const ManagedBuffer &obj)
ManagedBuffer copy assignment.
Definition: ManagedBuffer.hpp:205
Definition: ManagedBuffer.hpp:135
bool operator<(const ManagedBuffer &) const
sortable operator for ManagedBuffer
Definition: ManagedBuffer.hpp:227
size_t slabIndex
Definition: ManagedBuffer.hpp:142
Pothos::ManagedBuffer::Impl * nextBuffer
Definition: ManagedBuffer.hpp:143