FACT++  1.0
std::shared_ptr<char> MemoryStock::pop ( bool  block)
inlineprivate

Definition at line 42 of file MemoryManager.h.

43  {
44  if (block)
45  {
46  // No free slot available, next alloc would exceed max memory:
47  // block until a slot is available
48  std::unique_lock<std::mutex> lock(fMutexCond);
49  while (fMemoryStock.empty() && fAllocated+fChunkSize>fMaxMemory)
50  fCond.wait(lock);
51  }
52  else
53  {
54  // No free slot available, next alloc would exceed max memory
55  // return an empty pointer
57  return std::shared_ptr<char>();
58  }
59 
60  // We will return this amount of memory
61  // This is not 100% thread safe, but it is not a super accurate measure anyway
62  fInUse += fChunkSize;
63  if (fInUse>fMaxInUse)
64  fMaxInUse = fInUse;
65 
66  if (fMemoryStock.empty())
67  {
68  // No free slot available, allocate a new one
70  return std::shared_ptr<char>(new char[fChunkSize]);
71  }
72 
73  // Get the next free slot from the stack and return it
74  const std::lock_guard<std::mutex> lock(fMutexMem);
75 
76  const auto mem = fMemoryStock.front();
77  fMemoryStock.pop_front();
78  return mem;
79  };
std::mutex fMutexMem
Definition: MemoryManager.h:29
size_t fMaxInUse
Definition: MemoryManager.h:27
std::condition_variable fCond
Definition: MemoryManager.h:31
size_t fMaxMemory
Definition: MemoryManager.h:22
size_t fInUse
Definition: MemoryManager.h:24
std::forward_list< std::shared_ptr< char > > fMemoryStock
Definition: MemoryManager.h:33
size_t fAllocated
Definition: MemoryManager.h:25
size_t fChunkSize
Definition: MemoryManager.h:21
std::mutex fMutexCond
Definition: MemoryManager.h:30