checkpoint
This commit is contained in:
46
Alpm/SafeCachePackageHandle.cs
Normal file
46
Alpm/SafeCachePackageHandle.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.ConstrainedExecution;
|
||||
|
||||
namespace Foodsoft.Alpm
|
||||
{
|
||||
internal sealed class SafeCachePackageHandle : SafePackageHandle
|
||||
{
|
||||
internal SafeAlpmHandle SafeAlpmHandle => SafeDatabaseHandle.SafeAlpmHandle;
|
||||
internal SafeDatabaseHandle SafeDatabaseHandle { get; } = null!;
|
||||
|
||||
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
|
||||
[PrePrepareMethod]
|
||||
internal SafeCachePackageHandle(IntPtr ptr, SafeDatabaseHandle dbHandle)
|
||||
: base()
|
||||
{
|
||||
var success = false;
|
||||
|
||||
RuntimeHelpers.PrepareConstrainedRegions();
|
||||
try { }
|
||||
finally
|
||||
{
|
||||
dbHandle.DangerousAddRef(ref success);
|
||||
if (success)
|
||||
{
|
||||
SafeDatabaseHandle = dbHandle;
|
||||
handle = ptr;
|
||||
}
|
||||
}
|
||||
|
||||
if (!success) throw new ObjectDisposedException(GetType().FullName);
|
||||
}
|
||||
|
||||
protected override bool ReleaseHandle()
|
||||
{
|
||||
SafeDatabaseHandle.DangerousRelease();
|
||||
return true;
|
||||
}
|
||||
|
||||
public override bool IsInvalid
|
||||
{
|
||||
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success), PrePrepareMethod]
|
||||
get => handle == IntPtr.Zero;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user