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