thread_safe_allocator< RawAllocator, Mutex > Class Template Reference
Allocator storage

## Detailed Description

template<class RawAllocator, class Mutex>
class foonathan::memory::thread_safe_allocator< RawAllocator, Mutex >

An alias template for allocator_storage using the direct_storage policy with a mutex.

It has a similar effect as allocator_adapter but performs synchronization. The `Mutex` will default to `std::mutex` if threading is supported, otherwise there is no default.

## Typedefs

using | allocator_type = typename StoragePolicy::allocator_type
using | storage_policy = direct_storage< RawAllocator >
using | mutex = Mutex
using | is_stateful = typename traits::is_stateful

## Member Functions

bool | is_composable () const noexcept
void * | allocate_node (std::size_t size, std::size_t alignment)
void * | allocate_array (std::size_t count, std::size_t size, std::size_t alignment)
void | 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
std::size_t | max_node_size () const
std::size_t | max_array_size () const
std::size_t | max_alignment () const
void * | try_allocate_node (std::size_t size, std::size_t alignment) noexcept
void * | try_allocate_array (std::size_t count, std::size_t size, std::size_t alignment) noexcept
bool | try_deallocate_node (void *ptr, 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
auto | get_allocator () noexcept -> decltype(std::declval< storage_policy >().get_allocator())
auto | get_allocator () const noexcept -> decltype(std::declval< const storage_policy >().get_allocator())
auto | lock () noexcept -> implementation_defined
auto | lock () const noexcept -> implementation_defined

## Related Symbols

template<class Mutex , class RawAllocator >
auto | make_thread_safe_allocator (RawAllocator &&allocator) -> thread_safe_allocator< typename std::decay< RawAllocator >::type, Mutex >

## Member Functions

## allocate_node()

void * allocate_node | ( | std::size_t | size,
|  | std::size_t | alignment
| ) |  |

inherited

Effects:
  Calls the function on the stored allocator. The `Mutex` will be locked during the operation.

## allocate_array()

void * allocate_array | ( | std::size_t | count,
|  | std::size_t | size,
|  | std::size_t | alignment
| ) |  |

inherited

Effects:
  Calls the function on the stored allocator. The `Mutex` will be locked during the operation.

## deallocate_node()

void deallocate_node | ( | void * | ptr,
|  | std::size_t | size,
|  | std::size_t | alignment
| ) |  |

noexcept inherited

Effects:
  Calls the function on the stored allocator. The `Mutex` will be locked during the operation.

## deallocate_array()

void deallocate_array | ( | void * | ptr,
|  | std::size_t | count,
|  | std::size_t | size,
|  | std::size_t | alignment
| ) |  |

noexcept inherited

Effects:
  Calls the function on the stored allocator. The `Mutex` will be locked during the operation.

## max_node_size()

std::size_t max_node_size | ( |  | ) | const

inherited

Effects:
  Calls the function on the stored allocator. The `Mutex` will be locked during the operation.

## max_array_size()

std::size_t max_array_size | ( |  | ) | const

inherited

Effects:
  Calls the function on the stored allocator. The `Mutex` will be locked during the operation.

## max_alignment()

std::size_t max_alignment | ( |  | ) | const

inherited

Effects:
  Calls the function on the stored allocator. The `Mutex` will be locked during the operation.

## try_allocate_node()

void * try_allocate_node | ( | std::size_t | size,
|  | std::size_t | alignment
| ) |  |

noexcept inherited

Effects:
  Calls the function on the stored composable allocator. The `Mutex` will be locked during the operation.

Requires:
  The allocator must be composable, i.e. is_composable() must return `true`.

Note
  This check is done at compile-time where possible, and at runtime in the case of type-erased storage.

## try_allocate_array()

void * try_allocate_array | ( | std::size_t | count,
|  | std::size_t | size,
|  | std::size_t | alignment
| ) |  |

noexcept inherited

Effects:
  Calls the function on the stored composable allocator. The `Mutex` will be locked during the operation.

Requires:
  The allocator must be composable, i.e. is_composable() must return `true`.

Note
  This check is done at compile-time where possible, and at runtime in the case of type-erased storage.

## try_deallocate_node()

bool try_deallocate_node | ( | void * | ptr,
|  | std::size_t | size,
|  | std::size_t | alignment
| ) |  |

noexcept inherited

Effects:
  Calls the function on the stored composable allocator. The `Mutex` will be locked during the operation.

Requires:
  The allocator must be composable, i.e. is_composable() must return `true`.

Note
  This check is done at compile-time where possible, and at runtime in the case of type-erased storage.

## try_deallocate_array()

bool try_deallocate_array | ( | void * | ptr,
|  | std::size_t | count,
|  | std::size_t | size,
|  | std::size_t | alignment
| ) |  |

noexcept inherited

Effects:
  Calls the function on the stored composable allocator. The `Mutex` will be locked during the operation.

Requires:
  The allocator must be composable, i.e. is_composable() must return `true`.

Note
  This check is done at compile-time where possible, and at runtime in the case of type-erased storage.

## get_allocator() [1/2]

auto get_allocator | ( |  | ) | -> decltype(std::declval<storage_policy>().get_allocator())

noexcept inherited

Effects:
  Forwards to the `StoragePolicy`.

Returns:
  Returns a reference to the stored allocator.

Note
  This does not lock the `Mutex`.

## get_allocator() [2/2]

auto get_allocator | ( |  | ) | const -> decltype(std::declval<const storage_policy>().get_allocator())

noexcept inherited

Effects:
  Forwards to the `StoragePolicy`.

Returns:
  Returns a reference to the stored allocator.

Note
  This does not lock the `Mutex`.

## lock() [1/2]

auto lock | ( |  | ) | -> implementation_defined

noexcept inherited

Returns:
  A proxy object that acts like a pointer to the stored allocator. It cannot be reassigned to point to another allocator object and only moving is supported, which is destructive. As long as the proxy object lives and is not moved from, the `Mutex` will be kept locked.

## lock() [2/2]

auto lock | ( |  | ) | const -> implementation_defined

noexcept inherited

Returns:
  A proxy object that acts like a pointer to the stored allocator. It cannot be reassigned to point to another allocator object and only moving is supported, which is destructive. As long as the proxy object lives and is not moved from, the `Mutex` will be kept locked.

## is_composable()

bool is_composable | ( |  | ) | const

noexcept inherited

Returns:
  Whether or not the stored allocator is composable, that is you can use the compositioning functions.

Note
  Due to type-erased allocators, this function can not be `constexpr`.

## Related Functions

## make_thread_safe_allocator()

template<class Mutex , class RawAllocator >

auto make_thread_safe_allocator | ( | RawAllocator && | allocator | ) | -> thread_safe_allocator<typename std::decay<RawAllocator>::type, Mutex>

related

Returns:
  A new thread_safe_allocator object created by forwarding to the constructor, specifying a certain mutex type.

Requires:
  It requires threading support from the implementation.