template<class T>
Corrade::Containers::ArrayNewAllocator struct new in 2020.06

New-based allocator for growable arrays.

An ArrayAllocator that allocates and deallocates memory using the C++ new[] / delete[] constructs, reserving an extra space before to store array capacity.

All reallocation operations expect that T is nothrow move-constructible.

Public types

enum (anonymous): std::size_t { AllocationOffset = Implementation::AllocatorTraits<T>::Offset }
using Type = T

Public static functions

static auto allocate(std::size_t capacity) -> T*
Allocate (but not construct) an array of given capacity.
static void reallocate(T*& array, std::size_t prevSize, std::size_t newCapacity)
Reallocate an array to given capacity.
static void deallocate(T* data)
Deallocate an array.
static auto grow(T* array, std::size_t desired) -> std::size_t
Grow an array.
static auto capacity(T* array) -> std::size_t
Array capacity.
static auto base(T* array) -> void*
Array base address.
static void deleter(T* data, std::size_t size)
Array deleter.

Enum documentation

template<class T>
enum Corrade::Containers::ArrayNewAllocator<T>::(anonymous): std::size_t

Enumerators
AllocationOffset

Offset at the beginning of the allocation to store allocation capacity. At least as large as std::size_t. If the type alignment is larger than that (for example double on a 32-bit platform), then it's equal to type alignment, but only at most as large as the default allocation alignment.

Typedef documentation

template<class T>
typedef T Corrade::Containers::ArrayNewAllocator<T>::Type

Pointer type

Function documentation

template<class T>
static T* Corrade::Containers::ArrayNewAllocator<T>::allocate(std::size_t capacity)

Allocate (but not construct) an array of given capacity.

new[]-allocates a char array with an extra space to store capacity before the front, returning it cast to T*. The allocation is guaranteed to follow T allocation requirements up to the platform default allocation alignment.

template<class T>
static void Corrade::Containers::ArrayNewAllocator<T>::reallocate(T*& array, std::size_t prevSize, std::size_t newCapacity)

Reallocate an array to given capacity.

Calls allocate(), move-constructs prevSize elements from array into the new array, calls destructors on the original elements, calls deallocate() and updates the array reference to point to the new array. The allocation is guaranteed to follow T allocation requirements up to the platform default allocation alignment.

template<class T>
static void Corrade::Containers::ArrayNewAllocator<T>::deallocate(T* data)

Deallocate an array.

Calls delete[] on a pointer offset by the extra space needed to store its capacity.

template<class T>
static std::size_t Corrade::Containers::ArrayNewAllocator<T>::grow(T* array, std::size_t desired)

Grow an array.

If current occupied size (including the space needed to store capacity) is less than 64 bytes, the capacity always doubled, with the allocation being at least as large as __STDCPP_DEFAULT_NEW_ALIGNMENT__ (usually 2*sizeof(std::size_t)). After that, the capacity is increased to 1.5x of current capacity (again including the space needed to store capacity). This is similar to what MSVC STL does with std::vector, except for libc++ / libstdc++, which both use a factor of 2. With a factor of 2 the allocation would crawl forward in memory, never able to reuse the holes after previous allocations, with a factor 1.5 it's possible after four reallocations. Further info in Folly FBVector docs.

template<class T>
static std::size_t Corrade::Containers::ArrayNewAllocator<T>::capacity(T* array)

Array capacity.

Retrieves the capacity that's stored before the front of the array.

template<class T>
static void* Corrade::Containers::ArrayNewAllocator<T>::base(T* array)

Array base address.

Returns the address with AllocationOffset subtracted.

template<class T>
static void Corrade::Containers::ArrayNewAllocator<T>::deleter(T* data, std::size_t size)

Array deleter.

Calls a destructor on size elements and then delegates into deallocate().