MySQL 8.0.40
Source Code Documentation
|
Aligned allocation routines. More...
#include <aligned_alloc.h>
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 |
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:
^ ^ | | | | | ptr returned by Aligned_alloc_impl
\ \ 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.
using ut::detail::Aligned_alloc::allocator_metadata = Aligned_alloc_metadata<uint32_t, uint32_t> |
|
inlinestatic |
Dynamically allocates storage of given size and at the address aligned to the requested alignment.
[in] | size | Size of storage (in bytes) requested to be allocated. |
[in] | alignment | Alignment requirement for storage to be allocated. |
|
inlinestatic |
Returns the number of bytes requested to be allocated.
[in] | data | Pointer to storage allocated through Aligned_alloc::alloc() |
|
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.
|
inlinestaticnoexcept |
Releases storage dynamically allocated through Aligned_alloc::alloc().
[in] | data | Pointer to storage allocated through Aligned_alloc::alloc() |