MySQL 9.0.0
Source Code Documentation
mysql::binlog::event::resource::Memory_resource Class Reference

Polymorphism-free memory resource class with custom allocator and deallocator functions. More...

#include <memory_resource.h>

Public Types

using Size_t = std::size_t
 
using Ptr_t = void *
 
using Allocator_t = std::function< Ptr_t(Size_t)>
 
using Deallocator_t = std::function< void(Ptr_t)>
 

Public Member Functions

 Memory_resource (const Allocator_t &allocator, const Deallocator_t &deallocator)
 Construct a new Memory_resource that uses the given allocator and deallocator. More...
 
 Memory_resource ()
 Construct a new Memory_resource that uses std::malloc and std::free. More...
 
void * allocate (Size_t n) const
 Allocate memory using the provided allocator. More...
 
void deallocate (Ptr_t p) const
 Deallocate memory using the provided deallocator. More...
 
Deallocator_t get_deallocator () const
 Return the deallocator. More...
 

Private Attributes

const Allocator_t m_allocator
 The allocator object. More...
 
const Deallocator_t m_deallocator
 The deallocator object. More...
 

Detailed Description

Polymorphism-free memory resource class with custom allocator and deallocator functions.

This is used as the "back-end" for Allocator objects. The allocator and deallocator functions are member objects of type std::function, specified as constructor arguments.

Example: a class that needs to allocate memory, and should have a way to use a specified PSI key, can be implemented as follows:

  • For each type the class needs to allocate, it has one member object of type Allocator. The constructor takes a Memory_resource parameter, which defaults to a default-constructed Memory_resource. The constructor passes the Memory_resource to the Allocator constructors. The class calls the Allocator member functions for its memory management.
  • API clients that don't need a PSI key don't need to pass a Memory_resource to the constructor; they use the default value for the parameter. API clients that need a PSI key, pass a Memory_resource object where the allocate/deallocate functions are PSI-enabled. Such a Memory_resource object can be obtained from the factory function psi_memory_resource in sql/psi_memory_resource.h.

Notes on design choices:

We avoid using polymorphism to configure the behavior of the allocate/deallocate member functions. Classes that use polymorphic memory resources would have to take a reference or pointer to the memory resource as argument, so either the class can't own the memory resource or it has to be dynamically allocated. If the class can't own the memory resource, it complicates the usage since the caller has to ensure the Memory_resource outlives the class that uses it. If the Memory_resource has to be allocated dynamically, that allocation cannot use a custom Memory_resource, which may defeat the purpose of using custom Memory_resources to track all allocations.

We also avoid static polymorphism, since every user of a statically polymorphic Memory_resource would have to be a template, which has two problems: first, two classes with the same behavior but different allocation strategies would be different types. Second, any class that allocates memory and has a requirement that the API client can configure the Memory_resource, has to be templated and therefore be defined in a header.

With the current solution, the caller just creates a Memory_resource object and passes it to the allocator.

Member Typedef Documentation

◆ Allocator_t

◆ Deallocator_t

◆ Ptr_t

◆ Size_t

Constructor & Destructor Documentation

◆ Memory_resource() [1/2]

mysql::binlog::event::resource::Memory_resource::Memory_resource ( const Allocator_t allocator,
const Deallocator_t deallocator 
)
inline

Construct a new Memory_resource that uses the given allocator and deallocator.

Parameters
allocatorThe allocator, e.g. std::malloc or my_malloc.
deallocatorThe deallocator, e.g. std::free or my_free.

◆ Memory_resource() [2/2]

mysql::binlog::event::resource::Memory_resource::Memory_resource ( )
inline

Construct a new Memory_resource that uses std::malloc and std::free.

Member Function Documentation

◆ allocate()

void * mysql::binlog::event::resource::Memory_resource::allocate ( Size_t  n) const
inline

Allocate memory using the provided allocator.

Parameters
nThe size.
Returns
The new memory.

◆ deallocate()

void mysql::binlog::event::resource::Memory_resource::deallocate ( Ptr_t  p) const
inline

Deallocate memory using the provided deallocator.

Parameters
pThe pointer.

◆ get_deallocator()

Deallocator_t mysql::binlog::event::resource::Memory_resource::get_deallocator ( ) const
inline

Return the deallocator.

Member Data Documentation

◆ m_allocator

const Allocator_t mysql::binlog::event::resource::Memory_resource::m_allocator
private

The allocator object.

◆ m_deallocator

const Deallocator_t mysql::binlog::event::resource::Memory_resource::m_deallocator
private

The deallocator object.


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