< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 24
Coverable lines: 24
Total lines: 71
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%
.ctor(...)100%110%
Dispose()100%110%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/MemoryBlocks/NativeHeapMemoryBlock.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    /// <summary>
 11    /// Represents memory block allocated on native heap.
 12    /// </summary>
 13    /// <remarks>
 14    /// Owns the native memory resource.
 15    /// </remarks>
 16    internal sealed class NativeHeapMemoryBlock : AbstractMemoryBlock
 17    {
 18        private sealed unsafe class DisposableData : CriticalDisposableObject
 19        {
 20            private IntPtr _pointer;
 21
 022            public DisposableData(int size)
 023            {
 24#if FEATURE_CER
 25                // make sure the current thread isn't aborted in between allocating and storing the pointer
 26                RuntimeHelpers.PrepareConstrainedRegions();
 27                try
 28                { /* intentionally left blank */ }
 29                finally
 30#endif
 031                {
 032                    _pointer = Marshal.AllocHGlobal(size);
 033                }
 034            }
 35
 36            protected override void Release()
 037            {
 38#if FEATURE_CER
 39                // make sure the current thread isn't aborted in between zeroing the pointer and freeing the memory
 40                RuntimeHelpers.PrepareConstrainedRegions();
 41                try
 42                { /* intentionally left blank */ }
 43                finally
 44#endif
 045                {
 046                    IntPtr ptr = Interlocked.Exchange(ref _pointer, IntPtr.Zero);
 047                    if (ptr != IntPtr.Zero)
 048                    {
 049                        Marshal.FreeHGlobal(ptr);
 050                    }
 051                }
 052            }
 53
 054            public byte* Pointer => (byte*)_pointer;
 55        }
 56
 57        private readonly DisposableData _data;
 58        private readonly int _size;
 59
 060        internal NativeHeapMemoryBlock(int size)
 061        {
 062            _data = new DisposableData(size);
 063            _size = size;
 064        }
 65
 066        public override void Dispose() => _data.Dispose();
 067        public override unsafe byte* Pointer => _data.Pointer;
 068        public override int Size => _size;
 69    }
 70}
 71