54 lines
1.6 KiB
C#
54 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
|
|
{
|
|
[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);
|
|
}
|
|
|
|
internal SafeAlpmHandle SafeAlpmHandle
|
|
{
|
|
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
|
|
[PrePrepareMethod]
|
|
get;
|
|
} = null!;
|
|
|
|
public override bool IsInvalid
|
|
{
|
|
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
|
|
[PrePrepareMethod]
|
|
get => handle == IntPtr.Zero;
|
|
}
|
|
|
|
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
|
|
[PrePrepareMethod]
|
|
protected override bool ReleaseHandle()
|
|
{
|
|
var err = alpm.alpm_db_unregister(handle);
|
|
SafeAlpmHandle.DangerousRelease();
|
|
return err == 0;
|
|
}
|
|
}
|
|
} |