Class AllocatorManager
Manages custom memory allocators.
Namespace: Unity.Collections
Syntax
public static class AllocatorManager
Fields
FirstUserIndex
Index in the global function table of the first user-defined allocator.
Declaration
public const ushort FirstUserIndex = 64
Field Value
Type | Description |
---|---|
UInt16 | Index in the global function table of the first user-defined allocator. |
Remarks
The indexes from 0 up to FirstUserIndex
are reserved and so should not be used for your own allocators.
Invalid
Corresponds to Allocator.Invalid.
Declaration
public static readonly AllocatorManager.AllocatorHandle Invalid
Field Value
Type | Description |
---|---|
AllocatorManager.AllocatorHandle | Corresponds to Allocator.Invalid. |
kErrorBufferOverflow
Memory allocation Buffer Overflow status
Declaration
public const int kErrorBufferOverflow = -1
Field Value
Type | Description |
---|---|
Int32 |
kErrorNone
Memory allocation Success status
Declaration
public const int kErrorNone = 0
Field Value
Type | Description |
---|---|
Int32 |
MaxNumCustomAllocators
Maximum number of user-defined allocators.
Declaration
public const ushort MaxNumCustomAllocators = 32768
Field Value
Type | Description |
---|---|
UInt16 |
None
Corresponds to Allocator.None.
Declaration
public static readonly AllocatorManager.AllocatorHandle None
Field Value
Type | Description |
---|---|
AllocatorManager.AllocatorHandle | Corresponds to Allocator.None. |
Persistent
Corresponds to Allocator.Persistent.
Declaration
public static readonly AllocatorManager.AllocatorHandle Persistent
Field Value
Type | Description |
---|---|
AllocatorManager.AllocatorHandle | Corresponds to Allocator.Persistent. |
Temp
Corresponds to Allocator.Temp.
Declaration
public static readonly AllocatorManager.AllocatorHandle Temp
Field Value
Type | Description |
---|---|
AllocatorManager.AllocatorHandle | Corresponds to Allocator.Temp. |
TempJob
Corresponds to Allocator.TempJob.
Declaration
public static readonly AllocatorManager.AllocatorHandle TempJob
Field Value
Type | Description |
---|---|
AllocatorManager.AllocatorHandle | Corresponds to Allocator.TempJob. |
Methods
Allocate(AllocatorManager.AllocatorHandle, Int32, Int32, Int32)
Allocates memory from an allocator.
Declaration
public static void *Allocate(AllocatorManager.AllocatorHandle handle, int itemSizeInBytes, int alignmentInBytes, int items = 1)
Parameters
Type | Name | Description |
---|---|---|
AllocatorManager.AllocatorHandle | handle | A handle to the allocator. |
Int32 | itemSizeInBytes | The number of bytes to allocate. |
Int32 | alignmentInBytes | The alignment in bytes (must be a power of two). |
Int32 | items | The number of values to allocate space for. Defaults to 1. |
Returns
Type | Description |
---|---|
Void* | A pointer to the allocated memory. |
Allocate<T>(ref T, Int32, Int32, Int32)
Allocates memory directly from an allocator.
Declaration
public static void *Allocate<T>(this ref T t, int sizeOf, int alignOf, int items = 1)
where T : struct, AllocatorManager.IAllocator
Parameters
Type | Name | Description |
---|---|---|
T | t | The allocator of type T used to allocator memory. |
Int32 | sizeOf | The number of bytes to allocate to item. |
Int32 | alignOf | The alignment in bytes. |
Int32 | items | The number of items. Defaults to 1. |
Returns
Type | Description |
---|---|
Void* | A pointer to the allocated memory. |
Type Parameters
Name | Description |
---|---|
T | The type of allocator. |
Allocate<T>(AllocatorManager.AllocatorHandle, Int32)
Allocates enough memory for an unmanaged value of a given type.
Declaration
public static T*Allocate<T>(AllocatorManager.AllocatorHandle handle, int items = 1)
where T : struct
Parameters
Type | Name | Description |
---|---|---|
AllocatorManager.AllocatorHandle | handle | A handle to the allocator. |
Int32 | items | The number of values to allocate for space for. Defaults to 1. |
Returns
Type | Description |
---|---|
T* | A pointer to the allocated memory. |
Type Parameters
Name | Description |
---|---|
T | The type of value to allocate for. |
ConvertToAllocatorHandle(Allocator)
Convert an Allocator to an AllocatorHandle, keeping the Version.
Declaration
public static AllocatorManager.AllocatorHandle ConvertToAllocatorHandle(Allocator a)
Parameters
Type | Name | Description |
---|---|---|
Allocator | a | The Allocator to convert. |
Returns
Type | Description |
---|---|
AllocatorManager.AllocatorHandle | The AllocatorHandle of an allocator. |
Free(AllocatorManager.AllocatorHandle, Void*)
Frees an allocation.
Declaration
public static void Free(AllocatorManager.AllocatorHandle handle, void *pointer)
Parameters
Type | Name | Description |
---|---|---|
AllocatorManager.AllocatorHandle | handle | A handle to the allocator. |
Void* | pointer | A pointer to the allocated memory. |
Free(AllocatorManager.AllocatorHandle, Void*, Int32, Int32, Int32)
Frees an allocation.
Declaration
public static void Free(AllocatorManager.AllocatorHandle handle, void *pointer, int itemSizeInBytes, int alignmentInBytes, int items = 1)
Parameters
Type | Name | Description |
---|---|---|
AllocatorManager.AllocatorHandle | handle | A handle to the allocator. |
Void* | pointer | A pointer to the allocated memory. |
Int32 | itemSizeInBytes | The size in bytes of the allocation. |
Int32 | alignmentInBytes | The alignment in bytes (must be a power of two). |
Int32 | items | The number of values that the memory was allocated for. |
Remarks
For some allocators, the size of the allocation must be known to properly deallocate.
Other allocators only need the pointer when deallocating and so will ignore itemSizeInBytes
, alignmentInBytes
and items
.
Free<T>(AllocatorManager.AllocatorHandle, T*, Int32)
Frees an allocation.
Declaration
public static void Free<T>(AllocatorManager.AllocatorHandle handle, T*pointer, int items = 1)
where T : struct
Parameters
Type | Name | Description |
---|---|---|
AllocatorManager.AllocatorHandle | handle | A handle to the allocator. |
T* | pointer | A pointer to the allocated memory. |
Int32 | items | The number of values that the memory was allocated for. |
Type Parameters
Name | Description |
---|---|
T | The type of value that the memory was allocated for. |
Remarks
For some allocators, the size of the allocation must be known to properly deallocate.
Other allocators only need the pointer when deallocating and so will ignore T
and items
.
Register<T>(ref T, Boolean, Boolean, Int32)
Saves an allocator's function pointers in a free slot of the global function table. Thread safe.
Declaration
[ExcludeFromBurstCompatTesting("Uses managed delegate")]
public static void Register<T>(this ref T t, bool IsAutoDispose = false, bool isGlobal = false, int globalIndex = 0)
where T : struct, AllocatorManager.IAllocator
Parameters
Type | Name | Description |
---|---|---|
T | t | Reference to the allocator. |
Boolean | IsAutoDispose | Flag indicating if the allocator will automatically dispose allocations. |
Boolean | isGlobal | Flag indicating if the allocator is a global allocator. |
Int32 | globalIndex | Index into the global function table of the allocator to be created. |
Type Parameters
Name | Description |
---|---|
T | The type of allocator to register. |
Try(ref AllocatorManager.Block)
Invokes the allocator function of a block.
Declaration
public static int Try(ref AllocatorManager.Block block)
Parameters
Type | Name | Description |
---|---|---|
AllocatorManager.Block | block | The block to allocate, deallocate, or reallocate. |
Returns
Type | Description |
---|---|
Int32 | 0 if successful. Otherwise, returns the error code from the block's allocator function. |
Remarks
The allocator function is looked up from a global table.
- If the block range's Pointer is null, it will allocate.
- If the block range's Pointer is not null, it will reallocate.
- If the block range's Items is 0, it will deallocate.
UnmanagedUnregister<T>(ref T)
Removes an allocator's function pointers from the global function table, without managed code
Declaration
public static void UnmanagedUnregister<T>(this ref T t)
where T : struct, AllocatorManager.IAllocator
Parameters
Type | Name | Description |
---|---|---|
T | t | Reference to the allocator. |
Type Parameters
Name | Description |
---|---|
T | The type of allocator to unregister. |
Unregister<T>(ref T)
Removes an allocator's function pointers from the global function table.
Declaration
[ExcludeFromBurstCompatTesting("Uses managed delegate")]
public static void Unregister<T>(this ref T t)
where T : struct, AllocatorManager.IAllocator
Parameters
Type | Name | Description |
---|---|---|
T | t | Reference to the allocator. |
Type Parameters
Name | Description |
---|---|
T | The type of allocator to unregister. |