forked from dotnet/winforms
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dotnet:main' into master
- Loading branch information
Showing
165 changed files
with
8,698 additions
and
817 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/System.Private.Windows.Core/src/System/Collections/Generic/ArrayBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Runtime.CompilerServices; | ||
|
||
namespace System.Collections.Generic; | ||
|
||
/// <summary> | ||
/// Array based collection that tries to avoid copying the internal array and caps the maximum capacity. | ||
/// </summary> | ||
/// <remarks> | ||
/// <para> | ||
/// To mitigate corrupted length attacks, the backing array has an initial allocation size cap. | ||
/// </para> | ||
/// </remarks> | ||
internal class ArrayBuilder<T> | ||
{ | ||
private const int MaxInitialArrayLength = 1024 * 10; | ||
|
||
private T[] _items; | ||
private readonly int _maxCount; | ||
private int _count; | ||
|
||
/// <param name="expectedCount"> | ||
/// The <see cref="_count"/> cannot grow past this value and is expected to be this value | ||
/// when the collection is "finished". | ||
/// </param> | ||
internal ArrayBuilder(int expectedCount) | ||
{ | ||
_items = new T[Math.Min(expectedCount, MaxInitialArrayLength)]; | ||
_maxCount = expectedCount; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public void Add(T item) | ||
{ | ||
T[] array = _items; | ||
int count = _count; | ||
if ((uint)count < (uint)_items.Length) | ||
{ | ||
_count = count + 1; | ||
array[count] = item; | ||
} | ||
else | ||
{ | ||
AddWithResize(item); | ||
} | ||
} | ||
|
||
private void AddWithResize(T item) | ||
{ | ||
if (_items.Length == _maxCount) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(item), "Collection is at max capacity."); | ||
} | ||
|
||
Debug.Assert(_count == _items.Length); | ||
int size = _count; | ||
|
||
Debug.Assert(_items.Length > 0); | ||
int newCapacity = Math.Min(_maxCount, 2 * _items.Length); | ||
|
||
Array.Resize(ref _items, newCapacity); | ||
_count = size + 1; | ||
_items[size] = item; | ||
} | ||
|
||
public T[] ToArray() | ||
{ | ||
Debug.Assert(_count == _maxCount); | ||
Debug.Assert(_count == _items.Length); | ||
|
||
return _items; | ||
} | ||
} |
Oops, something went wrong.