Hedgehog  3.1.0
A library to generate hybrid pipeline workflow systems
Loading...
Searching...
No Matches
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_MEMORY_MANAGER_H_
22#define HEDGEHOG_MEMORY_MANAGER_H_
23
24#pragma once
25
26#include "../managed_memory.h"
28#include "../../../tools/concepts.h"
29
31namespace hh {
32
36template<tool::ManageableMemory T>
38 public:
42
44 ~MemoryManager() override = default;
45
49 std::shared_ptr<AbstractMemoryManager> copy() override {
50 return std::make_shared<MemoryManager>(this->capacity());
51 }
52
54 virtual void initializeMemoryManager() {}
55
60 std::shared_ptr<ManagedMemory> getManagedMemory() final {
61 std::shared_ptr<ManagedMemory> managedMemory = nullptr;
62 managedMemory = this->pool()->pop_front();
63 managedMemory->preProcess();
64 return managedMemory;
65 }
66
67 private:
74 void recycleMemory(std::shared_ptr<ManagedMemory> const &managedMemory) final {
76 managedMemory->postProcess();
77 if (managedMemory->canBeRecycled()) {
78 this->profiler()->addReleaseMarker();
79 managedMemory->clean();
80 this->pool()->push_back(managedMemory);
81 }
82 memoryManagerMutex_.unlock();
83 }
84
87 void initialize() final {
89 if (!this->isInitialized()) {
90 this->initialized();
91 std::for_each(
92 this->pool()->begin(), this->pool()->end(),
93 [this](std::shared_ptr<ManagedMemory> &emptyShared) {
94 emptyShared = std::make_shared<T>();
95 emptyShared->memoryManager(this);
96 }
97 );
99 }
100 memoryManagerMutex_.unlock();
101 }
102
105 [[nodiscard]] std::string managedType() const final { return hh::tool::typeToStr<T>(); }
106};
107}
108#endif //HEDGEHOG_MEMORY_MANAGER_H_
Hedgehog main namespace.
Abstract Memory manager.
std::mutex memoryManagerMutex_
Mutex for user interface.
std::shared_ptr< NvtxProfiler > const & profiler() const
Accessor to NVTX profiler.
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.
Base memory manager.
virtual void initializeMemoryManager()
User-definable initialization step for a memory manager.
std::shared_ptr< ManagedMemory > getManagedMemory() final
Get managed memory from the pool.
std::shared_ptr< AbstractMemoryManager > copy() override
Default copy method.
void recycleMemory(std::shared_ptr< ManagedMemory > const &managedMemory) final
Recycling mechanism for managed memory.
MemoryManager(size_t const &capacity)
Create a memory manager with a certain capacity.
void initialize() final
Initialize the memory manager.
~MemoryManager() override=default
Default destructor.
std::string managedType() const final
Getter to real managed type as string.