Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Hezium.Memory.Tests/CoreApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,23 @@ public void BigArray_ExtensionApis_Work()
array2.Fill(null);
array2[3] = 42;
Assert.Equal([null, null, null, 42, null, null, null], array2.ToArray());

BigArray<int> bigArray = GC.AllocateBigArray<int>(8, pinned: true);
for (int i = 0; i < bigArray.Length; ++i)
{
bigArray[i] = i * 2;
}

unsafe
{
fixed (int* ttPtr = bigArray)
{
for (int i = 0; i < bigArray.Length; ++i)
{
Assert.Equal(bigArray[i], *(ttPtr + i));
}
}
}
}

[Fact]
Expand Down
15 changes: 15 additions & 0 deletions Hezium.Memory/BigArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,19 @@ public Span<T> AsSpan(nint start, int length)

return MemoryMarshal.CreateSpan(ref Unsafe.Add(ref MemoryExtensions.GetBigArrayDataReference(this), start), length);
}

/// <summary>
/// Gets a reference to the element that can be used for pinning.
/// </summary>
/// <returns>A reference to the first element of the span, or a null reference if the array is empty.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ref T GetPinnableReference()
{
if (_length == 0)
{
return ref Unsafe.NullRef<T>();
}

return ref MemoryExtensions.GetBigArrayDataReference(this);
}
}
Loading