Template Class Allocator

Class Documentation

template<typename PrimaryAllocator, typename Arena, typename FallbackAllocator, typename LockingPolicy>
class wrench::Allocator

The Allocator type is a simple implementation which allows an allocator to be composed of other allocators, to create allocators with useful properties for different contexts.

The allocator will always try to allocate from the primary allocator, unless the primary allocation fails, in which case it will allocate from the fallback allocator. It is a good idea to always set the fallback allocator to the AlignedHeapAllocator, so that allocation will always fallback to the system allocator.

All allocation and free operations are locked using the locking policy provided. Since the default is to not lock, the allocations are not thread safe unless a locking policy is provided which does actually lock.

The

Allocator type is a simple implementation which allows an allocator to be composed of other allocators, to create allocators with useful properties for different contexts.
Template Parameters
  • PrimaryAllocator: The type of the primary allocator.

  • Arena: The type of the arena for the allocator.

  • FallbackAllocator: The type of the fallback allocator.

  • LockingPolicy: The type of the locking policy.

The allocator will always try to allocate from the primary allocator, unless the primary allocation fails, in which case it will allocate from the fallback allocator.

All allocation and free operations are locked, using the locking policy provided. The default locking policy is to not lock.

Template Parameters
  • PrimaryAllocator: The type of the primary allocator.

  • Arena: The type of the arena for the allocator.

  • FallbackAllocator: The type of the fallback allocator.

  • LockingPolicy: The type of the locking policy.

Public Types

using Guard = std::lock_guard<LockingPolicy>

Defines the type of the lock guard.

Public Functions

template<typename ...Args>
Allocator(size_t size, Args&&... args)

Constructor which sets the size of the arena, if the arena requires a size, and forwards the args to the primary allocator.

If the arena has a constant size, then it will be created with that size, and size will be ignored.

Parameters
  • size: The size of the arena, in bytes.

  • args: The arguments fro the primary allocator.

Template Parameters
  • Args: The types of arguments for the primary allocator.

~Allocator() noexcept = default

Default destructor composed allocators know how to clean themselves up.

Allocator(Allocator &&other) noexcept = default

Move constructor, defaulted.

Parameters
  • other: The other allocator to move into this one.

auto operator= (Allocator &&other) noexcept -> Allocator &=default

Move assignment, defaulted.

Parameters
  • other: The other allocator to move into this one.

Allocator(const Allocator&) = delete

Copy constructor deleted, allocator can’t be copied.

auto operator= (const Allocator &) -> Allocator &=delete

Copy assignment deleted, allocator can’t be copied.

auto alloc (size_t size, size_t alignment=alignof(std::max_align_t)) noexcept -> void *

Allocates size bytes of memory with align alignment.

Parameters
  • size: The size of the memory to allocate.

  • alignment: The alignment of the allocation.

auto free (void *ptr) noexcept -> void

Frees the memory pointed to by ptr.

Parameters
  • ptr: The pointer to the memory to free.

auto free (void *ptr, size_t size) noexcept -> void

Frees the memory pointed to by \ptr, with a size of size.

Parameters
  • ptr: The pointer to the memory to free.

  • size: The size of the memory to free.

auto reset () noexcept -> void

Resets the primary and fallback allocators.

template<typename T, typename... Args> auto create (Args &&... args) noexcept -> T *

Allocates and constructs an object of type T. If this is used, then destroy should be used to destruct and free the object, rather than free. If the fallback allocator fails, this will return a nullptr.

Parameters
  • args: The arguments for constructing the objects.

Template Parameters
  • T: The type of the object to allocate.

  • Args: The types of the arguments for constructing T.

template<typename T> auto recycle (T *ptr) noexcept -> void

Recycles the object pointed to by ptr, destructing it and then releasing the memory back to the allocator.

Parameters
  • ptr: A pointer to the object to destroy.

Template Parameters
  • T: The type of the object.

Public Static Attributes

constexpr bool contexpr_arena_size = Arena::contexpr_size

Returns true if the arena has a contexpr size.