Hedgehog  0.0.0
A library to generate hybrid pipeline workflow systems
static_memory_manager.h
1 // NIST-developed software is provided by NIST as a public service. You may use, copy and distribute copies of the
2 // software in any medium, provided that you keep intact this entire notice. You may improve, modify and create
3 // derivative works of the software or any portion of the software, and you may copy and distribute such modifications
4 // or works. Modified works should carry a notice stating that you changed the software and should note the date and
5 // nature of any such change. Please explicitly acknowledge the National Institute of Standards and Technology as the
6 // source of the software. NIST-developed software is expressly provided "AS IS." NIST MAKES NO WARRANTY OF ANY KIND,
7 // EXPRESS, IMPLIED, IN FACT OR ARISING BY OPERATION OF LAW, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
8 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT AND DATA ACCURACY. NIST NEITHER REPRESENTS NOR
9 // WARRANTS THAT THE OPERATION OF THE SOFTWARE WILL BE UNINTERRUPTED OR ERROR-FREE, OR THAT ANY DEFECTS WILL BE
10 // CORRECTED. NIST DOES NOT WARRANT OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF THE SOFTWARE OR THE RESULTS
11 // THEREOF, INCLUDING BUT NOT LIMITED TO THE CORRECTNESS, ACCURACY, RELIABILITY, OR USEFULNESS OF THE SOFTWARE. You
12 // are solely responsible for determining the appropriateness of using and distributing the software and you assume
13 // all risks associated with its use, including but not limited to the risks and costs of program errors, compliance
14 // with applicable laws, damage to or loss of data, programs or equipment, and the unavailability or interruption of
15 // operation. This software is not intended to be used in any situation where a failure could cause risk of injury or
16 // damage to property. The software developed by NIST employees is not subject to copyright protection within the
17 // United States.
18 
19 
20 #ifndef HEDGEHOG_STATIC_MEMORY_MANAGER_H
21 #define HEDGEHOG_STATIC_MEMORY_MANAGER_H
22 
23 #include "abstract_memory_manager.h"
25 namespace hh {
82 template<class ManagedMemory, class... Args>
83 class StaticMemoryManager : public AbstractMemoryManager<ManagedMemory> {
84  private:
85 
93  template<class TestManagedMemory, class... TestArgs>
94  static std::true_type test(
95  decltype(new TestManagedMemory((std::declval<TestArgs>())...)));
96 
101  template<class TestManagedMemory, class ...TestArgs>
102  static std::false_type test(...);
103  public:
105  enum { value = std::is_same_v<decltype(test<ManagedMemory, Args...>(0)), std::true_type> };
106  };
107 
108  static_assert(HasConstructor::value,
109  "The Memory that you are trying to manage does not have the right constructor definition.");
110 
111  std::tuple<Args...> args_ = {};
112 
113  public:
115  StaticMemoryManager() = delete;
116 
120  explicit StaticMemoryManager(size_t const &capacity, Args ... args) :
121  AbstractMemoryManager<ManagedMemory>(capacity), args_(std::forward<Args>(args)...) {
122  }
123 
127  : AbstractMemoryManager<ManagedMemory>(rhs.capacity()), args_(rhs.args_) {}
128 
131  void initialize() final {
132  std::lock_guard<std::mutex> lk(this->memoryManagerMutex());
133  if (!this->isInitialized()) {
134  this->initialized();
135  initialize(std::make_index_sequence<sizeof...(Args)>());
136  this->initializeMemoryManager();
137  }
138  }
139 
142  std::shared_ptr<AbstractMemoryManager < ManagedMemory>> copy() override {
143  return std::make_shared<StaticMemoryManager<ManagedMemory, Args...>>(*this);
144  }
145 
146  private:
149  template<size_t... Is>
150  void initialize(std::index_sequence<Is...>) {
151  std::for_each(
152  this->pool()->begin(), this->pool()->end(),
153  [this](std::shared_ptr<ManagedMemory> &emptyShared) {
154  emptyShared = std::make_shared<ManagedMemory>(std::get<Is>(args_)...);
155  emptyShared->memoryManager(this);
156  }
157  );
158  }
159 };
160 }
161 
162 #endif //HEDGEHOG_STATIC_MEMORY_MANAGER_H
Derived class from AbstractMemoryManager for statically allocated MemoryData, used for example for GP...
static std::true_type test(decltype(new TestManagedMemory((std::declval< TestArgs >())...)))
True test, testing if a constructor with the right parameters exist and can be called.
std::shared_ptr< AbstractMemoryManager< ManagedMemory > > copy() override
Copy method used for task duplication and execution pipeline.
Hedgehog main namespace.
Abstract interface for Hedgehog&#39;s Memory manager.
bool isInitialized() const
Initialized flag accessor.
StaticMemoryManager()=delete
Deleted default constructor.
virtual void initializeMemoryManager()
User-definable initialization step for a memory manager.
void initialized()
Flag the memory manager has initialized.
size_t capacity() const
Capacity accessor.
void initialize(std::index_sequence< Is... >)
Private initialize method to call a specific constructor for the type.
SFINAE construct to test if ManagedMemory has a constructor with Args... as parameter.
void initialize() final
Initialize method, calling the private initialize method with the pack arguments, and the user defina...
std::tuple< Args... > args_
Values to pass to the constructor.
STL namespace.
StaticMemoryManager(StaticMemoryManager< ManagedMemory, Args... > &rhs)
Copy constructor used by the copy method.
StaticMemoryManager(size_t const &capacity, Args ... args)
Constructor to use defining the pool capacity and the arguments to give the type constructor.
std::unique_ptr< behavior::Pool< ManagedMemory > > const & pool() const
Inside pool accessor.
std::mutex & memoryManagerMutex()
User api mutex accessor.