Class FirstFitAllocatorBase
Defined in File first_fit_allocator_base.hpp
-
class FirstFitAllocatorBase
Memory management helper class using first-fit allocation strategy.
This class keeps track of allocated and free regions within a large memory chunk and can efficiently find the first available block that fits a requested size. It works only with indices and relies on an external class to hold the actual memory.
Internally, it uses a binary segment tree to track the largest available block in any given area of memory. All operations have logarithmic time complexity.
Public Types
-
enum class Error
Error codes used by the first-fit allocator classes.
Values:
-
enumerator kAlreadyInUse
Returned when the suballocator is already in use and some memory has not been released yet.
-
enumerator kInvalidSize
Returned if the size is invalid (negative or too big).
-
enumerator kOutOfMemory
Returned if the class can’t allocate enough memory.
-
enumerator kBlockNotAllocated
Returned if we attempt to release a block of memory not allocated yet.
-
enumerator kLogicError
This error happens when there is a logical issue during execution. This should never happen.
-
enumerator kAlreadyInUse
-
using unexpected_t = unexpected<Error>
Unexpected type used by this class.
Public Functions
-
FirstFitAllocatorBase()
Default constructor.
-
expected_t<void> allocate(int32_t size)
Allocate internal data structures to handle memory requests for a given size.
Requires 32 * 2^ceil(log2(size)) bytes of memory for internal bookkeeping.
- Parameters
size – Maximum size of memory that can be managed.
- Returns
Success or error status. Error::kInvalidSize if size is invalid.
-
expected_t<int32_t> acquire(int32_t size)
Acquire a contiguous block of memory of the specified size.
If such a contiguous block exists, returns the lowest index where the block starts. This block will be marked as allocated until a call to release() is made.
- Parameters
size – Size of the memory block to acquire.
- Returns
Index of the allocated block on success, Error::kOutOfMemory if no suitable block exists.
-
expected_t<void> release(int32_t index)
Release a previously acquired block of memory.
The block must have been previously acquired using acquire(). Once released, the memory becomes available for future allocations.
- Parameters
index – Starting index of the block to release.
- Returns
Success or error status. Error::kBlockNotAllocated if the block was not previously allocated.
-
enum class Error