Memory.cs 22 KB

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