holoscan::FirstFitAllocator
holoscan::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.
Template parameters
Type of elements that will be stored in the allocated memory.
Constructors
FirstFitAllocator
Default constructor.
Methods
allocate
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
Total number of elements of type T to support.
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
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
Number of elements of type T to acquire.
release
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
Pointer to the memory block to release.
get_number_of_chunks
Calculate the number of chunks needed for a given size.
Returns: Number of chunks needed to accommodate at least the requested size.
Parameters
Number of elements requested.