using System; using System.Runtime.ConstrainedExecution; using System.Threading; namespace System.Runtime.InteropServices { /// Represents a wrapper class for operating system handles. This class must be inherited. // Token: 0x0200033F RID: 831 public abstract class SafeHandle : CriticalFinalizerObject, IDisposable { // Token: 0x06002316 RID: 8982 RVA: 0x0007A7F8 File Offset: 0x000789F8 protected SafeHandle() { throw new NotImplementedException(); } /// Initializes a new instance of the class with the specified invalid handle value. /// The value of an invalid handle (usually 0 or -1). Your implementation of should return true for this value. /// true to reliably let release the handle during the finalization phase; otherwise, false (not recommended). /// The derived class resides in an assembly without unmanaged code access permission. // Token: 0x06002317 RID: 8983 RVA: 0x0007A808 File Offset: 0x00078A08 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] protected SafeHandle(IntPtr invalidHandleValue, bool ownsHandle) { this.invalid_handle_value = invalidHandleValue; this.owns_handle = ownsHandle; this.refcount = 1; } /// Marks the handle for releasing and freeing resources. /// /// /// // Token: 0x06002318 RID: 8984 RVA: 0x0007A828 File Offset: 0x00078A28 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] public void Close() { if (this.refcount == 0) { throw new ObjectDisposedException(base.GetType().FullName); } int num; int num2; do { num = this.refcount; num2 = num - 1; } while (Interlocked.CompareExchange(ref this.refcount, num2, num) != num); if (num2 == 0 && this.owns_handle && !this.IsInvalid) { this.ReleaseHandle(); this.handle = this.invalid_handle_value; this.refcount = -1; } } /// Manually increments the reference counter on instances. /// true if the reference counter was successfully incremented; otherwise, false. /// /// /// // Token: 0x06002319 RID: 8985 RVA: 0x0007A8A8 File Offset: 0x00078AA8 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] public void DangerousAddRef(ref bool success) { if (this.refcount <= 0) { throw new ObjectDisposedException(base.GetType().FullName); } for (;;) { int num = this.refcount; int num2 = num + 1; if (num <= 0) { break; } if (Interlocked.CompareExchange(ref this.refcount, num2, num) == num) { goto Block_3; } } throw new ObjectDisposedException(base.GetType().FullName); Block_3: success = true; } /// Returns the value of the field. /// An IntPtr representing the value of the field. If the handle has been marked invalid with , this method still returns the original handle value, which can be a stale value. /// /// /// // Token: 0x0600231A RID: 8986 RVA: 0x0007A90C File Offset: 0x00078B0C [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] public IntPtr DangerousGetHandle() { if (this.refcount <= 0) { throw new ObjectDisposedException(base.GetType().FullName); } return this.handle; } /// Manually decrements the reference counter on a instance. /// /// /// // Token: 0x0600231B RID: 8987 RVA: 0x0007A934 File Offset: 0x00078B34 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] public void DangerousRelease() { if (this.refcount <= 0) { throw new ObjectDisposedException(base.GetType().FullName); } int num; int num2; do { num = this.refcount; num2 = num - 1; } while (Interlocked.CompareExchange(ref this.refcount, num2, num) != num); if (num2 == 0 && this.owns_handle && !this.IsInvalid) { this.ReleaseHandle(); this.handle = this.invalid_handle_value; } } /// Releases all resources used by the class. /// /// /// // Token: 0x0600231C RID: 8988 RVA: 0x0007A9AC File Offset: 0x00078BAC [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] public void Dispose() { this.Dispose(true); GC.SuppressFinalize(this); } /// Marks a handle as no longer used. /// /// /// // Token: 0x0600231D RID: 8989 RVA: 0x0007A9BC File Offset: 0x00078BBC [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] public void SetHandleAsInvalid() { this.handle = this.invalid_handle_value; } /// Releases the unmanaged resources used by the class specifying whether to perform a normal dispose operation. /// true for a normal dispose operation; false to finalize the handle. // Token: 0x0600231E RID: 8990 RVA: 0x0007A9CC File Offset: 0x00078BCC [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] protected virtual void Dispose(bool disposing) { if (disposing) { this.Close(); } } /// When overridden in a derived class, executes the code required to free the handle. /// true if the handle is released successfully; otherwise, in the event of a catastrophic failure, false. In this case, it generates a releaseHandleFailed MDA Managed Debugging Assistant. // Token: 0x0600231F RID: 8991 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] protected abstract bool ReleaseHandle(); /// Sets the handle to the specified pre-existing handle. /// The pre-existing handle to use. // Token: 0x06002320 RID: 8992 RVA: 0x0007A9E0 File Offset: 0x00078BE0 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] protected void SetHandle(IntPtr handle) { this.handle = handle; } /// Gets a value indicating whether the handle is closed. /// true if the handle is closed; otherwise, false. /// /// /// // Token: 0x17000659 RID: 1625 // (get) Token: 0x06002321 RID: 8993 RVA: 0x0007A9EC File Offset: 0x00078BEC public bool IsClosed { [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] get { return this.refcount <= 0; } } /// When overridden in a derived class, gets a value indicating whether the handle value is invalid. /// true if the handle value is invalid; otherwise, false. /// /// /// // Token: 0x1700065A RID: 1626 // (get) Token: 0x06002322 RID: 8994 public abstract bool IsInvalid { [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] get; } /// Frees all resources associated with the handle. // Token: 0x06002323 RID: 8995 RVA: 0x0007A9FC File Offset: 0x00078BFC ~SafeHandle() { if (this.owns_handle && !this.IsInvalid) { this.ReleaseHandle(); this.handle = this.invalid_handle_value; } } /// Specifies the handle to be wrapped. // Token: 0x04000DA7 RID: 3495 protected IntPtr handle; // Token: 0x04000DA8 RID: 3496 private IntPtr invalid_handle_value; // Token: 0x04000DA9 RID: 3497 private int refcount; // Token: 0x04000DAA RID: 3498 private bool owns_handle; } }