| | | 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 | | |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | using System.Numerics; |
| | | 6 | | using System.Runtime.CompilerServices; |
| | | 7 | | |
| | | 8 | | namespace 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. |
| | 139210 | 20 | | return BitOperations.Log2((uint)bufferSize - 1 | 15) - 3; |
| | | 21 | | } |
| | | 22 | | |
| | | 23 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 24 | | internal static int GetMaxSizeForBucket(int binIndex) |
| | | 25 | | { |
| | 69623 | 26 | | int maxSize = 16 << binIndex; |
| | 69623 | 27 | | Debug.Assert(maxSize >= 0); |
| | 69623 | 28 | | 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 | | |
| | 0 | 43 | | GCMemoryInfo memoryInfo = GC.GetGCMemoryInfo(); |
| | | 44 | | |
| | 0 | 45 | | if (memoryInfo.MemoryLoadBytes >= memoryInfo.HighMemoryLoadThresholdBytes * HighPressureThreshold) |
| | | 46 | | { |
| | 0 | 47 | | return MemoryPressure.High; |
| | | 48 | | } |
| | | 49 | | |
| | 0 | 50 | | if (memoryInfo.MemoryLoadBytes >= memoryInfo.HighMemoryLoadThresholdBytes * MediumPressureThreshold) |
| | | 51 | | { |
| | 0 | 52 | | return MemoryPressure.Medium; |
| | | 53 | | } |
| | | 54 | | |
| | 0 | 55 | | return MemoryPressure.Low; |
| | | 56 | | } |
| | | 57 | | } |
| | | 58 | | } |
| | | 59 | | |