From 3fbfb968d068e0d4a08fcc97c538a0aa4b4a952a Mon Sep 17 00:00:00 2001 From: ikpil Date: Sat, 24 Feb 2024 20:00:51 +0900 Subject: [PATCH] test RcRentedArray --- src/DotRecast.Core/Buffers/RcRentedArray.cs | 27 +++++++------------ test/DotRecast.Core.Test/RcRentedArrayTest.cs | 2 +- 2 files changed, 10 insertions(+), 19 deletions(-) diff --git a/src/DotRecast.Core/Buffers/RcRentedArray.cs b/src/DotRecast.Core/Buffers/RcRentedArray.cs index 901b603..6e6cb11 100644 --- a/src/DotRecast.Core/Buffers/RcRentedArray.cs +++ b/src/DotRecast.Core/Buffers/RcRentedArray.cs @@ -6,7 +6,7 @@ namespace DotRecast.Core.Buffers { public static class RcRentedArray { - public static RcRentedArray RentDisposableArray(int minimumLength) + public static RcRentedArray Rent(int minimumLength) { var array = ArrayPool.Shared.Rent(minimumLength); return new RcRentedArray(ArrayPool.Shared, array, minimumLength); @@ -17,7 +17,6 @@ namespace DotRecast.Core.Buffers { private ArrayPool _owner; private T[] _array; - private readonly RcAtomicInteger _disposed; public int Length { get; } @@ -26,35 +25,27 @@ namespace DotRecast.Core.Buffers _owner = owner; _array = array; Length = length; - _disposed = new RcAtomicInteger(0); } - public T this[int index] + public ref T this[int index] { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { RcThrowHelper.ThrowExceptionIfIndexOutOfRange(index, Length); - return _array[index]; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - set - { - RcThrowHelper.ThrowExceptionIfIndexOutOfRange(index, Length); - _array[index] = value; + return ref _array[index]; } } public void Dispose() { - if (1 != _disposed.IncrementAndGet()) - return; - - _owner?.Return(_array, true); - _array = null; - _owner = null; + if (null != _owner && null != _array) + { + _owner.Return(_array, true); + _owner = null; + _array = null; + } } } } \ No newline at end of file diff --git a/test/DotRecast.Core.Test/RcRentedArrayTest.cs b/test/DotRecast.Core.Test/RcRentedArrayTest.cs index b099ffa..07d1f1b 100644 --- a/test/DotRecast.Core.Test/RcRentedArrayTest.cs +++ b/test/DotRecast.Core.Test/RcRentedArrayTest.cs @@ -31,7 +31,7 @@ public class RcRentedArrayTest { int length = Math.Max(2, (int)(rand.Next() * 2048)); var values = RandomValues(length); - using var array = RcRentedArray.RentDisposableArray(length); + using var array = RcRentedArray.Rent(length); for (int i = 0; i < array.Length; ++i) {