MySQL 8.3.0
Source Code Documentation
ut::detail::Aligned_alloc_impl Struct Reference

#include <aligned_alloc.h>

Public Types

using data_segment_ptr = void *
 Alias that we will be using to denote ptr to DATA segment. More...
 

Static Public Member Functions

template<bool Zero_initialized>
static std::pair< data_segment_ptr, std::size_t > alloc (std::size_t size, std::size_t alignment) noexcept
 Dynamically allocates storage of given size and at the address aligned to the requested alignment. More...
 
static void free (void *ptr) noexcept
 Releases storage allocated through alloc(). More...
 

Static Public Attributes

static constexpr uint32_t metadata_size = alignof(max_align_t)
 Block of memory returned by this functor will have an additional (metadata) segment (at no additional cost of higher memory consumption) which is guaranteed to be this big, and which can be used to store whatever arbitrary data. More...
 

Member Typedef Documentation

◆ data_segment_ptr

Alias that we will be using to denote ptr to DATA segment.

Member Function Documentation

◆ alloc()

template<bool Zero_initialized>
static std::pair< data_segment_ptr, std::size_t > ut::detail::Aligned_alloc_impl::alloc ( std::size_t  size,
std::size_t  alignment 
)
inlinestaticnoexcept

Dynamically allocates storage of given size and at the address aligned to the requested alignment.

It is guaranteed that storage allocated by this functor is always (size + alignment) big and that there is always alignof(std::max_align_t) spare bytes within that segment which can be freely used. This means that pointer which is returned by this function can always be safely reversed by alignof(std::max_align_t) bytes and hence make this sub-segment accessible to any subsequent implementation.

Reversing the pointer for a value which is bigger than alignof(std::max_align_t) bytes is in certain cases possible and can be checked by inspecting the returned offset value. Some more specialized implementations can take advantage of that fact too.

This property is very important because it can be taken advantage of by other implementations so they are able to store whatever metadata they would like into this segment of alignof(std::max_align_t) bytes, or FWIW (pointer - offset) bytes in more specialized cases.

For example, let's say that size=10, alignment=32, and underlying allocation function used by this function implementation returns a pointer at address 128. This address must be a multiple of alignof(std::max_align_t) which in this example is 16.


| VARLEN | META | DATA |

128 144 160 170

DATA is an actual data which has been requested with given size (10) and alignment (32). META is the alignof(std::max_align_t) segment that can always be freely used by other implementations. VARLEN is the leftover variable-length segment that specialized implementations can further make use of by deducing its size from returned offset.

See Aligned_alloc and Aligned_alloc_arr implementations to see an example of how META segment can be used.

Parameters
[in]sizeSize of storage (in bytes) requested to be allocated.
[in]alignmentAlignment requirement for storage to be allocated. Must be power of two and larger than alignof(std::max_align_t).
Returns
{pointer, offset} where pointer is a pointer to dynamically allocated storage aligned to requested alignment, and offset is distance in bytes from pointer which has been originally returned by underlying dynamic allocation function (e.g. std::malloc). Otherwise {nullptr, 0} if dynamic storage allocation failed.

◆ free()

static void ut::detail::Aligned_alloc_impl::free ( void *  ptr)
inlinestaticnoexcept

Releases storage allocated through alloc().

Parameters
[in]ptrdata_segment_pointer decreased by offset bytes. Both are obtained through alloc().

Member Data Documentation

◆ metadata_size

constexpr uint32_t ut::detail::Aligned_alloc_impl::metadata_size = alignof(max_align_t)
staticconstexpr

Block of memory returned by this functor will have an additional (metadata) segment (at no additional cost of higher memory consumption) which is guaranteed to be this big, and which can be used to store whatever arbitrary data.

See Aligned_alloc and Aligned_alloc_arr for exemplary usages of it.


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