CookMem 1.6.0
A C++ memory context / allocator
Loading...
Searching...
No Matches
cookmem::MemResource< Context > Class Template Reference

#include <memresource.h>

Inheritance diagram for cookmem::MemResource< Context >:
Collaboration diagram for cookmem::MemResource< Context >:

Public Member Functions

 MemResource (Context &context) noexcept
Context & getContext () const noexcept

Protected Member Functions

void * do_allocate (std::size_t bytes, std::size_t alignment) override
void do_deallocate (void *ptr, std::size_t bytes, std::size_t alignment) override
bool do_is_equal (const std::pmr::memory_resource &other) const noexcept override

Detailed Description

template<class Context>
class cookmem::MemResource< Context >

A std::pmr::memory_resource that allocates from a memory context, so that the std::pmr containers can use it. It needs C++17, and is available when COOKMEM_HAS_PMR is 1.

Alignments larger than the allocation alignment use allocateAligned (). When the context cannot allocate the memory, std::bad_alloc is thrown, as std::pmr::memory_resource requires.

Template Parameters
Contexta memory context, such as cookmem::MemContext or cookmem::SimpleMemContext, with void as the value type.

Constructor & Destructor Documentation

◆ MemResource()

template<class Context>
cookmem::MemResource< Context >::MemResource ( Context & context)
inlineexplicitnoexcept

Constructor.

Parameters
contextthe memory context to allocate from. The caller owns it, and it must outlive this resource and the memory allocated from it.

Member Function Documentation

◆ do_allocate()

template<class Context>
void * cookmem::MemResource< Context >::do_allocate ( std::size_t bytes,
std::size_t alignment )
inlineoverrideprotected

Allocate memory from the context.

Parameters
bytesthe size of the memory.
alignmentthe alignment of the memory, a power of two.
Returns
the memory allocated.
Exceptions
std::bad_allocif the memory cannot be allocated.

◆ do_deallocate()

template<class Context>
void cookmem::MemResource< Context >::do_deallocate ( void * ptr,
std::size_t bytes,
std::size_t alignment )
inlineoverrideprotected

Free memory allocated by do_allocate ().

Parameters
ptrthe memory to free.
bytesthe size of the memory, which is only passed to the logger.
alignmentthe alignment of the memory, which is not needed.

◆ do_is_equal()

template<class Context>
bool cookmem::MemResource< Context >::do_is_equal ( const std::pmr::memory_resource & other) const
inlineoverrideprotectednoexcept

Check whether memory allocated by this resource can be freed by another resource. That is the case when the other resource is a MemResource of the same type for the same context.

Parameters
otherthe other resource.
Returns
true if the two resources use the same context.

◆ getContext()

template<class Context>
Context & cookmem::MemResource< Context >::getContext ( ) const
inlinenoexcept

Get the memory context.

Returns
the memory context this resource allocates from.

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