mirror of
https://github.com/netchx/netch.git
synced 2026-03-26 18:49:46 +08:00
done
This commit is contained in:
38
Netch/3rd/DnsClient.NET/Interop/DisposableIntPtr.cs
Normal file
38
Netch/3rd/DnsClient.NET/Interop/DisposableIntPtr.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace DnsClient
|
||||
{
|
||||
internal class DisposableIntPtr : IDisposable
|
||||
{
|
||||
public IntPtr Ptr => _ptr;
|
||||
|
||||
public bool IsValid { get; private set; } = true;
|
||||
|
||||
private IntPtr _ptr;
|
||||
|
||||
private DisposableIntPtr()
|
||||
{
|
||||
}
|
||||
|
||||
public static DisposableIntPtr Alloc(int size)
|
||||
{
|
||||
var ptr = new DisposableIntPtr();
|
||||
try
|
||||
{
|
||||
ptr._ptr = Marshal.AllocHGlobal(size);
|
||||
}
|
||||
catch (OutOfMemoryException)
|
||||
{
|
||||
ptr.IsValid = false;
|
||||
}
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Marshal.FreeHGlobal(_ptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user