3#include <AMReX_Config.H>
20#include <unordered_set>
56 [[nodiscard]]
void*
alloc (std::size_t nbytes)
final;
79 [[nodiscard]] std::pair<void*,std::size_t>
80 alloc_in_place (
void* pt, std::size_t szmin, std::size_t szmax)
final;
104 void free (
void* vp)
final;
141 std::
size_t sizeOf (
void* p) const noexcept;
158 void PrintUsage (std::
string const& name,
bool print_max_usage) const;
172 void PrintUsage (std::ostream& os, std::
string const& name, std::
string const& space) const;
192 Node (
void* a_block,
void* a_owner, std::size_t a_size,
MemStat* a_stat=
nullptr) noexcept
194 m_block(a_block), m_owner(a_owner), m_size(a_size), m_stat(a_stat) {}
197 bool operator< (
const Node& rhs)
const noexcept
199 return std::less<>{}(m_block, rhs.m_block);
205 return m_block == rhs.m_block;
209 [[nodiscard]]
void*
block () const noexcept {
return m_block; }
212 void block (
void* blk)
noexcept { m_block = blk; }
215 [[nodiscard]] std::size_t
size () const noexcept {
return m_size; }
218 void size (std::size_t sz)
noexcept { m_size = sz; }
220 [[nodiscard]]
void*
owner () const noexcept {
return m_owner; }
223 return m_owner == rhs.m_owner;
233 std::size_t operator() (
const Node& n)
const noexcept {
234 return std::hash<void*>{}(n.m_block);
250 std::vector<std::pair<void*,std::size_t> >
m_alloc;
256 using NL = std::set<Node>;
Memory arena base class and global arena accessors.
Abstract base class for memory arenas.
Definition AMReX_Arena.H:154
The nodes in our free list and block list.
Definition AMReX_CArena.H:190
std::size_t size() const noexcept
The size of the memory block.
Definition AMReX_CArena.H:215
void block(void *blk) noexcept
Set block address.
Definition AMReX_CArena.H:212
void size(std::size_t sz) noexcept
Set size.
Definition AMReX_CArena.H:218
void * owner() const noexcept
Definition AMReX_CArena.H:220
MemStat * mem_stat() const
Get the MemStat object of the function where this block was allocated.
Definition AMReX_CArena.H:227
void mem_stat(MemStat *a_stat) noexcept
Set MemStat.
Definition AMReX_CArena.H:230
bool coalescable(const Node &rhs) const noexcept
Definition AMReX_CArena.H:222
void * block() const noexcept
The block address.
Definition AMReX_CArena.H:209
Node(void *a_block, void *a_owner, std::size_t a_size, MemStat *a_stat=nullptr) noexcept
Definition AMReX_CArena.H:192
Coalescing first-fit dynamic memory arena.
Definition AMReX_CArena.H:37
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:262
std::size_t sizeOf(void *p) const noexcept
Return the size of the allocation referenced by p (0 if unknown).
Definition AMReX_CArena.cpp:526
CArena(const CArena &rhs)=delete
std::atomic< std::size_t > m_actually_used
Definition AMReX_CArena.H:279
static constexpr std::size_t DefaultHunkSize
The default memory hunk size to grab from the heap.
Definition AMReX_CArena.H:180
void * shrink_in_place(void *pt, std::size_t new_size) final
Shrink allocation size in-place.
Definition AMReX_CArena.cpp:258
std::mutex carena_mutex
Definition AMReX_CArena.H:283
std::size_t freeUnused_protected() final
Definition AMReX_CArena.cpp:438
std::size_t m_max_actually_used
The max amount of memory given out via alloc().
Definition AMReX_CArena.H:281
std::size_t heap_space_actually_used() const noexcept
Return the total amount of memory given out via alloc. Thread-safe.
Definition AMReX_CArena.cpp:520
std::size_t heap_space_used() const noexcept
The current amount of heap space used by the CArena object. Thread-safe.
Definition AMReX_CArena.cpp:514
std::pair< void *, std::size_t > alloc_in_place(void *pt, std::size_t szmin, std::size_t szmax) final
Allocate memory in-place if possible.
Definition AMReX_CArena.cpp:185
CArena & operator=(const CArena &rhs)=delete
friend std::ostream & operator<<(std::ostream &os, const CArena &arena)
Print a summary of the arena's free and busy lists to os.
Definition AMReX_CArena.cpp:578
void ResetMaxUsageCounter() final
Reset the maximum usage counter.
Definition AMReX_CArena.H:175
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:256
std::size_t m_hunk
The minimal size of hunks to request from system.
Definition AMReX_CArena.H:271
std::size_t freeUnused() final
Free unused memory back to the system. Return value is the amount of memory freed.
Definition AMReX_CArena.cpp:407
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:269
void * alloc(std::size_t nbytes) final
Allocate some memory.
Definition AMReX_CArena.cpp:49
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:316
std::size_t largestFreeBlock() const final
Return the largest free memory block already held by this CArena.
Definition AMReX_CArena.cpp:427
~CArena() override
The destructor.
Definition AMReX_CArena.cpp:41
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:476
void * alloc_protected(std::size_t nbytes)
Definition AMReX_CArena.cpp:57
void PrintUsage(std::string const &name, bool print_max_usage) const
Print memory usage information of this arena.
Definition AMReX_CArena.cpp:541
std::vector< std::pair< void *, std::size_t > > m_alloc
The list of blocks allocated.
Definition AMReX_CArena.H:250
std::atomic< std::size_t > m_used
Definition AMReX_CArena.H:274
std::size_t freeableMemory() const
Return the amount of memory that can be released back to the heap.
Definition AMReX_CArena.cpp:414
std::size_t m_max_used
The max amount of heap space currently allocated.
Definition AMReX_CArena.H:276
Definition AMReX_Amr.cpp:50
bool operator==(A1 const &a1, A2 const &a2)
Definition AMReX_GpuAllocators.H:214
Configuration options for constructing an Arena.
Definition AMReX_Arena.H:92
Definition AMReX_CArena.H:232
Definition AMReX_Arena.H:21