Struct Stack
- Namespace
- Misaki.HighPerformance.LowLevel.Buffer
- Assembly
- Misaki.HighPerformance.LowLevel.dll
Provides a stack-based memory allocator for unmanaged memory, enabling fast allocation and deallocation of memory blocks within a preallocated buffer.
public struct Stack : IMemoryAllocator<Stack, Stack.CreationOptions>, IDisposable
- Implements
- Inherited Members
Remarks
This is not a thread-safe implementation.
Constructors
Stack(nuint)
Initializes a new instance of the StackAllocator class with a buffer of the specified size.
public Stack(nuint size)
Parameters
sizenuintThe size, in bytes, of the memory buffer to allocate for stack-based allocations. Must be greater than zero.
Properties
Buffer
public readonly byte* Buffer { get; }
Property Value
- byte*
Offset
public readonly nuint Offset { get; }
Property Value
Size
public readonly nuint Size { get; }
Property Value
Methods
Allocate(nuint, nuint, AllocationOption)
Allocates a block of memory of the specified size and alignment from the buffer.
public void* Allocate(nuint size, nuint alignment, AllocationOption allocationOption = AllocationOption.None)
Parameters
sizenuintThe number of bytes to allocate. Must be greater than zero and less than or equal to the remaining buffer size.
alignmentnuintThe alignment, in bytes, for the allocated memory block. Must be a power of two and greater than zero.
allocationOptionAllocationOptionAn option specifying additional allocation behavior, such as whether the allocated memory should be cleared. The default is None.
Returns
- void*
A pointer to the beginning of the allocated memory block if successful; otherwise, null if there is insufficient space in the buffer.
Create(in CreationOptions)
Creates a new instance of the allocator with the specified options.
public static Stack Create(in Stack.CreationOptions opts)
Parameters
optsStack.CreationOptionsThe options for creating the allocator.
Returns
- Stack
The created allocator instance.
CreateScope(AllocationHandle)
Creates a new scope instance associated with the current stack context.
public Stack.Scope CreateScope(AllocationHandle handle)
Parameters
handleAllocationHandle
Returns
- Stack.Scope
A Stack.Scope object that represents a scope tied to this stack.
Remarks
The instance of Stack must be pinned or allocated on the native heap to ensure that the pointer remains valid for the lifetime of the scope.
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
Free(void*)
Frees a previously allocated block of memory.
public readonly void Free(void* ptr)
Parameters
ptrvoid*A pointer to the memory block to free.
Remarks
The pointer must have been returned by a previous call to the Allocate(nuint, nuint, AllocationOption) or Reallocate(void*, nuint, nuint, nuint, AllocationOption) method. After calling this method, the pointer is no longer valid and must not be used.
Reallocate(void*, nuint, nuint, nuint, AllocationOption)
Reallocates a block of memory to a new size and alignment.
public void* Reallocate(void* ptr, nuint oldSize, nuint newSize, nuint alignment, AllocationOption allocationOption)
Parameters
ptrvoid*A pointer to the memory block to reallocate.
oldSizenuintThe size of the original memory block.
newSizenuintThe size of the new memory block.
alignmentnuintThe alignment of the new memory block.
allocationOptionAllocationOptionThe allocation options.
Returns
- void*
A pointer to the reallocated memory block. null if reallocation fails.
Remarks
The returned pointer must be freed using the Free(void*) method to avoid memory leaks. If the reallocation fails, the original pointer remains valid and must be freed by the caller.
Reset()
Resets the internal offset to its initial position.
public void Reset()