|
| 1 | +#pragma warning disable |
| 2 | +// Copied verbatim from https://github.com/dotnet/runtime/blob/d2650b6ae7023a2d9d2c74c56116f1f18472ab04/src/libraries/Common/src/System/Net/ArrayBuffer.cs |
| 3 | + |
| 4 | +// Licensed to the .NET Foundation under one or more agreements. |
| 5 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 6 | + |
| 7 | +using System.Buffers; |
| 8 | +using System.Diagnostics; |
| 9 | +using System.Runtime.CompilerServices; |
| 10 | +using System.Runtime.InteropServices; |
| 11 | + |
| 12 | +namespace System.Net |
| 13 | +{ |
| 14 | + // Warning: Mutable struct! |
| 15 | + // The purpose of this struct is to simplify buffer management. |
| 16 | + // It manages a sliding buffer where bytes can be added at the end and removed at the beginning. |
| 17 | + // [ActiveSpan/Memory] contains the current buffer contents; these bytes will be preserved |
| 18 | + // (copied, if necessary) on any call to EnsureAvailableBytes. |
| 19 | + // [AvailableSpan/Memory] contains the available bytes past the end of the current content, |
| 20 | + // and can be written to in order to add data to the end of the buffer. |
| 21 | + // Commit(byteCount) will extend the ActiveSpan by [byteCount] bytes into the AvailableSpan. |
| 22 | + // Discard(byteCount) will discard [byteCount] bytes as the beginning of the ActiveSpan. |
| 23 | + |
| 24 | + [StructLayout(LayoutKind.Auto)] |
| 25 | + internal struct ArrayBuffer : IDisposable |
| 26 | + { |
| 27 | +#if NET |
| 28 | + private static int ArrayMaxLength => Array.MaxLength; |
| 29 | +#else |
| 30 | + private const int ArrayMaxLength = 0X7FFFFFC7; |
| 31 | +#endif |
| 32 | + |
| 33 | + private readonly bool _usePool; |
| 34 | + private byte[] _bytes; |
| 35 | + private int _activeStart; |
| 36 | + private int _availableStart; |
| 37 | + |
| 38 | + // Invariants: |
| 39 | + // 0 <= _activeStart <= _availableStart <= bytes.Length |
| 40 | + |
| 41 | + public ArrayBuffer(int initialSize, bool usePool = false) |
| 42 | + { |
| 43 | + Debug.Assert(initialSize > 0 || usePool); |
| 44 | + |
| 45 | + _usePool = usePool; |
| 46 | + _bytes = initialSize == 0 |
| 47 | + ? Array.Empty<byte>() |
| 48 | + : usePool ? ArrayPool<byte>.Shared.Rent(initialSize) : new byte[initialSize]; |
| 49 | + _activeStart = 0; |
| 50 | + _availableStart = 0; |
| 51 | + } |
| 52 | + |
| 53 | + public ArrayBuffer(byte[] buffer) |
| 54 | + { |
| 55 | + Debug.Assert(buffer.Length > 0); |
| 56 | + |
| 57 | + _usePool = false; |
| 58 | + _bytes = buffer; |
| 59 | + _activeStart = 0; |
| 60 | + _availableStart = 0; |
| 61 | + } |
| 62 | + |
| 63 | + public void Dispose() |
| 64 | + { |
| 65 | + _activeStart = 0; |
| 66 | + _availableStart = 0; |
| 67 | + |
| 68 | + byte[] array = _bytes; |
| 69 | + _bytes = null!; |
| 70 | + |
| 71 | + if (array is not null) |
| 72 | + { |
| 73 | + ReturnBufferIfPooled(array); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + // This is different from Dispose as the instance remains usable afterwards (_bytes will not be null). |
| 78 | + public void ClearAndReturnBuffer() |
| 79 | + { |
| 80 | + Debug.Assert(_usePool); |
| 81 | + Debug.Assert(_bytes is not null); |
| 82 | + |
| 83 | + _activeStart = 0; |
| 84 | + _availableStart = 0; |
| 85 | + |
| 86 | + byte[] bufferToReturn = _bytes; |
| 87 | + _bytes = Array.Empty<byte>(); |
| 88 | + ReturnBufferIfPooled(bufferToReturn); |
| 89 | + } |
| 90 | + |
| 91 | + public int ActiveLength => _availableStart - _activeStart; |
| 92 | + public Span<byte> ActiveSpan => new Span<byte>(_bytes, _activeStart, _availableStart - _activeStart); |
| 93 | + public ReadOnlySpan<byte> ActiveReadOnlySpan => new ReadOnlySpan<byte>(_bytes, _activeStart, _availableStart - _activeStart); |
| 94 | + public Memory<byte> ActiveMemory => new Memory<byte>(_bytes, _activeStart, _availableStart - _activeStart); |
| 95 | + |
| 96 | + public int AvailableLength => _bytes.Length - _availableStart; |
| 97 | + public Span<byte> AvailableSpan => _bytes.AsSpan(_availableStart); |
| 98 | + public Memory<byte> AvailableMemory => _bytes.AsMemory(_availableStart); |
| 99 | + public Memory<byte> AvailableMemorySliced(int length) => new Memory<byte>(_bytes, _availableStart, length); |
| 100 | + |
| 101 | + public int Capacity => _bytes.Length; |
| 102 | + public int ActiveStartOffset => _activeStart; |
| 103 | + |
| 104 | + public byte[] DangerousGetUnderlyingBuffer() => _bytes; |
| 105 | + |
| 106 | + public void Discard(int byteCount) |
| 107 | + { |
| 108 | + Debug.Assert(byteCount <= ActiveLength, $"Expected {byteCount} <= {ActiveLength}"); |
| 109 | + _activeStart += byteCount; |
| 110 | + |
| 111 | + if (_activeStart == _availableStart) |
| 112 | + { |
| 113 | + _activeStart = 0; |
| 114 | + _availableStart = 0; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + public void Commit(int byteCount) |
| 119 | + { |
| 120 | + Debug.Assert(byteCount <= AvailableLength); |
| 121 | + _availableStart += byteCount; |
| 122 | + } |
| 123 | + |
| 124 | + // Ensure at least [byteCount] bytes to write to. |
| 125 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 126 | + public void EnsureAvailableSpace(int byteCount) |
| 127 | + { |
| 128 | + if (byteCount > AvailableLength) |
| 129 | + { |
| 130 | + EnsureAvailableSpaceCore(byteCount); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + private void EnsureAvailableSpaceCore(int byteCount) |
| 135 | + { |
| 136 | + Debug.Assert(AvailableLength < byteCount); |
| 137 | + |
| 138 | + if (_bytes.Length == 0) |
| 139 | + { |
| 140 | + Debug.Assert(_usePool && _activeStart == 0 && _availableStart == 0); |
| 141 | + _bytes = ArrayPool<byte>.Shared.Rent(byteCount); |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + int totalFree = _activeStart + AvailableLength; |
| 146 | + if (byteCount <= totalFree) |
| 147 | + { |
| 148 | + // We can free up enough space by just shifting the bytes down, so do so. |
| 149 | + Buffer.BlockCopy(_bytes, _activeStart, _bytes, 0, ActiveLength); |
| 150 | + _availableStart = ActiveLength; |
| 151 | + _activeStart = 0; |
| 152 | + Debug.Assert(byteCount <= AvailableLength); |
| 153 | + return; |
| 154 | + } |
| 155 | + |
| 156 | + int desiredSize = ActiveLength + byteCount; |
| 157 | + |
| 158 | + if ((uint)desiredSize > ArrayMaxLength) |
| 159 | + { |
| 160 | + throw new OutOfMemoryException(); |
| 161 | + } |
| 162 | + |
| 163 | + // Double the existing buffer size (capped at Array.MaxLength). |
| 164 | + int newSize = Math.Max(desiredSize, (int)Math.Min(ArrayMaxLength, 2 * (uint)_bytes.Length)); |
| 165 | + |
| 166 | + byte[] newBytes = _usePool ? |
| 167 | + ArrayPool<byte>.Shared.Rent(newSize) : |
| 168 | + new byte[newSize]; |
| 169 | + byte[] oldBytes = _bytes; |
| 170 | + |
| 171 | + if (ActiveLength != 0) |
| 172 | + { |
| 173 | + Buffer.BlockCopy(oldBytes, _activeStart, newBytes, 0, ActiveLength); |
| 174 | + } |
| 175 | + |
| 176 | + _availableStart = ActiveLength; |
| 177 | + _activeStart = 0; |
| 178 | + |
| 179 | + _bytes = newBytes; |
| 180 | + ReturnBufferIfPooled(oldBytes); |
| 181 | + |
| 182 | + Debug.Assert(byteCount <= AvailableLength); |
| 183 | + } |
| 184 | + |
| 185 | + public void Grow() |
| 186 | + { |
| 187 | + EnsureAvailableSpaceCore(AvailableLength + 1); |
| 188 | + } |
| 189 | + |
| 190 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 191 | + private void ReturnBufferIfPooled(byte[] buffer) |
| 192 | + { |
| 193 | + // The buffer may be Array.Empty<byte>() |
| 194 | + if (_usePool && buffer.Length > 0) |
| 195 | + { |
| 196 | + ArrayPool<byte>.Shared.Return(buffer); |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | +} |
0 commit comments