Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
SafeBuffer.cs
Go to the documentation of this file.
4
6
8{
9 private nuint _numBytes;
10
11 private static nuint Uninitialized => UIntPtr.MaxValue;
12
13 [CLSCompliant(false)]
14 public ulong ByteLength
15 {
16 get
17 {
19 {
20 throw NotInitialized();
21 }
22 return _numBytes;
23 }
24 }
25
26 protected SafeBuffer(bool ownsHandle)
27 : base(ownsHandle)
28 {
30 }
31
32 [CLSCompliant(false)]
33 public void Initialize(ulong numBytes)
34 {
35 if (IntPtr.Size == 4)
36 {
37 }
38 if (numBytes >= Uninitialized)
39 {
41 }
42 _numBytes = (nuint)numBytes;
43 }
44
45 [CLSCompliant(false)]
46 public void Initialize(uint numElements, uint sizeOfEachElement)
47 {
48 Initialize((ulong)numElements * (ulong)sizeOfEachElement);
49 }
50
51 [CLSCompliant(false)]
52 public void Initialize<T>(uint numElements) where T : struct
53 {
54 Initialize(numElements, AlignedSizeOf<T>());
55 }
56
57 [CLSCompliant(false)]
58 public unsafe void AcquirePointer(ref byte* pointer)
59 {
61 {
62 throw NotInitialized();
63 }
64 pointer = null;
65 bool success = false;
66 DangerousAddRef(ref success);
67 pointer = (byte*)(void*)handle;
68 }
69
70 public void ReleasePointer()
71 {
73 {
74 throw NotInitialized();
75 }
77 }
78
79 [CLSCompliant(false)]
80 public unsafe T Read<T>(ulong byteOffset) where T : struct
81 {
83 {
84 throw NotInitialized();
85 }
86 uint num = SizeOf<T>();
87 byte* ptr = (byte*)(void*)handle + byteOffset;
88 SpaceCheck(ptr, num);
89 T source = default(T);
90 bool success = false;
91 try
92 {
93 DangerousAddRef(ref success);
94 Buffer.Memmove(ref Unsafe.As<T, byte>(ref source), ref *ptr, num);
95 return source;
96 }
97 finally
98 {
99 if (success)
100 {
102 }
103 }
104 }
105
106 [CLSCompliant(false)]
107 public void ReadArray<T>(ulong byteOffset, T[] array, int index, int count) where T : struct
108 {
109 if (array == null)
110 {
111 throw new ArgumentNullException("array", SR.ArgumentNull_Buffer);
112 }
113 if (index < 0)
114 {
116 }
117 if (count < 0)
118 {
120 }
121 if (array.Length - index < count)
122 {
124 }
125 ReadSpan(byteOffset, new Span<T>(array, index, count));
126 }
127
128 [CLSCompliant(false)]
129 public unsafe void ReadSpan<T>(ulong byteOffset, Span<T> buffer) where T : struct
130 {
132 {
133 throw NotInitialized();
134 }
135 uint num = AlignedSizeOf<T>();
136 byte* ptr = (byte*)(void*)handle + byteOffset;
137 SpaceCheck(ptr, checked((nuint)(num * buffer.Length)));
138 bool success = false;
139 try
140 {
141 DangerousAddRef(ref success);
142 ref T reference = ref MemoryMarshal.GetReference(buffer);
143 for (int i = 0; i < buffer.Length; i++)
144 {
145 Buffer.Memmove(ref Unsafe.Add(ref reference, i), ref Unsafe.AsRef<T>(ptr + num * i), 1u);
146 }
147 }
148 finally
149 {
150 if (success)
151 {
153 }
154 }
155 }
156
157 [CLSCompliant(false)]
158 public unsafe void Write<T>(ulong byteOffset, T value) where T : struct
159 {
161 {
162 throw NotInitialized();
163 }
164 uint num = SizeOf<T>();
165 byte* ptr = (byte*)(void*)handle + byteOffset;
166 SpaceCheck(ptr, num);
167 bool success = false;
168 try
169 {
170 DangerousAddRef(ref success);
171 Buffer.Memmove(ref *ptr, ref Unsafe.As<T, byte>(ref value), num);
172 }
173 finally
174 {
175 if (success)
176 {
178 }
179 }
180 }
181
182 [CLSCompliant(false)]
183 public void WriteArray<T>(ulong byteOffset, T[] array, int index, int count) where T : struct
184 {
185 if (array == null)
186 {
187 throw new ArgumentNullException("array", SR.ArgumentNull_Buffer);
188 }
189 if (index < 0)
190 {
192 }
193 if (count < 0)
194 {
196 }
197 if (array.Length - index < count)
198 {
200 }
201 WriteSpan(byteOffset, new ReadOnlySpan<T>(array, index, count));
202 }
203
204 [CLSCompliant(false)]
205 public unsafe void WriteSpan<T>(ulong byteOffset, ReadOnlySpan<T> data) where T : struct
206 {
208 {
209 throw NotInitialized();
210 }
211 uint num = AlignedSizeOf<T>();
212 byte* ptr = (byte*)(void*)handle + byteOffset;
213 SpaceCheck(ptr, checked((nuint)(num * data.Length)));
214 bool success = false;
215 try
216 {
217 DangerousAddRef(ref success);
218 ref T reference = ref MemoryMarshal.GetReference(data);
219 for (int i = 0; i < data.Length; i++)
220 {
221 Buffer.Memmove(ref Unsafe.AsRef<T>(ptr + num * i), ref Unsafe.Add(ref reference, i), 1u);
222 }
223 }
224 finally
225 {
226 if (success)
227 {
229 }
230 }
231 }
232
233 private unsafe void SpaceCheck(byte* ptr, nuint sizeInBytes)
234 {
235 if (_numBytes < sizeInBytes)
236 {
238 }
239 if ((ulong)(ptr - (byte*)(void*)handle) > (ulong)(_numBytes - sizeInBytes))
240 {
242 }
243 }
244
245 private static void NotEnoughRoom()
246 {
248 }
249
254
255 internal static uint AlignedSizeOf<T>() where T : struct
256 {
257 uint num = SizeOf<T>();
258 if (num == 1 || num == 2)
259 {
260 return num;
261 }
262 return (uint)((num + 3) & -4);
263 }
264
265 internal static uint SizeOf<T>() where T : struct
266 {
267 if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
268 {
270 }
271 return (uint)Unsafe.SizeOf<T>();
272 }
273}
static void Memmove(ref byte dest, ref byte src, nuint len)
Definition Buffer.cs:215
unsafe void WriteSpan< T >(ulong byteOffset, ReadOnlySpan< T > data)
unsafe void SpaceCheck(byte *ptr, nuint sizeInBytes)
void ReadArray< T >(ulong byteOffset, T[] array, int index, int count)
unsafe T Read< T >(ulong byteOffset)
Definition SafeBuffer.cs:80
unsafe void Write< T >(ulong byteOffset, T value)
unsafe void ReadSpan< T >(ulong byteOffset, Span< T > buffer)
void Initialize< T >(uint numElements)
Definition SafeBuffer.cs:52
static InvalidOperationException NotInitialized()
void WriteArray< T >(ulong byteOffset, T[] array, int index, int count)
unsafe void AcquirePointer(ref byte *pointer)
Definition SafeBuffer.cs:58
void Initialize(uint numElements, uint sizeOfEachElement)
Definition SafeBuffer.cs:46
void DangerousAddRef(ref bool success)
Definition SafeHandle.cs:76
static string Arg_BufferTooSmall
Definition SR.cs:42
static string InvalidOperation_MustCallInitialize
Definition SR.cs:1468
static string ArgumentOutOfRange_UIntPtrMax
Definition SR.cs:1114
static string ArgumentNull_Buffer
Definition SR.cs:22
static string Argument_InvalidOffLen
Definition SR.cs:22
static string ArgumentOutOfRange_NeedNonNegNum
Definition SR.cs:32
static string Argument_NeedStructWithNoRefs
Definition SR.cs:796
Definition SR.cs:7
static int Size
Definition IntPtr.cs:21
static UIntPtr MaxValue
Definition UIntPtr.cs:31