ReadOnlyMemory.cs 20 KB

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