memory
Typedefs | Member Functions | Related Functions
tracked_allocator< Tracker, RawAllocator > Class Template Reference

Detailed Description

template<class Tracker, class RawAllocator>
class foonathan::memory::tracked_allocator< Tracker, RawAllocator >

A RawAllocator adapter that tracks another allocator using a tracker.

It wraps another RawAllocator and calls the tracker function before forwarding to it. The class can then be used anywhere a RawAllocator is required and the memory usage will be tracked.
If the RawAllocator uses deeply_tracked_block_allocator as BlockAllocator, it will also track growth and shrinking of the allocator.

Typedefs

using allocator_type = typename allocator_traits< RawAllocator >::allocator_type
 
using tracker = Tracker
 
using is_stateful = std::integral_constant< bool, traits::is_stateful::value||!std::is_empty< Tracker >::value >
 

Member Functions

 ~tracked_allocator () noexcept
 
void * allocate_node (std::size_t size, std::size_t alignment)
 
void * try_allocate_node (std::size_t size, std::size_t alignment) noexcept
 
void * allocate_array (std::size_t count, std::size_t size, std::size_t alignment)
 
void * try_allocate_array (std::size_t count, std::size_t size, std::size_t alignment) noexcept
 
void deallocate_node (void *ptr, std::size_t size, std::size_t alignment) noexcept
 
bool try_deallocate_node (void *ptr, std::size_t size, std::size_t alignment) noexcept
 
void deallocate_array (void *ptr, std::size_t count, std::size_t size, std::size_t alignment) noexcept
 
bool try_deallocate_array (void *ptr, std::size_t count, std::size_t size, std::size_t alignment) noexcept
 
 tracked_allocator (tracker t={}) noexcept
 
 tracked_allocator (tracker t, allocator_type &&allocator) noexcept
 
 tracked_allocator (tracked_allocator &&other) noexcept
 
tracked_allocatoroperator= (tracked_allocator &&other) noexcept
 
std::size_t max_node_size () const
 
std::size_t max_array_size () const
 
std::size_t max_alignment () const
 
allocator_type & get_allocator () noexcept
 
const allocator_type & get_allocator () const noexcept
 
tracker & get_tracker () noexcept
 
const tracker & get_tracker () const noexcept
 

Related Functions

template<class Tracker , class RawAllocator >
auto make_tracked_allocator (Tracker t, RawAllocator &&alloc) -> tracked_allocator< Tracker, typename std::decay< RawAllocator >::type >
 

Constructors

◆ tracked_allocator() [1/3]

tracked_allocator ( tracker  t = {})
explicitnoexcept
Effects:
Creates it by giving it a tracker and the tracked RawAllocator. It will embed both objects.
Note
This will never call the Tracker::on_allocator_growth() function.

◆ tracked_allocator() [2/3]

tracked_allocator ( tracker  t,
allocator_type &&  allocator 
)
noexcept
Effects:
Creates it by giving it a tracker and the tracked RawAllocator. It will embed both objects.
Note
This will never call the Tracker::on_allocator_growth() function.

◆ ~tracked_allocator()

~tracked_allocator ( )
noexcept
Effects:
Destroys both tracker and allocator.
Note
This will never call the Tracker::on_allocator_shrinking() function.

◆ tracked_allocator() [3/3]

tracked_allocator ( tracked_allocator< Tracker, RawAllocator > &&  other)
noexcept
Effects:
Moving moves both the tracker and the allocator.

Member Functions

◆ operator=()

tracked_allocator & operator= ( tracked_allocator< Tracker, RawAllocator > &&  other)
noexcept
Effects:
Moving moves both the tracker and the allocator.

◆ allocate_node()

void * allocate_node ( std::size_t  size,
std::size_t  alignment 
)
Effects:
Calls Tracker::on_node_allocation() and forwards to the allocator. If a growth occurs and the allocator is deeply tracked, also calls Tracker::on_allocator_growth().
Returns:
The result of allocate_node()

◆ try_allocate_node()

void * try_allocate_node ( std::size_t  size,
std::size_t  alignment 
)
noexcept
Effects:
Calls the composable node allocation function. If allocation was successful, also calls Tracker::on_node_allocation().
Returns:
The result of try_allocate_node().

◆ allocate_array()

void * allocate_array ( std::size_t  count,
std::size_t  size,
std::size_t  alignment 
)
Effects:
Calls Tracker::on_array_allocation() and forwards to the allocator. If a growth occurs and the allocator is deeply tracked, also calls Tracker::on_allocator_growth().
Returns:
The result of allocate_array()

◆ try_allocate_array()

void * try_allocate_array ( std::size_t  count,
std::size_t  size,
std::size_t  alignment 
)
noexcept
Effects:
Calls the composable array allocation function. If allocation was succesful, also calls Tracker::on_array_allocation().
Returns:
The result of try_allocate_array().

◆ deallocate_node()

void deallocate_node ( void *  ptr,
std::size_t  size,
std::size_t  alignment 
)
noexcept
Effects:
Calls Tracker::on_node_deallocation() and forwards to the allocator's deallocate_node(). If shrinking occurs and the allocator is deeply tracked, also calls Tracker::on_allocator_shrinking().

◆ try_deallocate_node()

bool try_deallocate_node ( void *  ptr,
std::size_t  size,
std::size_t  alignment 
)
noexcept
Effects:
Calls the composable node deallocation function. If it was succesful, also calls Tracker::on_node_deallocation().
Returns:
The result of try_deallocate_node().

◆ deallocate_array()

void deallocate_array ( void *  ptr,
std::size_t  count,
std::size_t  size,
std::size_t  alignment 
)
noexcept
Effects:
Calls Tracker::on_array_deallocation() and forwards to the allocator's deallocate_array(). If shrinking occurs and the allocator is deeply tracked, also calls Tracker::on_allocator_shrinking().

◆ try_deallocate_array()

bool try_deallocate_array ( void *  ptr,
std::size_t  count,
std::size_t  size,
std::size_t  alignment 
)
noexcept
Effects:
Calls the composable array deallocation function. If it was succesful, also calls Tracker::on_array_deallocation().
Returns:
The result of try_deallocate_array().

◆ max_node_size()

std::size_t max_node_size ( ) const
Returns:
The result of the corresponding function on the wrapped allocator.

◆ max_array_size()

std::size_t max_array_size ( ) const
Returns:
The result of the corresponding function on the wrapped allocator.

◆ max_alignment()

std::size_t max_alignment ( ) const
Returns:
The result of the corresponding function on the wrapped allocator.

◆ get_allocator() [1/2]

allocator_type & get_allocator ( )
noexcept
Returns:
A (const) reference to the wrapped allocator.

◆ get_allocator() [2/2]

const allocator_type & get_allocator ( ) const
noexcept
Returns:
A (const) reference to the wrapped allocator.

◆ get_tracker() [1/2]

tracker & get_tracker ( )
noexcept
Returns:
A (const) reference to the tracker.

◆ get_tracker() [2/2]

const tracker & get_tracker ( ) const
noexcept
Returns:
A (const) reference to the tracker.

Related Functions

◆ make_tracked_allocator()

auto make_tracked_allocator ( Tracker  t,
RawAllocator &&  alloc 
) -> tracked_allocator<Tracker, typename std::decay<RawAllocator>::type>
related
Effects:
Takes a RawAllocator and wraps it with a tracker.
Returns:
A tracked_allocator with the corresponding parameters forwarded to the constructor.