Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Memory.cs
Go to the documentation of this file.
8
9namespace System;
10
11[DebuggerTypeProxy(typeof(MemoryDebugView<>))]
12[DebuggerDisplay("{ToString(),raw}")]
13public readonly struct Memory<T> : IEquatable<Memory<T>>
14{
15 private readonly object _object;
16
17 private readonly int _index;
18
19 private readonly int _length;
20
21 public static Memory<T> Empty => default(Memory<T>);
22
23 public int Length => _length;
24
25 public bool IsEmpty => _length == 0;
26
27 public unsafe Span<T> Span
28 {
29 [MethodImpl(MethodImplOptions.AggressiveInlining)]
30 get
31 {
32 ref T ptr = ref Unsafe.NullRef<T>();
33 int length = 0;
34 object @object = _object;
35 if (@object != null)
36 {
37 if (typeof(T) == typeof(char) && @object.GetType() == typeof(string))
38 {
39 ptr = ref Unsafe.As<char, T>(ref Unsafe.As<string>(@object).GetRawStringData());
40 length = Unsafe.As<string>(@object).Length;
41 }
42 else if (RuntimeHelpers.ObjectHasComponentSize(@object))
43 {
44 ptr = ref MemoryMarshal.GetArrayDataReference(Unsafe.As<T[]>(@object));
45 length = Unsafe.As<T[]>(@object).Length;
46 }
47 else
48 {
49 Span<T> span = Unsafe.As<MemoryManager<T>>(@object).GetSpan();
50 ptr = ref MemoryMarshal.GetReference(span);
51 length = span.Length;
52 }
53 nuint num = (uint)_index & 0x7FFFFFFFu;
54 int length2 = _length;
55 if ((ulong)((long)num + (long)(uint)length2) > (ulong)(uint)length)
56 {
58 }
59 ptr = ref Unsafe.Add(ref ptr, (IntPtr)(void*)num);
61 }
62 return new Span<T>(ref ptr, length);
63 }
64 }
65
66 [MethodImpl(MethodImplOptions.AggressiveInlining)]
67 public Memory(T[]? array)
68 {
69 if (array == null)
70 {
71 this = default(Memory<T>);
72 return;
73 }
74 if (!typeof(T).IsValueType && array.GetType() != typeof(T[]))
75 {
77 }
78 _object = array;
79 _index = 0;
80 _length = array.Length;
81 }
82
83 [MethodImpl(MethodImplOptions.AggressiveInlining)]
84 internal Memory(T[] array, int start)
85 {
86 if (array == null)
87 {
88 if (start != 0)
89 {
91 }
92 this = default(Memory<T>);
93 return;
94 }
95 if (!typeof(T).IsValueType && array.GetType() != typeof(T[]))
96 {
98 }
99 if ((uint)start > (uint)array.Length)
100 {
102 }
103 _object = array;
104 _index = start;
105 _length = array.Length - start;
106 }
107
108 [MethodImpl(MethodImplOptions.AggressiveInlining)]
109 public Memory(T[]? array, int start, int length)
110 {
111 if (array == null)
112 {
113 if (start != 0 || length != 0)
114 {
116 }
117 this = default(Memory<T>);
118 return;
119 }
120 if (!typeof(T).IsValueType && array.GetType() != typeof(T[]))
121 {
123 }
124 if ((ulong)((long)(uint)start + (long)(uint)length) > (ulong)(uint)array.Length)
125 {
127 }
128 _object = array;
129 _index = start;
130 _length = length;
131 }
132
133 [MethodImpl(MethodImplOptions.AggressiveInlining)]
135 {
136 if (length < 0)
137 {
139 }
141 _index = 0;
142 _length = length;
143 }
144
145 [MethodImpl(MethodImplOptions.AggressiveInlining)]
147 {
148 if (length < 0 || start < 0)
149 {
151 }
153 _index = start;
154 _length = length;
155 }
156
157 [MethodImpl(MethodImplOptions.AggressiveInlining)]
158 internal Memory(object obj, int start, int length)
159 {
160 _object = obj;
161 _index = start;
162 _length = length;
163 }
164
165 public static implicit operator Memory<T>(T[]? array)
166 {
167 return new Memory<T>(array);
168 }
169
170 public static implicit operator Memory<T>(ArraySegment<T> segment)
171 {
172 return new Memory<T>(segment.Array, segment.Offset, segment.Count);
173 }
174
175 public static implicit operator ReadOnlyMemory<T>(Memory<T> memory)
176 {
177 return Unsafe.As<Memory<T>, ReadOnlyMemory<T>>(ref memory);
178 }
179
180 public override string ToString()
181 {
182 if (typeof(T) == typeof(char))
183 {
184 if (!(_object is string text))
185 {
186 return Span.ToString();
187 }
188 return text.Substring(_index, _length);
189 }
190 return $"System.Memory<{typeof(T).Name}>[{_length}]";
191 }
192
193 [MethodImpl(MethodImplOptions.AggressiveInlining)]
195 {
196 if ((uint)start > (uint)_length)
197 {
199 }
200 return new Memory<T>(_object, _index + start, _length - start);
201 }
202
203 [MethodImpl(MethodImplOptions.AggressiveInlining)]
204 public Memory<T> Slice(int start, int length)
205 {
206 if ((ulong)((long)(uint)start + (long)(uint)length) > (ulong)(uint)_length)
207 {
209 }
210 return new Memory<T>(_object, _index + start, length);
211 }
212
214 {
215 Span.CopyTo(destination.Span);
216 }
217
219 {
220 return Span.TryCopyTo(destination.Span);
221 }
222
223 public unsafe MemoryHandle Pin()
224 {
225 object @object = _object;
226 if (@object != null)
227 {
228 if (typeof(T) == typeof(char) && @object is string text)
229 {
230 GCHandle handle = GCHandle.Alloc(@object, GCHandleType.Pinned);
231 return new MemoryHandle(Unsafe.AsPointer(ref Unsafe.Add(ref text.GetRawStringData(), _index)), handle);
232 }
234 {
235 if (_index < 0)
236 {
237 void* pointer = Unsafe.Add<T>(Unsafe.AsPointer(ref MemoryMarshal.GetArrayDataReference(Unsafe.As<T[]>(@object))), _index & 0x7FFFFFFF);
238 return new MemoryHandle(pointer);
239 }
240 GCHandle handle2 = GCHandle.Alloc(@object, GCHandleType.Pinned);
241 void* pointer2 = Unsafe.Add<T>(Unsafe.AsPointer(ref MemoryMarshal.GetArrayDataReference(Unsafe.As<T[]>(@object))), _index);
242 return new MemoryHandle(pointer2, handle2);
243 }
244 return Unsafe.As<MemoryManager<T>>(@object).Pin(_index);
245 }
246 return default(MemoryHandle);
247 }
248
249 public T[] ToArray()
250 {
251 return Span.ToArray();
252 }
253
254 [EditorBrowsable(EditorBrowsableState.Never)]
255 public override bool Equals([NotNullWhen(true)] object? obj)
256 {
257 if (obj is ReadOnlyMemory<T> readOnlyMemory)
258 {
259 return readOnlyMemory.Equals(this);
260 }
261 if (obj is Memory<T> other)
262 {
263 return Equals(other);
264 }
265 return false;
266 }
267
268 public bool Equals(Memory<T> other)
269 {
270 if (_object == other._object && _index == other._index)
271 {
272 return _length == other._length;
273 }
274 return false;
275 }
276
277 [EditorBrowsable(EditorBrowsableState.Never)]
278 public override int GetHashCode()
279 {
280 if (_object == null)
281 {
282 return 0;
283 }
285 }
286}
static unsafe bool ObjectHasComponentSize(object obj)
static unsafe ref byte GetArrayDataReference(Array array)
static void ThrowArrayTypeMismatchException()
static void ThrowArgumentOutOfRangeException(System.ExceptionArgument argument)
readonly int _length
Definition Memory.cs:19
Memory(T[]? array, int start, int length)
Definition Memory.cs:109
unsafe MemoryHandle Pin()
Definition Memory.cs:223
T[] ToArray()
Definition Memory.cs:249
Memory(T[] array, int start)
Definition Memory.cs:84
Memory< T > Slice(int start, int length)
Definition Memory.cs:204
Memory(MemoryManager< T > manager, int length)
Definition Memory.cs:134
override int GetHashCode()
Definition Memory.cs:278
bool TryCopyTo(Memory< T > destination)
Definition Memory.cs:218
Memory< T > Slice(int start)
Definition Memory.cs:194
override bool Equals([NotNullWhen(true)] object? obj)
Definition Memory.cs:255
bool Equals(Memory< T > other)
Definition Memory.cs:268
override string ToString()
Definition Memory.cs:180
readonly object _object
Definition Memory.cs:15
readonly int _index
Definition Memory.cs:17
void CopyTo(Memory< T > destination)
Definition Memory.cs:213
bool IsEmpty
Definition Memory.cs:25
Memory(object obj, int start, int length)
Definition Memory.cs:158
Memory(T[]? array)
Definition Memory.cs:67
Memory(MemoryManager< T > manager, int start, int length)
Definition Memory.cs:146
static GCHandle Alloc(object? value)
Definition GCHandle.cs:81
void CopyTo(Span< T > destination)
Definition Span.cs:224
T[] ToArray()
Definition Span.cs:291
int Length
Definition Span.cs:70
override string ToString()
Definition Span.cs:261
bool TryCopyTo(Span< T > destination)
Definition Span.cs:236