MySQL 9.1.0
Source Code Documentation
|
Aligned allocation routines which are instrumented through PFS (performance-schema). More...
#include <aligned_alloc.h>
Public Types | |
using | allocator_metadata = Aligned_alloc_metadata< uint32_t, uint32_t > |
using | pfs_metadata = PFS_metadata |
Static Public Member Functions | |
template<bool Zero_initialized> | |
static void * | alloc (std::size_t size, std::size_t alignment, pfs_metadata::pfs_memory_key_t key) |
Dynamically allocates storage of given size at the address aligned to the requested alignment. More... | |
static void | free (PFS_metadata::data_segment_ptr data) noexcept |
Releases storage dynamically allocated through Aligned_alloc_pfs::alloc(). More... | |
static allocator_metadata::meta_2_t | datalen (PFS_metadata::data_segment_ptr data) |
Returns the number of bytes requested to be allocated. More... | |
Static Private Member Functions | |
static void * | deduce (PFS_metadata::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< true > | |
static constexpr auto | is_pfs_instrumented_v |
Aligned allocation routines which are instrumented through PFS (performance-schema).
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_pfs::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_pfs, 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.
PFS-wise this allocation routine will be storing the information that PFS needs to do its own work:
Memory layout representation looks like the following:
^ ^ ^ ^ | | | | | | ------------------------— | | | | OWNER | DATALEN' | KEY | | | | ------------------------— | | | | | ptr returned by | | Aligned_alloc_impl | | | | ptr to be returned to call-site | will be pointing here
\ \ 0 \ \ alignof(max_align_t) - 1
VARLEN1 and ALIGNED-ALLOC-META are direct byproduct of Aligned_alloc_impl (and Aligned_alloc_metadata) layout and guarantees.
VARLEN1 is the leftover variable-length segment 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.
VARLEN1-OFFSET in ALIGNED-ALLOC-META segment encodes the offset to VARLEN1 segment which represents the original pointer obtained by underlying allocation Aligned_alloc_impl mechanism.
PFS-META, VARLEN2 and PFS-META-OFFSET are memory layout representation of PFS_metadata.
OWNER field encode the owning thread. DATALEN' field encodes total size of memory consumed and not only the size of the DATA segment. KEY field encodes the PFS/PSI key.
VARLEN2 is the leftover variable-length segment that specialized implementations can further make use of by deducing its size from the following formulae: requested_alignment - sizeof(PFS-META-OFFSET) - sizeof(PFS-META). In code that would be alignment - PFS_metadata::size. Not used by this implementation.
PFS-META-OFFSET is a field which allows us to recover the pointer to PFS-META segment from a pointer to DATA segment. Having a pointer to PFS-META segment allows us to deduce the VARLEN1-OFFSET field from ALIGNED-ALLOC-META segment which finally gives us a pointer obtained by the underlying allocation Aligned_alloc_impl mechanism.
using ut::detail::Aligned_alloc_pfs::allocator_metadata = Aligned_alloc_metadata<uint32_t, uint32_t> |
|
inlinestatic |
Dynamically allocates storage of given size 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. |
[in] | key | PSI memory key to be used for PFS memory instrumentation. |
|
inlinestatic |
Returns the number of bytes requested to be allocated.
[in] | data | Pointer to storage allocated through Aligned_alloc_pfs::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_pfs::alloc().
[in] | data | Pointer to storage allocated through Aligned_alloc_pfs::alloc() |