Class AllocatorManager
Manages custom memory allocators.
Namespace: Unity.Collections
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 |
---|---|
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 |
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>(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. |
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
.
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 : struct, 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 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
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. |