carb::Allocator

Defined in carb/Memory.h

Structs

template<class T, class Align = std::integral_constant<size_t, 0>>
class Allocator

A class implementing the ‘Allocator’ C++ Named Requirement.

This class is usable for C++ classes that require an allocator, such as std::vector.

Note

This class requires dynamic or static linking to carb.dll/libcarb.so/libcarb.dylib in order to function.

Template Parameters
  • T – The type to allocate

  • Align – The requested alignment as a std::integral_constant<size_t, value>. This must be a type in order to conform to the requirements of std::allocator_traits that template parameters be types. The constant value must be zero or a power of two. Zero indicates to use T’s required alignment.

Public Types

using pointer = T*

pointer

using const_pointer = const T*

const_pointer

using reference = T&

reference

using const_reference = const T&

const_reference

using void_pointer = void*

void_pointer

using const_void_pointer = const void*

const_void_pointer

using value_type = T

value_type

using size_type = std::size_t

size_type

using difference_type = std::ptrdiff_t

difference_type

Public Functions

constexpr Allocator() noexcept = default

Constructor.

constexpr Allocator(const Allocator&) noexcept = default

Copy constructor.

constexpr Allocator &operator=(const Allocator&) noexcept = default

Copy-assign operator.

template<class U, class UAlign>
inline constexpr Allocator(const Allocator<U, UAlign> &other) noexcept

Copy constructor.

template<class U, class UAlign>
inline constexpr Allocator &operator=(const Allocator<U, UAlign> &other) noexcept

Copy-assign operator.

~Allocator() = default

Destructor.

inline constexpr bool operator==(const Allocator &other) const noexcept

Equality operator.

inline constexpr bool operator!=(const Allocator &other) const noexcept

Inequality operator.

inline pointer allocate(size_type n = 1)

Allocates suitable storage for an array object of type T[n] and creates the array, but does not construct array elements.

If alignment is suitable (that is, not less than the required alignment of T) it is used, otherwise the required alignment of T is used.

Throws

std::bad_alloc – if exceptions are enabled (CARB_EXCEPTIONS_ENABLED) and the underlying carb::allocate() failed.

Parameters

n – The number of elements of T to allocate space for.

Returns

A pointer to memory that can contain an array of type T[n], but no array elements have been constructed.

inline pointer allocate(size_type n, const_void_pointer p)

Same as allocate(size_type) but may use p (nullptr or a pointer obtained from allocate()) to aid locality.

Parameters
  • n – The number of elements of T to allocate space for.

  • p – May be nullptr or a pointer obtained from allocate(). If non-nullptr, p is returned.

Returns

A pointer to memory that can contain an array of type T[n], but no array elements have been constructed.

inline void deallocate(pointer p, size_type n) noexcept

Deallocates storage pointed to by p, which must be a value returned by a previous call to allocate() that has not been invalidated by an intervening call to deallocate.

Parameters
  • p – A value returned by a previous call to allocate() and not previously passed to deallocate.

  • n – Must be the same size value that was originally passed to allocate().

inline size_type max_size() const noexcept

Returns the largest value that can be passed to allocate().

Returns

the largest value that can be passed to allocate().

template<class X, class ...Args>
inline void construct(X *const p, Args&&... args)

Constructs an object of type X in previously-allocated storage at the address pointed to by p, using args as the constructor arguments.

Parameters
  • p – The pointer at which to construct.

  • args – The constructor arguments.

template<class X>
inline void destroy(X *const p)

Destructs an object of type X pointed to by p but does not deallocate any storage.

Parameters

p – The pointer to an object of type X to destroy.

Public Static Attributes

static constexpr size_t alignment = Align::value

Alignment (non-standard)

template<class U>
struct rebind

A struct that allows determining an allocator for class U through the other type.

Public Types

using other = Allocator<U, Align>

The type of Allocator<U>