> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.nvidia.com/holoscan/sdk-user-guide/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.nvidia.com/holoscan/sdk-user-guide/_mcp/server.

# holoscan::FirstFitAllocator

> Template memory management class using first-fit allocation strategy.

```cpp showLineNumbers={false}
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](firstfitallocatorbase) for efficient memory management with logarithmic time complexity for acquire and release operations.

```cpp showLineNumbers={false}
#include <holoscan/first_fit_allocator.hpp>
```

**Template parameters**

**`T`** `class`

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

---

---

## Constructors

### FirstFitAllocator \[#firstfitallocator]

```cpp showLineNumbers={false}
holoscan::FirstFitAllocator<T>::FirstFitAllocator() = default
```

Default constructor.

---

## Methods

### allocate \[#allocate]

```cpp showLineNumbers={false}
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 int` — default: 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 \[#acquire]

```cpp showLineNumbers={false}
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 \[#release]

```cpp showLineNumbers={false}
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 \[#getnumberofchunks]

```cpp showLineNumbers={false}
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

| Name           | Definition                                    | Description                         |
| -------------- | --------------------------------------------- | ----------------------------------- |
| `expected_t`   | `expected< E, FirstFitAllocatorBase::Error >` | Expected type used by this class.   |
| `unexpected_t` | `unexpected< FirstFitAllocatorBase::Error >`  | Unexpected type used by this class. |

---

## Member variables

| Name                 | Type                    | Description                                                         |
| -------------------- | ----------------------- | ------------------------------------------------------------------- |
| `memory_management_` | `FirstFitAllocatorBase` | Real memory management implementation.                              |
| `chunk_size_`        | `int32_t`               | Size of each chunk. Only multiples of this size can be allocated.   |
| `number_of_chunks_`  | `int32_t`               | Total number of chunks available.                                   |
| `buffer_`            | `std::unique_ptr< T[]>` | Buffer holding the pre-allocated memory that is provided on demand. |