MySQL 9.1.0
Source Code Documentation
temptable::Chunk Class Reference

Chunk is an abstraction with the purpose of representing a smallest logical memory-unit within the Block. More...

#include <chunk.h>

Public Types

using metadata_type = uintptr_t
 Type that we will be using for storing metadata information. More...
 

Public Member Functions

 Chunk (uint8_t *offset, size_t new_offset) noexcept
 Constructor which Block will use to create a fresh Chunk object at the given memory-offset. More...
 
 Chunk (void *data) noexcept
 Constructor which Block will use to re-create Chunk object from user-provided pointer which points to the data section of already existing Chunk in memory. More...
 
uint8_t * block () const
 Deduce the memory-address of belonging Block. More...
 
size_t offset () const
 Get the Chunk offset relative to the start of belonging Block. More...
 
uint8_t * data () const
 Get the pointer to the data section which will be provided to the end-user. More...
 

Static Public Member Functions

static size_t size_hint (size_t n_bytes)
 For given size, how much memory will be occupied by the Chunk. More...
 

Static Public Attributes

static constexpr size_t METADATA_SIZE = sizeof(Chunk::metadata_type)
 Chunk metadata size. More...
 

Static Private Member Functions

static Chunk::metadata_typechunk_offset_ptr (uint8_t *chunk)
 Deduce a pointer to the offset of given Chunk. More...
 
static uint8_t * chunk_data_ptr (uint8_t *chunk)
 Deduce a pointer to the data payload of given Chunk. More...
 

Private Attributes

uint8_t * m_offset
 A pointer to the actual memory-location where Chunk is located at. More...
 

Detailed Description

Chunk is an abstraction with the purpose of representing a smallest logical memory-unit within the Block.

Block allocations and deallocations are served in Chunks.

Chunk structure is:

  • bytes [0, 7]: 8 bytes that designate the relative offset of the chunk from the start of the belonging block. This is used in order to be able to deduce the block start from a given chunk.
  • bytes [8, chunk size): actual user data, pointer to this is returned to the user after a successful allocation request.

As it can be seen, Chunk doesn't hold almost any information (e.g. its size) but merely an offset relative to the Block address it belongs to. That's what it enables Block to implement allocations and deallocations in constant-time.

Part of the Chunk contract is to have its metadata properly aligned in memory. Given that this memory is provided by the Block, Chunk implements debug-asserts to actually check if this condition has been met. If that was not the case, then accessing unaligned memory addresses would:

  1. Incur performance penalty cost on architectures which can handle misaligned memory access (e.g. x86).
  2. Result with a CPU trap (exception) on architectures which cannot handle misaligned memory access (e.g. SPARC).

OTOH, checking if Chunk user data is properly aligned is not possible from this context because actual data-type is not known to a Chunk. This check however shall be implemented in the context where the type is known (e.g. Allocator)

Member Typedef Documentation

◆ metadata_type

Type that we will be using for storing metadata information.

Constructor & Destructor Documentation

◆ Chunk() [1/2]

temptable::Chunk::Chunk ( uint8_t *  offset,
size_t  new_offset 
)
inlinenoexcept

Constructor which Block will use to create a fresh Chunk object at the given memory-offset.

[in] Pointer to the actual memory-location where Chunk will be located at. [in] Offset relative to address of a Block that is creating this Chunk.

◆ Chunk() [2/2]

temptable::Chunk::Chunk ( void *  data)
inlineexplicitnoexcept

Constructor which Block will use to re-create Chunk object from user-provided pointer which points to the data section of already existing Chunk in memory.

This pointer is returned to the user upon every Chunk allocation.

[in] Pointer to the data section of existing Chunk.

Member Function Documentation

◆ block()

uint8_t * temptable::Chunk::block ( ) const
inline

Deduce the memory-address of belonging Block.

Returns
Memory-address of a Block this Chunk belongs to.

◆ chunk_data_ptr()

uint8_t * temptable::Chunk::chunk_data_ptr ( uint8_t *  chunk)
inlinestaticprivate

Deduce a pointer to the data payload of given Chunk.

[in] Pointer to the first memory location (m_offset) which represents the chunk.

Returns
Pointer to the memory location which represents the chunk data (payload).

◆ chunk_offset_ptr()

Chunk::metadata_type * temptable::Chunk::chunk_offset_ptr ( uint8_t *  chunk)
inlinestaticprivate

Deduce a pointer to the offset of given Chunk.

[in] Pointer to the first memory location (m_offset) which represents the chunk.

Returns
Pointer to the memory location which represents the chunk offset.

◆ data()

uint8_t * temptable::Chunk::data ( ) const
inline

Get the pointer to the data section which will be provided to the end-user.

Returns
Pointer to memory which will be used by the end-user.

◆ offset()

size_t temptable::Chunk::offset ( ) const
inline

Get the Chunk offset relative to the start of belonging Block.

Returns
Offset relative to the start of belonging Block.

◆ size_hint()

size_t temptable::Chunk::size_hint ( size_t  n_bytes)
inlinestatic

For given size, how much memory will be occupied by the Chunk.

This calculation takes into account both the metadata and data payload.

[in] Data payload size in bytes.

Returns
Size Chunk will occupy for given n_bytes.

Member Data Documentation

◆ m_offset

uint8_t* temptable::Chunk::m_offset
private

A pointer to the actual memory-location where Chunk is located at.

◆ METADATA_SIZE

constexpr size_t temptable::Chunk::METADATA_SIZE = sizeof(Chunk::metadata_type)
staticconstexpr

Chunk metadata size.

As described, there is only 1 element.


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