MySQL 9.0.0
Source Code Documentation
temptable::AllocatorState< AllocationScheme > Class Template Reference

Shared state between all instances of a given allocator. More...

#include <allocator.h>

Public Member Functions

 ~AllocatorState () noexcept
 Destroys the state, deallocate the current_block if it was left empty. More...
 
Blockget_block_for_new_allocation (size_t n_bytes_requested)
 Gets a Block from which a new allocation of the specified size should be performed. More...
 
void block_is_not_used_anymore (Block block) noexcept
 Informs the state object of a block that has no data allocated inside of it anymore for garbage collection. More...
 

Private Member Functions

void free_block (Block &block) noexcept
 Frees the specified block and takes care of all accounting. More...
 

Private Attributes

Block current_block
 Current not-yet-full block to feed allocations from. More...
 
size_t number_of_blocks = 0
 Number of created blocks so far (by this Allocator object). More...
 

Detailed Description

template<class AllocationScheme>
class temptable::AllocatorState< AllocationScheme >

Shared state between all instances of a given allocator.

STL allocators can (since C++11) carry state; however, that state should never be mutable, as the allocator can be copy-constructed and rebound without further notice, so e.g. deallocating memory in one allocator could mean freeing a block that an earlier copy of the allocator still thinks is valid.

Usually, mutable state will be external to the allocator (e.g. Mem_root_allocator will point to a MEM_ROOT, but it won't own the MEM_ROOT); however, TempTable was never written this way, and doesn't have a natural place to stick the allocator state. Thus, we need a kludge where the allocator's state is held in a shared_ptr, owned by all the instances together. This is suboptimal for performance, and also is against the style guide's recommendation to have clear ownership of objects, but at least it avoids the use-after-free.

Constructor & Destructor Documentation

◆ ~AllocatorState()

template<class AllocationScheme >
temptable::AllocatorState< AllocationScheme >::~AllocatorState ( )
inlinenoexcept

Destroys the state, deallocate the current_block if it was left empty.

Member Function Documentation

◆ block_is_not_used_anymore()

template<class AllocationScheme >
void temptable::AllocatorState< AllocationScheme >::block_is_not_used_anymore ( Block  block)
inlinenoexcept

Informs the state object of a block that has no data allocated inside of it anymore for garbage collection.

[in] The empty block to manage and possibly free.

◆ free_block()

template<class AllocationScheme >
void temptable::AllocatorState< AllocationScheme >::free_block ( Block block)
inlineprivatenoexcept

Frees the specified block and takes care of all accounting.

[in] The empty block to free.

◆ get_block_for_new_allocation()

template<class AllocationScheme >
Block * temptable::AllocatorState< AllocationScheme >::get_block_for_new_allocation ( size_t  n_bytes_requested)
inline

Gets a Block from which a new allocation of the specified size should be performed.

It will use the current Block or create a new one if it is too small. [in] Number of bytes that will be allocated from the returned block.

Member Data Documentation

◆ current_block

template<class AllocationScheme >
Block temptable::AllocatorState< AllocationScheme >::current_block
private

Current not-yet-full block to feed allocations from.

◆ number_of_blocks

template<class AllocationScheme >
size_t temptable::AllocatorState< AllocationScheme >::number_of_blocks = 0
private

Number of created blocks so far (by this Allocator object).

We use this number only as a hint as to how big block to create when a new block needs to be created.


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