Hedgehog  3.1.0
A library to generate hybrid pipeline workflow systems
Loading...
Searching...
No Matches
static_memory_manager.h
Go to the documentation of this file.
1
2
3// NIST-developed software is provided by NIST as a public service. You may use, copy and distribute copies of the
4// software in any medium, provided that you keep intact this entire notice. You may improve, modify and create
5// derivative works of the software or any portion of the software, and you may copy and distribute such modifications
6// or works. Modified works should carry a notice stating that you changed the software and should note the date and
7// nature of any such change. Please explicitly acknowledge the National Institute of Standards and Technology as the
8// source of the software. NIST-developed software is expressly provided "AS IS." NIST MAKES NO WARRANTY OF ANY KIND,
9// EXPRESS, IMPLIED, IN FACT OR ARISING BY OPERATION OF LAW, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
10// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT AND DATA ACCURACY. NIST NEITHER REPRESENTS NOR
11// WARRANTS THAT THE OPERATION OF THE SOFTWARE WILL BE UNINTERRUPTED OR ERROR-FREE, OR THAT ANY DEFECTS WILL BE
12// CORRECTED. NIST DOES NOT WARRANT OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF THE SOFTWARE OR THE RESULTS
13// THEREOF, INCLUDING BUT NOT LIMITED TO THE CORRECTNESS, ACCURACY, RELIABILITY, OR USEFULNESS OF THE SOFTWARE. You
14// are solely responsible for determining the appropriateness of using and distributing the software and you assume
15// all risks associated with its use, including but not limited to the risks and costs of program errors, compliance
16// with applicable laws, damage to or loss of data, programs or equipment, and the unavailability or interruption of
17// operation. This software is not intended to be used in any situation where a failure could cause risk of injury or
18// damage to property. The software developed by NIST employees is not subject to copyright protection within the
19// United States.
20
21#ifndef HEDGEHOG_STATIC_MEMORY_MANAGER_H_
22#define HEDGEHOG_STATIC_MEMORY_MANAGER_H_
23
24#pragma once
25
26#include "../managed_memory.h"
28
30namespace hh {
31
37template<class T, class ...Args>
39 static_assert(std::is_base_of_v<ManagedMemory, T>, "The type managed by the StaticMemoryManager should derive from hh::ManagedMemory.");
40 static_assert(std::is_constructible_v<T, Args...>, "The type managed by the StaticMemoryManager should be constructible with Args type(s).");
41 private:
42 std::tuple<Args...> args_ = {};
43 public:
49 explicit StaticMemoryManager(size_t const &capacity, Args ... args)
50 : AbstractMemoryManager(capacity), args_(std::forward<Args>(args)...) {}
51
56
58 ~StaticMemoryManager() override = default;
59
64 std::shared_ptr<ManagedMemory> getManagedMemory() final {
65 std::shared_ptr<ManagedMemory> managedMemory = nullptr;
66 managedMemory = this->pool()->pop_front();
67 managedMemory->preProcess();
68 return managedMemory;
69 }
70
74 std::shared_ptr<AbstractMemoryManager> copy() override {
75 return std::make_shared<StaticMemoryManager<T, Args...>>(*this);
76 }
77
79 virtual void initializeMemoryManager() {}
80
81 private:
88 void recycleMemory(std::shared_ptr<ManagedMemory> const &managedMemory) final {
90 managedMemory->postProcess();
91 if (managedMemory->canBeRecycled()) {
92 managedMemory->clean();
93 this->pool()->push_back(managedMemory);
94 }
95 memoryManagerMutex_.unlock();
96 }
97
100 void initialize() final {
101 memoryManagerMutex_.lock();
102 if (!this->isInitialized()) {
103 this->initialized();
104 initialize(std::make_index_sequence<sizeof...(Args)>());
106 }
107 memoryManagerMutex_.unlock();
108 }
109
112 template<size_t... Is>
113 void initialize(std::index_sequence<Is...>) {
114 std::for_each(
115 this->pool()->begin(), this->pool()->end(),
116 [this](std::shared_ptr<ManagedMemory> &emptyShared) {
117 emptyShared = std::make_shared<T>(std::get<Is>(args_)...);
118 emptyShared->memoryManager(this);
119 }
120 );
121 }
122
125 [[nodiscard]] std::string managedType() const final { return hh::tool::typeToStr<T>(); }
126
127};
128}
129
130#endif //HEDGEHOG_STATIC_MEMORY_MANAGER_H_
Hedgehog main namespace.
Abstract Memory manager.
std::mutex memoryManagerMutex_
Mutex for user interface.
bool isInitialized() const
Initialized flag accessor.
size_t capacity() const
Capacity accessor.
std::unique_ptr< tool::Pool< ManagedMemory > > const & pool() const
Inside pool accessor.
void initialized()
Flag the memory manager has initialized.
Static memory manager.
void initialize() final
Initialize the memory manager.
virtual void initializeMemoryManager()
User-definable initialization step for a memory manager.
void recycleMemory(std::shared_ptr< ManagedMemory > const &managedMemory) final
Recycling mechanism for managed memory.
~StaticMemoryManager() override=default
Default destructor.
std::shared_ptr< AbstractMemoryManager > copy() override
Default copy method.
std::tuple< Args... > args_
Values to pass to the constructor.
std::string managedType() const final
Getter to real managed type as string.
StaticMemoryManager(size_t const &capacity, Args ... args)
Constructor to a static memory manager.
std::shared_ptr< ManagedMemory > getManagedMemory() final
Get a managed memory from the pool.
void initialize(std::index_sequence< Is... >)
Initialize implementation using the tuple of arguments stored.
StaticMemoryManager(StaticMemoryManager< T, Args... > const &rhs)
Copy constructor.