Skip to content

Commit

Permalink
Cache GetProcessHeap
Browse files Browse the repository at this point in the history
  • Loading branch information
Perksey authored Oct 10, 2023
1 parent fa8e4a6 commit efaa18b
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Core/Silk.NET.Core/Native/Structs/WinString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public WinString(nuint nChars)
[DllImport("kernel32.dll")]
static extern void* HeapAlloc(void* heap, uint dwFlags, nuint dwBytes);

HString = (Header*)HeapAlloc(GetProcessHeap(), 0, (nuint)sizeof(Header) + 2 * nChars);
HString = (Header*)HeapAlloc(ProcessHeap, 0, (nuint)sizeof(Header) + 2 * nChars);
HString->Flags = 0;
HString->Length = (uint)nChars;
HString->Reserved0 = 0;
Expand All @@ -55,7 +55,7 @@ public void Release()

if (Interlocked.Decrement(ref HString->RefCount) < 1)
{
if (HeapFree(GetProcessHeap(), 0, HString) == 0)
if (HeapFree(ProcessHeap, 0, HString) == 0)
{
SilkMarshal.ThrowHResult(Marshal.GetHRForLastWin32Error());
}
Expand All @@ -78,6 +78,8 @@ public static WinString DangerousCreate(char* str, uint length, Header* header)
[DllImport("kernel32.dll")]
private static extern void* GetProcessHeap();

private static readonly void* ProcessHeap = GetProcessHeap();

public void Dispose() => Release();

public static implicit operator string(WinString winString) => winString.ToString();
Expand Down

0 comments on commit efaa18b

Please sign in to comment.