Struct UnsafeScratchAllocator
A fixed-size buffer from which you can make allocations.
Namespace: Unity.Collections.LowLevel.Unsafe
Assembly: Unity.Collections.dll
Syntax
public struct UnsafeScratchAllocator
Remarks
Allocations from a scratch allocator are not individually deallocated. Instead, when you're done using all the allocations from a scratch allocator, you dispose the allocator as a whole.
Constructors
UnsafeScratchAllocator(void*, int)
Initializes and returns an instance of UnsafeScratchAllocator.
Declaration
public UnsafeScratchAllocator(void* ptr, int capacityInBytes)
Parameters
Type | Name | Description |
---|---|---|
void* | ptr | An existing buffer to use as the allocator's internal buffer. |
int | capacityInBytes | The size in bytes of the internal buffer. |
Methods
Allocate(int, int)
Returns an allocation from the allocator's internal buffer.
Declaration
public void* Allocate(int sizeInBytes, int alignmentInBytes)
Parameters
Type | Name | Description |
---|---|---|
int | sizeInBytes | The size of the new allocation. |
int | alignmentInBytes | The alignment of the new allocation. |
Returns
Type | Description |
---|---|
void* | A pointer to the new allocation. |
Exceptions
Type | Condition |
---|---|
ArgumentException | Thrown if the new allocation would exceed the capacity of the allocator. |
Allocate<T>(int)
Returns an allocation from the allocator's internal buffer.
Declaration
public void* Allocate<T>(int count = 1) where T : struct
Parameters
Type | Name | Description |
---|---|---|
int | count | The number of elements to allocate space for. Defaults to 1. |
Returns
Type | Description |
---|---|
void* | A pointer to the new allocation. |
Type Parameters
Name | Description |
---|---|
T | The type of element to allocate space for. |
Remarks
The allocation size in bytes is at least count * sizeof(T)
. The space consumed by the allocation may be a little larger than this size due to alignment.
Exceptions
Type | Condition |
---|---|
ArgumentException | Thrown if the new allocation would exceed the capacity of the allocator. |