MySQL 8.0.30
Source Code Documentation
ut::detail::Aligned_alloc Struct Reference

Aligned allocation routines. More...

#include <aligned_alloc.h>

Inheritance diagram for ut::detail::Aligned_alloc:
[legend]

Public Types

using allocator_metadata = Aligned_alloc_metadata< uint32_t, uint32_t >
 

Static Public Member Functions

template<bool Zero_initialized>
static void * alloc (std::size_t size, std::size_t alignment)
 Dynamically allocates storage of given size and at the address aligned to the requested alignment. More...
 
static void free (Aligned_alloc_impl::data_segment_ptr data) noexcept
 Releases storage dynamically allocated through Aligned_alloc::alloc(). More...
 
static allocator_metadata::meta_2_t datalen (Aligned_alloc_impl::data_segment_ptr data)
 Returns the number of bytes requested to be allocated. More...
 

Static Private Member Functions

static void * deduce (Aligned_alloc_impl::data_segment_ptr data) noexcept
 Helper function which deduces the original pointer returned by Aligned_alloc_impl from a pointer which is passed to us by the call-site. More...
 

Additional Inherited Members

- Static Public Attributes inherited from ut::detail::allocator_traits< false >
static constexpr auto is_pfs_instrumented_v
 

Detailed Description

Aligned allocation routines.

They're implemented in terms of Aligned_alloc_impl (and Aligned_alloc_metadata), and given the guarantees it provides, Aligned_alloc::alloc() is able to encode offset and requested allocation datalen into the metadata section without sacrificing memory or making the implementation or end usage more complex.

Serializing offset into the metadata is what will enable Aligned_alloc::free() to later on recover original pointer returned by the underlying Aligned_alloc_impl allocation mechanism (std::malloc, std::calloc) and consequently be able to appropriately release it (std::free).

Serializing requested allocation datalen into the metadata is what will enable higher-kinded functions, implemented on top of Aligned_alloc, to take necessary actions such as cleaning up the resources by invoking appropriate number of destructors of non-trivially-destructible types. Otherwise, this would create a burden on end users by having to remember and carry the array size all around the code. This is equivalent to what we find in other standard implementations. For example, new int x[10] is always released without passing the array size: delete[] x; The same holds with this design.

Memory layout representation looks like the following:


| VARLEN | ALIGNED-ALLOC-META | ... DATA ... |

^ ^ | | | | | ptr returned by Aligned_alloc_impl

|

| DATALEN | VARLEN-OFFSET |

\ \ 0 \ \ alignof(max_align_t) - 1

VARLEN and ALIGNED-ALLOC-META are direct byproduct of Aligned_alloc_impl layout and guarantees.

VARLEN is the leftover variable segment of bytes that specialized implementations can further make use of by deducing its size from returned offset. Not used by this implementation.

DATALEN field in ALIGNED-ALLOC-META segment encodes the total length of DATA segment, which is the actual allocation size that client code has requested.

VARLEN-OFFSET in ALIGNED-ALLOC-META segment encodes the offset to VARLEN segment which represents the original pointer obtained by underlying allocation Aligned_alloc_impl mechanism.

Member Typedef Documentation

◆ allocator_metadata

Member Function Documentation

◆ alloc()

template<bool Zero_initialized>
static void * ut::detail::Aligned_alloc::alloc ( std::size_t  size,
std::size_t  alignment 
)
inlinestatic

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

Parameters
[in]sizeSize of storage (in bytes) requested to be allocated.
[in]alignmentAlignment requirement for storage to be allocated.
Returns
Pointer to the allocated storage. nullptr if dynamic storage allocation failed.

◆ datalen()

static allocator_metadata::meta_2_t ut::detail::Aligned_alloc::datalen ( Aligned_alloc_impl::data_segment_ptr  data)
inlinestatic

Returns the number of bytes requested to be allocated.

Parameters
[in]dataPointer to storage allocated through Aligned_alloc::alloc()
Returns
Number of bytes.

◆ deduce()

static void * ut::detail::Aligned_alloc::deduce ( Aligned_alloc_impl::data_segment_ptr  data)
inlinestaticprivatenoexcept

Helper function which deduces the original pointer returned by Aligned_alloc_impl from a pointer which is passed to us by the call-site.

◆ free()

static void ut::detail::Aligned_alloc::free ( Aligned_alloc_impl::data_segment_ptr  data)
inlinestaticnoexcept

Releases storage dynamically allocated through Aligned_alloc::alloc().

Parameters
[in]dataPointer to storage allocated through Aligned_alloc::alloc()

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