< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 12
Coverable lines: 12
Total lines: 59
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 4
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%440%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Private.CoreLib/src/System/Buffers/MemoryHandle.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.InteropServices;
 5
 6namespace System.Buffers
 7{
 8    /// <summary>
 9    /// A handle for the memory.
 10    /// </summary>
 11    public unsafe struct MemoryHandle : IDisposable
 12    {
 13        private void* _pointer;
 14        private GCHandle _handle;
 15        private IPinnable? _pinnable;
 16
 17        /// <summary>
 18        /// Creates a new memory handle for the memory.
 19        /// </summary>
 20        /// <param name="pointer">pointer to memory</param>
 21        /// <param name="pinnable">reference to manually managed object, or default if there is no memory manager</param
 22        /// <param name="handle">handle used to pin array buffers</param>
 23        /// <safety>Only stores the supplied pointer, GC handle, and pinnable reference into the struct's fields; it per
 24        [CLSCompliant(false)]
 25        public MemoryHandle(void* pointer, GCHandle handle = default, IPinnable? pinnable = default)
 26        {
 027            _pointer = pointer;
 028            _handle = handle;
 029            _pinnable = pinnable;
 030        }
 31
 32        /// <summary>
 33        /// Returns the pointer to memory, where the memory is assumed to be pinned and hence the address won't change.
 34        /// </summary>
 35        /// <safety>Returns the stored pointer value to already-pinned memory; obtaining the address performs no derefer
 36        [CLSCompliant(false)]
 037        public void* Pointer => _pointer;
 38
 39        /// <summary>
 40        /// Frees the pinned handle and releases IPinnable.
 41        /// </summary>
 42        public void Dispose()
 43        {
 044            if (_handle.IsAllocated)
 45            {
 046                _handle.Free();
 47            }
 48
 049            if (_pinnable != null)
 50            {
 051                _pinnable.Unpin();
 052                _pinnable = null;
 53            }
 54
 055            _pointer = null;
 056        }
 57    }
 58}
 59