-
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
124 additions
and
68 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using System.Buffers; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace DotRecast.Core.Buffers | ||
{ | ||
public static class RcRentedArray | ||
{ | ||
public static RcRentedArray<T> RentDisposableArray<T>(int minimumLength) | ||
{ | ||
var array = ArrayPool<T>.Shared.Rent(minimumLength); | ||
return new RcRentedArray<T>(ArrayPool<T>.Shared, array, minimumLength); | ||
} | ||
} | ||
|
||
public class RcRentedArray<T> : IDisposable | ||
{ | ||
private ArrayPool<T> _owner; | ||
private T[] _array; | ||
private readonly RcAtomicInteger _disposed; | ||
|
||
public int Length { get; } | ||
|
||
internal RcRentedArray(ArrayPool<T> owner, T[] array, int length) | ||
{ | ||
_owner = owner; | ||
_array = array; | ||
Length = length; | ||
_disposed = new RcAtomicInteger(0); | ||
} | ||
|
||
public T this[int index] | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
get | ||
{ | ||
ThrowHelper.ThrowExceptionIfIndexOutOfRange(index, Length); | ||
return _array[index]; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
set | ||
{ | ||
ThrowHelper.ThrowExceptionIfIndexOutOfRange(index, Length); | ||
_array[index] = value; | ||
} | ||
} | ||
|
||
|
||
public void Dispose() | ||
{ | ||
if (1 != _disposed.IncrementAndGet()) | ||
return; | ||
|
||
_owner?.Return(_array, true); | ||
_array = null; | ||
_owner = null; | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using DotRecast.Core.Buffers; | ||
using NUnit.Framework; | ||
|
||
namespace DotRecast.Core.Test; | ||
|
||
public class RcRentedArrayTest | ||
{ | ||
public List<int> RandomValues(int length) | ||
{ | ||
var rand = new RcRand(); | ||
|
||
// excepted values | ||
var list = new List<int>(); | ||
for (int i = 0; i < length; ++i) | ||
{ | ||
list.Add(rand.NextInt32()); | ||
} | ||
|
||
return list; | ||
} | ||
|
||
[Test] | ||
public void Test() | ||
{ | ||
var rand = new RcRand(); | ||
for (int loop = 0; loop < 1024; ++loop) | ||
{ | ||
int length = (int)(rand.Next() * 2048); | ||
var values = RandomValues(length); | ||
using var array = RcRentedArray.RentDisposableArray<float>(length); | ||
|
||
for (int i = 0; i < array.Length; ++i) | ||
{ | ||
array[i] = values[i]; | ||
} | ||
|
||
for (int i = 0; i < array.Length; ++i) | ||
{ | ||
Assert.That(array[i], Is.EqualTo(values[i])); | ||
} | ||
|
||
Assert.That(array[^1], Is.EqualTo(values[^1])); | ||
|
||
Assert.Throws<IndexOutOfRangeException>(() => array[-1] = 0); | ||
Assert.Throws<IndexOutOfRangeException>(() => array[array.Length + 1] = 0); | ||
Assert.Throws<IndexOutOfRangeException>(() => _ = array[-1]); | ||
Assert.Throws<IndexOutOfRangeException>(() => _ = array[array.Length + 1]); | ||
} | ||
} | ||
} |