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

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. Expects that T is trivially copyable. Similarly to ArrayNewAllocator it's reserving an extra space before to store array capacity.

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

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 the 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.

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*.

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.

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 the 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 sizeof(std::size_t) 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.