fw4spl
BufferManager.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
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 #include "fwMemory/BufferManager.hpp"
8 
9 #include "fwMemory/policy/NeverDump.hpp"
10 #include "fwMemory/stream/in/Buffer.hpp"
11 #include "fwMemory/stream/in/Raw.hpp"
12 
13 #include <fwCom/Signal.hxx>
14 
15 #include <fwCore/util/LazyInstantiator.hpp>
16 
17 #include <fwThread/Worker.hpp>
18 
19 #include <fwTools/System.hpp>
20 
21 #include <boost/filesystem.hpp>
22 #include <boost/filesystem/fstream.hpp>
23 
24 #include <iosfwd> // #include <strstream>
25 
26 #include <algorithm>
27 #include <functional>
28 #include <iomanip>
29 
30 namespace fwMemory
31 {
32 
33 SPTR(void) getLock( const BufferManager::sptr& manager, BufferManager::ConstBufferPtrType bufferPtr )
34 {
35  return manager->lockBuffer(bufferPtr).get();
36 }
37 
38 //-----------------------------------------------------------------------------
39 
40 BufferManager::sptr BufferManager::getDefault()
41 {
42  return ::fwCore::util::LazyInstantiator< BufferManager >::getInstance();
43 }
44 
45 //-----------------------------------------------------------------------------
46 
47 BufferManager::BufferManager() :
48  m_updatedSig(UpdatedSignalType::New()),
49  m_dumpPolicy(::fwMemory::policy::NeverDump::New()),
50  m_loadingMode(BufferManager::DIRECT),
51  m_worker( ::fwThread::Worker::New() )
52 {
53 }
54 
55 //-----------------------------------------------------------------------------
56 
57 BufferManager::~BufferManager()
58 {
59  m_worker->stop();
60  // TODO restore dumped buffers
61 }
62 
63 //-----------------------------------------------------------------------------
64 
65 std::shared_future<void> BufferManager::registerBuffer(BufferManager::BufferPtrType bufferPtr)
66 {
67  return m_worker->postTask<void>( std::bind(&BufferManager::registerBufferImpl, this, bufferPtr) );
68 }
69 
70 //------------------------------------------------------------------------------
71 
72 void BufferManager::registerBufferImpl(BufferManager::BufferPtrType bufferPtr)
73 {
74  m_bufferInfos.insert( BufferInfoMapType::value_type( bufferPtr, BufferInfo() ) );
75  m_updatedSig->asyncEmit();
76 }
77 
78 //-----------------------------------------------------------------------------
79 
80 std::shared_future<void> BufferManager::unregisterBuffer(BufferManager::BufferPtrType bufferPtr)
81 {
82  return m_worker->postTask<void>( std::bind(&BufferManager::unregisterBufferImpl, this, bufferPtr) );
83 }
84 
85 //------------------------------------------------------------------------------
86 
87 void BufferManager::unregisterBufferImpl(BufferManager::BufferPtrType bufferPtr)
88 {
89  auto& bufferInfo = m_bufferInfos[bufferPtr];
90  OSLM_ASSERT("There is still " << bufferInfo.lockCount() << " locks on this BufferObject (" << this << ")",
91  bufferInfo.lockCounter.expired());
92 
93  m_bufferInfos.erase(bufferPtr);
94  m_updatedSig->asyncEmit();
95 }
96 
97 //-----------------------------------------------------------------------------
98 
99 std::shared_future<void> BufferManager::allocateBuffer(BufferManager::BufferPtrType bufferPtr, SizeType size,
100  const ::fwMemory::BufferAllocationPolicy::sptr& policy)
101 {
102  return m_worker->postTask<void>(std::bind(&BufferManager::allocateBufferImpl, this, bufferPtr, size, policy));
103 }
104 
105 //------------------------------------------------------------------------------
106 
107 void BufferManager::allocateBufferImpl(BufferManager::BufferPtrType bufferPtr, SizeType size,
108  const ::fwMemory::BufferAllocationPolicy::sptr& policy)
109 {
110  BufferInfo& info = m_bufferInfos[bufferPtr];
111  SLM_ASSERT("Buffer has already been allocated", info.loaded && (*bufferPtr == NULL));
112 
113  if(!info.loaded)
114  {
115  info.clear();
116  }
117 
118  m_dumpPolicy->allocationRequest( info, bufferPtr, size );
119 
120  try
121  {
122  policy->allocate(*bufferPtr, size);
123  }
124  catch(...)
125  {
126  info.clear();
127  throw;
128  }
129 
130  info.istreamFactory =
131  std::make_shared< ::fwMemory::stream::in::Buffer >(*bufferPtr, size,
132  std::bind(&getLock, this->getSptr(), bufferPtr));
133 
134  info.lastAccess.modified();
135  info.size = size;
136  info.bufferPolicy = policy;
137  m_updatedSig->asyncEmit();
138 }
139 
140 //-----------------------------------------------------------------------------
141 
142 std::shared_future<void> BufferManager::setBuffer(BufferManager::BufferPtrType bufferPtr,
143  ::fwMemory::BufferManager::BufferType buffer,
144  SizeType size,
145  const ::fwMemory::BufferAllocationPolicy::sptr& policy)
146 {
147  return m_worker->postTask<void>(
148  std::bind(&BufferManager::setBufferImpl, this, bufferPtr, buffer, size, policy));
149 }
150 
151 //------------------------------------------------------------------------------
152 
153 void BufferManager::setBufferImpl(BufferManager::BufferPtrType bufferPtr, ::fwMemory::BufferManager::BufferType buffer,
154  SizeType size, const ::fwMemory::BufferAllocationPolicy::sptr& policy)
155 {
156  BufferInfo& info = m_bufferInfos[bufferPtr];
157 
158  SLM_ASSERT("Buffer is already set", *bufferPtr == NULL && info.loaded);
159 
160  m_dumpPolicy->setRequest( info, bufferPtr, size );
161 
162  if(!info.loaded)
163  {
164  info.clear();
165  }
166 
167  *bufferPtr = buffer;
168 
169  info.lastAccess.modified();
170  info.size = size;
171  info.bufferPolicy = policy;
172  info.fileFormat = ::fwMemory::OTHER;
173  info.fsFile.clear();
174  info.istreamFactory =
175  std::make_shared< ::fwMemory::stream::in::Buffer >(*bufferPtr, size,
176  std::bind(&getLock, this->getSptr(), bufferPtr));
177  info.userStreamFactory = false;
178  m_updatedSig->asyncEmit();
179 }
180 
181 //-----------------------------------------------------------------------------
182 
183 std::shared_future<void> BufferManager::reallocateBuffer(BufferManager::BufferPtrType bufferPtr, SizeType newSize)
184 {
185  return m_worker->postTask<void>( std::bind(&BufferManager::reallocateBufferImpl, this, bufferPtr, newSize) );
186 }
187 
188 //------------------------------------------------------------------------------
189 
190 void BufferManager::reallocateBufferImpl(BufferManager::BufferPtrType bufferPtr, SizeType newSize)
191 {
192  BufferInfo& info = m_bufferInfos[bufferPtr];
193  SLM_ASSERT("Buffer must be allocated or dumped", (*bufferPtr != NULL) || !info.loaded);
194 
195  m_dumpPolicy->reallocateRequest( info, bufferPtr, newSize );
196 
197  try
198  {
199  if(info.loaded)
200  {
201  info.bufferPolicy->reallocate(*bufferPtr, newSize);
202  }
203  else
204  {
205  this->restoreBuffer( info, bufferPtr, newSize );
206  }
207  }
208  catch( ::fwMemory::exception::Memory& )
209  {
210  m_updatedSig->asyncEmit();
211  throw;
212  }
213 
214  info.istreamFactory =
215  std::make_shared< ::fwMemory::stream::in::Buffer>(*bufferPtr, newSize,
216  std::bind(&getLock, this->getSptr(), bufferPtr));
217 
218  info.lastAccess.modified();
219  info.size = newSize;
220 
221  m_updatedSig->asyncEmit();
222 }
223 
224 //-----------------------------------------------------------------------------
225 
226 std::shared_future<void> BufferManager::destroyBuffer(BufferManager::BufferPtrType bufferPtr)
227 {
228  return m_worker->postTask<void>( std::bind(&BufferManager::destroyBufferImpl, this, bufferPtr) );
229 }
230 
231 //------------------------------------------------------------------------------
232 
233 void BufferManager::destroyBufferImpl(BufferManager::BufferPtrType bufferPtr)
234 {
235  BufferInfo& info = m_bufferInfos[bufferPtr];
236  SLM_ASSERT("Buffer must be allocated or dumped", (*bufferPtr != NULL) || !info.loaded);
237 
238  m_dumpPolicy->destroyRequest( info, bufferPtr );
239 
240  if(info.loaded)
241  {
242  info.bufferPolicy->destroy(*bufferPtr);
243  }
244 
245  info.clear();
246  info.lastAccess.modified();
247  m_updatedSig->asyncEmit();
248 }
249 
250 //-----------------------------------------------------------------------------
251 
252 std::shared_future<void> BufferManager::swapBuffer(BufferManager::BufferPtrType bufA,
253  BufferManager::BufferPtrType bufB)
254 {
255  return m_worker->postTask<void>( std::bind(&BufferManager::swapBufferImpl, this, bufA, bufB) );
256 }
257 
258 //------------------------------------------------------------------------------
259 
260 void BufferManager::swapBufferImpl(BufferManager::BufferPtrType bufA, BufferManager::BufferPtrType bufB)
261 {
262  BufferInfo& infoA = m_bufferInfos[bufA];
263  BufferInfo& infoB = m_bufferInfos[bufB];
264 
265  std::swap(*bufA, *bufB);
266  std::swap(infoA.size, infoB.size);
267  std::swap(infoA.loaded, infoB.loaded);
268  std::swap(infoA.fsFile, infoB.fsFile);
269  std::swap(infoA.bufferPolicy, infoB.bufferPolicy);
270  std::swap(infoA.istreamFactory, infoB.istreamFactory);
271  std::swap(infoA.userStreamFactory, infoB.userStreamFactory);
272  infoA.lastAccess.modified();
273  infoB.lastAccess.modified();
274 }
275 
276 //-----------------------------------------------------------------------------
277 
279 {
280  AutoUnlock(const BufferManager::sptr& manager, BufferManager::ConstBufferPtrType bufferPtr,
281  const BufferInfo& info) :
282  m_manager(manager),
283  m_bufferPtr(bufferPtr)
284  {
285  if ( !info.loaded )
286  {
287  bool restored = manager->restoreBuffer( bufferPtr ).get();
288  OSLM_ASSERT( "restore not OK ( "<< restored << " && " << *bufferPtr <<" != 0 ).",
289  restored && *bufferPtr != 0 );
290  FwCoreNotUsedMacro(restored);
291  }
292  }
293 
294  ~AutoUnlock()
295  {
296  try
297  {
298  m_manager->unlockBuffer(m_bufferPtr);
299  }
300  catch(std::exception& e)
301  {
302  OSLM_ASSERT( "Unlock Failed" << e.what(), 0 );
303  }
304  catch(...)
305  {
306  OSLM_ASSERT( "Unlock Failed", 0 );
307  }
308  }
309 
310  BufferManager::sptr m_manager;
311  BufferManager::ConstBufferPtrType m_bufferPtr;
312 };
313 
314 //------------------------------------------------------------------------------
315 
316 std::shared_future<SPTR(void)> BufferManager::lockBuffer(BufferManager::ConstBufferPtrType bufferPtr)
317 {
318  return m_worker->postTask<SPTR(void)>( std::bind(&BufferManager::lockBufferImpl, this, bufferPtr) );
319 }
320 
321 SPTR(void) BufferManager::lockBufferImpl(BufferManager::ConstBufferPtrType bufferPtr)
322 {
323  BufferManager::BufferPtrType castedBuffer = const_cast< BufferManager::BufferPtrType >(bufferPtr);
324  BufferInfo& info = m_bufferInfos[castedBuffer];
325 
326  m_dumpPolicy->lockRequest( info, castedBuffer );
327 
328  SPTR(void) counter = info.lockCounter.lock();
329  if ( !counter )
330  {
331  counter = std::make_shared< AutoUnlock >( this->getSptr(), bufferPtr, info);
332  info.lockCounter = counter;
333  }
334 
335  m_lastAccess.modified();
336 
337  m_updatedSig->asyncEmit();
338 
339  return counter;
340 }
341 
342 //-----------------------------------------------------------------------------
343 
344 std::shared_future<bool> BufferManager::unlockBuffer(BufferManager::ConstBufferPtrType bufferPtr)
345 {
346  return m_worker->postTask<bool>( std::bind(&BufferManager::unlockBufferImpl, this, bufferPtr) );
347 }
348 
349 //------------------------------------------------------------------------------
350 
351 bool BufferManager::unlockBufferImpl(BufferManager::ConstBufferPtrType bufferPtr)
352 {
353  BufferInfo& info = m_bufferInfos[bufferPtr];
354  m_dumpPolicy->unlockRequest( info, bufferPtr );
355 
356  m_updatedSig->asyncEmit();
357  return true;
358 }
359 
360 //-----------------------------------------------------------------------------
361 
362 std::shared_future<bool> BufferManager::dumpBuffer(BufferManager::ConstBufferPtrType bufferPtr)
363 {
364  return m_worker->postTask<bool>( std::bind(&BufferManager::dumpBufferImpl, this, bufferPtr) );
365 }
366 
367 //------------------------------------------------------------------------------
368 
369 bool BufferManager::dumpBufferImpl(BufferManager::ConstBufferPtrType bufferPtr)
370 {
371  BufferManager::BufferPtrType castedBuffer = const_cast< BufferManager::BufferPtrType >(bufferPtr);
372  BufferInfo& info = m_bufferInfos[castedBuffer];
373  return this->dumpBuffer(info, castedBuffer);
374 }
375 
376 //-----------------------------------------------------------------------------
377 
378 bool BufferManager::dumpBuffer(BufferInfo& info, BufferManager::BufferPtrType bufferPtr)
379 {
380  if ( !info.loaded || info.lockCount() > 0 || info.size == 0 )
381  {
382  return false;
383  }
384 
385  ::boost::filesystem::path tmp = ::fwTools::System::getTemporaryFolder();
386  ::boost::filesystem::path dumpedFile = ::boost::filesystem::unique_path( tmp/"fwMemory-%%%%-%%%%-%%%%-%%%%.raw" );
387 
388  OSLM_TRACE("dumping " << bufferPtr << " " << dumpedFile);
389  info.lockCounter.reset();
390 
391  if ( this->writeBufferImpl(*bufferPtr, info.size, dumpedFile) )
392  {
393  info.fsFile = ::fwMemory::FileHolder(dumpedFile, true);
394  info.fileFormat = ::fwMemory::RAW;
395  info.istreamFactory = std::make_shared< ::fwMemory::stream::in::Raw >(info.fsFile);
396  info.userStreamFactory = false;
397  info.bufferPolicy->destroy(*bufferPtr);
398  *bufferPtr = NULL;
399  info.loaded = false;
400 
401  m_dumpPolicy->dumpSuccess( info, bufferPtr );
402 
403  m_updatedSig->asyncEmit();
404  }
405 
406  return !info.loaded;
407 }
408 
409 //-----------------------------------------------------------------------------
410 
411 std::shared_future<bool> BufferManager::restoreBuffer(BufferManager::ConstBufferPtrType bufferPtr)
412 {
413  return m_worker->postTask<bool>( std::bind(&BufferManager::restoreBufferImpl, this, bufferPtr) );
414 }
415 
416 //------------------------------------------------------------------------------
417 
418 bool BufferManager::restoreBufferImpl(BufferManager::ConstBufferPtrType bufferPtr)
419 {
420  BufferManager::BufferPtrType castedBuffer = const_cast< BufferManager::BufferPtrType >(bufferPtr);
421  BufferInfo& info = m_bufferInfos[castedBuffer];
422  return this->restoreBuffer(info, castedBuffer);
423 }
424 
425 //-----------------------------------------------------------------------------
426 
428  BufferManager::BufferPtrType bufferPtr, BufferManager::SizeType allocSize)
429 {
430 
431  allocSize = ((allocSize) ? allocSize : info.size);
432  if ( !info.loaded )
433  {
434  OSLM_TRACE("Restoring " << bufferPtr);
435 
436  info.bufferPolicy->allocate(*bufferPtr, allocSize);
437 
438  char* charBuf = static_cast< char* >(*bufferPtr);
439  SizeType size = std::min(allocSize, info.size);
440  bool notFailed = false;
441  {
442  SPTR(std::istream) isptr = (*info.istreamFactory)();
443  std::istream& is = *isptr;
444  SizeType read = is.read(charBuf, size).gcount();
445 
446  FW_RAISE_IF(" Bad file size, expected: " << size << ", was: " << read, size - read != 0);
447  notFailed = !is.fail();
448  }
449 
450  if ( notFailed )
451  {
452  info.loaded = true;
453  info.fsFile.clear();
454  info.lastAccess.modified();
455 
456  m_dumpPolicy->restoreSuccess( info, bufferPtr );
457 
458  info.fileFormat = ::fwMemory::OTHER;
459  info.istreamFactory
460  = std::make_shared< ::fwMemory::stream::in::Buffer >(*bufferPtr,
461  allocSize,
462  std::bind(&getLock, this->getSptr(),
463  bufferPtr));
464  info.userStreamFactory = false;
465  m_updatedSig->asyncEmit();
466  return true;
467  }
468 
469  }
470  return false;
471 }
472 
473 //-----------------------------------------------------------------------------
474 
475 std::shared_future<bool> BufferManager::writeBuffer(BufferManager::ConstBufferType buffer,
476  SizeType size, ::boost::filesystem::path& path)
477 {
478  return m_worker->postTask<bool>( std::bind(&BufferManager::writeBufferImpl, this, buffer, size, path) );
479 }
480 
481 //------------------------------------------------------------------------------
482 
483 bool BufferManager::writeBufferImpl(BufferManager::ConstBufferType buffer,
484  SizeType size, ::boost::filesystem::path& path)
485 {
486  ::boost::filesystem::ofstream fs(path, std::ios::binary|std::ios::trunc);
487  FW_RAISE_IF("Memory management : Unable to open " << path, !fs.good());
488  const char* charBuf = static_cast< const char* >(buffer);
489  OSLM_TRACE("writing " << path);
490  fs.write(charBuf, size);
491  fs.close();
492  return !fs.bad();
493 }
494 
495 //-----------------------------------------------------------------------------
496 
497 std::shared_future<bool> BufferManager::readBuffer(BufferManager::BufferType buffer, SizeType size,
498  ::boost::filesystem::path& path)
499 {
500  return m_worker->postTask<bool>( std::bind(&BufferManager::readBufferImpl, this, buffer, size, path) );
501 }
502 
503 //------------------------------------------------------------------------------
504 
505 bool BufferManager::readBufferImpl(BufferManager::BufferType buffer, SizeType size, ::boost::filesystem::path& path)
506 {
507  ::boost::filesystem::ifstream fs(path, std::ios::in|std::ios::binary|std::ios::ate);
508  FW_RAISE_IF("Unable to read " << path, !fs.good());
509 
510  std::streampos fileSize = fs.tellg();
511  fs.seekg(0, std::ios::beg);
512 
513  FW_RAISE_IF(path << ": Bad file size, expected: " << size << ", was: " << fileSize,
514  size - fileSize != 0);
515 
516  OSLM_TRACE("reading " << path);
517  char* charBuf = static_cast< char* >(buffer);
518  fs.read(charBuf, size);
519 
520  fs.close();
521  return !fs.bad();
522 }
523 
524 //-----------------------------------------------------------------------------
525 
526 std::shared_future<std::string> BufferManager::toString() const
527 {
528  return m_worker->postTask<std::string>( std::bind(&BufferManager::toStringImpl, this ) );
529 }
530 
531 //------------------------------------------------------------------------------
532 
533 std::string BufferManager::toStringImpl() const
534 {
535  std::stringstream sstr("");
536  sstr << "nb Elem = " << m_bufferInfos.size() << std::endl;
537  sstr << std::setw(18) << "Buffer" << "->" << std::setw(18) << "Address" << " "
538  << std::setw(10) << "Size" << " "
539  << std::setw(18) << "Policy" << " "
540  << std::setw(6) << "Access" << " "
541  << std::setw(4) << "Lock" << " "
542  << "DumpStatus" << " "
543  << "File" << " "
544  << std::endl;
545  for( BufferInfoMapType::value_type item : m_bufferInfos )
546  {
547  BufferInfo& info = item.second;
548  sstr << std::setw(18) << item.first << "->" << std::setw(18) << *(item.first) << " "
549  << std::setw(10) << info.size << " "
550  << std::setw(18) << info.bufferPolicy << " "
551  << std::setw(6) << info.lastAccess << " "
552  << std::setw(4) << info.lockCount() << " "
553  << ((info.loaded) ? " " : "not") << " loaded "
554  << ::boost::filesystem::path(info.fsFile) << " "
555  << std::endl;
556  }
557  return sstr.str();
558 }
559 
560 //-----------------------------------------------------------------------------
561 
562 void BufferManager::setDumpPolicy( const ::fwMemory::IPolicy::sptr& policy )
563 {
564  m_dumpPolicy = policy;
565  policy->refresh();
566 }
567 
568 //-----------------------------------------------------------------------------
569 
570 ::fwMemory::IPolicy::sptr BufferManager::getDumpPolicy() const
571 {
572  return m_dumpPolicy;
573 }
574 
575 //-----------------------------------------------------------------------------
576 
577 std::shared_future<BufferManager::BufferInfoMapType> BufferManager::getBufferInfos() const
578 {
579  return m_worker->postTask<BufferInfoMapType>( std::bind(&BufferManager::getBufferInfosImpl, this) );
580 }
581 
582 //------------------------------------------------------------------------------
583 
584 BufferManager::BufferInfoMapType BufferManager::getBufferInfosImpl() const
585 {
586  return m_bufferInfos;
587 }
588 
589 //-----------------------------------------------------------------------------
590 
591 std::shared_future<BufferManager::BufferStats> BufferManager::getBufferStats() const
592 {
593  return m_worker->postTask<BufferManager::BufferStats>(
594  std::bind(&BufferManager::computeBufferStats, m_bufferInfos) );
595 }
596 
597 //------------------------------------------------------------------------------
598 
599 BufferManager::BufferStats BufferManager::computeBufferStats(const BufferInfoMapType& bufferInfo)
600 {
601  BufferStats stats = {0, 0};
602  for( const BufferInfoMapType::value_type& item : bufferInfo )
603  {
604  const BufferInfo& info = item.second;
605  if ( !info.loaded )
606  {
607  stats.totalDumped += info.size;
608  }
609  stats.totalManaged += info.size;
610  }
611  return stats;
612 }
613 
614 //-----------------------------------------------------------------------------
615 
616 std::shared_future<void> BufferManager::setIStreamFactory(BufferPtrType bufferPtr,
618  SizeType size,
619  ::fwMemory::FileHolder fsFile,
620  ::fwMemory::FileFormatType format,
621  const ::fwMemory::BufferAllocationPolicy::sptr& policy
622  )
623 {
624  return m_worker->postTask<void>(
625  std::bind(&BufferManager::setIStreamFactoryImpl, this, bufferPtr, factory, size, fsFile, format, policy) );
626 }
627 
628 //------------------------------------------------------------------------------
629 
630 void BufferManager::setIStreamFactoryImpl(BufferPtrType bufferPtr,
631  const SPTR(::fwMemory::stream::in::IFactory)& factory,
632  SizeType size,
633  ::fwMemory::FileHolder fsFile,
634  ::fwMemory::FileFormatType format,
635  const ::fwMemory::BufferAllocationPolicy::sptr& policy
636  )
637 {
638  BufferInfoMapType::iterator iterInfo = m_bufferInfos.find(bufferPtr);
639  FW_RAISE_IF("Buffer is not managed by fwMemory::BufferManager.", iterInfo == m_bufferInfos.end() );
640  BufferInfo& info = iterInfo->second;
641 
642  SLM_ASSERT("Buffer is already set", *bufferPtr == NULL && info.loaded);
643 
644  m_dumpPolicy->setRequest( info, bufferPtr, size );
645 
646  info.istreamFactory = factory;
647  info.userStreamFactory = true;
648  info.size = size;
649  info.fsFile = fsFile;
650  info.fileFormat = format;
651  info.bufferPolicy = policy;
652  info.loaded = false;
653 
654  m_dumpPolicy->dumpSuccess( info, bufferPtr );
655 
656  switch(m_loadingMode)
657  {
658  case BufferManager::DIRECT:
659  this->restoreBuffer(bufferPtr);
660  break;
661  case BufferManager::LAZY:
662  m_updatedSig->asyncEmit();
663  break;
664  default:
665  SLM_ASSERT("You shall not pass", 0);
666  }
667 }
668 
669 //-----------------------------------------------------------------------------
670 
671 std::shared_future<BufferManager::StreamInfo>
672 BufferManager::getStreamInfo(const BufferManager::ConstBufferPtrType bufferPtr) const
673 {
674  return m_worker->postTask<BufferManager::StreamInfo>(
675  std::bind(&BufferManager::getStreamInfoImpl, this, bufferPtr) );
676 }
677 
678 //------------------------------------------------------------------------------
679 
680 BufferManager::StreamInfo BufferManager::getStreamInfoImpl(const BufferManager::ConstBufferPtrType bufferPtr) const
681 {
682  StreamInfo streamInfo;
683  BufferInfoMapType::const_iterator iterInfo = m_bufferInfos.find(bufferPtr);
684  FW_RAISE_IF("Buffer is not managed by fwMemory::BufferManager.", iterInfo == m_bufferInfos.end() );
685  const BufferInfo& info = iterInfo->second;
686  streamInfo.size = info.size;
687  streamInfo.fsFile = info.fsFile;
688  streamInfo.format = info.fileFormat;
689  streamInfo.userStream = info.userStreamFactory;
690 
691  if(info.istreamFactory)
692  {
693  streamInfo.stream = (*info.istreamFactory)();
694  }
695 
696  return streamInfo;
697 }
698 
699 //-----------------------------------------------------------------------------
700 
701 BufferManager::LoadingModeType BufferManager::getLoadingMode() const
702 {
703  return m_loadingMode;
704 }
705 
706 //------------------------------------------------------------------------------
707 
708 void BufferManager::setLoadingMode(LoadingModeType mode)
709 {
710  m_loadingMode = mode;
711 }
712 
713 } //namespace fwMemory
virtual void registerBufferImpl(BufferPtrType bufferPtr)
BufferManager&#39;a Implementation.
#define SPTR(_cls_)
::fwMemory::FileFormatType format
format of the dumped file
FWMEMORY_API void setDumpPolicy(const std::shared_ptr< ::fwMemory::IPolicy > &policy)
Sets the dump policy.
virtual std::string toStringImpl() const
BufferManager&#39;a Implementation.
void setIStreamFactoryImpl(BufferPtrType bufferPtr, const std::shared_ptr< ::fwMemory::stream::in::IFactory > &factory, SizeType size,::fwMemory::FileHolder fsFile,::fwMemory::FileFormatType format, const ::fwMemory::BufferAllocationPolicy::sptr &policy)
BufferManager&#39;a Implementation.
FWMEMORY_API std::shared_future< BufferStats > getBufferStats() const
Returns managed buffers statistics.
virtual void reallocateBufferImpl(BufferPtrType bufferPtr, SizeType newSize)
BufferManager&#39;a Implementation.
StreamInfo getStreamInfoImpl(const ConstBufferPtrType bufferPtr) const
BufferManager&#39;a Implementation.
virtual bool unlockBufferImpl(ConstBufferPtrType bufferPtr)
BufferManager&#39;a Implementation.
bool readBufferImpl(BufferType buffer, SizeType size,::boost::filesystem::path &path)
BufferManager&#39;a Implementation.
#define OSLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:310
The namespace fwMemory contains tools to manage memory. Use for dump.
Definition: SReader.hpp:20
BufferInfoMapType getBufferInfosImpl() const
BufferManager&#39;a Implementation.
virtual FWMEMORY_API std::shared_future< std::shared_ptr< void > > lockBuffer(ConstBufferPtrType bufferPtr)
Hook called when a BufferObject is locked.
virtual FWMEMORY_API std::shared_future< std::string > toString() const
returns BufferManager status string
static FWMEMORY_API BufferManager::sptr getDefault()
Returns the current BufferManager instance.
virtual FWMEMORY_API std::shared_future< void > unregisterBuffer(BufferPtrType bufferPtr)
Hook called when a BufferObject is destroyed.
FWMEMORY_API std::shared_future< bool > readBuffer(BufferType buffer, SizeType size,::boost::filesystem::path &path)
Write/read a buffer.
bool restoreBufferImpl(ConstBufferPtrType buffer)
BufferManager&#39;a Implementation.
#define OSLM_TRACE(message)
Definition: spyLog.hpp:230
bool loaded
true if &#39;buffer&#39; is loaded
Definition: BufferInfo.hpp:42
virtual void unregisterBufferImpl(BufferPtrType bufferPtr)
BufferManager&#39;a Implementation.
FWMEMORY_API std::shared_ptr< ::fwMemory::IPolicy > getDumpPolicy() const
Returns the dump policy.
virtual FWMEMORY_API std::shared_future< bool > unlockBuffer(ConstBufferPtrType bufferPtr)
Hook called when a BufferObject lock is released.
Contains fwAtomsFilter::factory utilities.
FWMEMORY_API std::shared_future< StreamInfo > getStreamInfo(const ConstBufferPtrType bufferPtr) const
Returns stream info.
virtual FWMEMORY_API std::shared_future< void > swapBuffer(BufferPtrType bufA, BufferPtrType bufB)
Hook called when a request to swap two BufferObject contents is made.
virtual FWMEMORY_API std::shared_future< void > destroyBuffer(BufferPtrType bufferPtr)
Hook called when a destruction is requested from a BufferObject.
virtual std::shared_ptr< void > lockBufferImpl(ConstBufferPtrType bufferPtr)
BufferManager&#39;a Implementation.
bool userStream
true if stream has been set by the user
bool writeBufferImpl(ConstBufferType buffer, SizeType size,::boost::filesystem::path &path)
BufferManager&#39;a Implementation.
virtual FWMEMORY_API std::shared_future< void > allocateBuffer(BufferPtrType bufferPtr, SizeType size, const ::fwMemory::BufferAllocationPolicy::sptr &policy)
Hook called when an allocation is requested from a BufferObject.
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
bool dumpBufferImpl(ConstBufferPtrType buffer)
BufferManager&#39;a Implementation.
static FWTOOLS_APIconst::boost::filesystem::path getTemporaryFolder(const std::string &subFolderPrefix="") noexcept
Returns a unique per-process temporary folder. The top level temporary folder will be automatically d...
Definition: System.cpp:148
BufferManager implementation.
FWMEMORY_API std::shared_future< BufferInfoMapType > getBufferInfos() const
Returns the Buffer info map.
virtual void destroyBufferImpl(BufferPtrType bufferPtr)
BufferManager&#39;a Implementation.
virtual FWMEMORY_API std::shared_future< void > setBuffer(BufferPtrType bufferPtr,::fwMemory::BufferManager::BufferType buffer, SizeType size, const ::fwMemory::BufferAllocationPolicy::sptr &policy)
Hook called when a request is made to set BufferObject&#39;s buffer from an external buffer.
Implements an exception class for fwMemory.
Definition: Memory.hpp:23
virtual void setBufferImpl(BufferPtrType bufferPtr,::fwMemory::BufferManager::BufferType buffer, SizeType size, const ::fwMemory::BufferAllocationPolicy::sptr &policy)
BufferManager&#39;a Implementation.
FWMEMORY_API std::shared_future< bool > writeBuffer(ConstBufferType buffer, SizeType size,::boost::filesystem::path &path)
Write/read a buffer.
virtual FWMEMORY_API std::shared_future< void > registerBuffer(BufferPtrType bufferPtr)
Hook called when a new BufferObject is created.
FWMEMORY_API std::shared_future< bool > restoreBuffer(ConstBufferPtrType bufferPtr)
Dump/restore a buffer.
FWCORE_API void modified()
Increment global Logical counter and copy it to this LogicStamp.
Definition: LogicStamp.cpp:15
virtual void allocateBufferImpl(BufferPtrType bufferPtr, SizeType size, const ::fwMemory::BufferAllocationPolicy::sptr &policy)
BufferManager&#39;a Implementation.
virtual void swapBufferImpl(BufferPtrType bufA, BufferPtrType bufB)
BufferManager&#39;a Implementation.
FWMEMORY_API std::shared_future< bool > dumpBuffer(ConstBufferPtrType bufferPtr)
Dump/restore a buffer.
::fwMemory::FileHolder fsFile
path of the file containing the dumped buffer
virtual FWMEMORY_API std::shared_future< void > reallocateBuffer(BufferPtrType bufferPtr, SizeType newSize)
Hook called when a reallocation is requested from a BufferObject.