< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 6
Coverable lines: 6
Total lines: 58
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 2
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
IsCachedInvalidHandle(...)0%220%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/Common/src/Microsoft/Win32/SafeHandles/SafeHandleCache.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System;
 5using System.Diagnostics;
 6using System.Runtime.InteropServices;
 7using System.Threading;
 8
 9namespace Microsoft.Win32.SafeHandles
 10{
 11    /// <summary>Provides a cache for special instances of SafeHandles.</summary>
 12    /// <typeparam name="T">Specifies the type of SafeHandle.</typeparam>
 13    internal static class SafeHandleCache<T> where T : SafeHandle
 14    {
 15        private static T? s_invalidHandle;
 16
 17        /// <summary>
 18        /// Gets a cached, invalid handle.  As the instance is cached, it should either never be Disposed
 19        /// or it should override <see cref="SafeHandle.Dispose(bool)"/> to prevent disposal when the
 20        /// instance represents an invalid handle: <see cref="System.Runtime.InteropServices.SafeHandle.IsInvalid"/> ret
 21        /// </summary>
 22        internal static T GetInvalidHandle(Func<T> invalidHandleFactory)
 23        {
 24            return s_invalidHandle ?? CreateInvalidHandle(invalidHandleFactory);
 25
 26            static T CreateInvalidHandle(Func<T> invalidHandleFactory)
 27            {
 28                T newHandle = invalidHandleFactory();
 29                T? currentHandle = Interlocked.CompareExchange(ref s_invalidHandle, newHandle, null);
 30
 31                if (currentHandle == null)
 32                {
 33                    GC.SuppressFinalize(newHandle);
 34                    currentHandle = newHandle;
 35                }
 36                else
 37                {
 38                    newHandle.Dispose();
 39                }
 40
 41                Debug.Assert(currentHandle.IsInvalid);
 42                return currentHandle;
 43            }
 44        }
 45
 46        /// <summary>Gets whether the specified handle is invalid handle.</summary>
 47        /// <param name="handle">The handle to compare.</param>
 48        /// <returns>true if <paramref name="handle"/> is invalid handle; otherwise, false.</returns>
 49        internal static bool IsCachedInvalidHandle(SafeHandle handle)
 050        {
 051            Debug.Assert(handle != null);
 052            bool isCachedInvalidHandle = ReferenceEquals(handle, s_invalidHandle);
 053            Debug.Assert(!isCachedInvalidHandle || handle.IsInvalid, "The cached invalid handle must still be invalid.")
 054            return isCachedInvalidHandle;
 055        }
 56    }
 57}
 58