< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 16
Coverable lines: 16
Total lines: 53
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
.ctor(...)100%110%
Release()0%220%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/Utilities/PinnedObject.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.Runtime.CompilerServices;
 5using System.Runtime.InteropServices;
 6using System.Threading;
 7
 8namespace System.Reflection.Internal
 9{
 10    internal sealed class PinnedObject : CriticalDisposableObject
 11    {
 12        // can't be read-only since GCHandle is a mutable struct
 13        private GCHandle _handle;
 14
 15        // non-zero indicates a valid handle
 16        private int _isValid;
 17
 018        public PinnedObject(object obj)
 019        {
 20#if FEATURE_CER
 21            // Make sure the current thread isn't aborted in between allocating the handle and storing it.
 22            RuntimeHelpers.PrepareConstrainedRegions();
 23            try
 24            { /* intentionally left blank */ }
 25            finally
 26#endif
 027            {
 028                _handle = GCHandle.Alloc(obj, GCHandleType.Pinned);
 029                _isValid = 1;
 030            }
 031        }
 32
 33        protected override void Release()
 034        {
 35#if FEATURE_CER
 36            // Make sure the current thread isn't aborted in between zeroing the handle and freeing it.
 37            RuntimeHelpers.PrepareConstrainedRegions();
 38            try
 39            { /* intentionally left blank */ }
 40            finally
 41#endif
 042            {
 043                if (Interlocked.Exchange(ref _isValid, 0) != 0)
 044                {
 045                    _handle.Free();
 046                }
 047            }
 048        }
 49
 050        public unsafe byte* Pointer => (byte*)_handle.AddrOfPinnedObject();
 51    }
 52}
 53