MySQL 8.4.2
Source Code Documentation
TranxNodeAllocator Class Reference

This class provides memory allocating and freeing methods for TranxNode. More...

#include <semisync_source.h>

Classes

struct  Block
 A sequence memory which contains BLOCK_TRANX_NODES TranxNodes. More...
 

Public Member Functions

 TranxNodeAllocator (uint reserved_nodes)
 
 ~TranxNodeAllocator ()
 
TranxNodeallocate_node ()
 The pointer of the first node after 'last_node' in current_block is returned. More...
 
int free_all_nodes ()
 All nodes are freed. More...
 
int free_nodes_before (TranxNode *node)
 All Blocks before the given 'node' are free Block and moved into the rear of the Block link table. More...
 

Private Member Functions

int allocate_block ()
 Allocate a block and then assign it to current_block. More...
 
void free_block (Block *block)
 Free a given Block. More...
 
void free_blocks ()
 If there are some free Blocks and the total number of the Blocks in the Block link table is larger than the 'reserved_blocks', Some free Blocks will be freed until the total number of the Blocks is equal to the 'reserved_blocks' or there is only one free Block behind the 'current_block'. More...
 

Private Attributes

uint reserved_blocks
 
Blockfirst_block
 The 'first_block' is the head of the Block link table;. More...
 
Blocklast_block
 The 'last_block' is the rear of the Block link table;. More...
 
Blockcurrent_block
 current_block always points the Block in the Block link table in which the last allocated node is. More...
 
int last_node
 It always points to the last node which has been allocated in the current_block. More...
 
uint block_num
 How many Blocks are in the Block link table. More...
 

Detailed Description

This class provides memory allocating and freeing methods for TranxNode.

The main target is performance.

How to allocate a node

The pointer of the first node after 'last_node' in current_block is returned. current_block will move to the next free Block when all nodes of it are in use. A new Block is allocated and is put into the rear of the Block link table if no Block is free.

The list starts up empty (ie, there is no allocated Block).

After some nodes are freed, there probably are some free nodes before the sequence of the allocated nodes, but we do not reuse it. It is better to keep the allocated nodes are in the sequence, for it is more efficient for allocating and freeing TranxNode.

How to free nodes

There are two methods for freeing nodes. They are free_all_nodes and free_nodes_before.

'A Block is free' means all of its nodes are free.

free_nodes_before

As all allocated nodes are in the sequence, 'Before one node' means all nodes before given node in the same Block and all Blocks before the Block which containing the given node. As such, all Blocks before the given one ('node') are free Block and moved into the rear of the Block link table. The Block containing the given 'node', however, is not. For at least the given 'node' is still in use. This will waste at most one Block, but it is more efficient.

Constructor & Destructor Documentation

◆ TranxNodeAllocator()

TranxNodeAllocator::TranxNodeAllocator ( uint  reserved_nodes)
inline
Parameters
reserved_nodesThe number of reserved TranxNodes. It is used to set 'reserved_blocks' which can contain at least 'reserved_nodes' number of TranxNodes. When freeing memory, we will reserve at least reserved_blocks of Blocks not freed.

◆ ~TranxNodeAllocator()

TranxNodeAllocator::~TranxNodeAllocator ( )
inline

Member Function Documentation

◆ allocate_block()

int TranxNodeAllocator::allocate_block ( )
inlineprivate

Allocate a block and then assign it to current_block.

◆ allocate_node()

TranxNode * TranxNodeAllocator::allocate_node ( )
inline

The pointer of the first node after 'last_node' in current_block is returned.

current_block will move to the next free Block when all nodes of it are in use. A new Block is allocated and is put into the rear of the Block link table if no Block is free.

Returns
Return a TranxNode *, or NULL if an error occurred.

◆ free_all_nodes()

int TranxNodeAllocator::free_all_nodes ( )
inline

All nodes are freed.

Returns
Return 0, or 1 if an error occurred.

◆ free_block()

void TranxNodeAllocator::free_block ( Block block)
inlineprivate

Free a given Block.

Parameters
blockThe Block will be freed.

◆ free_blocks()

void TranxNodeAllocator::free_blocks ( )
inlineprivate

If there are some free Blocks and the total number of the Blocks in the Block link table is larger than the 'reserved_blocks', Some free Blocks will be freed until the total number of the Blocks is equal to the 'reserved_blocks' or there is only one free Block behind the 'current_block'.

◆ free_nodes_before()

int TranxNodeAllocator::free_nodes_before ( TranxNode node)
inline

All Blocks before the given 'node' are free Block and moved into the rear of the Block link table.

Parameters
nodeAll nodes before 'node' will be freed
Returns
Return 0, or 1 if an error occurred.

Member Data Documentation

◆ block_num

uint TranxNodeAllocator::block_num
private

How many Blocks are in the Block link table.

◆ current_block

Block* TranxNodeAllocator::current_block
private

current_block always points the Block in the Block link table in which the last allocated node is.

The Blocks before it are all in use and the Blocks after it are all free.

◆ first_block

Block* TranxNodeAllocator::first_block
private

The 'first_block' is the head of the Block link table;.

◆ last_block

Block* TranxNodeAllocator::last_block
private

The 'last_block' is the rear of the Block link table;.

◆ last_node

int TranxNodeAllocator::last_node
private

It always points to the last node which has been allocated in the current_block.

◆ reserved_blocks

uint TranxNodeAllocator::reserved_blocks
private

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