Class AllocatorManager
Manages custom memory allocators.
Namespace: Unity.Collections
Assembly: Unity.Collections.dll
Syntax
public static class AllocatorManager
Fields
AudioKernel
Corresponds to Allocator.AudioKernel.
Declaration
public static readonly AllocatorManager.AllocatorHandle AudioKernel
Field Value
Type | Description |
---|---|
AllocatorManager.AllocatorHandle | Corresponds to Allocator.AudioKernel. |
FirstUserIndex
Index in the global function table of the first user-defined allocator.
Declaration
public const ushort FirstUserIndex = 64
Field Value
Type | Description |
---|---|
ushort | 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. |
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. |
kErrorBufferOverflow
Declaration
public const int kErrorBufferOverflow = -1
Field Value
Type | Description |
---|---|
int |
kErrorNone
Memory allocation status
Declaration
public const int kErrorNone = 0
Field Value
Type | Description |
---|---|
int |
Methods
Allocate(AllocatorHandle, int, int, int)
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. |
int | itemSizeInBytes | The number of bytes to allocate. |
int | alignmentInBytes | The alignment in bytes (must be a power of two). |
int | items | The number of values to allocate space for. Defaults to 1. |
Returns
Type | Description |
---|---|
void* | A pointer to the allocated memory. |
Allocate<T>(AllocatorHandle, int)
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 : unmanaged
Parameters
Type | Name | Description |
---|---|---|
AllocatorManager.AllocatorHandle | handle | A handle to the allocator. |
int | 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. |
Free(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(AllocatorHandle, void*, int, int, int)
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. |
int | itemSizeInBytes | The size in bytes of the allocation. |
int | alignmentInBytes | The alignment in bytes (must be a power of two). |
int | 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>(AllocatorHandle, T*, int)
Frees an allocation.
Declaration
public static void Free<T>(AllocatorManager.AllocatorHandle handle, T* pointer, int items = 1) where T : unmanaged
Parameters
Type | Name | Description |
---|---|---|
AllocatorManager.AllocatorHandle | handle | A handle to the allocator. |
T* | pointer | A pointer to the allocated memory. |
int | 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
.
Initialize()
For internal use only.
Declaration
public static void Initialize()
Register<T>(ref T)
Saves an allocator's function pointers in a free slot of the global function table. Thread safe.
Declaration
public static void Register<T>(this ref T t) where T : unmanaged, AllocatorManager.IAllocator
Parameters
Type | Name | Description |
---|---|---|
T | t | Reference to the allocator. |
Type Parameters
Name | Description |
---|---|
T | The type of allocator to register. |
Shutdown()
For internal use only.
Declaration
public static void Shutdown()
Try(ref 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 |
---|---|
int | 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.
Unregister<T>(ref T)
Removes an allocator's function pointers from the global function table.
Declaration
public static void Unregister<T>(this ref T t) where T : unmanaged, AllocatorManager.IAllocator
Parameters
Type | Name | Description |
---|---|---|
T | t | Reference to the allocator. |
Type Parameters
Name | Description |
---|---|
T | The type of allocator to unregister. |