Memory.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. /// Forms a slice out of the given memory, beginning at 'startIndex'
  232. /// </summary>
  233. /// <param name="startIndex">The index at which to begin this slice.</param>
  234. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  235. public Memory<T> Slice(Index startIndex)
  236. {
  237. int actualIndex = startIndex.GetOffset(_length);
  238. return Slice(actualIndex);
  239. }
  240. /// <summary>
  241. /// Forms a slice out of the given memory using the range start and end indexes.
  242. /// </summary>
  243. /// <param name="range">The range used to slice the memory using its start and end indexes.</param>
  244. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  245. public Memory<T> Slice(Range range)
  246. {
  247. (int start, int length) = range.GetOffsetAndLength(_length);
  248. // It is expected for _index + start to be negative if the memory is already pre-pinned.
  249. return new Memory<T>(_object, _index + start, length);
  250. }
  251. /// <summary>
  252. /// Forms a slice out of the given memory using the range start and end indexes.
  253. /// </summary>
  254. /// <param name="range">The range used to slice the memory using its start and end indexes.</param>
  255. public Memory<T> this[Range range] => Slice(range);
  256. /// <summary>
  257. /// Returns a span from the memory.
  258. /// </summary>
  259. public unsafe Span<T> Span
  260. {
  261. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  262. get
  263. {
  264. // This property getter has special support for returning a mutable Span<char> that wraps
  265. // an immutable String instance. This is obviously a dangerous feature and breaks type safety.
  266. // However, we need to handle the case where a ReadOnlyMemory<char> was created from a string
  267. // and then cast to a Memory<T>. Such a cast can only be done with unsafe or marshaling code,
  268. // in which case that's the dangerous operation performed by the dev, and we're just following
  269. // suit here to make it work as best as possible.
  270. ref T refToReturn = ref Unsafe.AsRef<T>(null);
  271. int lengthOfUnderlyingSpan = 0;
  272. // Copy this field into a local so that it can't change out from under us mid-operation.
  273. object tmpObject = _object;
  274. if (tmpObject != null)
  275. {
  276. if (typeof(T) == typeof(char) && tmpObject.GetType() == typeof(string))
  277. {
  278. // Special-case string since it's the most common for ROM<char>.
  279. refToReturn = ref Unsafe.As<char, T>(ref Unsafe.As<string>(tmpObject).GetRawStringData());
  280. lengthOfUnderlyingSpan = Unsafe.As<string>(tmpObject).Length;
  281. }
  282. else if (RuntimeHelpers.ObjectHasComponentSize(tmpObject))
  283. {
  284. // We know the object is not null, it's not a string, and it is variable-length. The only
  285. // remaining option is for it to be a T[] (or a U[] which is blittable to T[], like int[]
  286. // and uint[]). Otherwise somebody used private reflection to set this field, and we're not
  287. // too worried about type safety violations at this point.
  288. // 'tmpObject is T[]' below also handles things like int[] <-> uint[] being convertible
  289. Debug.Assert(tmpObject is T[]);
  290. refToReturn = ref Unsafe.As<byte, T>(ref Unsafe.As<T[]>(tmpObject).GetRawSzArrayData());
  291. lengthOfUnderlyingSpan = Unsafe.As<T[]>(tmpObject).Length;
  292. }
  293. else
  294. {
  295. // We know the object is not null, and it's not variable-length, so it must be a MemoryManager<T>.
  296. // Otherwise somebody used private reflection to set this field, and we're not too worried about
  297. // type safety violations at that point. Note that it can't be a MemoryManager<U>, even if U and
  298. // T are blittable (e.g., MemoryManager<int> to MemoryManager<uint>), since there exists no
  299. // constructor or other public API which would allow such a conversion.
  300. Debug.Assert(tmpObject is MemoryManager<T>);
  301. Span<T> memoryManagerSpan = Unsafe.As<MemoryManager<T>>(tmpObject).GetSpan();
  302. refToReturn = ref MemoryMarshal.GetReference(memoryManagerSpan);
  303. lengthOfUnderlyingSpan = memoryManagerSpan.Length;
  304. }
  305. // If the Memory<T> or ReadOnlyMemory<T> instance is torn, this property getter has undefined behavior.
  306. // We try to detect this condition and throw an exception, but it's possible that a torn struct might
  307. // appear to us to be valid, and we'll return an undesired span. Such a span is always guaranteed at
  308. // least to be in-bounds when compared with the original Memory<T> instance, so using the span won't
  309. // AV the process.
  310. int desiredStartIndex = _index & ReadOnlyMemory<T>.RemoveFlagsBitMask;
  311. int desiredLength = _length;
  312. #if BIT64
  313. // See comment in Span<T>.Slice for how this works.
  314. if ((ulong)(uint)desiredStartIndex + (ulong)(uint)desiredLength > (ulong)(uint)lengthOfUnderlyingSpan)
  315. {
  316. ThrowHelper.ThrowArgumentOutOfRangeException();
  317. }
  318. #else
  319. if ((uint)desiredStartIndex > (uint)lengthOfUnderlyingSpan || (uint)desiredLength > (uint)(lengthOfUnderlyingSpan - desiredStartIndex))
  320. {
  321. ThrowHelper.ThrowArgumentOutOfRangeException();
  322. }
  323. #endif
  324. refToReturn = ref Unsafe.Add(ref refToReturn, desiredStartIndex);
  325. lengthOfUnderlyingSpan = desiredLength;
  326. }
  327. return new Span<T>(ref refToReturn, lengthOfUnderlyingSpan);
  328. }
  329. }
  330. /// <summary>
  331. /// Copies the contents of the memory into the destination. If the source
  332. /// and destination overlap, this method behaves as if the original values are in
  333. /// a temporary location before the destination is overwritten.
  334. ///
  335. /// <param name="destination">The Memory to copy items into.</param>
  336. /// <exception cref="System.ArgumentException">
  337. /// Thrown when the destination is shorter than the source.
  338. /// </exception>
  339. /// </summary>
  340. public void CopyTo(Memory<T> destination) => Span.CopyTo(destination.Span);
  341. /// <summary>
  342. /// Copies the contents of the memory into the destination. If the source
  343. /// and destination overlap, this method behaves as if the original values are in
  344. /// a temporary location before the destination is overwritten.
  345. ///
  346. /// <returns>If the destination is shorter than the source, this method
  347. /// return false and no data is written to the destination.</returns>
  348. /// </summary>
  349. /// <param name="destination">The span to copy items into.</param>
  350. public bool TryCopyTo(Memory<T> destination) => Span.TryCopyTo(destination.Span);
  351. /// <summary>
  352. /// Creates a handle for the memory.
  353. /// The GC will not move the memory until the returned <see cref="MemoryHandle"/>
  354. /// is disposed, enabling taking and using the memory's address.
  355. /// <exception cref="System.ArgumentException">
  356. /// An instance with nonprimitive (non-blittable) members cannot be pinned.
  357. /// </exception>
  358. /// </summary>
  359. public unsafe MemoryHandle Pin()
  360. {
  361. // Just like the Span property getter, we have special support for a mutable Memory<char>
  362. // that wraps an immutable String instance. This might happen if a caller creates an
  363. // immutable ROM<char> wrapping a String, then uses Unsafe.As to create a mutable M<char>.
  364. // This needs to work, however, so that code that uses a single Memory<char> field to store either
  365. // a readable ReadOnlyMemory<char> or a writable Memory<char> can still be pinned and
  366. // used for interop purposes.
  367. // It's possible that the below logic could result in an AV if the struct
  368. // is torn. This is ok since the caller is expecting to use raw pointers,
  369. // and we're not required to keep this as safe as the other Span-based APIs.
  370. object tmpObject = _object;
  371. if (tmpObject != null)
  372. {
  373. if (typeof(T) == typeof(char) && tmpObject is string s)
  374. {
  375. GCHandle handle = GCHandle.Alloc(tmpObject, GCHandleType.Pinned);
  376. ref char stringData = ref Unsafe.Add(ref s.GetRawStringData(), _index);
  377. return new MemoryHandle(Unsafe.AsPointer(ref stringData), handle);
  378. }
  379. else if (RuntimeHelpers.ObjectHasComponentSize(tmpObject))
  380. {
  381. // 'tmpObject is T[]' below also handles things like int[] <-> uint[] being convertible
  382. Debug.Assert(tmpObject is T[]);
  383. // Array is already pre-pinned
  384. if (_index < 0)
  385. {
  386. void* pointer = Unsafe.Add<T>(Unsafe.AsPointer(ref Unsafe.As<T[]>(tmpObject).GetRawSzArrayData()), _index & ReadOnlyMemory<T>.RemoveFlagsBitMask);
  387. return new MemoryHandle(pointer);
  388. }
  389. else
  390. {
  391. GCHandle handle = GCHandle.Alloc(tmpObject, GCHandleType.Pinned);
  392. void* pointer = Unsafe.Add<T>(Unsafe.AsPointer(ref Unsafe.As<T[]>(tmpObject).GetRawSzArrayData()), _index);
  393. return new MemoryHandle(pointer, handle);
  394. }
  395. }
  396. else
  397. {
  398. Debug.Assert(tmpObject is MemoryManager<T>);
  399. return Unsafe.As<MemoryManager<T>>(tmpObject).Pin(_index);
  400. }
  401. }
  402. return default;
  403. }
  404. /// <summary>
  405. /// Copies the contents from the memory into a new array. This heap
  406. /// allocates, so should generally be avoided, however it is sometimes
  407. /// necessary to bridge the gap with APIs written in terms of arrays.
  408. /// </summary>
  409. public T[] ToArray() => Span.ToArray();
  410. /// <summary>
  411. /// Determines whether the specified object is equal to the current object.
  412. /// Returns true if the object is Memory or ReadOnlyMemory and if both objects point to the same array and have the same length.
  413. /// </summary>
  414. [EditorBrowsable(EditorBrowsableState.Never)]
  415. public override bool Equals(object obj)
  416. {
  417. if (obj is ReadOnlyMemory<T>)
  418. {
  419. return ((ReadOnlyMemory<T>)obj).Equals(this);
  420. }
  421. else if (obj is Memory<T> memory)
  422. {
  423. return Equals(memory);
  424. }
  425. else
  426. {
  427. return false;
  428. }
  429. }
  430. /// <summary>
  431. /// Returns true if the memory points to the same array and has the same length. Note that
  432. /// this does *not* check to see if the *contents* are equal.
  433. /// </summary>
  434. public bool Equals(Memory<T> other)
  435. {
  436. return
  437. _object == other._object &&
  438. _index == other._index &&
  439. _length == other._length;
  440. }
  441. /// <summary>
  442. /// Serves as the default hash function.
  443. /// </summary>
  444. [EditorBrowsable(EditorBrowsableState.Never)]
  445. public override int GetHashCode()
  446. {
  447. // We use RuntimeHelpers.GetHashCode instead of Object.GetHashCode because the hash
  448. // code is based on object identity and referential equality, not deep equality (as common with string).
  449. return (_object != null) ? HashCode.Combine(RuntimeHelpers.GetHashCode(_object), _index, _length) : 0;
  450. }
  451. }
  452. }