holoscan::FirstFitAllocator

Beta
View as Markdown
template <class T>
class FirstFitAllocator

Template memory management class using first-fit allocation strategy.

This class pre-allocates memory for a given type and allows acquiring chunks of memory and releasing them efficiently. Only the allocate() function performs memory allocation; other functions use a constant amount of stack memory.

The allocator uses the FirstFitAllocatorBase for efficient memory management with logarithmic time complexity for acquire and release operations.

#include <holoscan/first_fit_allocator.hpp>

Template parameters

T
class

Type of elements that will be stored in the allocated memory.


Constructors

FirstFitAllocator

holoscan::FirstFitAllocator<T>::FirstFitAllocator() = defaultholoscan::FirstFitAllocator<T>::FirstFitAllocator() = default

Default constructor.


Methods

allocate

expected_t<int32_t> holoscan::FirstFitAllocator<T>::allocate(
const int32_t size,
const int chunk_size = 1
)

Allocate memory to handle requests for chunks of a given total size.

Pre-allocates memory that can be divided into chunks for later acquisition. The total memory allocated will be approximately sizeof(T) * size + 32 * size / chunk_size bytes.

Returns: Total number of elements that can be managed on success, error on failure. Error::kAlreadyInUse if already initialized, Error::kInvalidSize for invalid parameters, Error::kOutOfMemory if memory allocation fails.

Parameters

size
const int32_t

Total number of elements of type T to support.

chunk_size
const intDefaults to 1

Minimum chunk size. Memory will always be allocated in multiples of this size. This can improve allocation speed but reduces control over exact allocated size.

acquire

expected_t<std::pair<T *, int32_t>> holoscan::FirstFitAllocator<T>::acquire(
const int32_t size
)

Acquire a contiguous block of memory of the specified size.

If a suitable contiguous block exists, returns a pointer to that block and the actual size acquired. The actual size will be the smallest multiple of chunk_size that is greater than or equal to the requested size.

Returns: Pair containing pointer to the allocated block and actual number of elements acquired on success, Error::kOutOfMemory if no suitable block exists.

Parameters

size
const int32_t

Number of elements of type T to acquire.

release

expected_t<void> holoscan::FirstFitAllocator<T>::release(
const T *ptr
)

Release a previously acquired block of memory.

The pointer must have been returned by a previous call to acquire(). Once released, the memory becomes available for future acquisitions. A block cannot be released twice.

Returns: Success or error status. Error::kBlockNotAllocated if the pointer was not previously acquired or is invalid.

Parameters

ptr
const T *

Pointer to the memory block to release.

get_number_of_chunks

int32_t holoscan::FirstFitAllocator<T>::get_number_of_chunks(
const int32_t size
) const

Calculate the number of chunks needed for a given size.

Returns: Number of chunks needed to accommodate at least the requested size.

Parameters

size
const int32_t

Number of elements requested.


Types

Typedefs

NameDefinitionDescription
expected_texpected< E, FirstFitAllocatorBase::Error >Expected type used by this class.
unexpected_tunexpected< FirstFitAllocatorBase::Error >Unexpected type used by this class.

Member variables

NameTypeDescription
memory_management_FirstFitAllocatorBaseReal memory management implementation.
chunk_size_int32_tSize of each chunk. Only multiples of this size can be allocated.
number_of_chunks_int32_tTotal number of chunks available.
buffer_std::unique_ptr< T[]>Buffer holding the pre-allocated memory that is provided on demand.