< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 10
Coverable lines: 10
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
SelectBucketIndex(...)100%110%
GetMaxSizeForBucket(...)100%110%
GetMemoryPressure()0%440%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Private.CoreLib/src/System/Buffers/Utilities.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.Diagnostics;
 5using System.Numerics;
 6using System.Runtime.CompilerServices;
 7
 8namespace System.Buffers
 9{
 10    internal static class Utilities
 11    {
 12        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 13        internal static int SelectBucketIndex(int bufferSize)
 14        {
 15            // Buffers are bucketed so that a request between 2^(n-1) + 1 and 2^n is given a buffer of 2^n
 16            // Bucket index is log2(bufferSize - 1) with the exception that buffers between 1 and 16 bytes
 17            // are combined, and the index is slid down by 3 to compensate.
 18            // Zero is a valid bufferSize, and it is assigned the highest bucket index so that zero-length
 19            // buffers are not retained by the pool. The pool will return the Array.Empty singleton for these.
 020            return BitOperations.Log2((uint)bufferSize - 1 | 15) - 3;
 21        }
 22
 23        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 24        internal static int GetMaxSizeForBucket(int binIndex)
 25        {
 026            int maxSize = 16 << binIndex;
 027            Debug.Assert(maxSize >= 0);
 028            return maxSize;
 29        }
 30
 31        internal enum MemoryPressure
 32        {
 33            Low,
 34            Medium,
 35            High
 36        }
 37
 38        internal static MemoryPressure GetMemoryPressure()
 39        {
 40            const double HighPressureThreshold = .90;       // Percent of GC memory pressure threshold we consider "high
 41            const double MediumPressureThreshold = .70;     // Percent of GC memory pressure threshold we consider "medi
 42
 043            GCMemoryInfo memoryInfo = GC.GetGCMemoryInfo();
 44
 045            if (memoryInfo.MemoryLoadBytes >= memoryInfo.HighMemoryLoadThresholdBytes * HighPressureThreshold)
 46            {
 047                return MemoryPressure.High;
 48            }
 49
 050            if (memoryInfo.MemoryLoadBytes >= memoryInfo.HighMemoryLoadThresholdBytes * MediumPressureThreshold)
 51            {
 052                return MemoryPressure.Medium;
 53            }
 54
 055            return MemoryPressure.Low;
 56        }
 57    }
 58}
 59