From: Ben Adams Date: Wed, 11 Apr 2018 20:48:52 +0000 (+0100) Subject: Remove MemoryManager.Length (#17498) X-Git-Tag: accepted/tizen/unified/20190422.045933~2353 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b800f37c504d30c25bda3bdf148966fa91d17381;p=platform%2Fupstream%2Fcoreclr.git Remove MemoryManager.Length (#17498) * Remove MemoryManager.Length * Feedback * XML comment nits --- diff --git a/src/mscorlib/shared/System/Buffers/MemoryManager.cs b/src/mscorlib/shared/System/Buffers/MemoryManager.cs index cc47e02..acba3d2 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 { /// - /// Manager of Memory that provides the implementation. + /// Manager of that provides the implementation. /// public abstract class MemoryManager : IMemoryOwner, IPinnable { /// - /// The number of items in the Memory. + /// Returns a . /// - public virtual int Length => GetSpan().Length; - - /// - /// Returns a Memory. - /// - public virtual Memory Memory => new Memory(this, 0, Length); + public virtual Memory Memory => new Memory(this, GetSpan().Length); /// /// Returns a span wrapping the underlying memory. @@ -29,8 +24,8 @@ namespace System.Buffers /// /// Returns a handle to the memory that has been pinned and hence its address can be taken. - /// The offset to the element within the memory at which the returned points to. (default = 0) /// + /// The offset to the element within the memory at which the returned points to. (default = 0) public abstract MemoryHandle Pin(int elementIndex = 0); /// @@ -39,6 +34,21 @@ namespace System.Buffers public abstract void Unpin(); /// + /// Returns a for the current . + /// + /// The element count in the memory, starting at offset 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + protected Memory CreateMemory(int length) => new Memory(this, length); + + /// + /// Returns a for the current . + /// + /// The offset to the element which the returned memory starts at. + /// The element count in the memory, starting at element offset . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + protected Memory CreateMemory(int start, int length) => new Memory(this, start, length); + + /// /// Returns an array segment. /// Returns the default array segment if not overriden. /// diff --git a/src/mscorlib/shared/System/Memory.cs b/src/mscorlib/shared/System/Memory.cs index c3affff..a500ec5 100644 --- a/src/mscorlib/shared/System/Memory.cs +++ b/src/mscorlib/shared/System/Memory.cs @@ -115,27 +115,50 @@ namespace System /// /// Creates a new memory from a memory manager that provides specific method implementations beginning + /// at 0 index and ending at 'end' index (exclusive). + /// + /// The memory manager. + /// The number of items in the memory. + /// + /// Thrown when the specified is negative. + /// + /// For internal infrastructure only + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal Memory(MemoryManager 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; + } + + /// + /// Creates a new memory from a memory manager that provides specific method implementations beginning /// at 'start' index and ending at 'end' index (exclusive). /// /// The memory manager. /// The index at which to begin the memory. /// The number of items in the memory. - /// - /// Thrown when is null. - /// /// - /// Thrown when the specified or end index is not in the range (<0 or >=Length). + /// Thrown when the specified or is negative. /// + /// For internal infrastructure only [MethodImpl(MethodImplOptions.AggressiveInlining)] - public Memory(MemoryManager manager, int start, int length) + internal Memory(MemoryManager 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; }