forked from MattRix/UnityDecompiled
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNativeArray.cs
More file actions
244 lines (212 loc) · 6.73 KB
/
Copy pathNativeArray.cs
File metadata and controls
244 lines (212 loc) · 6.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
namespace UnityEngine.Collections
{
[DebuggerDisplay("Length = {Length}"), DebuggerTypeProxy(typeof(NativeArrayDebugView<>)), NativeContainer, NativeContainerSupportsMinMaxWriteRestriction]
public struct NativeArray<T> : IDisposable, IEnumerable<T>, IEnumerable where T : struct
{
public struct Enumerator : IEnumerator<T>, IDisposable, IEnumerator
{
private NativeArray<T> array;
private int index;
object IEnumerator.Current
{
get
{
return this.Current;
}
}
public T Current
{
get
{
return this.array[this.index];
}
}
public Enumerator(ref NativeArray<T> array)
{
this.array = array;
this.index = -1;
}
public void Dispose()
{
}
public bool MoveNext()
{
this.index++;
return this.index < this.array.Length;
}
public void Reset()
{
this.index = -1;
}
}
private IntPtr m_Buffer;
private int m_Length;
private int m_Stride;
private readonly int m_MinIndex;
private readonly int m_MaxIndex;
private readonly AtomicSafetyHandle m_Safety;
private DisposeSentinel m_DisposeSentinel;
private readonly Allocator m_AllocatorLabel;
public int Length
{
get
{
return this.m_Length;
}
}
public unsafe T this[int index]
{
get
{
AtomicSafetyHandleVersionMask* ptr = (AtomicSafetyHandleVersionMask*)((void*)this.m_Safety.versionNode);
if ((this.m_Safety.version & AtomicSafetyHandleVersionMask.Read) == (AtomicSafetyHandleVersionMask)0 && this.m_Safety.version != (*ptr & AtomicSafetyHandleVersionMask.ReadInv))
{
AtomicSafetyHandle.CheckReadAndThrowNoEarlyOut(this.m_Safety);
}
if (index < this.m_MinIndex || index > this.m_MaxIndex)
{
this.FailOutOfRangeError(index);
}
return UnsafeUtility.ReadArrayElement<T>(this.m_Buffer, index * this.m_Stride);
}
set
{
AtomicSafetyHandleVersionMask* ptr = (AtomicSafetyHandleVersionMask*)((void*)this.m_Safety.versionNode);
if ((this.m_Safety.version & AtomicSafetyHandleVersionMask.Write) == (AtomicSafetyHandleVersionMask)0 && this.m_Safety.version != (*ptr & AtomicSafetyHandleVersionMask.WriteInv))
{
AtomicSafetyHandle.CheckWriteAndThrowNoEarlyOut(this.m_Safety);
}
if (index < this.m_MinIndex || index > this.m_MaxIndex)
{
this.FailOutOfRangeError(index);
}
UnsafeUtility.WriteArrayElement<T>(this.m_Buffer, index * this.m_Stride, value);
}
}
public NativeArray(int length, Allocator allocMode)
{
NativeArray<T>.Allocate(length, allocMode, out this);
}
public NativeArray(T[] array, Allocator allocMode)
{
if (array == null)
{
throw new ArgumentNullException("array");
}
NativeArray<T>.Allocate(array.Length, allocMode, out this);
this.FromArray(array);
}
internal NativeArray(IntPtr dataPointer, int length)
{
this = new NativeArray<T>(dataPointer, length, Allocator.None);
}
internal NativeArray(IntPtr dataPointer, int length, int stride, AtomicSafetyHandle safety, Allocator allocMode)
{
this.m_Buffer = dataPointer;
this.m_Length = length;
this.m_Stride = stride;
this.m_AllocatorLabel = allocMode;
this.m_MinIndex = 0;
this.m_MaxIndex = length - 1;
this.m_Safety = safety;
this.m_DisposeSentinel = null;
}
private NativeArray(IntPtr dataPointer, int length, Allocator allocMode)
{
if (dataPointer == IntPtr.Zero)
{
throw new ArgumentOutOfRangeException("dataPointer", "Pointer must not be zero");
}
if (length < 0)
{
throw new ArgumentOutOfRangeException("length", "Length must be >= 0");
}
this.m_Buffer = dataPointer;
this.m_Length = length;
this.m_Stride = UnsafeUtility.SizeOf<T>();
this.m_AllocatorLabel = allocMode;
this.m_MinIndex = 0;
this.m_MaxIndex = length - 1;
this.m_Safety = AtomicSafetyHandle.Create();
this.m_DisposeSentinel = ((allocMode != Allocator.None) ? DisposeSentinel.Create(IntPtr.Zero, Allocator.Invalid, 0, null) : null);
}
public void Dispose()
{
AtomicSafetyHandle.CheckDeallocateAndThrow(this.m_Safety);
AtomicSafetyHandle.Release(this.m_Safety);
DisposeSentinel.Clear(ref this.m_DisposeSentinel);
UnsafeUtility.Free(this.m_Buffer, this.m_AllocatorLabel);
this.m_Buffer = IntPtr.Zero;
this.m_Length = 0;
}
public IntPtr GetUnsafeReadBufferPtr()
{
AtomicSafetyHandle.CheckReadAndThrow(this.m_Safety);
return this.m_Buffer;
}
public IntPtr GetUnsafeWriteBufferPtr()
{
AtomicSafetyHandle.CheckWriteAndThrow(this.m_Safety);
return this.m_Buffer;
}
public void FromArray(T[] array)
{
AtomicSafetyHandle.CheckWriteAndThrow(this.m_Safety);
if (this.Length != array.Length)
{
throw new ArgumentException("Array length does not match the length of this instance");
}
for (int i = 0; i < this.Length; i++)
{
UnsafeUtility.WriteArrayElement<T>(this.m_Buffer, i * this.m_Stride, array[i]);
}
}
public T[] ToArray()
{
AtomicSafetyHandle.CheckReadAndThrow(this.m_Safety);
T[] array = new T[this.Length];
for (int i = 0; i < this.Length; i++)
{
array[i] = UnsafeUtility.ReadArrayElement<T>(this.m_Buffer, i * this.m_Stride);
}
return array;
}
private void FailOutOfRangeError(int index)
{
if (index < this.Length && (this.m_MinIndex != 0 || this.m_MaxIndex != this.Length - 1))
{
throw new IndexOutOfRangeException(string.Format("Index {0} is out of restricted IComputeForEach range [{1}...{2}] in ReadWriteBuffer.\nReadWriteBuffers are restricted to only read & write the element at the job index. You can use double buffering strategies to avoid race conditions due to reading & writing in parallel to the same elements from a job.", index, this.m_MinIndex, this.m_MaxIndex));
}
throw new IndexOutOfRangeException(string.Format("Index {0} is out of range of '{1}' Length.", index, this.Length));
}
private static void Allocate(int length, Allocator allocMode, out NativeArray<T> outArray)
{
if (allocMode <= Allocator.None)
{
throw new ArgumentOutOfRangeException("allocMode", "Allocator must be Temp, Job or Persistent");
}
if (length < 0)
{
throw new ArgumentOutOfRangeException("length", "Length must be >= 0");
}
long num = (long)UnsafeUtility.SizeOf<T>() * (long)length;
if (num > 2147483647L)
{
throw new ArgumentOutOfRangeException("length", "Length * sizeof(T) cannot exceed " + 2147483647 + "bytes");
}
outArray = new NativeArray<T>(UnsafeUtility.Malloc((int)num, UnsafeUtility.AlignOf<T>(), allocMode), length, allocMode);
}
public IEnumerator<T> GetEnumerator()
{
return new NativeArray<T>.Enumerator(ref this);
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
}