fw4spl
BufferManager.hpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #ifndef __FWMEMORY_BUFFERMANAGER_HPP__
8 #define __FWMEMORY_BUFFERMANAGER_HPP__
9 
10 #include "fwMemory/BufferInfo.hpp"
11 #include "fwMemory/config.hpp"
12 #include "fwMemory/FileHolder.hpp"
13 
14 #include <fwCom/Signal.hpp>
15 
16 #include <fwCore/base.hpp>
17 #include <fwCore/BaseObject.hpp>
18 #include <fwCore/mt/types.hpp>
19 
20 #include <boost/filesystem/path.hpp>
21 
22 #include <future>
23 
24 namespace fwThread
25 {
26 class Worker;
27 }
28 
29 namespace fwMemory
30 {
31 
32 class IPolicy;
33 
34 namespace stream
35 {
36 namespace in
37 {
38 class IFactory;
39 }
40 }
41 
42 class BufferManager;
43 
44 class Key
45 {
46 friend SPTR(BufferManager) getDefault();
47 
48 Key()
49 {
50 }
51 };
52 
62 class FWMEMORY_CLASS_API BufferManager : public ::fwCore::BaseObject
63 {
64 public:
65 
66  typedef void* BufferType;
67  typedef const void* ConstBufferType;
68  typedef BufferType* BufferPtrType;
69  typedef void const* const* ConstBufferPtrType;
70 
71  typedef BufferInfo::SizeType SizeType;
72 
73  typedef ::fwCom::Signal< void () > UpdatedSignalType;
74 
75  typedef std::map< ConstBufferPtrType, BufferInfo > BufferInfoMapType;
76 
79 
80  BufferManager();
81  virtual ~BufferManager();
82 
83  typedef enum
84  {
85  DIRECT,
86  LAZY
87  } LoadingModeType;
88 
89  struct BufferStats
90  {
91  SizeType totalDumped;
92  SizeType totalManaged;
93  };
94 
95  struct StreamInfo
96  {
97  SizeType size;
98  SPTR(std::istream) stream;
102  ::fwMemory::FileFormatType format;
105  };
106 
112  FWMEMORY_API virtual std::shared_future<void> registerBuffer(BufferPtrType bufferPtr);
113 
119  FWMEMORY_API virtual std::shared_future<void> unregisterBuffer(BufferPtrType bufferPtr);
120 
128  FWMEMORY_API virtual std::shared_future<void> allocateBuffer(BufferPtrType bufferPtr, SizeType size,
129  const ::fwMemory::BufferAllocationPolicy::sptr& policy);
130 
139  FWMEMORY_API virtual std::shared_future<void> setBuffer(BufferPtrType bufferPtr,
140  ::fwMemory::BufferManager::BufferType buffer,
141  SizeType size,
142  const ::fwMemory::BufferAllocationPolicy::sptr& policy);
143 
150  FWMEMORY_API virtual std::shared_future<void> reallocateBuffer(BufferPtrType bufferPtr, SizeType newSize);
151 
157  FWMEMORY_API virtual std::shared_future<void> destroyBuffer(BufferPtrType bufferPtr);
158 
165  FWMEMORY_API virtual std::shared_future<void> swapBuffer(BufferPtrType bufA, BufferPtrType bufB);
166 
174  FWMEMORY_API virtual std::shared_future<SPTR(void)> lockBuffer(ConstBufferPtrType bufferPtr);
175 
183  FWMEMORY_API virtual std::shared_future<bool> unlockBuffer(ConstBufferPtrType bufferPtr);
184 
188  FWMEMORY_API virtual std::shared_future<std::string> toString() const;
189 
199  FWMEMORY_API std::shared_future<bool> dumpBuffer(ConstBufferPtrType bufferPtr);
200  FWMEMORY_API std::shared_future<bool> restoreBuffer(ConstBufferPtrType bufferPtr);
214  FWMEMORY_API std::shared_future<bool> writeBuffer(ConstBufferType buffer, SizeType size,
215  ::boost::filesystem::path& path);
216  FWMEMORY_API std::shared_future<bool> readBuffer(BufferType buffer, SizeType size,
217  ::boost::filesystem::path& path);
225  SPTR(UpdatedSignalType) getUpdatedSignal(){
226  return m_updatedSig;
227  };
228 
234  FWMEMORY_API std::shared_future<BufferInfoMapType> getBufferInfos() const;
235 
239  FWMEMORY_API std::shared_future<BufferStats> getBufferStats() const;
240  FWMEMORY_API static BufferStats computeBufferStats(const BufferInfoMapType& bufferInfo);
241 
245  FWMEMORY_API void setDumpPolicy( const SPTR(::fwMemory::IPolicy)& policy );
246 
250  FWMEMORY_API SPTR(::fwMemory::IPolicy) getDumpPolicy() const;
251 
255  FWMEMORY_API std::shared_future<StreamInfo> getStreamInfo(const ConstBufferPtrType bufferPtr) const;
256 
257  FWMEMORY_API std::shared_future<void> setIStreamFactory(BufferPtrType bufferPtr,
259  SizeType size,
260  ::fwMemory::FileHolder fsFile,
261  ::fwMemory::FileFormatType format,
262  const ::fwMemory::BufferAllocationPolicy::sptr& policy
263  );
264 
265  FWMEMORY_API LoadingModeType getLoadingMode() const;
266  FWMEMORY_API void setLoadingMode(LoadingModeType mode);
267 
272  FWMEMORY_API static BufferManager::sptr getDefault();
273 
274  ::fwCore::mt::ReadWriteMutex& getMutex() const
275  {
276  return m_mutex;
277  }
278 protected:
279 
283  virtual void registerBufferImpl(BufferPtrType bufferPtr);
284  virtual void unregisterBufferImpl(BufferPtrType bufferPtr);
285  virtual void allocateBufferImpl(BufferPtrType bufferPtr, SizeType size,
286  const ::fwMemory::BufferAllocationPolicy::sptr& policy);
287  virtual void setBufferImpl(BufferPtrType bufferPtr, ::fwMemory::BufferManager::BufferType buffer, SizeType size,
288  const ::fwMemory::BufferAllocationPolicy::sptr& policy);
289  virtual void reallocateBufferImpl(BufferPtrType bufferPtr, SizeType newSize);
290  virtual void destroyBufferImpl(BufferPtrType bufferPtr);
291  virtual void swapBufferImpl(BufferPtrType bufA, BufferPtrType bufB);
292  virtual SPTR(void) lockBufferImpl(ConstBufferPtrType bufferPtr);
293  virtual bool unlockBufferImpl(ConstBufferPtrType bufferPtr);
294  virtual std::string toStringImpl() const;
295  bool dumpBufferImpl(ConstBufferPtrType buffer);
296  bool restoreBufferImpl(ConstBufferPtrType buffer);
297  bool writeBufferImpl(ConstBufferType buffer, SizeType size, ::boost::filesystem::path& path);
298  bool readBufferImpl(BufferType buffer, SizeType size, ::boost::filesystem::path& path);
299  BufferInfoMapType getBufferInfosImpl() const;
300  StreamInfo getStreamInfoImpl(const ConstBufferPtrType bufferPtr) const;
301  void setIStreamFactoryImpl(BufferPtrType bufferPtr,
302  const SPTR(::fwMemory::stream::in::IFactory)& factory,
303  SizeType size,
304  ::fwMemory::FileHolder fsFile,
305  ::fwMemory::FileFormatType format,
306  const ::fwMemory::BufferAllocationPolicy::sptr& policy
307  );
315  FWMEMORY_API bool dumpBuffer(BufferInfo& info, BufferPtrType bufferPtr);
316  FWMEMORY_API bool restoreBuffer(BufferInfo& info, BufferPtrType bufferPtr, SizeType size = 0);
319  SPTR(UpdatedSignalType) m_updatedSig;
320 
321  ::fwCore::LogicStamp m_lastAccess;
322  BufferInfoMapType m_bufferInfos;
323 
324  SPTR(::fwMemory::IPolicy) m_dumpPolicy;
325 
326  LoadingModeType m_loadingMode;
327 
328  SPTR(fwThread::Worker) m_worker;
329 
331  mutable ::fwCore::mt::ReadWriteMutex m_mutex;
332 };
333 
334 }
335 
336 #endif /* __FWMEMORY_BUFFERMANAGER_HPP__ */
#define SPTR(_cls_)
::fwMemory::FileFormatType format
format of the dumped file
#define fwCoreNonInstanciableClassDefinitionsMacro(_classinfo_)
Generate common code for Non Instanciable classes (Interfaces, Abstract classes, ...)
Base class for all FW4SPL&#39;s classes.
Definition: BaseObject.hpp:22
::boost::shared_mutex ReadWriteMutex
Defines a single writer, multiple readers mutex.
The namespace fwMemory contains tools to manage memory. Use for dump.
Definition: SReader.hpp:20
Contains fwAtomsFilter::factory utilities.
bool userStream
true if stream has been set by the user
mutable::fwCore::mt::ReadWriteMutex m_mutex
Mutex to protect concurrent access in BufferManager.
BufferManager implementation.
#define fwCoreAllowSharedFromThis()
Generate getSptr and getConstSptr methods.
Provides a Logical timestamp system.
Definition: LogicStamp.hpp:22
This class creates and manages a task loop. The default implementation create a loop in a new thread...
Definition: Worker.hpp:32
This namespace fwThread provides few tools to execute asynchronous tasks on different threads...
Defines the memory dump policy interface.
Definition: IPolicy.hpp:23
::fwMemory::FileHolder fsFile
path of the file containing the dumped buffer