MySQL 8.3.0
Source Code Documentation
temptable::Allocator< T, AllocationScheme > Class Template Reference

Custom memory allocator. More...

#include <allocator.h>

Classes

struct  rebind
 

Public Types

typedef T * pointer
 
typedef const T * const_pointer
 
typedef T & reference
 
typedef const T & const_reference
 
typedef T value_type
 
typedef size_t size_type
 
typedef ptrdiff_t difference_type
 

Public Member Functions

 Allocator (Block *shared_block, TableResourceMonitor &table_resource_monitor)
 Constructor. More...
 
template<class U >
 Allocator (const Allocator< U > &other)
 Constructor from allocator of another type. More...
 
template<class U >
 Allocator (Allocator< U > &&other) noexcept
 Move constructor from allocator of another type. More...
 
 ~Allocator ()
 Destructor. More...
 
 Allocator (const Allocator &)=default
 
template<class U >
void operator= (const Allocator< U > &)=delete
 Assignment operator, not used, thus disabled. More...
 
template<class U >
void operator= (const Allocator< U > &&)=delete
 Move operator, not used, thus disabled. More...
 
template<class U >
bool operator== (const Allocator< U > &rhs) const
 Equality operator. More...
 
template<class U >
bool operator!= (const Allocator< U > &rhs) const
 Inequality operator. More...
 
T * allocate (size_t n_elements)
 Allocate memory for storing n_elements number of elements. More...
 
void deallocate (T *ptr, size_t n_elements)
 Free a memory allocated by allocate(). More...
 
template<class U , class... Args>
void construct (U *mem, Args &&... args)
 Construct one object of type U on an already allocated chunk of memory, which must be large enough to store it. More...
 
template<class U >
void destroy (U *p)
 Destroy an object of type U. More...
 

Static Public Member Functions

static void init ()
 Initialize necessary structures. More...
 

Public Attributes

std::shared_ptr< AllocatorStatem_state
 Shared state between all the copies and rebinds of this allocator. More...
 
Blockm_shared_block
 A block of memory which is a state external to this allocator and can be shared among different instances of the allocator (not simultaneously). More...
 
TableResourceMonitorm_table_resource_monitor
 Table resource monitor control mechanism that limits the amount of resources that can be consumed at the per-table level. More...
 

Detailed Description

template<class T, class AllocationScheme = Exponential_growth_preferring_RAM_over_MMAP>
class temptable::Allocator< T, AllocationScheme >

Custom memory allocator.

All dynamic memory used by the TempTable engine is allocated through this allocator.

The purpose of this allocator is to minimize the number of calls to the OS for allocating new memory (e.g. malloc()) and to improve the spatial locality of reference. It is able to do so quite easily thanks to the Block/Chunk entities it is implemented in terms of. Due to the design of these entities, it is also able to feed allocations and deallocations in (amortized) constant-time and keep being CPU memory-access friendly because of the internal self-adjustment to word-size memory alignment. To learn even more about specifics and more properties please have a look at the respective header files of Header/Block/Chunk class declarations.

The most common use case, for which it is optimized, is to have the following performed by a single thread:

  • allocate many times (creation of a temp table and inserting data into it).
  • use the allocated memory (selects on the temp table).
  • free all the pieces (drop of the temp table).

The allocator allocates memory from the OS in large blocks (e.g. a few MiB) whose size also increases progressively by the increasing number of allocation requests. Exact block-size increase progress is defined by the block allocation scheme which, by default, is set to AllocationScheme::Exponential.

Allocator does not store a list of all allocated blocks but only keeps track of the current block which has not yet been entirely filled up and the overall number of allocated blocks. When current block gets filled up, new one is created and immediately made current.

Furthermore, it always keeps the last block alive. It cannot be deallocated by the user. Last block is automatically deallocated at the thread exit.

Allocator will also keep track of RAM-consumption and in case it reaches the threshold defined by temptable_max_ram, it will switch to MMAP-backed block allocations. It will switch back once RAM consumption is again below the threshold.

Member Typedef Documentation

◆ const_pointer

template<class T , class AllocationScheme = Exponential_growth_preferring_RAM_over_MMAP>
typedef const T* temptable::Allocator< T, AllocationScheme >::const_pointer

◆ const_reference

template<class T , class AllocationScheme = Exponential_growth_preferring_RAM_over_MMAP>
typedef const T& temptable::Allocator< T, AllocationScheme >::const_reference

◆ difference_type

template<class T , class AllocationScheme = Exponential_growth_preferring_RAM_over_MMAP>
typedef ptrdiff_t temptable::Allocator< T, AllocationScheme >::difference_type

◆ pointer

template<class T , class AllocationScheme = Exponential_growth_preferring_RAM_over_MMAP>
typedef T* temptable::Allocator< T, AllocationScheme >::pointer

◆ reference

template<class T , class AllocationScheme = Exponential_growth_preferring_RAM_over_MMAP>
typedef T& temptable::Allocator< T, AllocationScheme >::reference

◆ size_type

template<class T , class AllocationScheme = Exponential_growth_preferring_RAM_over_MMAP>
typedef size_t temptable::Allocator< T, AllocationScheme >::size_type

◆ value_type

template<class T , class AllocationScheme = Exponential_growth_preferring_RAM_over_MMAP>
typedef T temptable::Allocator< T, AllocationScheme >::value_type

Constructor & Destructor Documentation

◆ Allocator() [1/4]

template<class T , class AllocationScheme >
temptable::Allocator< T, AllocationScheme >::Allocator ( Block shared_block,
TableResourceMonitor table_resource_monitor 
)
inline

Constructor.

◆ Allocator() [2/4]

template<class T , class AllocationScheme >
template<class U >
temptable::Allocator< T, AllocationScheme >::Allocator ( const Allocator< U > &  other)
inline

Constructor from allocator of another type.

The state is copied into the new object.

Parameters
[in]otherSource Allocator object.

◆ Allocator() [3/4]

template<class T , class AllocationScheme >
template<class U >
temptable::Allocator< T, AllocationScheme >::Allocator ( Allocator< U > &&  other)
inlinenoexcept

Move constructor from allocator of another type.

Parameters
[in,out]otherSource Allocator object.

◆ ~Allocator()

template<class T , class AllocationScheme >
temptable::Allocator< T, AllocationScheme >::~Allocator ( )
inlinedefault

Destructor.

◆ Allocator() [4/4]

template<class T , class AllocationScheme = Exponential_growth_preferring_RAM_over_MMAP>
temptable::Allocator< T, AllocationScheme >::Allocator ( const Allocator< T, AllocationScheme > &  )
default

Member Function Documentation

◆ allocate()

template<class T , class AllocationScheme >
T * temptable::Allocator< T, AllocationScheme >::allocate ( size_t  n_elements)
inline

Allocate memory for storing n_elements number of elements.

Parameters
[in]n_elementsNumber of elements that must be allocated.

◆ construct()

template<class T , class AllocationScheme >
template<class U , class... Args>
void temptable::Allocator< T, AllocationScheme >::construct ( U mem,
Args &&...  args 
)
inline

Construct one object of type U on an already allocated chunk of memory, which must be large enough to store it.

Parameters
[in]memMemory where to create the object.
argsArguments to pass to U's constructor.

◆ deallocate()

template<class T , class AllocationScheme >
void temptable::Allocator< T, AllocationScheme >::deallocate ( T *  ptr,
size_t  n_elements 
)
inline

Free a memory allocated by allocate().

Parameters
[in,out]ptrPointer to memory to free.
[in]n_elementsNumber of elements allocated.

◆ destroy()

template<class T , class AllocationScheme >
template<class U >
void temptable::Allocator< T, AllocationScheme >::destroy ( U p)
inline

Destroy an object of type U.

The memory is not returned to the OS, this is the counterpart of construct().

Parameters
[in,out]pObject to destroy.

◆ init()

template<class T , class AllocationScheme >
void temptable::Allocator< T, AllocationScheme >::init ( void  )
inlinestatic

Initialize necessary structures.

Called once in the OS process lifetime, before other methods.

◆ operator!=()

template<class T , class AllocationScheme >
template<class U >
bool temptable::Allocator< T, AllocationScheme >::operator!= ( const Allocator< U > &  rhs) const
inline

Inequality operator.

Returns
true if not equal
Parameters
[in]rhsObject to compare with.

◆ operator=() [1/2]

template<class T , class AllocationScheme = Exponential_growth_preferring_RAM_over_MMAP>
template<class U >
void temptable::Allocator< T, AllocationScheme >::operator= ( const Allocator< U > &&  )
delete

Move operator, not used, thus disabled.

◆ operator=() [2/2]

template<class T , class AllocationScheme = Exponential_growth_preferring_RAM_over_MMAP>
template<class U >
void temptable::Allocator< T, AllocationScheme >::operator= ( const Allocator< U > &  )
delete

Assignment operator, not used, thus disabled.

◆ operator==()

template<class T , class AllocationScheme >
template<class U >
bool temptable::Allocator< T, AllocationScheme >::operator== ( const Allocator< U > &  rhs) const
inline

Equality operator.

Returns
true if equal
Parameters
[in]rhsObject to compare with.

Member Data Documentation

◆ m_shared_block

template<class T , class AllocationScheme = Exponential_growth_preferring_RAM_over_MMAP>
Block* temptable::Allocator< T, AllocationScheme >::m_shared_block

A block of memory which is a state external to this allocator and can be shared among different instances of the allocator (not simultaneously).

In order to speed up its operations, allocator may decide to consume the memory of this shared block.

◆ m_state

template<class T , class AllocationScheme = Exponential_growth_preferring_RAM_over_MMAP>
std::shared_ptr<AllocatorState> temptable::Allocator< T, AllocationScheme >::m_state

Shared state between all the copies and rebinds of this allocator.

See AllocatorState for details.

◆ m_table_resource_monitor

template<class T , class AllocationScheme = Exponential_growth_preferring_RAM_over_MMAP>
TableResourceMonitor& temptable::Allocator< T, AllocationScheme >::m_table_resource_monitor

Table resource monitor control mechanism that limits the amount of resources that can be consumed at the per-table level.


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