Table of Contents

Struct UnsafeStack<T>

Namespace
Misaki.HighPerformance.LowLevel.Collections
Assembly
Misaki.HighPerformance.LowLevel.dll

Provides a high-performance, unsafe stack data structure for unmanaged types, supporting manual memory management and allocation control.

public struct UnsafeStack<T> : IUnsafeCollection<T>, IUnsafeCollection, IDisposable where T : unmanaged

Type Parameters

T

The type of elements stored in the stack. Must be an unmanaged type.

Implements
Inherited Members

Constructors

UnsafeStack()

Initializes a new instance of UnsafeStack with a default size of 1 and a persistent allocation handle.

public UnsafeStack()

UnsafeStack(int, AllocationHandle, AllocationOption)

Initializes a new instance of the UnsafeStack class with the specified initial capacity and allocation options.

public UnsafeStack(int capacity, AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None)

Parameters

capacity int

The number of elements the stack can initially hold. Must be greater than zero.

handle AllocationHandle

A reference to an AllocationHandle used to manage the underlying memory allocation for the stack.

allocationOption AllocationOption

Specifies additional options for memory allocation. The default is AllocationOption.None.

Properties

Capacity

public readonly int Capacity { get; }

Property Value

int

Count

Gets the number of elements in a collection.

public readonly int Count { get; }

Property Value

int

IsCreated

Indicates whether the object has been created. Returns true if the object is created, otherwise false.

public readonly bool IsCreated { get; }

Property Value

bool

Remarks

If MHP_ENABLE_STACKTRACE is not defined, this property will only check if the underlying pointer is not null, which may not be sufficient to determine if the collection is fully initialized and ready for use.

Methods

Clear()

Removes all elements from the collection. The collection will be empty after this operation.

public void Clear()

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

GetEnumerator()

[UnscopedRef]
public UnsafeStack<T>.Enumerator GetEnumerator()

Returns

UnsafeStack<T>.Enumerator

GetUnsafePtr()

Returns a pointer to an unmanaged memory location. This pointer can be used for low-level memory operations.

public readonly void* GetUnsafePtr()

Returns

void*

The method returns a void pointer to the unsafe memory location.

Peek()

Returns the item at the top of the stack without removing it.

public readonly T Peek()

Returns

T

The item of type T at the top of the stack.

Exceptions

InvalidOperationException

Thrown when the stack is empty.

Pop()

Removes and returns the object at the top of the stack.

public T Pop()

Returns

T

The object removed from the top of the stack.

Exceptions

InvalidOperationException

Thrown when the stack is empty.

Push(scoped in T)

Adds an element to the top of the stack.

public void Push(scoped in T value)

Parameters

value T

The element to add to the stack.

Resize(int, AllocationOption)

Changes the size of a collection to the specified value.

public void Resize(int newSize, AllocationOption option = AllocationOption.None)

Parameters

newSize int

Specifies the new size to which the collection should be adjusted.

option AllocationOption

Specifies allocation options that may affect how memory is managed during the resize operation.

TryPeek(out T)

Attempts to return the item at the top of the stack without removing it.

public readonly bool TryPeek(out T value)

Parameters

value T

When this method returns, contains the item at the top of the stack if the stack is not empty; otherwise, the default value of T.

Returns

bool

true if an item was successfully returned; otherwise, false.

TryPop(out T)

Attempts to remove and return the object at the top of the stack.

public bool TryPop(out T value)

Parameters

value T

When this method returns, contains the object removed from the top of the stack, if the operation succeeded; otherwise, the default value of T.

Returns

bool

true if an object was successfully removed and returned from the stack; otherwise, false.