Files
hjha-server/ServerCore/NativeCollection/Stack.cs
xiaoou e9616125ce feat: initial commit - HJHA game server full source
6 major server modules (PdkFriendServer/GlobalSever/ServerCore/GameModule/GameNetModule) +
game logic (GameFix/GameDAL/ServerData) +
network layer (NetWorkMessage) +
data layer (ObjectModel) +
utilities (MrWu/Core/Config/CloudAPI/dll) +
adapters (zyxAdapter/base)

.NET 8.0 C# solution, 16 projects, 958 source files
2026-07-07 12:02:15 +08:00

90 lines
2.2 KiB
C#

using System;
using System.Runtime.CompilerServices;
namespace Server.NativeCollection
{
public unsafe class Stack<T> : INativeCollectionClass where T : unmanaged
{
private const int _defaultCapacity = 10;
private UnsafeType.Stack<T>* _stack;
private int _capacity;
public Stack(int initialCapacity = _defaultCapacity)
{
_capacity = initialCapacity;
_stack = UnsafeType.Stack<T>.Create(_capacity);
IsDisposed = false;
}
public int Count => _stack->Count;
public void Dispose()
{
if (IsDisposed)
{
return;
}
if (_stack != null)
{
_stack->Dispose();
NativeMemoryHelper.Free(_stack);
NativeMemoryHelper.RemoveNativeMemoryByte(Unsafe.SizeOf<UnsafeType.Stack<T>>());
IsDisposed = true;
}
}
public void Clear()
{
_stack->Clear();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Contains(in T obj)
{
return _stack->Contains(obj);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T Peak()
{
return _stack->Peak();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T Pop()
{
return _stack->Pop();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryPop(out T result)
{
var returnValue = _stack->TryPop(out result);
return returnValue;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Push(in T obj)
{
_stack->Push(obj);
}
~Stack()
{
Dispose();
}
public void ReInit()
{
if (IsDisposed)
{
_stack = UnsafeType.Stack<T>.Create(_capacity);
IsDisposed = false;
}
}
public bool IsDisposed { get; private set; }
}
}