| | | 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.Collections.Immutable; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.IO; |
| | | 7 | | using System.Reflection.Internal; |
| | | 8 | | using System.Runtime.CompilerServices; |
| | | 9 | | using System.Runtime.InteropServices; |
| | | 10 | | |
| | | 11 | | namespace System.Reflection.Metadata |
| | | 12 | | { |
| | | 13 | | [DebuggerDisplay("{GetDebuggerDisplay(),nq}")] |
| | | 14 | | public partial class BlobBuilder |
| | | 15 | | { |
| | | 16 | | // The implementation is akin to StringBuilder. |
| | | 17 | | // The differences: |
| | | 18 | | // - BlobBuilder allows efficient sequential write of the built content to a stream. |
| | | 19 | | // - BlobBuilder allows for chunk allocation customization. A custom allocator can use pooling strategy, for exa |
| | | 20 | | |
| | | 21 | | internal const int DefaultChunkSize = 256; |
| | | 22 | | |
| | | 23 | | // Must be at least the size of the largest primitive type we write atomically (Guid). |
| | | 24 | | internal const int MinChunkSize = 16; |
| | | 25 | | |
| | | 26 | | // Builders are linked like so: |
| | | 27 | | // |
| | | 28 | | // [1:first]->[2]->[3:last]<-[4:head] |
| | | 29 | | // ^_______________| |
| | | 30 | | // |
| | | 31 | | // In this case the content represented is a sequence (1,2,3,4). |
| | | 32 | | // This structure optimizes for append write operations and sequential enumeration from the start of the chain. |
| | | 33 | | // Data can only be written to the head node. Other nodes are "frozen". |
| | | 34 | | private BlobBuilder _nextOrPrevious; |
| | 0 | 35 | | private BlobBuilder FirstChunk => _nextOrPrevious._nextOrPrevious; |
| | | 36 | | |
| | | 37 | | // The sum of lengths of all preceding chunks (not including the current chunk), |
| | | 38 | | // or a difference between original buffer length of a builder that was linked as a suffix to another builder, |
| | | 39 | | // and the current length of the buffer (not that the buffers are swapped when suffix linking). |
| | | 40 | | private int _previousLengthOrFrozenSuffixLengthDelta; |
| | | 41 | | |
| | | 42 | | private byte[] _buffer; |
| | | 43 | | |
| | | 44 | | // The length of data in the buffer in lower 31 bits. |
| | | 45 | | // Head: highest bit is 0, length may be 0. |
| | | 46 | | // Non-head: highest bit is 1, lower 31 bits are not all 0. |
| | | 47 | | private uint _length; |
| | | 48 | | |
| | | 49 | | private const uint IsFrozenMask = 0x80000000; |
| | 0 | 50 | | internal bool IsHead => (_length & IsFrozenMask) == 0; |
| | 0 | 51 | | private int Length => (int)(_length & ~IsFrozenMask); |
| | 0 | 52 | | private uint FrozenLength => _length | IsFrozenMask; |
| | 0 | 53 | | private Span<byte> Span => _buffer.AsSpan(0, Length); |
| | | 54 | | |
| | 0 | 55 | | public BlobBuilder(int capacity = DefaultChunkSize) |
| | 0 | 56 | | { |
| | 0 | 57 | | if (capacity < 0) |
| | 0 | 58 | | { |
| | 0 | 59 | | Throw.ArgumentOutOfRange(nameof(capacity)); |
| | | 60 | | } |
| | | 61 | | |
| | 0 | 62 | | _nextOrPrevious = this; |
| | 0 | 63 | | _buffer = new byte[Math.Max(MinChunkSize, capacity)]; |
| | 0 | 64 | | } |
| | | 65 | | |
| | | 66 | | protected virtual BlobBuilder AllocateChunk(int minimalSize) |
| | 0 | 67 | | { |
| | 0 | 68 | | return new BlobBuilder(Math.Max(_buffer.Length, minimalSize)); |
| | 0 | 69 | | } |
| | | 70 | | |
| | | 71 | | protected virtual void FreeChunk() |
| | 0 | 72 | | { |
| | | 73 | | // nop |
| | 0 | 74 | | } |
| | | 75 | | |
| | | 76 | | public void Clear() |
| | 0 | 77 | | { |
| | 0 | 78 | | if (!IsHead) |
| | 0 | 79 | | { |
| | 0 | 80 | | Throw.InvalidOperationBuilderAlreadyLinked(); |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | // Swap buffer with the first chunk. |
| | | 84 | | // Note that we need to keep holding on all allocated buffers, |
| | | 85 | | // so that builders with custom allocator can release them. |
| | 0 | 86 | | var first = FirstChunk; |
| | 0 | 87 | | if (first != this) |
| | 0 | 88 | | { |
| | 0 | 89 | | var firstBuffer = first._buffer; |
| | 0 | 90 | | first._length = FrozenLength; |
| | 0 | 91 | | first._buffer = _buffer; |
| | 0 | 92 | | _buffer = firstBuffer; |
| | 0 | 93 | | } |
| | | 94 | | |
| | | 95 | | // free all chunks except for the current one |
| | 0 | 96 | | foreach (BlobBuilder chunk in GetChunks()) |
| | 0 | 97 | | { |
| | 0 | 98 | | if (chunk != this) |
| | 0 | 99 | | { |
| | 0 | 100 | | chunk.ClearAndFreeChunk(); |
| | 0 | 101 | | } |
| | 0 | 102 | | } |
| | | 103 | | |
| | 0 | 104 | | ClearChunk(); |
| | 0 | 105 | | } |
| | | 106 | | |
| | | 107 | | protected void Free() |
| | 0 | 108 | | { |
| | 0 | 109 | | Clear(); |
| | 0 | 110 | | FreeChunk(); |
| | 0 | 111 | | } |
| | | 112 | | |
| | | 113 | | // internal for testing |
| | | 114 | | internal void ClearChunk() |
| | 0 | 115 | | { |
| | 0 | 116 | | _length = 0; |
| | 0 | 117 | | _previousLengthOrFrozenSuffixLengthDelta = 0; |
| | 0 | 118 | | _nextOrPrevious = this; |
| | 0 | 119 | | } |
| | | 120 | | |
| | | 121 | | [Conditional("DEBUG")] |
| | | 122 | | private void CheckInvariants() |
| | 0 | 123 | | { |
| | 0 | 124 | | Debug.Assert(_buffer != null); |
| | 0 | 125 | | Debug.Assert(Length >= 0 && Length <= _buffer.Length); |
| | 0 | 126 | | Debug.Assert(_nextOrPrevious != null); |
| | | 127 | | |
| | 0 | 128 | | if (IsHead) |
| | 0 | 129 | | { |
| | 0 | 130 | | Debug.Assert(_previousLengthOrFrozenSuffixLengthDelta >= 0); |
| | | 131 | | |
| | | 132 | | // last chunk: |
| | 0 | 133 | | int totalLength = 0; |
| | 0 | 134 | | foreach (var chunk in GetChunks()) |
| | 0 | 135 | | { |
| | 0 | 136 | | Debug.Assert(chunk.IsHead || chunk.Length > 0); |
| | 0 | 137 | | totalLength += chunk.Length; |
| | 0 | 138 | | } |
| | | 139 | | |
| | 0 | 140 | | Debug.Assert(totalLength == Count); |
| | 0 | 141 | | } |
| | 0 | 142 | | } |
| | | 143 | | |
| | 0 | 144 | | public int Count => _previousLengthOrFrozenSuffixLengthDelta + Length; |
| | | 145 | | |
| | | 146 | | private int PreviousLength |
| | | 147 | | { |
| | | 148 | | get |
| | 0 | 149 | | { |
| | 0 | 150 | | Debug.Assert(IsHead); |
| | 0 | 151 | | return _previousLengthOrFrozenSuffixLengthDelta; |
| | 0 | 152 | | } |
| | | 153 | | set |
| | 0 | 154 | | { |
| | 0 | 155 | | Debug.Assert(IsHead); |
| | 0 | 156 | | _previousLengthOrFrozenSuffixLengthDelta = value; |
| | 0 | 157 | | } |
| | | 158 | | } |
| | | 159 | | |
| | 0 | 160 | | protected int FreeBytes => _buffer.Length - Length; |
| | | 161 | | |
| | | 162 | | // internal for testing |
| | 0 | 163 | | protected internal int ChunkCapacity => _buffer.Length; |
| | | 164 | | |
| | | 165 | | // internal for testing |
| | | 166 | | internal Chunks GetChunks() |
| | 0 | 167 | | { |
| | 0 | 168 | | if (!IsHead) |
| | 0 | 169 | | { |
| | 0 | 170 | | Throw.InvalidOperationBuilderAlreadyLinked(); |
| | | 171 | | } |
| | | 172 | | |
| | 0 | 173 | | return new Chunks(this); |
| | 0 | 174 | | } |
| | | 175 | | |
| | | 176 | | /// <summary> |
| | | 177 | | /// Returns a sequence of all blobs that represent the content of the builder. |
| | | 178 | | /// </summary> |
| | | 179 | | /// <exception cref="InvalidOperationException">Content is not available, the builder has been linked with anoth |
| | | 180 | | public Blobs GetBlobs() |
| | 0 | 181 | | { |
| | 0 | 182 | | if (!IsHead) |
| | 0 | 183 | | { |
| | 0 | 184 | | Throw.InvalidOperationBuilderAlreadyLinked(); |
| | | 185 | | } |
| | | 186 | | |
| | 0 | 187 | | return new Blobs(this); |
| | 0 | 188 | | } |
| | | 189 | | |
| | | 190 | | /// <summary> |
| | | 191 | | /// Compares the current content of this writer with another one. |
| | | 192 | | /// </summary> |
| | | 193 | | /// <exception cref="InvalidOperationException">Content is not available, the builder has been linked with anoth |
| | | 194 | | public bool ContentEquals(BlobBuilder other) |
| | 0 | 195 | | { |
| | 0 | 196 | | if (!IsHead) |
| | 0 | 197 | | { |
| | 0 | 198 | | Throw.InvalidOperationBuilderAlreadyLinked(); |
| | | 199 | | } |
| | | 200 | | |
| | 0 | 201 | | if (ReferenceEquals(this, other)) |
| | 0 | 202 | | { |
| | 0 | 203 | | return true; |
| | | 204 | | } |
| | | 205 | | |
| | 0 | 206 | | if (other == null) |
| | 0 | 207 | | { |
| | 0 | 208 | | return false; |
| | | 209 | | } |
| | | 210 | | |
| | 0 | 211 | | if (!other.IsHead) |
| | 0 | 212 | | { |
| | 0 | 213 | | Throw.InvalidOperationBuilderAlreadyLinked(); |
| | | 214 | | } |
| | | 215 | | |
| | 0 | 216 | | if (Count != other.Count) |
| | 0 | 217 | | { |
| | 0 | 218 | | return false; |
| | | 219 | | } |
| | | 220 | | |
| | 0 | 221 | | var leftEnumerator = GetChunks(); |
| | 0 | 222 | | var rightEnumerator = other.GetChunks(); |
| | 0 | 223 | | int leftStart = 0; |
| | 0 | 224 | | int rightStart = 0; |
| | | 225 | | |
| | 0 | 226 | | bool leftContinues = leftEnumerator.MoveNext(); |
| | 0 | 227 | | bool rightContinues = rightEnumerator.MoveNext(); |
| | | 228 | | |
| | 0 | 229 | | while (leftContinues && rightContinues) |
| | 0 | 230 | | { |
| | 0 | 231 | | Debug.Assert(leftStart == 0 || rightStart == 0); |
| | | 232 | | |
| | 0 | 233 | | var left = leftEnumerator.Current; |
| | 0 | 234 | | var right = rightEnumerator.Current; |
| | | 235 | | |
| | 0 | 236 | | int minLength = Math.Min(left.Length - leftStart, right.Length - rightStart); |
| | 0 | 237 | | if (!left._buffer.AsSpan(leftStart, minLength).SequenceEqual(right._buffer.AsSpan(rightStart, minLength) |
| | 0 | 238 | | { |
| | 0 | 239 | | return false; |
| | | 240 | | } |
| | | 241 | | |
| | 0 | 242 | | leftStart += minLength; |
| | 0 | 243 | | rightStart += minLength; |
| | | 244 | | |
| | | 245 | | // nothing remains in left chunk to compare: |
| | 0 | 246 | | if (leftStart == left.Length) |
| | 0 | 247 | | { |
| | 0 | 248 | | leftContinues = leftEnumerator.MoveNext(); |
| | 0 | 249 | | leftStart = 0; |
| | 0 | 250 | | } |
| | | 251 | | |
| | | 252 | | // nothing remains in left chunk to compare: |
| | 0 | 253 | | if (rightStart == right.Length) |
| | 0 | 254 | | { |
| | 0 | 255 | | rightContinues = rightEnumerator.MoveNext(); |
| | 0 | 256 | | rightStart = 0; |
| | 0 | 257 | | } |
| | 0 | 258 | | } |
| | | 259 | | |
| | 0 | 260 | | return leftContinues == rightContinues; |
| | 0 | 261 | | } |
| | | 262 | | |
| | | 263 | | /// <exception cref="InvalidOperationException">Content is not available, the builder has been linked with anoth |
| | | 264 | | public byte[] ToArray() |
| | 0 | 265 | | { |
| | 0 | 266 | | return ToArray(0, Count); |
| | 0 | 267 | | } |
| | | 268 | | |
| | | 269 | | /// <exception cref="ArgumentOutOfRangeException">Range specified by <paramref name="start"/> and <paramref name |
| | | 270 | | /// <exception cref="InvalidOperationException">Content is not available, the builder has been linked with anoth |
| | | 271 | | public byte[] ToArray(int start, int byteCount) |
| | 0 | 272 | | { |
| | 0 | 273 | | BlobUtilities.ValidateRange(Count, start, byteCount, nameof(byteCount)); |
| | | 274 | | |
| | 0 | 275 | | var result = new byte[byteCount]; |
| | | 276 | | |
| | 0 | 277 | | int chunkStart = 0; |
| | 0 | 278 | | int bufferStart = start; |
| | 0 | 279 | | int bufferEnd = start + byteCount; |
| | 0 | 280 | | foreach (var chunk in GetChunks()) |
| | 0 | 281 | | { |
| | 0 | 282 | | int chunkEnd = chunkStart + chunk.Length; |
| | 0 | 283 | | Debug.Assert(bufferStart >= chunkStart); |
| | | 284 | | |
| | 0 | 285 | | if (chunkEnd > bufferStart) |
| | 0 | 286 | | { |
| | 0 | 287 | | int bytesToCopy = Math.Min(bufferEnd, chunkEnd) - bufferStart; |
| | 0 | 288 | | Debug.Assert(bytesToCopy >= 0); |
| | | 289 | | |
| | 0 | 290 | | Array.Copy(chunk._buffer, bufferStart - chunkStart, result, bufferStart - start, bytesToCopy); |
| | 0 | 291 | | bufferStart += bytesToCopy; |
| | | 292 | | |
| | 0 | 293 | | if (bufferStart == bufferEnd) |
| | 0 | 294 | | { |
| | 0 | 295 | | break; |
| | | 296 | | } |
| | 0 | 297 | | } |
| | | 298 | | |
| | 0 | 299 | | chunkStart = chunkEnd; |
| | 0 | 300 | | } |
| | | 301 | | |
| | 0 | 302 | | Debug.Assert(bufferStart == bufferEnd); |
| | | 303 | | |
| | 0 | 304 | | return result; |
| | 0 | 305 | | } |
| | | 306 | | |
| | | 307 | | /// <exception cref="InvalidOperationException">Content is not available, the builder has been linked with anoth |
| | | 308 | | public ImmutableArray<byte> ToImmutableArray() |
| | 0 | 309 | | { |
| | 0 | 310 | | return ToImmutableArray(0, Count); |
| | 0 | 311 | | } |
| | | 312 | | |
| | | 313 | | /// <exception cref="ArgumentOutOfRangeException">Range specified by <paramref name="start"/> and <paramref name |
| | | 314 | | /// <exception cref="InvalidOperationException">Content is not available, the builder has been linked with anoth |
| | | 315 | | public ImmutableArray<byte> ToImmutableArray(int start, int byteCount) |
| | 0 | 316 | | { |
| | 0 | 317 | | byte[]? array = ToArray(start, byteCount); |
| | 0 | 318 | | return ImmutableCollectionsMarshal.AsImmutableArray(array); |
| | 0 | 319 | | } |
| | | 320 | | |
| | | 321 | | internal bool TryGetSpan(out ReadOnlySpan<byte> buffer) |
| | 0 | 322 | | { |
| | 0 | 323 | | if (_nextOrPrevious == this) |
| | 0 | 324 | | { |
| | | 325 | | // If the blob builder has one chunk, we can just return it and avoid copies. |
| | 0 | 326 | | buffer = Span; |
| | 0 | 327 | | return true; |
| | | 328 | | } |
| | | 329 | | |
| | 0 | 330 | | buffer = default; |
| | 0 | 331 | | return false; |
| | 0 | 332 | | } |
| | | 333 | | |
| | | 334 | | /// <exception cref="ArgumentNullException"><paramref name="destination"/> is null.</exception> |
| | | 335 | | /// <exception cref="InvalidOperationException">Content is not available, the builder has been linked with anoth |
| | | 336 | | public void WriteContentTo(Stream destination) |
| | 0 | 337 | | { |
| | 0 | 338 | | if (destination is null) |
| | 0 | 339 | | { |
| | 0 | 340 | | Throw.ArgumentNull(nameof(destination)); |
| | | 341 | | } |
| | | 342 | | |
| | 0 | 343 | | foreach (var chunk in GetChunks()) |
| | 0 | 344 | | { |
| | 0 | 345 | | destination.Write(chunk._buffer, 0, chunk.Length); |
| | 0 | 346 | | } |
| | 0 | 347 | | } |
| | | 348 | | |
| | | 349 | | /// <exception cref="ArgumentNullException"><paramref name="destination"/> is default(<see cref="BlobWriter"/>). |
| | | 350 | | /// <exception cref="InvalidOperationException">Content is not available, the builder has been linked with anoth |
| | | 351 | | public void WriteContentTo(ref BlobWriter destination) |
| | 0 | 352 | | { |
| | 0 | 353 | | if (destination.IsDefault) |
| | 0 | 354 | | { |
| | 0 | 355 | | Throw.ArgumentNull(nameof(destination)); |
| | | 356 | | } |
| | | 357 | | |
| | 0 | 358 | | foreach (var chunk in GetChunks()) |
| | 0 | 359 | | { |
| | 0 | 360 | | destination.WriteBytes(chunk.Span); |
| | 0 | 361 | | } |
| | 0 | 362 | | } |
| | | 363 | | |
| | | 364 | | /// <exception cref="ArgumentNullException"><paramref name="destination"/> is null.</exception> |
| | | 365 | | /// <exception cref="InvalidOperationException">Content is not available, the builder has been linked with anoth |
| | | 366 | | public void WriteContentTo(BlobBuilder destination) |
| | 0 | 367 | | { |
| | 0 | 368 | | if (destination is null) |
| | 0 | 369 | | { |
| | 0 | 370 | | Throw.ArgumentNull(nameof(destination)); |
| | | 371 | | } |
| | | 372 | | |
| | 0 | 373 | | foreach (var chunk in GetChunks()) |
| | 0 | 374 | | { |
| | 0 | 375 | | destination.WriteBytes(chunk.Span); |
| | 0 | 376 | | } |
| | 0 | 377 | | } |
| | | 378 | | |
| | | 379 | | /// <exception cref="ArgumentNullException"><paramref name="prefix"/> is null.</exception> |
| | | 380 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 381 | | public void LinkPrefix(BlobBuilder prefix) |
| | 0 | 382 | | { |
| | 0 | 383 | | if (prefix is null) |
| | 0 | 384 | | { |
| | 0 | 385 | | Throw.ArgumentNull(nameof(prefix)); |
| | | 386 | | } |
| | | 387 | | |
| | | 388 | | // TODO: consider copying data from right to left while there is space |
| | | 389 | | |
| | 0 | 390 | | if (!prefix.IsHead || !IsHead) |
| | 0 | 391 | | { |
| | 0 | 392 | | Throw.InvalidOperationBuilderAlreadyLinked(); |
| | | 393 | | } |
| | | 394 | | |
| | | 395 | | // avoid chaining empty chunks: |
| | 0 | 396 | | if (prefix.Count == 0) |
| | 0 | 397 | | { |
| | 0 | 398 | | prefix.ClearAndFreeChunk(); |
| | 0 | 399 | | return; |
| | | 400 | | } |
| | | 401 | | |
| | 0 | 402 | | PreviousLength += prefix.Count; |
| | | 403 | | |
| | | 404 | | // prefix is not a head anymore: |
| | 0 | 405 | | prefix._length = prefix.FrozenLength; |
| | | 406 | | |
| | | 407 | | // First and last chunks: |
| | | 408 | | // |
| | | 409 | | // [PrefixFirst]->[]->[PrefixLast] <- [prefix] [First]->[]->[Last] <- [this] |
| | | 410 | | // ^_________________| ^___________| |
| | | 411 | | // |
| | | 412 | | // Degenerate cases: |
| | | 413 | | // this == First == Last and/or prefix == PrefixFirst == PrefixLast. |
| | 0 | 414 | | var first = FirstChunk; |
| | 0 | 415 | | var prefixFirst = prefix.FirstChunk; |
| | 0 | 416 | | var last = _nextOrPrevious; |
| | 0 | 417 | | var prefixLast = prefix._nextOrPrevious; |
| | | 418 | | |
| | | 419 | | // Relink like so: |
| | | 420 | | // [PrefixFirst]->[]->[PrefixLast] -> [prefix] -> [First]->[]->[Last] <- [this] |
| | | 421 | | // ^________________________________________________________| |
| | | 422 | | |
| | 0 | 423 | | _nextOrPrevious = (last != this) ? last : prefix; |
| | 0 | 424 | | prefix._nextOrPrevious = (first != this) ? first : (prefixFirst != prefix) ? prefixFirst : prefix; |
| | | 425 | | |
| | 0 | 426 | | if (last != this) |
| | 0 | 427 | | { |
| | 0 | 428 | | last._nextOrPrevious = (prefixFirst != prefix) ? prefixFirst : prefix; |
| | 0 | 429 | | } |
| | | 430 | | |
| | 0 | 431 | | if (prefixLast != prefix) |
| | 0 | 432 | | { |
| | 0 | 433 | | prefixLast._nextOrPrevious = prefix; |
| | 0 | 434 | | } |
| | | 435 | | |
| | 0 | 436 | | prefix.CheckInvariants(); |
| | 0 | 437 | | CheckInvariants(); |
| | 0 | 438 | | } |
| | | 439 | | |
| | | 440 | | /// <exception cref="ArgumentNullException"><paramref name="suffix"/> is null.</exception> |
| | | 441 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 442 | | public void LinkSuffix(BlobBuilder suffix) |
| | 0 | 443 | | { |
| | 0 | 444 | | if (suffix is null) |
| | 0 | 445 | | { |
| | 0 | 446 | | Throw.ArgumentNull(nameof(suffix)); |
| | | 447 | | } |
| | | 448 | | |
| | | 449 | | // TODO: consider copying data from right to left while there is space |
| | | 450 | | |
| | 0 | 451 | | if (!IsHead || !suffix.IsHead) |
| | 0 | 452 | | { |
| | 0 | 453 | | Throw.InvalidOperationBuilderAlreadyLinked(); |
| | | 454 | | } |
| | | 455 | | |
| | | 456 | | // avoid chaining empty chunks: |
| | 0 | 457 | | if (suffix.Count == 0) |
| | 0 | 458 | | { |
| | 0 | 459 | | suffix.ClearAndFreeChunk(); |
| | 0 | 460 | | return; |
| | | 461 | | } |
| | | 462 | | |
| | 0 | 463 | | bool isEmpty = Count == 0; |
| | | 464 | | |
| | | 465 | | // swap buffers of the heads: |
| | 0 | 466 | | var suffixBuffer = suffix._buffer; |
| | 0 | 467 | | uint suffixLength = suffix._length; |
| | 0 | 468 | | int suffixPreviousLength = suffix.PreviousLength; |
| | 0 | 469 | | int oldSuffixLength = suffix.Length; |
| | 0 | 470 | | suffix._buffer = _buffer; |
| | 0 | 471 | | suffix._length = FrozenLength; // suffix is not a head anymore |
| | 0 | 472 | | _buffer = suffixBuffer; |
| | 0 | 473 | | _length = suffixLength; |
| | | 474 | | |
| | 0 | 475 | | PreviousLength += suffix.Length + suffixPreviousLength; |
| | | 476 | | |
| | | 477 | | // Update the _previousLength of the suffix so that suffix.Count = suffix._previousLength + suffix.Length do |
| | | 478 | | // Note that the resulting previous length might be negative. |
| | | 479 | | // The value is not used, other than for calculating the value of Count property. |
| | 0 | 480 | | suffix._previousLengthOrFrozenSuffixLengthDelta = suffixPreviousLength + oldSuffixLength - suffix.Length; |
| | | 481 | | |
| | 0 | 482 | | if (isEmpty) |
| | 0 | 483 | | { |
| | 0 | 484 | | var suffixLast = suffix._nextOrPrevious; |
| | 0 | 485 | | _nextOrPrevious = (suffixLast != suffix) ? suffixLast : this; |
| | 0 | 486 | | } |
| | | 487 | | else |
| | 0 | 488 | | { |
| | | 489 | | // First and last chunks: |
| | | 490 | | // |
| | | 491 | | // [First]->[]->[Last] <- [this] [SuffixFirst]->[]->[SuffixLast] <- [suffix] |
| | | 492 | | // ^___________| ^_________________| |
| | | 493 | | // |
| | | 494 | | // Degenerate cases: |
| | | 495 | | // this == First == Last and/or suffix == SuffixFirst == SuffixLast. |
| | 0 | 496 | | var first = FirstChunk; |
| | 0 | 497 | | var suffixFirst = suffix.FirstChunk; |
| | 0 | 498 | | var last = _nextOrPrevious; |
| | 0 | 499 | | var suffixLast = suffix._nextOrPrevious; |
| | | 500 | | |
| | | 501 | | // Relink like so: |
| | | 502 | | // [First]->[]->[Last] -> [suffix] -> [SuffixFirst]->[]->[SuffixLast] <- [this] |
| | | 503 | | // ^_______________________________________________________| |
| | 0 | 504 | | _nextOrPrevious = suffixLast; |
| | 0 | 505 | | suffix._nextOrPrevious = (suffixFirst != suffix) ? suffixFirst : (first != this) ? first : suffix; |
| | | 506 | | |
| | 0 | 507 | | if (last != this) |
| | 0 | 508 | | { |
| | 0 | 509 | | last._nextOrPrevious = suffix; |
| | 0 | 510 | | } |
| | | 511 | | |
| | 0 | 512 | | if (suffixLast != suffix) |
| | 0 | 513 | | { |
| | 0 | 514 | | suffixLast._nextOrPrevious = (first != this) ? first : suffix; |
| | 0 | 515 | | } |
| | 0 | 516 | | } |
| | | 517 | | |
| | 0 | 518 | | CheckInvariants(); |
| | 0 | 519 | | suffix.CheckInvariants(); |
| | 0 | 520 | | } |
| | | 521 | | |
| | | 522 | | private void AddLength(int value) |
| | 0 | 523 | | { |
| | 0 | 524 | | _length += (uint)value; |
| | 0 | 525 | | } |
| | | 526 | | |
| | | 527 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 528 | | private void Expand(int newLength) |
| | 0 | 529 | | { |
| | | 530 | | // TODO: consider converting the last chunk to a smaller one if there is too much empty space left |
| | | 531 | | |
| | | 532 | | // May happen only if the derived class attempts to write to a builder that is not last, |
| | | 533 | | // or if a builder prepended to another one is not discarded. |
| | 0 | 534 | | if (!IsHead) |
| | 0 | 535 | | { |
| | 0 | 536 | | Throw.InvalidOperationBuilderAlreadyLinked(); |
| | | 537 | | } |
| | | 538 | | |
| | 0 | 539 | | var newChunk = AllocateChunk(Math.Max(newLength, MinChunkSize)); |
| | 0 | 540 | | if (newChunk.ChunkCapacity < newLength) |
| | 0 | 541 | | { |
| | | 542 | | // The overridden allocator didn't provide large enough buffer: |
| | 0 | 543 | | throw new InvalidOperationException(SR.Format(SR.ReturnedBuilderSizeTooSmall, GetType(), nameof(Allocate |
| | | 544 | | } |
| | | 545 | | |
| | 0 | 546 | | var newBuffer = newChunk._buffer; |
| | | 547 | | |
| | 0 | 548 | | if (_length == 0) |
| | 0 | 549 | | { |
| | | 550 | | // If the first write into an empty buffer needs more space than the buffer provides, swap the buffers. |
| | 0 | 551 | | newChunk._buffer = _buffer; |
| | 0 | 552 | | _buffer = newBuffer; |
| | 0 | 553 | | } |
| | | 554 | | else |
| | 0 | 555 | | { |
| | | 556 | | // Otherwise append the new buffer. |
| | 0 | 557 | | var last = _nextOrPrevious; |
| | 0 | 558 | | var first = FirstChunk; |
| | | 559 | | |
| | 0 | 560 | | if (last == this) |
| | 0 | 561 | | { |
| | | 562 | | // single chunk in the chain |
| | 0 | 563 | | _nextOrPrevious = newChunk; |
| | 0 | 564 | | } |
| | | 565 | | else |
| | 0 | 566 | | { |
| | 0 | 567 | | newChunk._nextOrPrevious = first; |
| | 0 | 568 | | last._nextOrPrevious = newChunk; |
| | 0 | 569 | | _nextOrPrevious = newChunk; |
| | 0 | 570 | | } |
| | | 571 | | |
| | 0 | 572 | | newChunk._buffer = _buffer; |
| | 0 | 573 | | newChunk._length = FrozenLength; |
| | 0 | 574 | | newChunk._previousLengthOrFrozenSuffixLengthDelta = PreviousLength; |
| | | 575 | | |
| | 0 | 576 | | _buffer = newBuffer; |
| | 0 | 577 | | PreviousLength += Length; |
| | 0 | 578 | | _length = 0; |
| | 0 | 579 | | } |
| | | 580 | | |
| | 0 | 581 | | CheckInvariants(); |
| | 0 | 582 | | } |
| | | 583 | | |
| | | 584 | | /// <summary> |
| | | 585 | | /// Reserves a contiguous block of bytes. |
| | | 586 | | /// </summary> |
| | | 587 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="byteCount"/> is negative.</exception> |
| | | 588 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 589 | | public Blob ReserveBytes(int byteCount) |
| | 0 | 590 | | { |
| | 0 | 591 | | if (byteCount < 0) |
| | 0 | 592 | | { |
| | 0 | 593 | | Throw.ArgumentOutOfRange(nameof(byteCount)); |
| | | 594 | | } |
| | | 595 | | |
| | 0 | 596 | | int start = ReserveBytesImpl(byteCount); |
| | 0 | 597 | | Array.Clear(_buffer, start, byteCount); |
| | 0 | 598 | | return new Blob(_buffer, start, byteCount); |
| | 0 | 599 | | } |
| | | 600 | | |
| | | 601 | | private int ReserveBytesImpl(int byteCount) |
| | 0 | 602 | | { |
| | 0 | 603 | | Debug.Assert(byteCount >= 0); |
| | | 604 | | |
| | | 605 | | // If write is attempted to a frozen builder we fall back |
| | | 606 | | // to expand where an exception is thrown: |
| | 0 | 607 | | uint result = _length; |
| | 0 | 608 | | if (result > _buffer.Length - byteCount) |
| | 0 | 609 | | { |
| | 0 | 610 | | Expand(byteCount); |
| | 0 | 611 | | result = 0; |
| | 0 | 612 | | } |
| | | 613 | | |
| | 0 | 614 | | _length = result + (uint)byteCount; |
| | 0 | 615 | | return (int)result; |
| | 0 | 616 | | } |
| | | 617 | | |
| | | 618 | | private int ReserveBytesPrimitive(int byteCount) |
| | 0 | 619 | | { |
| | | 620 | | // If the primitive doesn't fit to the current chuck we'll allocate a new chunk that is at least MinChunkSiz |
| | | 621 | | // That chunk has to fit the primitive otherwise we might keep allocating new chunks and never end up with o |
| | 0 | 622 | | Debug.Assert(byteCount <= MinChunkSize); |
| | 0 | 623 | | return ReserveBytesImpl(byteCount); |
| | 0 | 624 | | } |
| | | 625 | | |
| | | 626 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="byteCount"/> is negative.</exception> |
| | | 627 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 628 | | public void WriteBytes(byte value, int byteCount) |
| | 0 | 629 | | { |
| | 0 | 630 | | if (byteCount < 0) |
| | 0 | 631 | | { |
| | 0 | 632 | | Throw.ArgumentOutOfRange(nameof(byteCount)); |
| | | 633 | | } |
| | | 634 | | |
| | 0 | 635 | | if (!IsHead) |
| | 0 | 636 | | { |
| | 0 | 637 | | Throw.InvalidOperationBuilderAlreadyLinked(); |
| | | 638 | | } |
| | | 639 | | |
| | 0 | 640 | | int bytesToCurrent = Math.Min(FreeBytes, byteCount); |
| | | 641 | | |
| | 0 | 642 | | _buffer.WriteBytes(Length, value, bytesToCurrent); |
| | 0 | 643 | | AddLength(bytesToCurrent); |
| | | 644 | | |
| | 0 | 645 | | int remaining = byteCount - bytesToCurrent; |
| | 0 | 646 | | if (remaining > 0) |
| | 0 | 647 | | { |
| | 0 | 648 | | Expand(remaining); |
| | | 649 | | |
| | 0 | 650 | | _buffer.WriteBytes(0, value, remaining); |
| | 0 | 651 | | AddLength(remaining); |
| | 0 | 652 | | } |
| | 0 | 653 | | } |
| | | 654 | | |
| | | 655 | | /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception> |
| | | 656 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="byteCount"/> is negative.</exception> |
| | | 657 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 658 | | public unsafe void WriteBytes(byte* buffer, int byteCount) |
| | 0 | 659 | | { |
| | 0 | 660 | | if (buffer is null) |
| | 0 | 661 | | { |
| | 0 | 662 | | Throw.ArgumentNull(nameof(buffer)); |
| | | 663 | | } |
| | | 664 | | |
| | 0 | 665 | | if (byteCount < 0) |
| | 0 | 666 | | { |
| | 0 | 667 | | Throw.ArgumentOutOfRange(nameof(byteCount)); |
| | | 668 | | } |
| | | 669 | | |
| | 0 | 670 | | if (!IsHead) |
| | 0 | 671 | | { |
| | 0 | 672 | | Throw.InvalidOperationBuilderAlreadyLinked(); |
| | | 673 | | } |
| | | 674 | | |
| | 0 | 675 | | WriteBytesUnchecked(new ReadOnlySpan<byte>(buffer, byteCount)); |
| | 0 | 676 | | } |
| | | 677 | | |
| | | 678 | | private void WriteBytesUnchecked(ReadOnlySpan<byte> buffer) |
| | 0 | 679 | | { |
| | 0 | 680 | | int bytesToCurrent = Math.Min(FreeBytes, buffer.Length); |
| | | 681 | | |
| | 0 | 682 | | buffer.Slice(0, bytesToCurrent).CopyTo(_buffer.AsSpan(Length)); |
| | | 683 | | |
| | 0 | 684 | | AddLength(bytesToCurrent); |
| | | 685 | | |
| | 0 | 686 | | ReadOnlySpan<byte> remaining = buffer.Slice(bytesToCurrent); |
| | 0 | 687 | | if (!remaining.IsEmpty) |
| | 0 | 688 | | { |
| | 0 | 689 | | Expand(remaining.Length); |
| | | 690 | | |
| | 0 | 691 | | remaining.CopyTo(_buffer); |
| | 0 | 692 | | AddLength(remaining.Length); |
| | 0 | 693 | | } |
| | 0 | 694 | | } |
| | | 695 | | |
| | | 696 | | /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception> |
| | | 697 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="byteCount"/> is negative.</exception> |
| | | 698 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 699 | | /// <returns>Bytes successfully written from the <paramref name="source" />.</returns> |
| | | 700 | | public int TryWriteBytes(Stream source, int byteCount) |
| | 0 | 701 | | { |
| | 0 | 702 | | if (source is null) |
| | 0 | 703 | | { |
| | 0 | 704 | | Throw.ArgumentNull(nameof(source)); |
| | | 705 | | } |
| | | 706 | | |
| | 0 | 707 | | if (byteCount < 0) |
| | 0 | 708 | | { |
| | 0 | 709 | | throw new ArgumentOutOfRangeException(nameof(byteCount)); |
| | | 710 | | } |
| | | 711 | | |
| | 0 | 712 | | if (byteCount == 0) |
| | 0 | 713 | | { |
| | 0 | 714 | | return 0; |
| | | 715 | | } |
| | | 716 | | |
| | 0 | 717 | | int bytesRead = 0; |
| | 0 | 718 | | int bytesToCurrent = Math.Min(FreeBytes, byteCount); |
| | | 719 | | |
| | 0 | 720 | | if (bytesToCurrent > 0) |
| | 0 | 721 | | { |
| | 0 | 722 | | bytesRead = source.TryReadAll(_buffer, Length, bytesToCurrent); |
| | 0 | 723 | | AddLength(bytesRead); |
| | | 724 | | |
| | 0 | 725 | | if (bytesRead != bytesToCurrent) |
| | 0 | 726 | | { |
| | 0 | 727 | | return bytesRead; |
| | | 728 | | } |
| | 0 | 729 | | } |
| | | 730 | | |
| | 0 | 731 | | int remaining = byteCount - bytesToCurrent; |
| | 0 | 732 | | if (remaining > 0) |
| | 0 | 733 | | { |
| | 0 | 734 | | Expand(remaining); |
| | 0 | 735 | | bytesRead = source.TryReadAll(_buffer, 0, remaining); |
| | 0 | 736 | | AddLength(bytesRead); |
| | | 737 | | |
| | 0 | 738 | | bytesRead += bytesToCurrent; |
| | 0 | 739 | | } |
| | | 740 | | |
| | 0 | 741 | | return bytesRead; |
| | 0 | 742 | | } |
| | | 743 | | |
| | | 744 | | /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception> |
| | | 745 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 746 | | public void WriteBytes(ImmutableArray<byte> buffer) |
| | 0 | 747 | | { |
| | 0 | 748 | | if (buffer.IsDefault) |
| | 0 | 749 | | { |
| | 0 | 750 | | Throw.ArgumentNull(nameof(buffer)); |
| | | 751 | | } |
| | | 752 | | |
| | 0 | 753 | | WriteBytes(buffer.AsSpan()); |
| | 0 | 754 | | } |
| | | 755 | | |
| | | 756 | | /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception> |
| | | 757 | | /// <exception cref="ArgumentOutOfRangeException">Range specified by <paramref name="start"/> and <paramref name |
| | | 758 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 759 | | public void WriteBytes(ImmutableArray<byte> buffer, int start, int byteCount) |
| | 0 | 760 | | { |
| | 0 | 761 | | if (buffer.IsDefault) |
| | 0 | 762 | | { |
| | 0 | 763 | | Throw.ArgumentNull(nameof(buffer)); |
| | | 764 | | } |
| | | 765 | | |
| | 0 | 766 | | BlobUtilities.ValidateRange(buffer.Length, start, byteCount, nameof(byteCount)); |
| | | 767 | | |
| | 0 | 768 | | WriteBytes(buffer.AsSpan(start, byteCount)); |
| | 0 | 769 | | } |
| | | 770 | | |
| | | 771 | | /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception> |
| | | 772 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 773 | | public void WriteBytes(byte[] buffer) |
| | 0 | 774 | | { |
| | 0 | 775 | | if (buffer is null) |
| | 0 | 776 | | { |
| | 0 | 777 | | Throw.ArgumentNull(nameof(buffer)); |
| | | 778 | | } |
| | | 779 | | |
| | 0 | 780 | | WriteBytes(buffer.AsSpan()); |
| | 0 | 781 | | } |
| | | 782 | | |
| | | 783 | | /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception> |
| | | 784 | | /// <exception cref="ArgumentOutOfRangeException">Range specified by <paramref name="start"/> and <paramref name |
| | | 785 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 786 | | public void WriteBytes(byte[] buffer, int start, int byteCount) |
| | 0 | 787 | | { |
| | 0 | 788 | | if (buffer is null) |
| | 0 | 789 | | { |
| | 0 | 790 | | Throw.ArgumentNull(nameof(buffer)); |
| | | 791 | | } |
| | | 792 | | |
| | 0 | 793 | | BlobUtilities.ValidateRange(buffer.Length, start, byteCount, nameof(byteCount)); |
| | | 794 | | |
| | 0 | 795 | | WriteBytes(buffer.AsSpan(start, byteCount)); |
| | 0 | 796 | | } |
| | | 797 | | |
| | | 798 | | internal void WriteBytes(ReadOnlySpan<byte> buffer) |
| | 0 | 799 | | { |
| | 0 | 800 | | if (!IsHead) |
| | 0 | 801 | | { |
| | 0 | 802 | | Throw.InvalidOperationBuilderAlreadyLinked(); |
| | | 803 | | } |
| | | 804 | | |
| | 0 | 805 | | WriteBytesUnchecked(buffer); |
| | 0 | 806 | | } |
| | | 807 | | |
| | | 808 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 809 | | public void PadTo(int position) |
| | 0 | 810 | | { |
| | 0 | 811 | | WriteBytes(0, position - Count); |
| | 0 | 812 | | } |
| | | 813 | | |
| | | 814 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 815 | | public void Align(int alignment) |
| | 0 | 816 | | { |
| | 0 | 817 | | int position = Count; |
| | 0 | 818 | | WriteBytes(0, BitArithmetic.Align(position, alignment) - position); |
| | 0 | 819 | | } |
| | | 820 | | |
| | | 821 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 822 | | public void WriteBoolean(bool value) |
| | 0 | 823 | | { |
| | 0 | 824 | | WriteByte((byte)(value ? 1 : 0)); |
| | 0 | 825 | | } |
| | | 826 | | |
| | | 827 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 828 | | public void WriteByte(byte value) |
| | 0 | 829 | | { |
| | 0 | 830 | | int start = ReserveBytesPrimitive(sizeof(byte)); |
| | 0 | 831 | | _buffer[start] = value; |
| | 0 | 832 | | } |
| | | 833 | | |
| | | 834 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 835 | | public void WriteSByte(sbyte value) |
| | 0 | 836 | | { |
| | 0 | 837 | | WriteByte(unchecked((byte)value)); |
| | 0 | 838 | | } |
| | | 839 | | |
| | | 840 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 841 | | public void WriteDouble(double value) |
| | 0 | 842 | | { |
| | 0 | 843 | | int start = ReserveBytesPrimitive(sizeof(double)); |
| | 0 | 844 | | _buffer.WriteDouble(start, value); |
| | 0 | 845 | | } |
| | | 846 | | |
| | | 847 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 848 | | public void WriteSingle(float value) |
| | 0 | 849 | | { |
| | 0 | 850 | | int start = ReserveBytesPrimitive(sizeof(float)); |
| | 0 | 851 | | _buffer.WriteSingle(start, value); |
| | 0 | 852 | | } |
| | | 853 | | |
| | | 854 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 855 | | public void WriteInt16(short value) |
| | 0 | 856 | | { |
| | 0 | 857 | | WriteUInt16(unchecked((ushort)value)); |
| | 0 | 858 | | } |
| | | 859 | | |
| | | 860 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 861 | | public void WriteUInt16(ushort value) |
| | 0 | 862 | | { |
| | 0 | 863 | | int start = ReserveBytesPrimitive(sizeof(ushort)); |
| | 0 | 864 | | _buffer.WriteUInt16(start, value); |
| | 0 | 865 | | } |
| | | 866 | | |
| | | 867 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 868 | | public void WriteInt16BE(short value) |
| | 0 | 869 | | { |
| | 0 | 870 | | WriteUInt16BE(unchecked((ushort)value)); |
| | 0 | 871 | | } |
| | | 872 | | |
| | | 873 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 874 | | public void WriteUInt16BE(ushort value) |
| | 0 | 875 | | { |
| | 0 | 876 | | int start = ReserveBytesPrimitive(sizeof(ushort)); |
| | 0 | 877 | | _buffer.WriteUInt16BE(start, value); |
| | 0 | 878 | | } |
| | | 879 | | |
| | | 880 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 881 | | public void WriteInt32BE(int value) |
| | 0 | 882 | | { |
| | 0 | 883 | | WriteUInt32BE(unchecked((uint)value)); |
| | 0 | 884 | | } |
| | | 885 | | |
| | | 886 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 887 | | public void WriteUInt32BE(uint value) |
| | 0 | 888 | | { |
| | 0 | 889 | | int start = ReserveBytesPrimitive(sizeof(uint)); |
| | 0 | 890 | | _buffer.WriteUInt32BE(start, value); |
| | 0 | 891 | | } |
| | | 892 | | |
| | | 893 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 894 | | public void WriteInt32(int value) |
| | 0 | 895 | | { |
| | 0 | 896 | | WriteUInt32(unchecked((uint)value)); |
| | 0 | 897 | | } |
| | | 898 | | |
| | | 899 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 900 | | public void WriteUInt32(uint value) |
| | 0 | 901 | | { |
| | 0 | 902 | | int start = ReserveBytesPrimitive(sizeof(uint)); |
| | 0 | 903 | | _buffer.WriteUInt32(start, value); |
| | 0 | 904 | | } |
| | | 905 | | |
| | | 906 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 907 | | public void WriteInt64(long value) |
| | 0 | 908 | | { |
| | 0 | 909 | | WriteUInt64(unchecked((ulong)value)); |
| | 0 | 910 | | } |
| | | 911 | | |
| | | 912 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 913 | | public void WriteUInt64(ulong value) |
| | 0 | 914 | | { |
| | 0 | 915 | | int start = ReserveBytesPrimitive(sizeof(ulong)); |
| | 0 | 916 | | _buffer.WriteUInt64(start, value); |
| | 0 | 917 | | } |
| | | 918 | | |
| | | 919 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 920 | | public void WriteDecimal(decimal value) |
| | 0 | 921 | | { |
| | 0 | 922 | | int start = ReserveBytesPrimitive(BlobUtilities.SizeOfSerializedDecimal); |
| | 0 | 923 | | _buffer.WriteDecimal(start, value); |
| | 0 | 924 | | } |
| | | 925 | | |
| | | 926 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 927 | | public void WriteGuid(Guid value) |
| | 0 | 928 | | { |
| | 0 | 929 | | int start = ReserveBytesPrimitive(BlobUtilities.SizeOfGuid); |
| | 0 | 930 | | _buffer.WriteGuid(start, value); |
| | 0 | 931 | | } |
| | | 932 | | |
| | | 933 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 934 | | public void WriteDateTime(DateTime value) |
| | 0 | 935 | | { |
| | 0 | 936 | | WriteInt64(value.Ticks); |
| | 0 | 937 | | } |
| | | 938 | | |
| | | 939 | | /// <summary> |
| | | 940 | | /// Writes a reference to a heap (heap offset) or a table (row number). |
| | | 941 | | /// </summary> |
| | | 942 | | /// <param name="reference">Heap offset or table row number.</param> |
| | | 943 | | /// <param name="isSmall">True to encode the reference as 16-bit integer, false to encode as 32-bit integer.</pa |
| | | 944 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 945 | | public void WriteReference(int reference, bool isSmall) |
| | 0 | 946 | | { |
| | | 947 | | // This code is a very hot path, hence we don't check if the reference actually fits 2B. |
| | | 948 | | |
| | 0 | 949 | | if (isSmall) |
| | 0 | 950 | | { |
| | 0 | 951 | | Debug.Assert(unchecked((ushort)reference) == reference); |
| | 0 | 952 | | WriteUInt16((ushort)reference); |
| | 0 | 953 | | } |
| | | 954 | | else |
| | 0 | 955 | | { |
| | 0 | 956 | | WriteInt32(reference); |
| | 0 | 957 | | } |
| | 0 | 958 | | } |
| | | 959 | | |
| | | 960 | | /// <summary> |
| | | 961 | | /// Writes UTF-16 (little-endian) encoded string at the current position. |
| | | 962 | | /// </summary> |
| | | 963 | | /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception> |
| | | 964 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 965 | | public void WriteUTF16(char[] value) |
| | 0 | 966 | | { |
| | 0 | 967 | | if (value is null) |
| | 0 | 968 | | { |
| | 0 | 969 | | Throw.ArgumentNull(nameof(value)); |
| | | 970 | | } |
| | | 971 | | |
| | 0 | 972 | | if (!IsHead) |
| | 0 | 973 | | { |
| | 0 | 974 | | Throw.InvalidOperationBuilderAlreadyLinked(); |
| | | 975 | | } |
| | | 976 | | |
| | 0 | 977 | | WriteUTF16(value.AsSpan()); |
| | 0 | 978 | | } |
| | | 979 | | |
| | | 980 | | /// <summary> |
| | | 981 | | /// Writes UTF-16 (little-endian) encoded string at the current position. |
| | | 982 | | /// </summary> |
| | | 983 | | /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception> |
| | | 984 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 985 | | public void WriteUTF16(string value) |
| | 0 | 986 | | { |
| | 0 | 987 | | if (value is null) |
| | 0 | 988 | | { |
| | 0 | 989 | | Throw.ArgumentNull(nameof(value)); |
| | | 990 | | } |
| | | 991 | | |
| | 0 | 992 | | if (!IsHead) |
| | 0 | 993 | | { |
| | 0 | 994 | | Throw.InvalidOperationBuilderAlreadyLinked(); |
| | | 995 | | } |
| | | 996 | | |
| | 0 | 997 | | WriteUTF16(value.AsSpan()); |
| | 0 | 998 | | } |
| | | 999 | | |
| | | 1000 | | private void WriteUTF16(ReadOnlySpan<char> value) |
| | 0 | 1001 | | { |
| | 0 | 1002 | | if (!IsHead) |
| | 0 | 1003 | | { |
| | 0 | 1004 | | Throw.InvalidOperationBuilderAlreadyLinked(); |
| | | 1005 | | } |
| | | 1006 | | |
| | 0 | 1007 | | if (BitConverter.IsLittleEndian) |
| | 0 | 1008 | | { |
| | 0 | 1009 | | WriteBytesUnchecked(MemoryMarshal.AsBytes(value)); |
| | 0 | 1010 | | } |
| | | 1011 | | else |
| | 0 | 1012 | | { |
| | 0 | 1013 | | foreach (char c in value) |
| | 0 | 1014 | | { |
| | 0 | 1015 | | WriteUInt16(c); |
| | 0 | 1016 | | } |
| | 0 | 1017 | | } |
| | 0 | 1018 | | } |
| | | 1019 | | |
| | | 1020 | | /// <summary> |
| | | 1021 | | /// Writes string in SerString format (see ECMA-335-II 23.3 Custom attributes). |
| | | 1022 | | /// </summary> |
| | | 1023 | | /// <remarks> |
| | | 1024 | | /// The string is UTF-8 encoded and prefixed by the its size in bytes. |
| | | 1025 | | /// Null string is represented as a single byte 0xFF. |
| | | 1026 | | /// </remarks> |
| | | 1027 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 1028 | | public void WriteSerializedString(string? value) |
| | 0 | 1029 | | { |
| | 0 | 1030 | | if (value == null) |
| | 0 | 1031 | | { |
| | 0 | 1032 | | WriteByte(0xff); |
| | 0 | 1033 | | return; |
| | | 1034 | | } |
| | | 1035 | | |
| | 0 | 1036 | | WriteUTF8(value, 0, value.Length, allowUnpairedSurrogates: true, prependSize: true); |
| | 0 | 1037 | | } |
| | | 1038 | | |
| | | 1039 | | /// <summary> |
| | | 1040 | | /// Writes string in User String (#US) heap format (see ECMA-335-II 24.2.4 #US and #Blob heaps): |
| | | 1041 | | /// </summary> |
| | | 1042 | | /// <remarks> |
| | | 1043 | | /// The string is UTF-16 encoded and prefixed by the its size in bytes. |
| | | 1044 | | /// |
| | | 1045 | | /// This final byte holds the value 1 if and only if any UTF-16 character within the string has any bit set in i |
| | | 1046 | | /// or its low byte is any of the following: 0x01-0x08, 0x0E-0x1F, 0x27, 0x2D, 0x7F. Otherwise, it holds 0. |
| | | 1047 | | /// The 1 signifies Unicode characters that require handling beyond that normally provided for 8-bit encoding se |
| | | 1048 | | /// </remarks> |
| | | 1049 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 1050 | | public void WriteUserString(string value) |
| | 0 | 1051 | | { |
| | 0 | 1052 | | if (value is null) |
| | 0 | 1053 | | { |
| | 0 | 1054 | | Throw.ArgumentNull(nameof(value)); |
| | | 1055 | | } |
| | | 1056 | | |
| | 0 | 1057 | | WriteCompressedInteger(BlobUtilities.GetUserStringByteLength(value.Length)); |
| | 0 | 1058 | | WriteUTF16(value); |
| | 0 | 1059 | | WriteByte(BlobUtilities.GetUserStringTrailingByte(value)); |
| | 0 | 1060 | | } |
| | | 1061 | | |
| | | 1062 | | /// <summary> |
| | | 1063 | | /// Writes UTF-8 encoded string at the current position. |
| | | 1064 | | /// </summary> |
| | | 1065 | | /// <param name="value">Constant value.</param> |
| | | 1066 | | /// <param name="allowUnpairedSurrogates"> |
| | | 1067 | | /// True to encode unpaired surrogates as specified, otherwise replace them with U+FFFD character. |
| | | 1068 | | /// </param> |
| | | 1069 | | /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception> |
| | | 1070 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 1071 | | public void WriteUTF8(string value, bool allowUnpairedSurrogates = true) |
| | 0 | 1072 | | { |
| | 0 | 1073 | | if (value is null) |
| | 0 | 1074 | | { |
| | 0 | 1075 | | Throw.ArgumentNull(nameof(value)); |
| | | 1076 | | } |
| | | 1077 | | |
| | 0 | 1078 | | WriteUTF8(value, 0, value.Length, allowUnpairedSurrogates, prependSize: false); |
| | 0 | 1079 | | } |
| | | 1080 | | |
| | | 1081 | | internal unsafe void WriteUTF8(string str, int start, int length, bool allowUnpairedSurrogates, bool prependSize |
| | 0 | 1082 | | { |
| | 0 | 1083 | | Debug.Assert(start >= 0); |
| | 0 | 1084 | | Debug.Assert(length >= 0); |
| | 0 | 1085 | | Debug.Assert(start + length <= str.Length); |
| | | 1086 | | |
| | 0 | 1087 | | if (!IsHead) |
| | 0 | 1088 | | { |
| | 0 | 1089 | | Throw.InvalidOperationBuilderAlreadyLinked(); |
| | | 1090 | | } |
| | | 1091 | | |
| | 0 | 1092 | | fixed (char* strPtr = str) |
| | 0 | 1093 | | { |
| | 0 | 1094 | | char* currentPtr = strPtr + start; |
| | | 1095 | | char* nextPtr; |
| | | 1096 | | |
| | | 1097 | | // the max size of compressed int is 4B: |
| | 0 | 1098 | | int byteLimit = FreeBytes - (prependSize ? sizeof(uint) : 0); |
| | | 1099 | | |
| | 0 | 1100 | | int bytesToCurrent = BlobUtilities.GetUTF8ByteCount(currentPtr, length, byteLimit, out nextPtr); |
| | 0 | 1101 | | int charsToCurrent = (int)(nextPtr - currentPtr); |
| | 0 | 1102 | | int charsToNext = length - charsToCurrent; |
| | 0 | 1103 | | int bytesToNext = BlobUtilities.GetUTF8ByteCount(nextPtr, charsToNext); |
| | | 1104 | | |
| | 0 | 1105 | | if (prependSize) |
| | 0 | 1106 | | { |
| | 0 | 1107 | | WriteCompressedInteger(bytesToCurrent + bytesToNext); |
| | 0 | 1108 | | } |
| | | 1109 | | |
| | 0 | 1110 | | _buffer.WriteUTF8(Length, currentPtr, charsToCurrent, bytesToCurrent, allowUnpairedSurrogates); |
| | 0 | 1111 | | AddLength(bytesToCurrent); |
| | | 1112 | | |
| | 0 | 1113 | | if (bytesToNext > 0) |
| | 0 | 1114 | | { |
| | 0 | 1115 | | Expand(bytesToNext); |
| | | 1116 | | |
| | 0 | 1117 | | _buffer.WriteUTF8(0, nextPtr, charsToNext, bytesToNext, allowUnpairedSurrogates); |
| | 0 | 1118 | | AddLength(bytesToNext); |
| | 0 | 1119 | | } |
| | 0 | 1120 | | } |
| | 0 | 1121 | | } |
| | | 1122 | | |
| | | 1123 | | /// <summary> |
| | | 1124 | | /// Implements compressed signed integer encoding as defined by ECMA-335-II chapter 23.2: Blobs and signatures. |
| | | 1125 | | /// </summary> |
| | | 1126 | | /// <remarks> |
| | | 1127 | | /// If the value lies between -64 (0xFFFFFFC0) and 63 (0x3F), inclusive, encode as a one-byte integer: |
| | | 1128 | | /// bit 7 clear, value bits 5 through 0 held in bits 6 through 1, sign bit (value bit 31) in bit 0. |
| | | 1129 | | /// |
| | | 1130 | | /// If the value lies between -8192 (0xFFFFE000) and 8191 (0x1FFF), inclusive, encode as a two-byte integer: |
| | | 1131 | | /// 15 set, bit 14 clear, value bits 12 through 0 held in bits 13 through 1, sign bit(value bit 31) in bit 0. |
| | | 1132 | | /// |
| | | 1133 | | /// If the value lies between -268435456 (0xF000000) and 268435455 (0x0FFFFFFF), inclusive, encode as a four-byt |
| | | 1134 | | /// 31 set, 30 set, bit 29 clear, value bits 27 through 0 held in bits 28 through 1, sign bit(value bit 31) in b |
| | | 1135 | | /// </remarks> |
| | | 1136 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> can't be represented as a compressed |
| | | 1137 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 1138 | | public void WriteCompressedSignedInteger(int value) |
| | 0 | 1139 | | { |
| | 0 | 1140 | | BlobWriterImpl.WriteCompressedSignedInteger(this, value); |
| | 0 | 1141 | | } |
| | | 1142 | | |
| | | 1143 | | /// <summary> |
| | | 1144 | | /// Implements compressed unsigned integer encoding as defined by ECMA-335-II chapter 23.2: Blobs and signatures |
| | | 1145 | | /// </summary> |
| | | 1146 | | /// <remarks> |
| | | 1147 | | /// If the value lies between 0 (0x00) and 127 (0x7F), inclusive, |
| | | 1148 | | /// encode as a one-byte integer (bit 7 is clear, value held in bits 6 through 0). |
| | | 1149 | | /// |
| | | 1150 | | /// If the value lies between 28 (0x80) and 214 - 1 (0x3FFF), inclusive, |
| | | 1151 | | /// encode as a 2-byte integer with bit 15 set, bit 14 clear (value held in bits 13 through 0). |
| | | 1152 | | /// |
| | | 1153 | | /// Otherwise, encode as a 4-byte integer, with bit 31 set, bit 30 set, bit 29 clear (value held in bits 28 thro |
| | | 1154 | | /// </remarks> |
| | | 1155 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> can't be represented as a compressed |
| | | 1156 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 1157 | | public void WriteCompressedInteger(int value) |
| | 0 | 1158 | | { |
| | 0 | 1159 | | BlobWriterImpl.WriteCompressedInteger(this, unchecked((uint)value)); |
| | 0 | 1160 | | } |
| | | 1161 | | |
| | | 1162 | | /// <summary> |
| | | 1163 | | /// Writes a constant value (see ECMA-335 Partition II section 22.9) at the current position. |
| | | 1164 | | /// </summary> |
| | | 1165 | | /// <exception cref="ArgumentException"><paramref name="value"/> is not of a constant type.</exception> |
| | | 1166 | | /// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</e |
| | | 1167 | | public void WriteConstant(object? value) |
| | 0 | 1168 | | { |
| | 0 | 1169 | | BlobWriterImpl.WriteConstant(this, value); |
| | 0 | 1170 | | } |
| | | 1171 | | |
| | | 1172 | | internal string GetDebuggerDisplay() |
| | 0 | 1173 | | { |
| | 0 | 1174 | | return IsHead ? |
| | 0 | 1175 | | string.Join("->", GetChunks().Select(chunk => $"[{Display(chunk._buffer, chunk.Length)}]")) : |
| | 0 | 1176 | | $"<{Display(_buffer, Length)}>"; |
| | 0 | 1177 | | } |
| | | 1178 | | |
| | | 1179 | | private static string Display(byte[] bytes, int length) |
| | 0 | 1180 | | { |
| | | 1181 | | const int MaxDisplaySize = 64; |
| | | 1182 | | |
| | 0 | 1183 | | return (length <= MaxDisplaySize) ? |
| | 0 | 1184 | | BitConverter.ToString(bytes, 0, length) : |
| | 0 | 1185 | | BitConverter.ToString(bytes, 0, MaxDisplaySize / 2) + "-...-" + BitConverter.ToString(bytes, length - Ma |
| | 0 | 1186 | | } |
| | | 1187 | | |
| | | 1188 | | private void ClearAndFreeChunk() |
| | 0 | 1189 | | { |
| | 0 | 1190 | | ClearChunk(); |
| | 0 | 1191 | | FreeChunk(); |
| | 0 | 1192 | | } |
| | | 1193 | | } |
| | | 1194 | | } |
| | | 1195 | | |