30#ifndef detail_ut_aligned_alloc_h
31#define detail_ut_aligned_alloc_h
112 template <
bool Zero_initialized>
113 static inline std::pair<data_segment_ptr, std::size_t>
alloc(
114 std::size_t size, std::size_t alignment)
noexcept {
117 assert(alignment >
alignof(std::max_align_t));
205 const std::size_t data_len = size + alignment;
206 void *
mem = Alloc_fn::alloc<Zero_initialized>(data_len);
216 reinterpret_cast<void *
>(
reinterpret_cast<std::uintptr_t
>(
mem) + 1);
217 std::size_t
buf_size = data_len - 1;
223 [[maybe_unused]]
auto ret = std::align(alignment, size,
buf,
buf_size);
224 assert(ret !=
nullptr);
226 return {
buf,
reinterpret_cast<std::uintptr_t
>(
buf) -
227 reinterpret_cast<std::uintptr_t
>(
mem)};
273template <
typename Meta_1_type,
typename Meta_2_type>
289 "Aligned_alloc_impl provides a strong guarantee "
290 "of only up to Aligned_alloc_impl::metadata_size bytes.");
295 std::size_t meta_1_v)
noexcept {
296 assert(meta_1_v <= std::numeric_limits<meta_1_t>::max());
302 std::size_t meta_2_v)
noexcept {
303 assert(meta_2_v <= std::numeric_limits<meta_2_t>::max());
328 return reinterpret_cast<meta_1_t *
>(data) - 1;
404 template <
bool Zero_initialized>
405 static inline void *
alloc(std::size_t size, std::size_t alignment) {
406 auto ret = Aligned_alloc_impl::alloc<Zero_initialized>(size, alignment);
466 return reinterpret_cast<void *
>(
reinterpret_cast<std::uintptr_t
>(data) -
573 template <
bool Zero_initialized>
574 static inline void *
alloc(std::size_t size, std::size_t alignment,
583 const auto total_len = size + metadata_len;
585 Aligned_alloc_impl::alloc<Zero_initialized>(total_len, alignment);
586 if (
unlikely(!ret.first))
return nullptr;
595#ifdef HAVE_PSI_MEMORY_INTERFACE
601 const auto datalen = total_len + ret.second;
616 return static_cast<uint8_t *
>(ret.first) + metadata_len;
628#ifdef HAVE_PSI_MEMORY_INTERFACE
668 return reinterpret_cast<void *
>(
reinterpret_cast<std::uintptr_t
>(pfs_meta) -
676template <
bool Pfs_memory_instrumentation_on>
688template <
bool Pfs_memory_instrumentation_on>
695template <
typename Impl>
697 template <
bool Zero_initialized,
typename T = Impl>
700 return Impl::template alloc<Zero_initialized>(size, alignment,
key);
702 template <
bool Zero_initialized,
typename T = Impl>
705 return Impl::template alloc<Zero_initialized>(size, alignment);
708 static inline size_t datalen(
void *ptr) {
return Impl::datalen(ptr); }
#define PSI_MEMORY_CALL(M)
Definition: psi_memory.h:35
static char buf[MAX_BUF]
Definition: conf_to_src.cc:72
constexpr DWORD buf_size
Definition: create_def.cc:227
unsigned int PSI_memory_key
Instrumented memory key.
Definition: psi_memory_bits.h:48
#define free(A)
Definition: lexyy.cc:915
Header for compiler-dependent features.
constexpr bool likely(bool expr)
Definition: my_compiler.h:56
constexpr bool unlikely(bool expr)
Definition: my_compiler.h:57
Definition: buf0block_hint.cc:29
Definition: ut0tuple.h:56
static PSI_memory_key memory_alloc(PSI_memory_key, size_t, struct PSI_thread **owner)
Definition: psi_memory_v2_empty.cc:34
static void memory_free(PSI_memory_key, size_t, struct PSI_thread *)
Definition: psi_memory_v2_empty.cc:52
constexpr size_t calc_align(size_t n, size_t m)
Calculates the smallest multiple of m that is not smaller than n when m is a power of two.
Definition: helper.h:44
typename select_alloc_impl< Pfs_memory_instrumentation_on >::type select_alloc_impl_t
Just a small helper type which saves us some keystrokes.
Definition: aligned_alloc.h:690
This file contains a set of libraries providing overloads for regular dynamic allocation routines whi...
Definition: aligned_alloc.h:47
required string key
Definition: replication_asynchronous_connection_failover.proto:59
required string type
Definition: replication_group_member_actions.proto:33
static MEM_ROOT mem
Definition: sql_servers.cc:99
Implementation bits and pieces for PFS metadata handling.
Small wrapper which utilizes SFINAE to dispatch the call to appropriate aligned allocator implementat...
Definition: aligned_alloc.h:696
static void free(void *ptr)
Definition: aligned_alloc.h:707
static size_t datalen(void *ptr)
Definition: aligned_alloc.h:708
static std::enable_if< T::is_pfs_instrumented_v, void * >::type alloc(size_t size, size_t alignment, PSI_memory_key key)
Definition: aligned_alloc.h:699
static std::enable_if<!T::is_pfs_instrumented_v, void * >::type alloc(size_t size, size_t alignment, PSI_memory_key)
Definition: aligned_alloc.h:704
Definition: aligned_alloc.h:50
static constexpr uint32_t metadata_size
Block of memory returned by this functor will have an additional (metadata) segment (at no additional...
Definition: aligned_alloc.h:57
static void free(void *ptr) noexcept
Releases storage allocated through alloc().
Definition: aligned_alloc.h:235
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.
Definition: aligned_alloc.h:113
void * data_segment_ptr
Alias that we will be using to denote ptr to DATA segment.
Definition: aligned_alloc.h:60
Aligned allocation routines which are instrumented through PFS (performance-schema).
Definition: aligned_alloc.h:560
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.
Definition: aligned_alloc.h:574
static void free(PFS_metadata::data_segment_ptr data) noexcept
Releases storage dynamically allocated through Aligned_alloc_pfs::alloc().
Definition: aligned_alloc.h:625
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 whic...
Definition: aligned_alloc.h:661
static allocator_metadata::meta_2_t datalen(PFS_metadata::data_segment_ptr data)
Returns the number of bytes requested to be allocated.
Definition: aligned_alloc.h:650
Aligned allocation routines.
Definition: aligned_alloc.h:393
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.
Definition: aligned_alloc.h:405
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 whic...
Definition: aligned_alloc.h:461
static allocator_metadata::meta_2_t datalen(Aligned_alloc_impl::data_segment_ptr data)
Returns the number of bytes requested to be allocated.
Definition: aligned_alloc.h:450
static void free(Aligned_alloc_impl::data_segment_ptr data) noexcept
Releases storage dynamically allocated through Aligned_alloc::alloc().
Definition: aligned_alloc.h:436
static void free(void *ptr)
Definition: allocator_traits.h:104
Simple allocator traits.
Definition: allocator_traits.h:81
Simple utility metafunction which selects appropriate allocator variant (implementation) depending on...
Definition: aligned_alloc.h:677