Files
DotNetAlpm/Alpm/SafeDatabaseHandle.cs
2020-04-29 13:21:13 -04:00

52 lines
1.6 KiB
C#

using System;
using System.Runtime.CompilerServices;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;
namespace Foodsoft.Alpm
{
internal sealed class SafeDatabaseHandle : SafeHandle
{
internal SafeAlpmHandle SafeAlpmHandle
{
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[PrePrepareMethod]
get;
} = null!; // we always throw in the constructor on failure
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
[PrePrepareMethod]
internal SafeDatabaseHandle(IntPtr dbPtr, SafeAlpmHandle safeAlpmHandle)
: base(IntPtr.Zero, true)
{
var success = false;
RuntimeHelpers.PrepareConstrainedRegions();
try { }
finally
{
safeAlpmHandle.DangerousAddRef(ref success);
if (success)
{
SafeAlpmHandle = safeAlpmHandle;
handle = dbPtr;
}
}
if (!success) throw new ObjectDisposedException(GetType().FullName);
}
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail), PrePrepareMethod]
protected override bool ReleaseHandle()
{
SafeAlpmHandle.DangerousRelease();
return true;
}
public override bool IsInvalid
{
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success), PrePrepareMethod]
get => handle == IntPtr.Zero;
}
}
}