ReadOnlyMemory.cs 20 KB

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