summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBen Adams <thundercat@illyriad.co.uk>2018-04-11 21:48:52 +0100
committerAhson Khan <ahkha@microsoft.com>2018-04-11 13:48:52 -0700
commitb800f37c504d30c25bda3bdf148966fa91d17381 (patch)
tree3b37fba03c702364790f19939f3b34dfb558c5e1 /src
parent2c51fd0a660a09469d3ee365fb9e081abaf04a27 (diff)
downloadcoreclr-b800f37c504d30c25bda3bdf148966fa91d17381.tar.gz
coreclr-b800f37c504d30c25bda3bdf148966fa91d17381.tar.bz2
coreclr-b800f37c504d30c25bda3bdf148966fa91d17381.zip
Remove MemoryManager.Length (#17498)
* Remove MemoryManager.Length * Feedback * XML comment nits
Diffstat (limited to 'src')
-rw-r--r--src/mscorlib/shared/System/Buffers/MemoryManager.cs28
-rw-r--r--src/mscorlib/shared/System/Memory.cs41
2 files changed, 51 insertions, 18 deletions
diff --git a/src/mscorlib/shared/System/Buffers/MemoryManager.cs b/src/mscorlib/shared/System/Buffers/MemoryManager.cs
index cc47e02a27..acba3d2a94 100644
--- a/src/mscorlib/shared/System/Buffers/MemoryManager.cs
+++ b/src/mscorlib/shared/System/Buffers/MemoryManager.cs
@@ -8,19 +8,14 @@ using System.Runtime.CompilerServices;
namespace System.Buffers
{
/// <summary>
- /// Manager of Memory<typeparamref name="T"/> that provides the implementation.
+ /// Manager of <see cref="System.Memory{T}"/> that provides the implementation.
/// </summary>
public abstract class MemoryManager<T> : IMemoryOwner<T>, IPinnable
{
/// <summary>
- /// The number of items in the Memory<typeparamref name="T"/>.
+ /// Returns a <see cref="System.Memory{T}"/>.
/// </summary>
- public virtual int Length => GetSpan().Length;
-
- /// <summary>
- /// Returns a Memory<typeparamref name="T"/>.
- /// </summary>
- public virtual Memory<T> Memory => new Memory<T>(this, 0, Length);
+ public virtual Memory<T> Memory => new Memory<T>(this, GetSpan().Length);
/// <summary>
/// Returns a span wrapping the underlying memory.
@@ -29,8 +24,8 @@ namespace System.Buffers
/// <summary>
/// Returns a handle to the memory that has been pinned and hence its address can be taken.
- /// <param name="elementIndex">The offset to the element within the memory at which the returned <see cref="MemoryHandle"/> points to. (default = 0)</param>
/// </summary>
+ /// <param name="elementIndex">The offset to the element within the memory at which the returned <see cref="MemoryHandle"/> points to. (default = 0)</param>
public abstract MemoryHandle Pin(int elementIndex = 0);
/// <summary>
@@ -39,6 +34,21 @@ namespace System.Buffers
public abstract void Unpin();
/// <summary>
+ /// Returns a <see cref="System.Memory{T}"/> for the current <see cref="MemoryManager{T}"/>.
+ /// </summary>
+ /// <param name="length">The element count in the memory, starting at offset 0.</param>
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ protected Memory<T> CreateMemory(int length) => new Memory<T>(this, length);
+
+ /// <summary>
+ /// Returns a <see cref="System.Memory{T}"/> for the current <see cref="MemoryManager{T}"/>.
+ /// </summary>
+ /// <param name="start">The offset to the element which the returned memory starts at.</param>
+ /// <param name="length">The element count in the memory, starting at element offset <paramref name="start"/>.</param>
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ protected Memory<T> CreateMemory(int start, int length) => new Memory<T>(this, start, length);
+
+ /// <summary>
/// Returns an array segment.
/// <remarks>Returns the default array segment if not overriden.</remarks>
/// </summary>
diff --git a/src/mscorlib/shared/System/Memory.cs b/src/mscorlib/shared/System/Memory.cs
index c3affff17d..a500ec5b92 100644
--- a/src/mscorlib/shared/System/Memory.cs
+++ b/src/mscorlib/shared/System/Memory.cs
@@ -115,27 +115,50 @@ namespace System
/// <summary>
/// Creates a new memory from a memory manager that provides specific method implementations beginning
+ /// at 0 index and ending at 'end' index (exclusive).
+ /// </summary>
+ /// <param name="manager">The memory manager.</param>
+ /// <param name="length">The number of items in the memory.</param>
+ /// <exception cref="System.ArgumentOutOfRangeException">
+ /// Thrown when the specified <paramref name="length"/> is negative.
+ /// </exception>
+ /// <remarks>For internal infrastructure only</remarks>
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ internal Memory(MemoryManager<T> manager, int length)
+ {
+ Debug.Assert(manager != null);
+
+ if (length < 0)
+ ThrowHelper.ThrowArgumentOutOfRangeException();
+
+ _object = manager;
+ _index = (1 << 31); // Mark as MemoryManager type
+ // Before using _index, check if _index < 0, then 'and' it with RemoveFlagsBitMask
+ _length = length;
+ }
+
+ /// <summary>
+ /// Creates a new memory from a memory manager that provides specific method implementations beginning
/// at 'start' index and ending at 'end' index (exclusive).
/// </summary>
/// <param name="manager">The memory manager.</param>
/// <param name="start">The index at which to begin the memory.</param>
/// <param name="length">The number of items in the memory.</param>
- /// <exception cref="System.ArgumentNullException">
- /// Thrown when <paramref name="manager"/> is null.
- /// </exception>
/// <exception cref="System.ArgumentOutOfRangeException">
- /// Thrown when the specified <paramref name="start"/> or end index is not in the range (&lt;0 or &gt;=Length).
+ /// Thrown when the specified <paramref name="start"/> or <paramref name="length"/> is negative.
/// </exception>
+ /// <remarks>For internal infrastructure only</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public Memory(MemoryManager<T> manager, int start, int length)
+ internal Memory(MemoryManager<T> manager, int start, int length)
{
- if (manager == null)
- ThrowHelper.ThrowArgumentNullException(ExceptionArgument.manager);
- if ((uint)start > (uint)manager.Length || (uint)length > (uint)(manager.Length - start))
+ Debug.Assert(manager != null);
+
+ if (length < 0 || start < 0)
ThrowHelper.ThrowArgumentOutOfRangeException();
_object = manager;
- _index = start | (1 << 31); // Before using _index, check if _index < 0, then 'and' it with RemoveFlagsBitMask
+ _index = start | (1 << 31); // Mark as MemoryManager type
+ // Before using _index, check if _index < 0, then 'and' it with RemoveFlagsBitMask
_length = length;
}