Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit be089bc

Browse files
ahsonkhansafern
authored andcommitted
Fixing Retain to return MemoryHandle with correct PinnedPointer (#14248)
* Fixing Retain to return MemoryHandle with correct PinnedPointer. * Make MemoryHandle AddOffset internal. Signed-off-by: dotnet-bot-corefx-mirror <dotnet-bot@microsoft.com>
1 parent 55a68df commit be089bc

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

src/Common/src/CoreLib/System/Buffers/MemoryHandle.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ public unsafe struct MemoryHandle : IDisposable
2121
_handle = handle;
2222
}
2323

24+
internal void AddOffset(int offset)
25+
{
26+
if (_pointer == null)
27+
{
28+
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.pointer);
29+
}
30+
else
31+
{
32+
_pointer = (void*)((byte*)_pointer + offset);
33+
}
34+
}
35+
2436
[CLSCompliant(false)]
2537
public void* PinnedPointer => _pointer;
2638

src/Common/src/CoreLib/System/Memory.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ public unsafe MemoryHandle Retain(bool pin = false)
185185
if (_index < 0)
186186
{
187187
memoryHandle = ((OwnedMemory<T>)_arrayOrOwnedMemory).Pin();
188+
memoryHandle.AddOffset((_index & RemoveOwnedFlagBitMask) * Unsafe.SizeOf<T>());
188189
}
189190
else
190191
{

src/Common/src/CoreLib/System/ReadOnlyMemory.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ public unsafe MemoryHandle Retain(bool pin = false)
170170
if (_index < 0)
171171
{
172172
memoryHandle = ((OwnedMemory<T>)_arrayOrOwnedMemory).Pin();
173+
memoryHandle.AddOffset((_index & RemoveOwnedFlagBitMask) * Unsafe.SizeOf<T>());
173174
}
174175
else
175176
{

0 commit comments

Comments
 (0)