CETL 0.0.0
 
Loading...
Searching...
No Matches
cetl::pmr::MemoryResourceDeleter< MemoryResourceType > Class Template Referencefinal

#include "cetl/pmr/memory.hpp"

Public Member Functions

 MemoryResourceDeleter (MemoryResourceType *mem_resource, std::size_t mem_size, std::size_t mem_align=alignof(std::max_align_t)) noexcept
 
 MemoryResourceDeleter (MemoryResourceDeleter &&rhs) noexcept
 
MemoryResourceDeleteroperator= (MemoryResourceDeleter &&rhs) noexcept
 
 MemoryResourceDeleter (const MemoryResourceDeleter &)=delete
 
MemoryResourceDeleteroperator= (const MemoryResourceDeleter &)=delete
 
void operator() (void *p) noexcept
 
std::size_t size () const noexcept
 
std::size_t alignment () const noexcept
 
MemoryResourceType * resource () const noexcept
 

Detailed Description

template<typename MemoryResourceType>
class cetl::pmr::MemoryResourceDeleter< MemoryResourceType >

RAII helper for MemoryResourceType::allocate() and MemoryResourceType::deallocate().

This type is designed to work with std::unique_ptr.

Template Parameters
MemoryResourceTypeThe memory resource type.

Example usage:

// Let's say you wanted to store a bunch of buffers in a heap so you can get the largest one quickly.
// You could do something like this:
struct ByteBuffer
{
MemoryResourcePointer data;
std::size_t size;
};
cetl::pmr::memory_resource* resource = cetl::pmr::new_delete_resource();
auto buffer_0 = ByteBuffer{{resource->allocate(256), {resource, 256}}, 256};
auto buffer_1 = ByteBuffer{{resource->allocate(512), {resource, 512}}, 512};
auto buffer_2 = ByteBuffer{{resource->allocate(1024), {resource, 1024}}, 1024};
buffers.push_back(std::move(buffer_0));
buffers.push_back(std::move(buffer_1));
buffers.push_back(std::move(buffer_2));
std::make_heap(buffers.begin(), buffers.end(), [](const auto& lhs, const auto& rhs) {
return lhs.size < rhs.size;
});
auto& largest_buffer = buffers.front();
std::cout << "Largest buffer size: " << largest_buffer.size << std::endl;
// Now as long as the vector "buffers" is in scope, the buffers will be valid. When the vector is destroyed,
// the buffers will be freed using the correct memory resource.

(See full example here...)

Constructor & Destructor Documentation

◆ MemoryResourceDeleter() [1/2]

template<typename MemoryResourceType>
cetl::pmr::MemoryResourceDeleter< MemoryResourceType >::MemoryResourceDeleter ( MemoryResourceType * mem_resource,
std::size_t mem_size,
std::size_t mem_align = alignof(std::max_align_t) )
inlinenoexcept

Designated constructor.

Parameters
mem_resourceThe memory resource to use for deallocation.
mem_sizeThe size of the memory to deallocate.
mem_alignThe alignment of the memory to deallocate.

Referenced by MemoryResourceDeleter(), and operator=().

Here is the caller graph for this function:

◆ MemoryResourceDeleter() [2/2]

template<typename MemoryResourceType>
cetl::pmr::MemoryResourceDeleter< MemoryResourceType >::MemoryResourceDeleter ( MemoryResourceDeleter< MemoryResourceType > && rhs)
inlinenoexcept

To support unique_ptr move semantics.

Parameters
rhsThe deleter to move from.

References MemoryResourceDeleter().

Member Function Documentation

◆ alignment()

template<typename MemoryResourceType>
std::size_t cetl::pmr::MemoryResourceDeleter< MemoryResourceType >::alignment ( ) const
inlinenoexcept

Alignment of the memory this deleter will or did deallocate.

Returns
Alignment.

◆ operator()()

template<typename MemoryResourceType>
void cetl::pmr::MemoryResourceDeleter< MemoryResourceType >::operator() ( void * p)
inlinenoexcept

Functor called by smart-pointer to deallocate memory.

Parameters
pThe memory to deallocate.

References CETL_DEBUG_ASSERT.

◆ operator=()

template<typename MemoryResourceType>
MemoryResourceDeleter & cetl::pmr::MemoryResourceDeleter< MemoryResourceType >::operator= ( MemoryResourceDeleter< MemoryResourceType > && rhs)
inlinenoexcept

To support unique_ptr move semantics.

Parameters
rhsThe deleter to move from.
Returns
Reference to this.

References MemoryResourceDeleter().

◆ resource()

template<typename MemoryResourceType>
MemoryResourceType * cetl::pmr::MemoryResourceDeleter< MemoryResourceType >::resource ( ) const
inlinenoexcept

The memory resource this deleter will or did use to deallocate memory.

Returns
The memory resource or nullptr if this deleter was moved from and is now invalid.

◆ size()

template<typename MemoryResourceType>
std::size_t cetl::pmr::MemoryResourceDeleter< MemoryResourceType >::size ( ) const
inlinenoexcept

Size of the memory this deleter will or did deallocate.

Returns
Size in bytes.

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