PlayRho  2.0.0
An interactive physics engine & library.
playrho::pmr::PoolMemoryResource Class Referencefinal

Pool memory resource. More...

#include <playrho/pmr/PoolMemoryResource.hpp>

Inheritance diagram for playrho::pmr::PoolMemoryResource:
[legend]
Collaboration diagram for playrho::pmr::PoolMemoryResource:
[legend]

Classes

class  BufferRecord
 Buffer record for PoolMemoryResource. More...
 
struct  Stats
 Statistics data. More...
 

Public Member Functions

 PoolMemoryResource (const PoolMemoryOptions &options={}, memory_resource *upstream=nullptr)
 Default and initializing constructor. More...
 
 PoolMemoryResource (const PoolMemoryResource &other)=delete
 Copy construction deleted!
 
 PoolMemoryResource (memory_resource *upstream)
 Upstream initializing constructor. More...
 
 PoolMemoryResource (PoolMemoryResource &&other)=delete
 Moves construction deleted!
 
 ~PoolMemoryResource () noexcept override
 Destructor. More...
 
void * do_allocate (std::size_t num_bytes, std::size_t alignment) override
 Do the requested allocation. More...
 
void do_deallocate (void *p, std::size_t num_bytes, std::size_t alignment) override
 Do the requested deallocation.
 
bool do_is_equal (const memory_resource &other) const noexcept override
 Do an equality comparison with the given instance. More...
 
PoolMemoryOptions GetOptions () const noexcept
 Gets the options used by this instance. More...
 
Stats GetStats () const noexcept
 Gets the current statistics.
 
memory_resourceGetUpstream () const noexcept
 Gets the upstream resource.
 
PoolMemoryResourceoperator= (const PoolMemoryResource &other)=delete
 Copy assignment deleted!
 
PoolMemoryResourceoperator= (PoolMemoryResource &&other)=delete
 Moves assignment deleted!
 
- Public Member Functions inherited from playrho::pmr::memory_resource
 memory_resource ()=default
 Default constructor.
 
 memory_resource (const memory_resource &)=default
 Copy constructor.
 
virtual ~memory_resource ()
 Destructor.
 
void * allocate (std::size_t bytes, std::size_t alignment=alignof(std::max_align_t))
 Allocates memory.
 
void deallocate (void *p, std::size_t bytes, std::size_t alignment=alignof(std::max_align_t))
 Deallocates memory.
 
bool is_equal (const memory_resource &other) const noexcept
 Compare for equality with other resource.
 

Static Public Member Functions

static std::size_t GetMaxNumBytes () noexcept
 Gets the maximum number of bytes supported for any allocations this class does. More...
 

Detailed Description

Pool memory resource.

Note
This is a memory pooling implementation of a memory_resource. It is similar to std::pmr::unsynchronized_pool_resource.
See also
https://en.cppreference.com/w/cpp/memory/unsynchronized_pool_resource

Constructor & Destructor Documentation

◆ PoolMemoryResource() [1/2]

playrho::pmr::PoolMemoryResource::PoolMemoryResource ( const PoolMemoryOptions options = {},
memory_resource upstream = nullptr 
)

Default and initializing constructor.

Note
Constructs an instance with the given options such that options.reserveBuffers buffers are pre-allocated with options.reserveBytes bytes and are immediately available for do_allocate calls.
Precondition
Upstream memory resource won't throw any exceptions if called to deallocate buffers it's previously allocated.
Postcondition
GetOptions() returns the given options.
Exceptions
std::length_errorif options.reserveBuffers > options.limitBuffers.
std::bad_array_new_lengthif options.reserveBytes > GetMaxNumBytes().
std::bad_allocif an allocation of options.reserveBytes fails for any of the options.reserveBuffers buffers.

◆ PoolMemoryResource() [2/2]

playrho::pmr::PoolMemoryResource::PoolMemoryResource ( memory_resource upstream)
inline

Upstream initializing constructor.

Precondition
Upstream memory resource won't throw any exceptions if called to deallocate buffers it's previously allocated.

◆ ~PoolMemoryResource()

playrho::pmr::PoolMemoryResource::~PoolMemoryResource ( )
overridenoexcept

Destructor.

Note
Deallocates the allocated memory.
Warning
Terminates if upstream resource throws on deallocating any memory.

Member Function Documentation

◆ do_allocate()

void * playrho::pmr::PoolMemoryResource::do_allocate ( std::size_t  num_bytes,
std::size_t  alignment 
)
overridevirtual

Do the requested allocation.

Note
Setting Options::limitBuffers to 1 can help identify code that's not reserving enough capacity ahead of time to avoid having to move data over to new buffers whenever existing capacity runs out by causing std::length to get thrown then instead.
This function provides the strong exception guarantee. The state of the instance will be unchanged if an exception is thrown.
Exceptions
std::bad_array_new_lengthif num_bytes is greater than GetMaxNumBytes().
std::length_errorIf called with no deallocated buffers available and the number of allocated buffers has reached (or exceeded) Options::limitBuffers.

Implements playrho::pmr::memory_resource.

◆ do_is_equal()

bool playrho::pmr::PoolMemoryResource::do_is_equal ( const memory_resource other) const
overridevirtualnoexcept

Do an equality comparison with the given instance.

Returns
true if the address of the other instance is the same as the address of this instance, false otherwise.

Implements playrho::pmr::memory_resource.

◆ GetMaxNumBytes()

std::size_t playrho::pmr::PoolMemoryResource::GetMaxNumBytes ( )
staticnoexcept

Gets the maximum number of bytes supported for any allocations this class does.

See also
PoolMemoryResource(const Options& options), do_allocate.

Referenced by do_allocate().

◆ GetOptions()

PoolMemoryOptions playrho::pmr::PoolMemoryResource::GetOptions ( ) const
inlinenoexcept

Gets the options used by this instance.

See also
PoolMemoryResource(const Options&, memory_resource*).

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