< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 25
Coverable lines: 25
Total lines: 57
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 6
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%
Dispose(...)0%220%
GetMemoryBlockImpl(...)100%110%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/MemoryBlocks/ByteArrayMemoryProvider.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.Collections.Immutable;
 5using System.Diagnostics;
 6using System.IO;
 7using System.Runtime.InteropServices;
 8using System.Threading;
 9
 10namespace System.Reflection.Internal
 11{
 12    internal sealed class ByteArrayMemoryProvider : MemoryBlockProvider
 13    {
 14        private readonly ImmutableArray<byte> _array;
 15        private PinnedObject? _pinned;
 16
 017        public ByteArrayMemoryProvider(ImmutableArray<byte> array)
 018        {
 019            Debug.Assert(!array.IsDefault);
 020            _array = array;
 021        }
 22
 23        protected override void Dispose(bool disposing)
 024        {
 025            Debug.Assert(disposing);
 026            Interlocked.Exchange(ref _pinned, null)?.Dispose();
 027        }
 28
 029        public override int Size => _array.Length;
 030        public ImmutableArray<byte> Array => _array;
 31
 32        protected override AbstractMemoryBlock GetMemoryBlockImpl(int start, int size)
 033        {
 034            return new ByteArrayMemoryBlock(this, start, size);
 035        }
 36
 37        internal unsafe byte* Pointer
 38        {
 39            get
 040            {
 041                if (_pinned == null)
 042                {
 043                    var newPinned = new PinnedObject(ImmutableCollectionsMarshal.AsArray(_array)!);
 44
 045                    if (Interlocked.CompareExchange(ref _pinned, newPinned, null) != null)
 046                    {
 47                        // another thread has already allocated the handle:
 048                        newPinned.Dispose();
 049                    }
 050                }
 51
 052                return _pinned.Pointer;
 053            }
 54        }
 55    }
 56}
 57