ReadOnlyMemory.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Buffers;
  5. using System.Diagnostics;
  6. using System.Runtime.CompilerServices;
  7. using System.Runtime.InteropServices;
  8. using EditorBrowsableAttribute = System.ComponentModel.EditorBrowsableAttribute;
  9. using EditorBrowsableState = System.ComponentModel.EditorBrowsableState;
  10. using Internal.Runtime.CompilerServices;
  11. namespace System
  12. {
  13. /// <summary>
  14. /// Represents a contiguous region of memory, similar to <see cref="ReadOnlySpan{T}"/>.
  15. /// Unlike <see cref="ReadOnlySpan{T}"/>, it is not a byref-like type.
  16. /// </summary>
  17. [DebuggerTypeProxy(typeof(MemoryDebugView<>))]
  18. [DebuggerDisplay("{ToString(),raw}")]
  19. public readonly struct ReadOnlyMemory<T>
  20. {
  21. // NOTE: With the current implementation, Memory<T> and ReadOnlyMemory<T> must have the same layout,
  22. // as code uses Unsafe.As to cast between them.
  23. // The highest order bit of _index is used to discern whether _object is a pre-pinned array.
  24. // (_index < 0) => _object is a pre-pinned array, so Pin() will not allocate a new GCHandle
  25. // (else) => Pin() needs to allocate a new GCHandle to pin the object.
  26. private readonly object _object;
  27. private readonly int _index;
  28. private readonly int _length;
  29. internal const int RemoveFlagsBitMask = 0x7FFFFFFF;
  30. /// <summary>
  31. /// Creates a new memory over the entirety of the target array.
  32. /// </summary>
  33. /// <param name="array">The target array.</param>
  34. /// <remarks>Returns default when <paramref name="array"/> is null.</remarks>
  35. /// <exception cref="System.ArrayTypeMismatchException">Thrown when <paramref name="array"/> is covariant and array's type is not exactly T[].</exception>
  36. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  37. public ReadOnlyMemory(T[] array)
  38. {
  39. if (array == null)
  40. {
  41. this = default;
  42. return; // returns default
  43. }
  44. _object = array;
  45. _index = 0;
  46. _length = array.Length;
  47. }
  48. /// <summary>
  49. /// Creates a new memory over the portion of the target array beginning
  50. /// at 'start' index and ending at 'end' index (exclusive).
  51. /// </summary>
  52. /// <param name="array">The target array.</param>
  53. /// <param name="start">The index at which to begin the memory.</param>
  54. /// <param name="length">The number of items in the memory.</param>
  55. /// <remarks>Returns default when <paramref name="array"/> is null.</remarks>
  56. /// <exception cref="System.ArrayTypeMismatchException">Thrown when <paramref name="array"/> is covariant and array's type is not exactly T[].</exception>
  57. /// <exception cref="System.ArgumentOutOfRangeException">
  58. /// Thrown when the specified <paramref name="start"/> or end index is not in the range (&lt;0 or &gt;=Length).
  59. /// </exception>
  60. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  61. public ReadOnlyMemory(T[] array, int start, int length)
  62. {
  63. if (array == null)
  64. {
  65. if (start != 0 || length != 0)
  66. ThrowHelper.ThrowArgumentOutOfRangeException();
  67. this = default;
  68. return; // returns default
  69. }
  70. #if BIT64
  71. // See comment in Span<T>.Slice for how this works.
  72. if ((ulong)(uint)start + (ulong)(uint)length > (ulong)(uint)array.Length)
  73. ThrowHelper.ThrowArgumentOutOfRangeException();
  74. #else
  75. if ((uint)start > (uint)array.Length || (uint)length > (uint)(array.Length - start))
  76. ThrowHelper.ThrowArgumentOutOfRangeException();
  77. #endif
  78. _object = array;
  79. _index = start;
  80. _length = length;
  81. }
  82. /// <summary>Creates a new memory over the existing object, start, and length. No validation is performed.</summary>
  83. /// <param name="obj">The target object.</param>
  84. /// <param name="start">The index at which to begin the memory.</param>
  85. /// <param name="length">The number of items in the memory.</param>
  86. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  87. internal ReadOnlyMemory(object obj, int start, int length)
  88. {
  89. // No validation performed in release builds; caller must provide any necessary validation.
  90. // 'obj is T[]' below also handles things like int[] <-> uint[] being convertible
  91. Debug.Assert((obj == null) || (typeof(T) == typeof(char) && obj is string) || (obj is T[]) || (obj is MemoryManager<T>));
  92. _object = obj;
  93. _index = start;
  94. _length = length;
  95. }
  96. /// <summary>
  97. /// Defines an implicit conversion of an array to a <see cref="ReadOnlyMemory{T}"/>
  98. /// </summary>
  99. public static implicit operator ReadOnlyMemory<T>(T[] array) => new ReadOnlyMemory<T>(array);
  100. /// <summary>
  101. /// Defines an implicit conversion of a <see cref="ArraySegment{T}"/> to a <see cref="ReadOnlyMemory{T}"/>
  102. /// </summary>
  103. public static implicit operator ReadOnlyMemory<T>(ArraySegment<T> segment) => new ReadOnlyMemory<T>(segment.Array, segment.Offset, segment.Count);
  104. /// <summary>
  105. /// Returns an empty <see cref="ReadOnlyMemory{T}"/>
  106. /// </summary>
  107. public static ReadOnlyMemory<T> Empty => default;
  108. /// <summary>
  109. /// The number of items in the memory.
  110. /// </summary>
  111. public int Length => _length;
  112. /// <summary>
  113. /// Returns true if Length is 0.
  114. /// </summary>
  115. public bool IsEmpty => _length == 0;
  116. /// <summary>
  117. /// For <see cref="ReadOnlyMemory{Char}"/>, returns a new instance of string that represents the characters pointed to by the memory.
  118. /// Otherwise, returns a <see cref="string"/> with the name of the type and the number of elements.
  119. /// </summary>
  120. public override string ToString()
  121. {
  122. if (typeof(T) == typeof(char))
  123. {
  124. return (_object is string str) ? str.Substring(_index, _length) : Span.ToString();
  125. }
  126. return string.Format("System.ReadOnlyMemory<{0}>[{1}]", typeof(T).Name, _length);
  127. }
  128. /// <summary>
  129. /// Forms a slice out of the given memory, beginning at 'start'.
  130. /// </summary>
  131. /// <param name="start">The index at which to begin this slice.</param>
  132. /// <exception cref="System.ArgumentOutOfRangeException">
  133. /// Thrown when the specified <paramref name="start"/> index is not in range (&lt;0 or &gt;=Length).
  134. /// </exception>
  135. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  136. public ReadOnlyMemory<T> Slice(int start)
  137. {
  138. if ((uint)start > (uint)_length)
  139. {
  140. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start);
  141. }
  142. // It is expected for _index + start to be negative if the memory is already pre-pinned.
  143. return new ReadOnlyMemory<T>(_object, _index + start, _length - start);
  144. }
  145. /// <summary>
  146. /// Forms a slice out of the given memory, beginning at 'start', of given length
  147. /// </summary>
  148. /// <param name="start">The index at which to begin this slice.</param>
  149. /// <param name="length">The desired length for the slice (exclusive).</param>
  150. /// <exception cref="System.ArgumentOutOfRangeException">
  151. /// Thrown when the specified <paramref name="start"/> or end index is not in range (&lt;0 or &gt;=Length).
  152. /// </exception>
  153. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  154. public ReadOnlyMemory<T> Slice(int start, int length)
  155. {
  156. #if BIT64
  157. // See comment in Span<T>.Slice for how this works.
  158. if ((ulong)(uint)start + (ulong)(uint)length > (ulong)(uint)_length)
  159. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start);
  160. #else
  161. if ((uint)start > (uint)_length || (uint)length > (uint)(_length - start))
  162. ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start);
  163. #endif
  164. // It is expected for _index + start to be negative if the memory is already pre-pinned.
  165. return new ReadOnlyMemory<T>(_object, _index + start, length);
  166. }
  167. /// <summary>
  168. /// Returns a span from the memory.
  169. /// </summary>
  170. public unsafe ReadOnlySpan<T> Span
  171. {
  172. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  173. get
  174. {
  175. ref T refToReturn = ref Unsafe.AsRef<T>(null);
  176. int lengthOfUnderlyingSpan = 0;
  177. // Copy this field into a local so that it can't change out from under us mid-operation.
  178. object tmpObject = _object;
  179. if (tmpObject != null)
  180. {
  181. if (typeof(T) == typeof(char) && tmpObject.GetType() == typeof(string))
  182. {
  183. // Special-case string since it's the most common for ROM<char>.
  184. refToReturn = ref Unsafe.As<char, T>(ref Unsafe.As<string>(tmpObject).GetRawStringData());
  185. lengthOfUnderlyingSpan = Unsafe.As<string>(tmpObject).Length;
  186. }
  187. else if (RuntimeHelpers.ObjectHasComponentSize(tmpObject))
  188. {
  189. // We know the object is not null, it's not a string, and it is variable-length. The only
  190. // remaining option is for it to be a T[] (or a U[] which is blittable to T[], like int[]
  191. // and uint[]). Otherwise somebody used private reflection to set this field, and we're not
  192. // too worried about type safety violations at this point.
  193. // 'tmpObject is T[]' below also handles things like int[] <-> uint[] being convertible
  194. Debug.Assert(tmpObject is T[]);
  195. refToReturn = ref Unsafe.As<byte, T>(ref Unsafe.As<T[]>(tmpObject).GetRawSzArrayData());
  196. lengthOfUnderlyingSpan = Unsafe.As<T[]>(tmpObject).Length;
  197. }
  198. else
  199. {
  200. // We know the object is not null, and it's not variable-length, so it must be a MemoryManager<T>.
  201. // Otherwise somebody used private reflection to set this field, and we're not too worried about
  202. // type safety violations at that point. Note that it can't be a MemoryManager<U>, even if U and
  203. // T are blittable (e.g., MemoryManager<int> to MemoryManager<uint>), since there exists no
  204. // constructor or other public API which would allow such a conversion.
  205. Debug.Assert(tmpObject is MemoryManager<T>);
  206. Span<T> memoryManagerSpan = Unsafe.As<MemoryManager<T>>(tmpObject).GetSpan();
  207. refToReturn = ref MemoryMarshal.GetReference(memoryManagerSpan);
  208. lengthOfUnderlyingSpan = memoryManagerSpan.Length;
  209. }
  210. // If the Memory<T> or ReadOnlyMemory<T> instance is torn, this property getter has undefined behavior.
  211. // We try to detect this condition and throw an exception, but it's possible that a torn struct might
  212. // appear to us to be valid, and we'll return an undesired span. Such a span is always guaranteed at
  213. // least to be in-bounds when compared with the original Memory<T> instance, so using the span won't
  214. // AV the process.
  215. int desiredStartIndex = _index & RemoveFlagsBitMask;
  216. int desiredLength = _length;
  217. #if BIT64
  218. // See comment in Span<T>.Slice for how this works.
  219. if ((ulong)(uint)desiredStartIndex + (ulong)(uint)desiredLength > (ulong)(uint)lengthOfUnderlyingSpan)
  220. {
  221. ThrowHelper.ThrowArgumentOutOfRangeException();
  222. }
  223. #else
  224. if ((uint)desiredStartIndex > (uint)lengthOfUnderlyingSpan || (uint)desiredLength > (uint)(lengthOfUnderlyingSpan - desiredStartIndex))
  225. {
  226. ThrowHelper.ThrowArgumentOutOfRangeException();
  227. }
  228. #endif
  229. refToReturn = ref Unsafe.Add(ref refToReturn, desiredStartIndex);
  230. lengthOfUnderlyingSpan = desiredLength;
  231. }
  232. return new ReadOnlySpan<T>(ref refToReturn, lengthOfUnderlyingSpan);
  233. }
  234. }
  235. /// <summary>
  236. /// Copies the contents of the read-only memory into the destination. If the source
  237. /// and destination overlap, this method behaves as if the original values are in
  238. /// a temporary location before the destination is overwritten.
  239. ///
  240. /// <param name="destination">The Memory to copy items into.</param>
  241. /// <exception cref="System.ArgumentException">
  242. /// Thrown when the destination is shorter than the source.
  243. /// </exception>
  244. /// </summary>
  245. public void CopyTo(Memory<T> destination) => Span.CopyTo(destination.Span);
  246. /// <summary>
  247. /// Copies the contents of the readonly-only memory into the destination. If the source
  248. /// and destination overlap, this method behaves as if the original values are in
  249. /// a temporary location before the destination is overwritten.
  250. ///
  251. /// <returns>If the destination is shorter than the source, this method
  252. /// return false and no data is written to the destination.</returns>
  253. /// </summary>
  254. /// <param name="destination">The span to copy items into.</param>
  255. public bool TryCopyTo(Memory<T> destination) => Span.TryCopyTo(destination.Span);
  256. /// <summary>
  257. /// Creates a handle for the memory.
  258. /// The GC will not move the memory until the returned <see cref="MemoryHandle"/>
  259. /// is disposed, enabling taking and using the memory's address.
  260. /// <exception cref="System.ArgumentException">
  261. /// An instance with nonprimitive (non-blittable) members cannot be pinned.
  262. /// </exception>
  263. /// </summary>
  264. public unsafe MemoryHandle Pin()
  265. {
  266. // It's possible that the below logic could result in an AV if the struct
  267. // is torn. This is ok since the caller is expecting to use raw pointers,
  268. // and we're not required to keep this as safe as the other Span-based APIs.
  269. object tmpObject = _object;
  270. if (tmpObject != null)
  271. {
  272. if (typeof(T) == typeof(char) && tmpObject is string s)
  273. {
  274. GCHandle handle = GCHandle.Alloc(tmpObject, GCHandleType.Pinned);
  275. ref char stringData = ref Unsafe.Add(ref s.GetRawStringData(), _index);
  276. return new MemoryHandle(Unsafe.AsPointer(ref stringData), handle);
  277. }
  278. else if (RuntimeHelpers.ObjectHasComponentSize(tmpObject))
  279. {
  280. // 'tmpObject is T[]' below also handles things like int[] <-> uint[] being convertible
  281. Debug.Assert(tmpObject is T[]);
  282. // Array is already pre-pinned
  283. if (_index < 0)
  284. {
  285. void* pointer = Unsafe.Add<T>(Unsafe.AsPointer(ref Unsafe.As<T[]>(tmpObject).GetRawSzArrayData()), _index & RemoveFlagsBitMask);
  286. return new MemoryHandle(pointer);
  287. }
  288. else
  289. {
  290. GCHandle handle = GCHandle.Alloc(tmpObject, GCHandleType.Pinned);
  291. void* pointer = Unsafe.Add<T>(Unsafe.AsPointer(ref Unsafe.As<T[]>(tmpObject).GetRawSzArrayData()), _index);
  292. return new MemoryHandle(pointer, handle);
  293. }
  294. }
  295. else
  296. {
  297. Debug.Assert(tmpObject is MemoryManager<T>);
  298. return Unsafe.As<MemoryManager<T>>(tmpObject).Pin(_index);
  299. }
  300. }
  301. return default;
  302. }
  303. /// <summary>
  304. /// Copies the contents from the memory into a new array. This heap
  305. /// allocates, so should generally be avoided, however it is sometimes
  306. /// necessary to bridge the gap with APIs written in terms of arrays.
  307. /// </summary>
  308. public T[] ToArray() => Span.ToArray();
  309. /// <summary>Determines whether the specified object is equal to the current object.</summary>
  310. [EditorBrowsable(EditorBrowsableState.Never)]
  311. public override bool Equals(object obj)
  312. {
  313. if (obj is ReadOnlyMemory<T> readOnlyMemory)
  314. {
  315. return Equals(readOnlyMemory);
  316. }
  317. else if (obj is Memory<T> memory)
  318. {
  319. return Equals(memory);
  320. }
  321. else
  322. {
  323. return false;
  324. }
  325. }
  326. /// <summary>
  327. /// Returns true if the memory points to the same array and has the same length. Note that
  328. /// this does *not* check to see if the *contents* are equal.
  329. /// </summary>
  330. public bool Equals(ReadOnlyMemory<T> other)
  331. {
  332. return
  333. _object == other._object &&
  334. _index == other._index &&
  335. _length == other._length;
  336. }
  337. /// <summary>Returns the hash code for this <see cref="ReadOnlyMemory{T}"/></summary>
  338. [EditorBrowsable(EditorBrowsableState.Never)]
  339. public override int GetHashCode()
  340. {
  341. // We use RuntimeHelpers.GetHashCode instead of Object.GetHashCode because the hash
  342. // code is based on object identity and referential equality, not deep equality (as common with string).
  343. return (_object != null) ? HashCode.Combine(RuntimeHelpers.GetHashCode(_object), _index, _length) : 0;
  344. }
  345. /// <summary>Gets the state of the memory as individual fields.</summary>
  346. /// <param name="start">The offset.</param>
  347. /// <param name="length">The count.</param>
  348. /// <returns>The object.</returns>
  349. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  350. internal object GetObjectStartLength(out int start, out int length)
  351. {
  352. start = _index;
  353. length = _length;
  354. return _object;
  355. }
  356. }
  357. }