3#include <AMReX_Config.H>
14#include <unordered_set>
46 [[nodiscard]]
void*
alloc (std::size_t nbytes)
final;
51 [[nodiscard]] std::pair<void*,std::size_t>
52 alloc_in_place (
void* pt, std::size_t szmin, std::size_t szmax)
final;
64 void free (
void* vp)
final;
86 std::
size_t sizeOf (
void* p) const noexcept;
88 void PrintUsage (std::
string const& name,
bool print_max_usage) const;
90 void PrintUsage (std::ostream& os, std::
string const& name, std::
string const& space) const;
109 Node (
void* a_block,
void* a_owner, std::size_t a_size,
MemStat* a_stat=
nullptr) noexcept
111 m_block(a_block), m_owner(a_owner), m_size(a_size), m_stat(a_stat) {}
114 bool operator< (
const Node& rhs)
const noexcept
116 return std::less<>{}(m_block, rhs.m_block);
122 return m_block == rhs.m_block;
126 [[nodiscard]]
void*
block () const noexcept {
return m_block; }
129 void block (
void* blk)
noexcept { m_block = blk; }
132 [[nodiscard]] std::size_t
size () const noexcept {
return m_size; }
135 void size (std::size_t sz)
noexcept { m_size = sz; }
137 [[nodiscard]]
void*
owner () const noexcept {
return m_owner; }
140 return m_owner == rhs.m_owner;
150 std::size_t operator() (
const Node& n)
const noexcept {
151 return std::hash<void*>{}(n.m_block);
167 std::vector<std::pair<void*,std::size_t> >
m_alloc;
173 using NL = std::set<Node>;
A virtual base class for objects that manage their own dynamic memory allocation.
Definition AMReX_Arena.H:105
The nodes in our free list and block list.
Definition AMReX_CArena.H:107
MemStat * m_stat
Used for profiling if this Node represents a user allocated block of memory.
Definition AMReX_CArena.H:163
std::size_t size() const noexcept
The size of the memory block.
Definition AMReX_CArena.H:132
std::size_t m_size
The size of the block we represent.
Definition AMReX_CArena.H:161
void block(void *blk) noexcept
Set block address.
Definition AMReX_CArena.H:129
void size(std::size_t sz) noexcept
Set size.
Definition AMReX_CArena.H:135
void * owner() const noexcept
Definition AMReX_CArena.H:137
void * m_owner
The starting address of the original allocation.
Definition AMReX_CArena.H:159
MemStat * mem_stat() const
Get the MemStat object of the function where this block was allocated.
Definition AMReX_CArena.H:144
void mem_stat(MemStat *a_stat) noexcept
Set MemStat.
Definition AMReX_CArena.H:147
void * m_block
The block of memory we reference.
Definition AMReX_CArena.H:157
bool coalescable(const Node &rhs) const noexcept
Definition AMReX_CArena.H:139
void * block() const noexcept
The block address.
Definition AMReX_CArena.H:126
Node(void *a_block, void *a_owner, std::size_t a_size, MemStat *a_stat=nullptr) noexcept
Definition AMReX_CArena.H:109
A Concrete Class for Dynamic Memory Management using first fit. This is a coalescing memory manager....
Definition AMReX_CArena.H:28
NL m_freelist
The free list of allocated but not currently used blocks. Maintained in lo to hi memory sorted order.
Definition AMReX_CArena.H:179
std::size_t sizeOf(void *p) const noexcept
Return the amount of memory in this pointer. Return 0 for unknown pointer.
Definition AMReX_CArena.cpp:468
CArena(const CArena &rhs)=delete
static constexpr std::size_t DefaultHunkSize
The default memory hunk size to grab from the heap.
Definition AMReX_CArena.H:97
void * shrink_in_place(void *pt, std::size_t new_size) final
Definition AMReX_CArena.cpp:209
std::mutex carena_mutex
Definition AMReX_CArena.H:198
std::size_t freeUnused_protected() final
Definition AMReX_CArena.cpp:377
std::size_t m_max_actually_used
The max amount of memory given out via alloc().
Definition AMReX_CArena.H:196
std::size_t heap_space_actually_used() const noexcept
Return the total amount of memory given out via alloc.
Definition AMReX_CArena.cpp:462
std::size_t heap_space_used() const noexcept
The current amount of heap space used by the CArena object.
Definition AMReX_CArena.cpp:456
std::pair< void *, std::size_t > alloc_in_place(void *pt, std::size_t szmin, std::size_t szmax) final
Definition AMReX_CArena.cpp:136
CArena & operator=(const CArena &rhs)=delete
friend std::ostream & operator<<(std::ostream &os, const CArena &arena)
Definition AMReX_CArena.cpp:520
void ResetMaxUsageCounter() final
Definition AMReX_CArena.H:92
std::set< Node > NL
The type of our freelist and blocklist. We use a set sorted from lo to hi memory addresses.
Definition AMReX_CArena.H:173
std::size_t m_hunk
The minimal size of hunks to request from system.
Definition AMReX_CArena.H:188
std::size_t freeUnused() final
Free unused memory back to the system. Return value is the amount memory freed.
Definition AMReX_CArena.cpp:357
std::size_t m_used
The amount of heap space currently allocated.
Definition AMReX_CArena.H:190
std::unordered_set< Node, Node::hash > m_busylist
The list of busy blocks. A block is either on the freelist or on the blocklist, but not on both.
Definition AMReX_CArena.H:186
void * alloc(std::size_t nbytes) final
Allocate some memory.
Definition AMReX_CArena.cpp:29
CArena(CArena &&rhs)=delete
void free(void *vp) final
Free up allocated memory. Merge neighboring free memory chunks into largest possible chunk.
Definition AMReX_CArena.cpp:267
~CArena() override
The destructor.
Definition AMReX_CArena.cpp:21
bool hasFreeDeviceMemory(std::size_t sz) final
Does the device have enough free memory for allocating this much memory? For CPU builds,...
Definition AMReX_CArena.cpp:418
void * alloc_protected(std::size_t nbytes)
Definition AMReX_CArena.cpp:37
void PrintUsage(std::string const &name, bool print_max_usage) const
Definition AMReX_CArena.cpp:483
std::vector< std::pair< void *, std::size_t > > m_alloc
The list of blocks allocated.
Definition AMReX_CArena.H:167
std::size_t freeableMemory() const
Return the amount of memory that can be freed.
Definition AMReX_CArena.cpp:364
std::size_t m_max_used
The max amount of heap space currently allocated.
Definition AMReX_CArena.H:192
std::size_t m_actually_used
The amount of memory given out via alloc().
Definition AMReX_CArena.H:194
Definition AMReX_Amr.cpp:49
bool operator==(A1 const &a1, A2 const &a2)
Definition AMReX_GpuAllocators.H:202
Definition AMReX_Arena.H:53
Definition AMReX_CArena.H:149
Definition AMReX_Arena.H:12