MySQL 8.0.31
Source Code Documentation
ut::allocator< T, Allocator_base > Class Template Reference

Allocator that allows std::* containers to manage their memory through ut::malloc* and ut::free library functions. More...

#include <ut0new.h>

Inheritance diagram for ut::allocator< T, Allocator_base >:
[legend]

Classes

struct  rebind
 

Public Types

using pointer = T *
 
using const_pointer = const T *
 
using reference = T &
 
using const_reference = const T &
 
using value_type = T
 
using size_type = size_t
 
using difference_type = ptrdiff_t
 

Public Member Functions

 allocator (PSI_memory_key key=mem_key_std)
 Default constructor. More...
 
 allocator (const allocator< T, Allocator_base > &)=default
 
allocator< T, Allocator_base > & operator= (const allocator< T, Allocator_base > &)=default
 
 allocator (allocator< T, Allocator_base > &&)=default
 
allocator< T, Allocator_base > & operator= (allocator< T, Allocator_base > &&)=default
 
 ~allocator ()=default
 
template<typename U >
 allocator (const allocator< U, Allocator_base > &other)
 Copy-construct a new instance of allocator with type T by using existing instance of allocator constructed with a different type U. More...
 
bool operator== (const ut::allocator< T, Allocator_base > &) const
 Equality of allocators instantiated with same types T. More...
 
bool operator!= (const ut::allocator< T, Allocator_base > &other) const
 Non-equality of allocators instantiated with same types T. More...
 
size_type max_size () const
 Return the maximum number of objects that can be allocated by this allocator. More...
 
pointer allocate (size_type n_elements, const_pointer hint=nullptr)
 Allocates chunk of memory that can hold n_elements objects of type T. More...
 
void deallocate (pointer ptr, size_type n_elements=0)
 Releases the memory allocated through ut::allocator<T>::allocate(). More...
 

Detailed Description

template<typename T, typename Allocator_base = std::conditional_t< ut::WITH_PFS_MEMORY, detail::allocator_base_pfs<T>, detail::allocator_base<T>>>
class ut::allocator< T, Allocator_base >

Allocator that allows std::* containers to manage their memory through ut::malloc* and ut::free library functions.

Main purpose of this custom allocator is to instrument all of the memory allocations and deallocations that are being done by std::* containers under the hood, and have them recorded through the PFS (memory) engine.

Other than std::* containers, this allocator is of course also suitable for use in any other allocator-aware containers and/or code.

Given that ut::malloc* and ut::free library functions already handle all the PFS and non-PFS implementation bits and pieces, this allocator is a mere wrapper around them.

Example which uses default PFS key (mem_key_std) to trace all std::vector allocations and deallocations: std::vector<int, ut::allocator<int>> vec; vec.push_back(...); ... vec.push_back(...);

Example which uses user-provided PFS key to trace std::vector allocations and deallocations: ut::allocator<int> allocator(some_other_psi_key); std::vector<int, ut::allocator<int>> vec(allocator); vec.push_back(...); ... vec.push_back(...);

Member Typedef Documentation

◆ const_pointer

template<typename T , typename Allocator_base = std::conditional_t< ut::WITH_PFS_MEMORY, detail::allocator_base_pfs<T>, detail::allocator_base<T>>>
using ut::allocator< T, Allocator_base >::const_pointer = const T *

◆ const_reference

template<typename T , typename Allocator_base = std::conditional_t< ut::WITH_PFS_MEMORY, detail::allocator_base_pfs<T>, detail::allocator_base<T>>>
using ut::allocator< T, Allocator_base >::const_reference = const T &

◆ difference_type

template<typename T , typename Allocator_base = std::conditional_t< ut::WITH_PFS_MEMORY, detail::allocator_base_pfs<T>, detail::allocator_base<T>>>
using ut::allocator< T, Allocator_base >::difference_type = ptrdiff_t

◆ pointer

template<typename T , typename Allocator_base = std::conditional_t< ut::WITH_PFS_MEMORY, detail::allocator_base_pfs<T>, detail::allocator_base<T>>>
using ut::allocator< T, Allocator_base >::pointer = T *

◆ reference

template<typename T , typename Allocator_base = std::conditional_t< ut::WITH_PFS_MEMORY, detail::allocator_base_pfs<T>, detail::allocator_base<T>>>
using ut::allocator< T, Allocator_base >::reference = T &

◆ size_type

template<typename T , typename Allocator_base = std::conditional_t< ut::WITH_PFS_MEMORY, detail::allocator_base_pfs<T>, detail::allocator_base<T>>>
using ut::allocator< T, Allocator_base >::size_type = size_t

◆ value_type

template<typename T , typename Allocator_base = std::conditional_t< ut::WITH_PFS_MEMORY, detail::allocator_base_pfs<T>, detail::allocator_base<T>>>
using ut::allocator< T, Allocator_base >::value_type = T

Constructor & Destructor Documentation

◆ allocator() [1/4]

template<typename T , typename Allocator_base = std::conditional_t< ut::WITH_PFS_MEMORY, detail::allocator_base_pfs<T>, detail::allocator_base<T>>>
ut::allocator< T, Allocator_base >::allocator ( PSI_memory_key  key = mem_key_std)
inlineexplicit

Default constructor.

Parameters
[in]keyperformance schema key.

◆ allocator() [2/4]

template<typename T , typename Allocator_base = std::conditional_t< ut::WITH_PFS_MEMORY, detail::allocator_base_pfs<T>, detail::allocator_base<T>>>
ut::allocator< T, Allocator_base >::allocator ( const allocator< T, Allocator_base > &  )
default

◆ allocator() [3/4]

template<typename T , typename Allocator_base = std::conditional_t< ut::WITH_PFS_MEMORY, detail::allocator_base_pfs<T>, detail::allocator_base<T>>>
ut::allocator< T, Allocator_base >::allocator ( allocator< T, Allocator_base > &&  )
default

◆ ~allocator()

template<typename T , typename Allocator_base = std::conditional_t< ut::WITH_PFS_MEMORY, detail::allocator_base_pfs<T>, detail::allocator_base<T>>>
ut::allocator< T, Allocator_base >::~allocator ( )
default

◆ allocator() [4/4]

template<typename T , typename Allocator_base = std::conditional_t< ut::WITH_PFS_MEMORY, detail::allocator_base_pfs<T>, detail::allocator_base<T>>>
template<typename U >
ut::allocator< T, Allocator_base >::allocator ( const allocator< U, Allocator_base > &  other)
inline

Copy-construct a new instance of allocator with type T by using existing instance of allocator constructed with a different type U.

Parameters
[in]otherthe allocator to copy from.

Member Function Documentation

◆ allocate()

template<typename T , typename Allocator_base = std::conditional_t< ut::WITH_PFS_MEMORY, detail::allocator_base_pfs<T>, detail::allocator_base<T>>>
pointer ut::allocator< T, Allocator_base >::allocate ( size_type  n_elements,
const_pointer  hint = nullptr 
)
inline

Allocates chunk of memory that can hold n_elements objects of type T.

Returned pointer is always valid. In case underlying allocation function was not able to fulfill the allocation request, this function will throw std::bad_alloc exception. After successful allocation, returned pointer must be passed back to ut::allocator<T>::deallocate() when no longer needed.

Parameters
[in]n_elementsnumber of elements
[in]hintpointer to a nearby memory location, not used by this implementation
Returns
pointer to the allocated memory

◆ deallocate()

template<typename T , typename Allocator_base = std::conditional_t< ut::WITH_PFS_MEMORY, detail::allocator_base_pfs<T>, detail::allocator_base<T>>>
void ut::allocator< T, Allocator_base >::deallocate ( pointer  ptr,
size_type  n_elements = 0 
)
inline

Releases the memory allocated through ut::allocator<T>::allocate().

Parameters
[in,out]ptrpointer to memory to free
[in]n_elementsnumber of elements allocated (unused)

◆ max_size()

template<typename T , typename Allocator_base = std::conditional_t< ut::WITH_PFS_MEMORY, detail::allocator_base_pfs<T>, detail::allocator_base<T>>>
size_type ut::allocator< T, Allocator_base >::max_size ( ) const
inline

Return the maximum number of objects that can be allocated by this allocator.

This number is somewhat lower for PFS-enabled builds because of extra few bytes needed for PFS.

◆ operator!=()

template<typename T , typename Allocator_base = std::conditional_t< ut::WITH_PFS_MEMORY, detail::allocator_base_pfs<T>, detail::allocator_base<T>>>
bool ut::allocator< T, Allocator_base >::operator!= ( const ut::allocator< T, Allocator_base > &  other) const
inline

Non-equality of allocators instantiated with same types T.

◆ operator=() [1/2]

template<typename T , typename Allocator_base = std::conditional_t< ut::WITH_PFS_MEMORY, detail::allocator_base_pfs<T>, detail::allocator_base<T>>>
allocator< T, Allocator_base > & ut::allocator< T, Allocator_base >::operator= ( allocator< T, Allocator_base > &&  )
default

◆ operator=() [2/2]

template<typename T , typename Allocator_base = std::conditional_t< ut::WITH_PFS_MEMORY, detail::allocator_base_pfs<T>, detail::allocator_base<T>>>
allocator< T, Allocator_base > & ut::allocator< T, Allocator_base >::operator= ( const allocator< T, Allocator_base > &  )
default

◆ operator==()

template<typename T , typename Allocator_base = std::conditional_t< ut::WITH_PFS_MEMORY, detail::allocator_base_pfs<T>, detail::allocator_base<T>>>
bool ut::allocator< T, Allocator_base >::operator== ( const ut::allocator< T, Allocator_base > &  ) const
inline

Equality of allocators instantiated with same types T.


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