|
|
|
@ -3,6 +3,7 @@ |
|
|
|
// Ported from: https://github.com/SixLabors/Fonts/
|
|
|
|
|
|
|
|
using System; |
|
|
|
using System.Buffers; |
|
|
|
using System.Runtime.CompilerServices; |
|
|
|
|
|
|
|
namespace Avalonia.Utilities |
|
|
|
@ -11,7 +12,7 @@ namespace Avalonia.Utilities |
|
|
|
/// A helper type for avoiding allocations while building arrays.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T">The type of item contained in the array.</typeparam>
|
|
|
|
internal struct ArrayBuilder<T> |
|
|
|
internal struct ArrayBuilder<T> : IDisposable |
|
|
|
where T : struct |
|
|
|
{ |
|
|
|
private const int DefaultCapacity = 4; |
|
|
|
@ -135,7 +136,7 @@ namespace Avalonia.Utilities |
|
|
|
} |
|
|
|
|
|
|
|
// Same expansion algorithm as List<T>.
|
|
|
|
var newCapacity = length == 0 ? DefaultCapacity : (uint)length * 2u; |
|
|
|
var newCapacity = length == 0 ? DefaultCapacity : length * 2; |
|
|
|
|
|
|
|
if (newCapacity > MaxCoreClrArrayLength) |
|
|
|
{ |
|
|
|
@ -144,10 +145,11 @@ namespace Avalonia.Utilities |
|
|
|
|
|
|
|
if (newCapacity < min) |
|
|
|
{ |
|
|
|
newCapacity = (uint)min; |
|
|
|
newCapacity = min; |
|
|
|
} |
|
|
|
|
|
|
|
var array = new T[newCapacity]; |
|
|
|
Dispose(); |
|
|
|
var array = ArrayPool<T>.Shared.Rent(newCapacity); |
|
|
|
|
|
|
|
if (_size > 0) |
|
|
|
{ |
|
|
|
@ -180,5 +182,13 @@ namespace Avalonia.Utilities |
|
|
|
/// <returns>The <see cref="ArraySlice{T}"/>.</returns>
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
|
|
public ArraySlice<T> AsSlice(int start, int length) => new ArraySlice<T>(_data!, start, length); |
|
|
|
|
|
|
|
public void Dispose() |
|
|
|
{ |
|
|
|
if (_data != null) |
|
|
|
{ |
|
|
|
ArrayPool<T>.Shared.Return(_data); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|