Struct HeapAllocator
Represents a generic best-fit heap allocation algorithm that operates on abstract integer indices.
Namespace: Unity.Rendering
Syntax
public struct HeapAllocator : IDisposable
Remarks
You can use this to suballocate memory, GPU buffer contents, and DX12 descriptors. This supports alignments, resizing, and coalescing of freed blocks.
Constructors
HeapAllocator(UInt64, UInt32)
Creates a new HeapAllocator with the given initial size and alignment.
Declaration
public HeapAllocator(ulong size = 0UL, uint minimumAlignment = 1U)
Parameters
Type | Name | Description |
---|---|---|
UInt64 | size | The initial size of the allocator. |
UInt32 | minimumAlignment | The initial alignment of the allocator. |
Remarks
You can resize the allocator later.
Properties
Empty
Indicates whether the allocator is empty. This is true if the allocator is empty and false otherwise.
Declaration
public readonly bool Empty { get; }
Property Value
Type | Description |
---|---|
Boolean |
FreeSpace
The amount of available free space in the allocator.
Declaration
public readonly ulong FreeSpace { get; }
Property Value
Type | Description |
---|---|
UInt64 |
Full
Indicates whether the allocator is full. This is true if the allocator is full and false otherwise.
Declaration
public readonly bool Full { get; }
Property Value
Type | Description |
---|---|
Boolean |
IsCreated
Indicates whether the allocator has been created and not yet allocated.
Declaration
public readonly bool IsCreated { get; }
Property Value
Type | Description |
---|---|
Boolean |
Size
The size of the heap that the allocator manages.
Declaration
public readonly ulong Size { get; }
Property Value
Type | Description |
---|---|
UInt64 |
UsedSpace
The amount of used space in the allocator.
Declaration
public readonly ulong UsedSpace { get; }
Property Value
Type | Description |
---|---|
UInt64 |
Methods
Allocate(UInt64, UInt32)
Attempt to allocate a block from the heap with at least the given size and alignment.
Declaration
public HeapBlock Allocate(ulong size, uint alignment = 1U)
Parameters
Type | Name | Description |
---|---|---|
UInt64 | size | The size of the block to allocate. |
UInt32 | alignment | Alignment of the allocated block. |
Returns
Type | Description |
---|---|
HeapBlock | Returns a new allocated HeapBlock on success. Returns an empty block on failure. |
Remarks
The allocated block might be bigger than the requested size, but will never be smaller. If the allocation fails, this method returns an empty block.
Clear()
Clears the allocator.
Declaration
public void Clear()
Dispose()
Declaration
public void Dispose()
Release(HeapBlock)
Releases a given block of memory and marks it as free.
Declaration
public void Release(HeapBlock block)
Parameters
Type | Name | Description |
---|---|---|
HeapBlock | block | The HeapBlock to release to the allocator. |
Remarks
You must have wholly allocated the given block before you pass it into this method. However, it's legal to release big allocations in smaller non-overlapping sub-blocks.
Resize(UInt64)
Attempts to grow or shrink the allocator. Growing always succeeds, but shrinking might fail if the end of the heap is allocated.
Declaration
public bool Resize(ulong newSize)
Parameters
Type | Name | Description |
---|---|---|
UInt64 | newSize | The new size of the allocator. |
Returns
Type | Description |
---|---|
Boolean | Returns true if the operation is a success. Returns false otherwise. |