Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
MemoryMarshal.cs
Go to the documentation of this file.
7
9
10public static class MemoryMarshal
11{
12 [MethodImpl(MethodImplOptions.AggressiveInlining)]
13 [Intrinsic]
15 public static ref T GetArrayDataReference<T>(T[] array)
16 {
17 return ref Unsafe.As<byte, T>(ref Unsafe.As<RawArrayData>(array).Data);
18 }
19
20 [MethodImpl(MethodImplOptions.AggressiveInlining)]
21 public unsafe static ref byte GetArrayDataReference(Array array)
22 {
23 return ref Unsafe.AddByteOffset(ref Unsafe.As<RawData>(array).Data, (nuint)RuntimeHelpers.GetMethodTable(array)->BaseSize - (nuint)(2 * sizeof(IntPtr)));
24 }
25
26 [MethodImpl(MethodImplOptions.AggressiveInlining)]
27 public static Span<byte> AsBytes<T>(Span<T> span) where T : struct
28 {
29 if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
30 {
32 }
33 return new Span<byte>(ref Unsafe.As<T, byte>(ref GetReference(span)), checked(span.Length * Unsafe.SizeOf<T>()));
34 }
35
36 [MethodImpl(MethodImplOptions.AggressiveInlining)]
38 {
39 if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
40 {
42 }
43 return new ReadOnlySpan<byte>(ref Unsafe.As<T, byte>(ref GetReference(span)), checked(span.Length * Unsafe.SizeOf<T>()));
44 }
45
50
52 {
53 return ref span._pointer.Value;
54 }
55
57 {
58 return ref span._pointer.Value;
59 }
60
61 [MethodImpl(MethodImplOptions.AggressiveInlining)]
63 {
64 if (span.Length == 0)
65 {
66 return ref Unsafe.AsRef<T>((void*)1);
67 }
68 return ref span._pointer.Value;
69 }
70
71 [MethodImpl(MethodImplOptions.AggressiveInlining)]
73 {
74 if (span.Length == 0)
75 {
76 return ref Unsafe.AsRef<T>((void*)1);
77 }
78 return ref span._pointer.Value;
79 }
80
81 [MethodImpl(MethodImplOptions.AggressiveInlining)]
83 {
84 if (RuntimeHelpers.IsReferenceOrContainsReferences<TFrom>())
85 {
87 }
88 if (RuntimeHelpers.IsReferenceOrContainsReferences<TTo>())
89 {
91 }
92 uint num = (uint)Unsafe.SizeOf<TFrom>();
93 uint num2 = (uint)Unsafe.SizeOf<TTo>();
94 uint length = (uint)span.Length;
95 int length2;
96 if (num == num2)
97 {
98 length2 = (int)length;
99 }
100 else if (num == 1)
101 {
102 length2 = (int)(length / num2);
103 }
104 else
105 {
106 ulong num3 = (ulong)((long)length * (long)num) / (ulong)num2;
107 length2 = checked((int)num3);
108 }
109 return new Span<TTo>(ref Unsafe.As<TFrom, TTo>(ref span._pointer.Value), length2);
110 }
111
112 [MethodImpl(MethodImplOptions.AggressiveInlining)]
114 {
115 if (RuntimeHelpers.IsReferenceOrContainsReferences<TFrom>())
116 {
118 }
119 if (RuntimeHelpers.IsReferenceOrContainsReferences<TTo>())
120 {
122 }
123 uint num = (uint)Unsafe.SizeOf<TFrom>();
124 uint num2 = (uint)Unsafe.SizeOf<TTo>();
125 uint length = (uint)span.Length;
126 int length2;
127 if (num == num2)
128 {
129 length2 = (int)length;
130 }
131 else if (num == 1)
132 {
133 length2 = (int)(length / num2);
134 }
135 else
136 {
137 ulong num3 = (ulong)((long)length * (long)num) / (ulong)num2;
138 length2 = checked((int)num3);
139 }
140 return new ReadOnlySpan<TTo>(ref Unsafe.As<TFrom, TTo>(ref GetReference(span)), length2);
141 }
142
143 [MethodImpl(MethodImplOptions.AggressiveInlining)]
145 {
146 return new Span<T>(ref reference, length);
147 }
148
149 [MethodImpl(MethodImplOptions.AggressiveInlining)]
151 {
152 return new ReadOnlySpan<T>(ref reference, length);
153 }
154
155 [CLSCompliant(false)]
157 {
158 if (value == null)
159 {
160 return default(ReadOnlySpan<char>);
161 }
162 return new ReadOnlySpan<char>(value, string.wcslen(value));
163 }
164
165 [CLSCompliant(false)]
167 {
168 if (value == null)
169 {
170 return default(ReadOnlySpan<byte>);
171 }
172 return new ReadOnlySpan<byte>(value, string.strlen(value));
173 }
174
176 {
177 int start;
178 int length;
179 object objectStartLength = memory.GetObjectStartLength(out start, out length);
180 if (objectStartLength != null && (!(typeof(T) == typeof(char)) || !(objectStartLength.GetType() == typeof(string))))
181 {
183 {
185 return true;
186 }
188 {
189 segment = new ArraySegment<T>(segment2.Array, segment2.Offset + start, length);
190 return true;
191 }
192 }
193 if (length == 0)
194 {
195 segment = ArraySegment<T>.Empty;
196 return true;
197 }
198 segment = default(ArraySegment<T>);
199 return false;
200 }
201
203 {
204 int start;
205 int length;
206 return (manager = memory.GetObjectStartLength(out start, out length) as TManager) != null;
207 }
208
210 {
211 if ((manager = memory.GetObjectStartLength(out start, out length) as TManager) == null)
212 {
213 start = 0;
214 length = 0;
215 return false;
216 }
217 return true;
218 }
219
221 {
222 for (int i = 0; i < memory.Length; i++)
223 {
224 yield return memory.Span[i];
225 }
226 }
227
228 public static bool TryGetString(ReadOnlyMemory<char> memory, [NotNullWhen(true)] out string? text, out int start, out int length)
229 {
230 if (memory.GetObjectStartLength(out var start2, out var length2) is string text2)
231 {
232 text = text2;
233 start = start2;
234 length = length2;
235 return true;
236 }
237 text = null;
238 start = 0;
239 length = 0;
240 return false;
241 }
242
243 [MethodImpl(MethodImplOptions.AggressiveInlining)]
244 public static T Read<T>(ReadOnlySpan<byte> source) where T : struct
245 {
246 if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
247 {
249 }
250 if (Unsafe.SizeOf<T>() > source.Length)
251 {
253 }
254 return Unsafe.ReadUnaligned<T>(ref GetReference(source));
255 }
256
257 [MethodImpl(MethodImplOptions.AggressiveInlining)]
258 public static bool TryRead<T>(ReadOnlySpan<byte> source, out T value) where T : struct
259 {
260 if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
261 {
263 }
264 if (Unsafe.SizeOf<T>() > (uint)source.Length)
265 {
266 value = default(T);
267 return false;
268 }
269 value = Unsafe.ReadUnaligned<T>(ref GetReference(source));
270 return true;
271 }
272
273 [MethodImpl(MethodImplOptions.AggressiveInlining)]
274 public static void Write<T>(Span<byte> destination, ref T value) where T : struct
275 {
276 if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
277 {
279 }
280 if ((uint)Unsafe.SizeOf<T>() > (uint)destination.Length)
281 {
283 }
284 Unsafe.WriteUnaligned(ref GetReference(destination), value);
285 }
286
287 [MethodImpl(MethodImplOptions.AggressiveInlining)]
288 public static bool TryWrite<T>(Span<byte> destination, ref T value) where T : struct
289 {
290 if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
291 {
293 }
294 if (Unsafe.SizeOf<T>() > (uint)destination.Length)
295 {
296 return false;
297 }
298 Unsafe.WriteUnaligned(ref GetReference(destination), value);
299 return true;
300 }
301
302 [MethodImpl(MethodImplOptions.AggressiveInlining)]
303 public static ref T AsRef<T>(Span<byte> span) where T : struct
304 {
305 if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
306 {
308 }
309 if (Unsafe.SizeOf<T>() > (uint)span.Length)
310 {
312 }
313 return ref Unsafe.As<byte, T>(ref GetReference(span));
314 }
315
316 [MethodImpl(MethodImplOptions.AggressiveInlining)]
317 public static ref readonly T AsRef<T>(ReadOnlySpan<byte> span) where T : struct
318 {
319 if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
320 {
322 }
323 if (Unsafe.SizeOf<T>() > (uint)span.Length)
324 {
326 }
327 return ref Unsafe.As<byte, T>(ref GetReference(span));
328 }
329
330 [MethodImpl(MethodImplOptions.AggressiveInlining)]
332 {
333 if (array == null)
334 {
335 if (start != 0 || length != 0)
336 {
338 }
339 return default(Memory<T>);
340 }
341 if (!typeof(T).IsValueType && array.GetType() != typeof(T[]))
342 {
344 }
345 if ((uint)start > (uint)array.Length || (uint)length > (uint)(array.Length - start))
346 {
348 }
349 return new Memory<T>((object)array, start | int.MinValue, length);
350 }
351}
static unsafe MethodTable * GetMethodTable(object obj)
static unsafe bool ObjectHasComponentSize(object obj)
static bool TryGetString(ReadOnlyMemory< char > memory, [NotNullWhen(true)] out string? text, out int start, out int length)
static unsafe ref T GetNonNullPinnableReference< T >(Span< T > span)
static Span< T > CreateSpan< T >(ref T reference, int length)
static IEnumerable< T > ToEnumerable< T >(ReadOnlyMemory< T > memory)
static Memory< T > CreateFromPinnedArray< T >(T[]? array, int start, int length)
static ref T AsRef< T >(Span< byte > span)
static bool TryRead< T >(ReadOnlySpan< byte > source, out T value)
static ReadOnlySpan< T > CreateReadOnlySpan< T >(ref T reference, int length)
static ref T GetReference< T >(Span< T > span)
static Span< byte > AsBytes< T >(Span< T > span)
static unsafe ReadOnlySpan< char > CreateReadOnlySpanFromNullTerminated(char *value)
static bool TryGetArray< T >(ReadOnlyMemory< T > memory, out ArraySegment< T > segment)
static void Write< T >(Span< byte > destination, ref T value)
static Span< TTo > Cast< TFrom, TTo >(Span< TFrom > span)
static T Read< T >(ReadOnlySpan< byte > source)
static unsafe ReadOnlySpan< byte > CreateReadOnlySpanFromNullTerminated(byte *value)
static ref T GetArrayDataReference< T >(T[] array)
static Memory< T > AsMemory< T >(ReadOnlyMemory< T > memory)
static bool TryGetMemoryManager< T, TManager >(ReadOnlyMemory< T > memory, [NotNullWhen(true)] out TManager? manager)
static bool TryWrite< T >(Span< byte > destination, ref T value)
static unsafe ref byte GetArrayDataReference(Array array)
static void ThrowArrayTypeMismatchException()
static void ThrowArgumentOutOfRangeException(System.ExceptionArgument argument)
static void ThrowInvalidTypeWithPointersNotSupported(Type targetType)