Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
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
14 changes: 10 additions & 4 deletions src/mscorlib/shared/System/Memory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,16 @@ public override string ToString()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Memory<T> Slice(int start)
{
int actualLength = _length & RemoveFlagsBitMask;
// Used to maintain the high-bit which indicates whether the Memory has been pre-pinned or not.
int capturedLength = _length;
int actualLength = capturedLength & RemoveFlagsBitMask;
if ((uint)start > (uint)actualLength)
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start);
}

return new Memory<T>(_object, _index + start, actualLength - start);
// It is expected for (capturedLength - start) to be negative if the memory is already pre-pinned.
return new Memory<T>(_object, _index + start, capturedLength - start);
}

/// <summary>
Expand All @@ -245,13 +248,16 @@ public Memory<T> Slice(int start)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Memory<T> Slice(int start, int length)
{
int actualLength = _length & RemoveFlagsBitMask;
// Used to maintain the high-bit which indicates whether the Memory has been pre-pinned or not.
int capturedLength = _length;
int actualLength = capturedLength & RemoveFlagsBitMask;
if ((uint)start > (uint)actualLength || (uint)length > (uint)(actualLength - start))
{
ThrowHelper.ThrowArgumentOutOfRangeException();
}

return new Memory<T>(_object, _index + start, length);
// Set the high-bit to match the this._length high bit (1 for pre-pinned, 0 for unpinned).
return new Memory<T>(_object, _index + start, length | (capturedLength & ~RemoveFlagsBitMask));
}

/// <summary>
Expand Down
12 changes: 9 additions & 3 deletions src/mscorlib/shared/System/ReadOnlyMemory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,16 @@ public override string ToString()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ReadOnlyMemory<T> Slice(int start)
{
int actualLength = _length & RemoveFlagsBitMask;
// Used to maintain the high-bit which indicates whether the Memory has been pre-pinned or not.
int capturedLength = _length;
int actualLength = capturedLength & RemoveFlagsBitMask;
if ((uint)start > (uint)actualLength)
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start);
}

return new ReadOnlyMemory<T>(_object, _index + start, actualLength - start);
// It is expected for (capturedLength - start) to be negative if the memory is already pre-pinned.
return new ReadOnlyMemory<T>(_object, _index + start, capturedLength - start);
}

/// <summary>
Expand All @@ -167,13 +170,16 @@ public ReadOnlyMemory<T> Slice(int start)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ReadOnlyMemory<T> Slice(int start, int length)
{
// Used to maintain the high-bit which indicates whether the Memory has been pre-pinned or not.
int capturedLength = _length;
int actualLength = _length & RemoveFlagsBitMask;
if ((uint)start > (uint)actualLength || (uint)length > (uint)(actualLength - start))
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start);
}

return new ReadOnlyMemory<T>(_object, _index + start, length);
// Set the high-bit to match the this._length high bit (1 for pre-pinned, 0 for unpinned).
return new ReadOnlyMemory<T>(_object, _index + start, length | (capturedLength & ~RemoveFlagsBitMask));
}

/// <summary>
Expand Down