Block-Structured AMR Software Framework
amrex::CArena Class Reference

A Concrete Class for Dynamic Memory Management using first fit. This is a coalescing memory manager. It allocates (possibly) large chunks of heap space and apportions it out as requested. It merges together neighboring chunks on each free(). More...

#include <AMReX_CArena.H>

Inheritance diagram for amrex::CArena:
amrex::Arena

Classes

class  Node
 The nodes in our free list and block list. More...
 

Public Member Functions

 CArena (std::size_t hunk_size=0, ArenaInfo info=ArenaInfo())
 Construct a coalescing memory manager. hunk_size is the minimum size of hunks of memory to allocate from the heap. If hunk_size == 0 we use DefaultHunkSize as specified below. More...
 
 CArena (const CArena &rhs)=delete
 
 CArena (CArena &&rhs)=delete
 
CArenaoperator= (const CArena &rhs)=delete
 
CArenaoperator= (CArena &&rhs)=delete
 
 ~CArena () override
 The destructor. More...
 
void * alloc (std::size_t nbytes) final
 Allocate some memory. More...
 
std::pair< void *, std::size_t > alloc_in_place (void *pt, std::size_t szmin, std::size_t szmax) final
 
void * shrink_in_place (void *pt, std::size_t new_size) final
 
void free (void *vp) final
 Free up allocated memory. Merge neighboring free memory chunks into largest possible chunk. More...
 
std::size_t freeUnused () final
 Free unused memory back to the system. Return value is the amount memory freed. More...
 
bool hasFreeDeviceMemory (std::size_t sz) final
 Does the device have enough free memory for allocating this much memory? For CPU builds, this always return true. This is not a const function because it may attempt to release memory back to the system. More...
 
std::size_t heap_space_used () const noexcept
 The current amount of heap space used by the CArena object. More...
 
std::size_t heap_space_actually_used () const noexcept
 Return the total amount of memory given out via alloc. More...
 
std::size_t sizeOf (void *p) const noexcept
 Return the amount of memory in this pointer. Return 0 for unknown pointer. More...
 
void PrintUsage (std::string const &name) const
 
void PrintUsage (std::ostream &os, std::string const &name, std::string const &space) const
 
- Public Member Functions inherited from amrex::Arena
virtual ~Arena ()=default
 
 Arena () noexcept=default
 
 Arena (const Arena &rhs)=delete
 
 Arena (Arena &&rhs)=delete
 
Arenaoperator= (const Arena &rhs)=delete
 
Arenaoperator= (Arena &&rhs)=delete
 
virtual bool isDeviceAccessible () const
 
virtual bool isHostAccessible () const
 
virtual bool isManaged () const
 
virtual bool isDevice () const
 
virtual bool isPinned () const
 
void registerForProfiling (const std::string &memory_name)
 Add this Arena to the list of Arenas that are profiled by TinyProfiler. More...
 
virtual bool isStreamOrderedArena () const
 Is this GPU stream ordered memory allocator? More...
 
const ArenaInfoarenaInfo () const
 Return the ArenaInfo object for querying. More...
 

Static Public Attributes

constexpr static std::size_t DefaultHunkSize = 1024*1024*8
 The default memory hunk size to grab from the heap. More...
 
- Static Public Attributes inherited from amrex::Arena
static const std::size_t align_size = 16
 

Protected Types

using NL = std::set< Node >
 The type of our freelist and blocklist. We use a set sorted from lo to hi memory addresses. More...
 

Protected Member Functions

void * alloc_protected (std::size_t nbytes)
 
std::size_t freeUnused_protected () final
 
- Protected Member Functions inherited from amrex::Arena
void * allocate_system (std::size_t nbytes)
 
void deallocate_system (void *p, std::size_t nbytes)
 

Protected Attributes

std::vector< std::pair< void *, std::size_t > > m_alloc
 The list of blocks allocated via ::operator new(). More...
 
NL m_freelist
 The free list of allocated but not currently used blocks. Maintained in lo to hi memory sorted order. More...
 
std::unordered_set< Node, Node::hashm_busylist
 The list of busy blocks. A block is either on the freelist or on the blocklist, but not on both. More...
 
std::size_t m_hunk
 The minimal size of hunks to request from system. More...
 
std::size_t m_used {0}
 The amount of heap space currently allocated. More...
 
std::size_t m_actually_used {0}
 The amount of memory given out via alloc(). More...
 
std::mutex carena_mutex
 
- Protected Attributes inherited from amrex::Arena
ArenaInfo arena_info
 
struct amrex::Arena::ArenaProfiler m_profiler
 

Friends

std::ostream & operator<< (std::ostream &os, const CArena &arena)
 

Additional Inherited Members

- Static Public Member Functions inherited from amrex::Arena
static std::size_t align (std::size_t sz)
 Given a minimum required arena size of sz bytes, this returns the next largest arena size that will align to align_size bytes. More...
 
static void Initialize ()
 
static void PrintUsage ()
 
static void PrintUsageToFiles (std::string const &filename, std::string const &message)
 
static void Finalize ()
 

Detailed Description

A Concrete Class for Dynamic Memory Management using first fit. This is a coalescing memory manager. It allocates (possibly) large chunks of heap space and apportions it out as requested. It merges together neighboring chunks on each free().

Member Typedef Documentation

◆ NL

using amrex::CArena::NL = std::set<Node>
protected

The type of our freelist and blocklist. We use a set sorted from lo to hi memory addresses.

Constructor & Destructor Documentation

◆ CArena() [1/3]

amrex::CArena::CArena ( std::size_t  hunk_size = 0,
ArenaInfo  info = ArenaInfo() 
)

Construct a coalescing memory manager. hunk_size is the minimum size of hunks of memory to allocate from the heap. If hunk_size == 0 we use DefaultHunkSize as specified below.

◆ CArena() [2/3]

amrex::CArena::CArena ( const CArena rhs)
delete

◆ CArena() [3/3]

amrex::CArena::CArena ( CArena &&  rhs)
delete

◆ ~CArena()

amrex::CArena::~CArena ( )
override

The destructor.

Member Function Documentation

◆ alloc()

void * amrex::CArena::alloc ( std::size_t  nbytes)
finalvirtual

Allocate some memory.

Implements amrex::Arena.

◆ alloc_in_place()

std::pair< void *, std::size_t > amrex::CArena::alloc_in_place ( void *  pt,
std::size_t  szmin,
std::size_t  szmax 
)
finalvirtual

Try to allocate in-place by extending the capacity of given pointer.

Reimplemented from amrex::Arena.

◆ alloc_protected()

void * amrex::CArena::alloc_protected ( std::size_t  nbytes)
protected

◆ free()

void amrex::CArena::free ( void *  vp)
finalvirtual

Free up allocated memory. Merge neighboring free memory chunks into largest possible chunk.

Implements amrex::Arena.

◆ freeUnused()

std::size_t amrex::CArena::freeUnused ( )
finalvirtual

Free unused memory back to the system. Return value is the amount memory freed.

Reimplemented from amrex::Arena.

◆ freeUnused_protected()

std::size_t amrex::CArena::freeUnused_protected ( )
finalprotectedvirtual

Reimplemented from amrex::Arena.

◆ hasFreeDeviceMemory()

bool amrex::CArena::hasFreeDeviceMemory ( std::size_t  sz)
finalvirtual

Does the device have enough free memory for allocating this much memory? For CPU builds, this always return true. This is not a const function because it may attempt to release memory back to the system.

Reimplemented from amrex::Arena.

◆ heap_space_actually_used()

std::size_t amrex::CArena::heap_space_actually_used ( ) const
noexcept

Return the total amount of memory given out via alloc.

◆ heap_space_used()

std::size_t amrex::CArena::heap_space_used ( ) const
noexcept

The current amount of heap space used by the CArena object.

◆ operator=() [1/2]

CArena& amrex::CArena::operator= ( CArena &&  rhs)
delete

◆ operator=() [2/2]

CArena& amrex::CArena::operator= ( const CArena rhs)
delete

◆ PrintUsage() [1/2]

void amrex::CArena::PrintUsage ( std::ostream &  os,
std::string const &  name,
std::string const &  space 
) const

◆ PrintUsage() [2/2]

void amrex::CArena::PrintUsage ( std::string const &  name) const

◆ shrink_in_place()

void * amrex::CArena::shrink_in_place ( void *  pt,
std::size_t  new_size 
)
finalvirtual

Try to shrink in-place

Reimplemented from amrex::Arena.

◆ sizeOf()

std::size_t amrex::CArena::sizeOf ( void *  p) const
noexcept

Return the amount of memory in this pointer. Return 0 for unknown pointer.

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  os,
const CArena arena 
)
friend

Member Data Documentation

◆ carena_mutex

std::mutex amrex::CArena::carena_mutex
protected

◆ DefaultHunkSize

constexpr static std::size_t amrex::CArena::DefaultHunkSize = 1024*1024*8
staticconstexpr

The default memory hunk size to grab from the heap.

◆ m_actually_used

std::size_t amrex::CArena::m_actually_used {0}
protected

The amount of memory given out via alloc().

◆ m_alloc

std::vector<std::pair<void*,std::size_t> > amrex::CArena::m_alloc
protected

The list of blocks allocated via ::operator new().

◆ m_busylist

std::unordered_set<Node, Node::hash> amrex::CArena::m_busylist
protected

The list of busy blocks. A block is either on the freelist or on the blocklist, but not on both.

◆ m_freelist

NL amrex::CArena::m_freelist
protected

The free list of allocated but not currently used blocks. Maintained in lo to hi memory sorted order.

◆ m_hunk

std::size_t amrex::CArena::m_hunk
protected

The minimal size of hunks to request from system.

◆ m_used

std::size_t amrex::CArena::m_used {0}
protected

The amount of heap space currently allocated.


The documentation for this class was generated from the following files: