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

Malloc-based allocator for growable arrays.

An ArrayAllocator that allocates and deallocates memory using the C std::malloc() / std::free() constructs in order to be able to use std::realloc() for fast reallocations. Similarly to ArrayNewAllocator it's reserving an extra space before to store array capacity.

All reallocation operations expect that T is trivially copyable. If it's not, use ArrayNewAllocator instead.

Compared to ArrayNewAllocator, this allocator stores array capacity in bytes and, together with the fact that std::free() doesn't care about the actual array type, growable arrays using this allocator can be freely cast to different compatible types using arrayAllocatorCast().

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 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::ArrayMallocAllocator<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::ArrayMallocAllocator<T>::Type

Pointer type

Function documentation

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

Allocate an array of given capacity.

std::malloc()'s an 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::ArrayMallocAllocator<T>::reallocate(T*& array, std::size_t prevSize, std::size_t newCapacity)

Reallocate an array to given capacity.

Calls std::realloc() on array (offset by the space to store capacity) and then updates the stored capacity to newCapacity and the array reference to point to the new (offset) location, in case the reallocation wasn't done in-place. The prevSize parameter is ignored, as std::realloc() always copies the whole original capacity. The allocation is guaranteed to follow T allocation requirements up to the platform default allocation alignment.

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

Deallocate an array.

Calls std::free() on a pointer offset by the extra space needed to store its capacity.

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

Grow an array.

Behaves the same as ArrayNewAllocator::grow().

template<class T>
static std::size_t Corrade::Containers::ArrayMallocAllocator<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::ArrayMallocAllocator<T>::base(T* array)

Array base address.

Returns the address with AllocationOffset subtracted.

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

Array deleter.

Since the types have trivial destructors, directly delegates into deallocate(). The size parameter is unused.