forked from bit/DotRecastNetSim
test RcRentedArray
This commit is contained in:
parent
3c4723c907
commit
3fbfb968d0
|
@ -6,7 +6,7 @@ namespace DotRecast.Core.Buffers
|
||||||
{
|
{
|
||||||
public static class RcRentedArray
|
public static class RcRentedArray
|
||||||
{
|
{
|
||||||
public static RcRentedArray<T> RentDisposableArray<T>(int minimumLength)
|
public static RcRentedArray<T> Rent<T>(int minimumLength)
|
||||||
{
|
{
|
||||||
var array = ArrayPool<T>.Shared.Rent(minimumLength);
|
var array = ArrayPool<T>.Shared.Rent(minimumLength);
|
||||||
return new RcRentedArray<T>(ArrayPool<T>.Shared, array, minimumLength);
|
return new RcRentedArray<T>(ArrayPool<T>.Shared, array, minimumLength);
|
||||||
|
@ -17,7 +17,6 @@ namespace DotRecast.Core.Buffers
|
||||||
{
|
{
|
||||||
private ArrayPool<T> _owner;
|
private ArrayPool<T> _owner;
|
||||||
private T[] _array;
|
private T[] _array;
|
||||||
private readonly RcAtomicInteger _disposed;
|
|
||||||
|
|
||||||
public int Length { get; }
|
public int Length { get; }
|
||||||
|
|
||||||
|
@ -26,35 +25,27 @@ namespace DotRecast.Core.Buffers
|
||||||
_owner = owner;
|
_owner = owner;
|
||||||
_array = array;
|
_array = array;
|
||||||
Length = length;
|
Length = length;
|
||||||
_disposed = new RcAtomicInteger(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public T this[int index]
|
public ref T this[int index]
|
||||||
{
|
{
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
RcThrowHelper.ThrowExceptionIfIndexOutOfRange(index, Length);
|
RcThrowHelper.ThrowExceptionIfIndexOutOfRange(index, Length);
|
||||||
return _array[index];
|
return ref _array[index];
|
||||||
}
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
set
|
|
||||||
{
|
|
||||||
RcThrowHelper.ThrowExceptionIfIndexOutOfRange(index, Length);
|
|
||||||
_array[index] = value;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
if (1 != _disposed.IncrementAndGet())
|
if (null != _owner && null != _array)
|
||||||
return;
|
{
|
||||||
|
_owner.Return(_array, true);
|
||||||
_owner?.Return(_array, true);
|
_owner = null;
|
||||||
_array = null;
|
_array = null;
|
||||||
_owner = null;
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -31,7 +31,7 @@ public class RcRentedArrayTest
|
||||||
{
|
{
|
||||||
int length = Math.Max(2, (int)(rand.Next() * 2048));
|
int length = Math.Max(2, (int)(rand.Next() * 2048));
|
||||||
var values = RandomValues(length);
|
var values = RandomValues(length);
|
||||||
using var array = RcRentedArray.RentDisposableArray<int>(length);
|
using var array = RcRentedArray.Rent<int>(length);
|
||||||
|
|
||||||
for (int i = 0; i < array.Length; ++i)
|
for (int i = 0; i < array.Length; ++i)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue