forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNativeArray.cs
More file actions
493 lines (389 loc) · 18.2 KB
/
Copy pathNativeArray.cs
File metadata and controls
493 lines (389 loc) · 18.2 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Unity.Burst;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine.Internal;
namespace Unity.Collections
{
public enum NativeArrayOptions
{
UninitializedMemory = 0,
ClearMemory = 1
}
[StructLayout(LayoutKind.Sequential)]
[NativeContainer]
[NativeContainerSupportsMinMaxWriteRestriction]
[NativeContainerSupportsDeallocateOnJobCompletion]
[NativeContainerSupportsDeferredConvertListToArray]
[DebuggerDisplay("Length = {Length}")]
[DebuggerTypeProxy(typeof(NativeArrayDebugView<>))]
public unsafe struct NativeArray<T> : IDisposable, IEnumerable<T>, IEquatable<NativeArray<T>> where T : struct
{
[NativeDisableUnsafePtrRestriction]
internal void* m_Buffer;
internal int m_Length;
internal int m_MinIndex;
internal int m_MaxIndex;
internal AtomicSafetyHandle m_Safety;
[NativeSetClassTypeToNullOnSchedule]
internal DisposeSentinel m_DisposeSentinel;
internal Allocator m_AllocatorLabel;
public NativeArray(int length, Allocator allocator, NativeArrayOptions options = NativeArrayOptions.ClearMemory)
{
Allocate(length, allocator, out this);
if ((options & NativeArrayOptions.ClearMemory) == NativeArrayOptions.ClearMemory)
UnsafeUtility.MemClear(m_Buffer, (long)Length * UnsafeUtility.SizeOf<T>());
}
public NativeArray(T[] array, Allocator allocator)
{
if (array == null)
throw new ArgumentNullException(nameof(array));
Allocate(array.Length, allocator, out this);
Copy(array, this);
}
public NativeArray(NativeArray<T> array, Allocator allocator)
{
Allocate(array.Length, allocator, out this);
Copy(array, this);
}
static void Allocate(int length, Allocator allocator, out NativeArray<T> array)
{
var totalSize = UnsafeUtility.SizeOf<T>() * (long)length;
// Native allocation is only valid for Temp, Job and Persistent.
if (allocator <= Allocator.None)
throw new ArgumentException("Allocator must be Temp, TempJob or Persistent", nameof(allocator));
if (length < 0)
throw new ArgumentOutOfRangeException(nameof(length), "Length must be >= 0");
IsUnmanagedAndThrow();
// Make sure we cannot allocate more than int.MaxValue (2,147,483,647 bytes)
// because the underlying UnsafeUtility.Malloc is expecting a int.
// TODO: change UnsafeUtility.Malloc to accept a UIntPtr length instead to match C++ API
if (totalSize > int.MaxValue)
throw new ArgumentOutOfRangeException(nameof(length), $"Length * sizeof(T) cannot exceed {int.MaxValue} bytes");
array = default(NativeArray<T>);
array.m_Buffer = UnsafeUtility.Malloc(totalSize, UnsafeUtility.AlignOf<T>(), allocator);
array.m_Length = length;
array.m_AllocatorLabel = allocator;
array.m_MinIndex = 0;
array.m_MaxIndex = length - 1;
DisposeSentinel.Create(out array.m_Safety, out array.m_DisposeSentinel, 1, allocator);
}
public int Length => m_Length;
[BurstDiscard]
internal static void IsUnmanagedAndThrow()
{
if (!UnsafeUtility.IsUnmanaged<T>())
{
throw new InvalidOperationException(
$"{typeof(T)} used in NativeArray<{typeof(T)}> must be unmanaged (contain no managed types).");
}
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
void CheckElementReadAccess(int index)
{
if (index < m_MinIndex || index > m_MaxIndex)
FailOutOfRangeError(index);
var versionPtr = (AtomicSafetyHandleVersionMask*)m_Safety.versionNode;
if ((m_Safety.version & AtomicSafetyHandleVersionMask.Read) == 0 && m_Safety.version != ((*versionPtr) & AtomicSafetyHandleVersionMask.WriteInv))
AtomicSafetyHandle.CheckReadAndThrowNoEarlyOut(m_Safety);
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
void CheckElementWriteAccess(int index)
{
if (index < m_MinIndex || index > m_MaxIndex)
FailOutOfRangeError(index);
var versionPtr = (AtomicSafetyHandleVersionMask*)m_Safety.versionNode;
if ((m_Safety.version & AtomicSafetyHandleVersionMask.Write) == 0 && m_Safety.version != ((*versionPtr) & AtomicSafetyHandleVersionMask.ReadInv))
AtomicSafetyHandle.CheckWriteAndThrowNoEarlyOut(m_Safety);
}
public T this[int index]
{
get
{
CheckElementReadAccess(index);
return UnsafeUtility.ReadArrayElement<T>(m_Buffer, index);
}
[WriteAccessRequired]
set
{
CheckElementWriteAccess(index);
UnsafeUtility.WriteArrayElement(m_Buffer, index, value);
}
}
public bool IsCreated => m_Buffer != null;
[WriteAccessRequired]
public void Dispose()
{
if (!UnsafeUtility.IsValidAllocator(m_AllocatorLabel))
throw new InvalidOperationException("The NativeArray can not be Disposed because it was not allocated with a valid allocator.");
DisposeSentinel.Dispose(ref m_Safety, ref m_DisposeSentinel);
UnsafeUtility.Free(m_Buffer, m_AllocatorLabel);
m_Buffer = null;
m_Length = 0;
}
[WriteAccessRequired]
public void CopyFrom(T[] array)
{
Copy(array, this);
}
[WriteAccessRequired]
public void CopyFrom(NativeArray<T> array)
{
Copy(array, this);
}
public void CopyTo(T[] array)
{
Copy(this, array);
}
public void CopyTo(NativeArray<T> array)
{
Copy(this, array);
}
public T[] ToArray()
{
var array = new T[Length];
Copy(this, array, Length);
return array;
}
void FailOutOfRangeError(int index)
{
if (index < Length && (m_MinIndex != 0 || m_MaxIndex != Length - 1))
throw new IndexOutOfRangeException(
$"Index {index} is out of restricted IJobParallelFor range [{m_MinIndex}...{m_MaxIndex}] in ReadWriteBuffer.\n" +
"ReadWriteBuffers 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.");
throw new IndexOutOfRangeException($"Index {index} is out of range of '{Length}' Length.");
}
public Enumerator GetEnumerator()
{
return new Enumerator(ref this);
}
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return new Enumerator(ref this);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
[ExcludeFromDocs]
public struct Enumerator : IEnumerator<T>
{
NativeArray<T> m_Array;
int m_Index;
public Enumerator(ref NativeArray<T> array)
{
m_Array = array;
m_Index = -1;
}
public void Dispose()
{
}
public bool MoveNext()
{
m_Index++;
return m_Index < m_Array.Length;
}
public void Reset()
{
m_Index = -1;
}
// Let NativeArray indexer check for out of range.
public T Current => m_Array[m_Index];
object IEnumerator.Current => Current;
}
public bool Equals(NativeArray<T> other)
{
return m_Buffer == other.m_Buffer && m_Length == other.m_Length;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
return obj is NativeArray<T> && Equals((NativeArray<T>)obj);
}
public override int GetHashCode()
{
unchecked
{
return ((int)m_Buffer * 397) ^ m_Length;
}
}
public static bool operator==(NativeArray<T> left, NativeArray<T> right)
{
return left.Equals(right);
}
public static bool operator!=(NativeArray<T> left, NativeArray<T> right)
{
return !left.Equals(right);
}
public static void Copy(NativeArray<T> src, NativeArray<T> dst)
{
AtomicSafetyHandle.CheckReadAndThrow(src.m_Safety);
AtomicSafetyHandle.CheckWriteAndThrow(dst.m_Safety);
if (src.Length != dst.Length)
throw new ArgumentException("source and destination length must be the same");
Copy(src, 0, dst, 0, src.Length);
}
public static void Copy(T[] src, NativeArray<T> dst)
{
AtomicSafetyHandle.CheckWriteAndThrow(dst.m_Safety);
if (src.Length != dst.Length)
throw new ArgumentException("source and destination length must be the same");
Copy(src, 0, dst, 0, src.Length);
}
public static void Copy(NativeArray<T> src, T[] dst)
{
AtomicSafetyHandle.CheckReadAndThrow(src.m_Safety);
if (src.Length != dst.Length)
throw new ArgumentException("source and destination length must be the same");
Copy(src, 0, dst, 0, src.Length);
}
public static void Copy(NativeArray<T> src, NativeArray<T> dst, int length)
{
Copy(src, 0, dst, 0, length);
}
public static void Copy(T[] src, NativeArray<T> dst, int length)
{
Copy(src, 0, dst, 0, length);
}
public static void Copy(NativeArray<T> src, T[] dst, int length)
{
Copy(src, 0, dst, 0, length);
}
public static void Copy(NativeArray<T> src, int srcIndex, NativeArray<T> dst, int dstIndex, int length)
{
AtomicSafetyHandle.CheckReadAndThrow(src.m_Safety);
AtomicSafetyHandle.CheckWriteAndThrow(dst.m_Safety);
if (length < 0)
throw new ArgumentOutOfRangeException(nameof(length), "length must be equal or greater than zero.");
if (srcIndex < 0 || srcIndex > src.Length || (srcIndex == src.Length && src.Length > 0))
throw new ArgumentOutOfRangeException(nameof(srcIndex), "srcIndex is outside the range of valid indexes for the source NativeArray.");
if (dstIndex < 0 || dstIndex > dst.Length || (dstIndex == dst.Length && dst.Length > 0))
throw new ArgumentOutOfRangeException(nameof(dstIndex), "dstIndex is outside the range of valid indexes for the destination NativeArray.");
if (srcIndex + length > src.Length)
throw new ArgumentException("length is greater than the number of elements from srcIndex to the end of the source NativeArray.", nameof(length));
if (dstIndex + length > dst.Length)
throw new ArgumentException("length is greater than the number of elements from dstIndex to the end of the destination NativeArray.", nameof(length));
UnsafeUtility.MemCpy(
(byte*)dst.m_Buffer + dstIndex * UnsafeUtility.SizeOf<T>(),
(byte*)src.m_Buffer + srcIndex * UnsafeUtility.SizeOf<T>(),
length * UnsafeUtility.SizeOf<T>());
}
public static void Copy(T[] src, int srcIndex, NativeArray<T> dst, int dstIndex, int length)
{
AtomicSafetyHandle.CheckWriteAndThrow(dst.m_Safety);
if (src == null)
throw new ArgumentNullException(nameof(src));
if (length < 0)
throw new ArgumentOutOfRangeException(nameof(length), "length must be equal or greater than zero.");
if (srcIndex < 0 || srcIndex > src.Length || (srcIndex == src.Length && src.Length > 0))
throw new ArgumentOutOfRangeException(nameof(srcIndex), "srcIndex is outside the range of valid indexes for the source array.");
if (dstIndex < 0 || dstIndex > dst.Length || (dstIndex == dst.Length && dst.Length > 0))
throw new ArgumentOutOfRangeException(nameof(dstIndex), "dstIndex is outside the range of valid indexes for the destination NativeArray.");
if (srcIndex + length > src.Length)
throw new ArgumentException("length is greater than the number of elements from srcIndex to the end of the source array.", nameof(length));
if (dstIndex + length > dst.Length)
throw new ArgumentException("length is greater than the number of elements from dstIndex to the end of the destination NativeArray.", nameof(length));
var handle = GCHandle.Alloc(src, GCHandleType.Pinned);
var addr = handle.AddrOfPinnedObject();
UnsafeUtility.MemCpy(
(byte*)dst.m_Buffer + dstIndex * UnsafeUtility.SizeOf<T>(),
(byte*)addr + srcIndex * UnsafeUtility.SizeOf<T>(),
length * UnsafeUtility.SizeOf<T>());
handle.Free();
}
public static void Copy(NativeArray<T> src, int srcIndex, T[] dst, int dstIndex, int length)
{
AtomicSafetyHandle.CheckReadAndThrow(src.m_Safety);
if (dst == null)
throw new ArgumentNullException(nameof(dst));
if (length < 0)
throw new ArgumentOutOfRangeException(nameof(length), "length must be equal or greater than zero.");
if (srcIndex < 0 || srcIndex > src.Length || (srcIndex == src.Length && src.Length > 0))
throw new ArgumentOutOfRangeException(nameof(srcIndex), "srcIndex is outside the range of valid indexes for the source NativeArray.");
if (dstIndex < 0 || dstIndex > dst.Length || (dstIndex == dst.Length && dst.Length > 0))
throw new ArgumentOutOfRangeException(nameof(dstIndex), "dstIndex is outside the range of valid indexes for the destination array.");
if (srcIndex + length > src.Length)
throw new ArgumentException("length is greater than the number of elements from srcIndex to the end of the source NativeArray.", nameof(length));
if (dstIndex + length > dst.Length)
throw new ArgumentException("length is greater than the number of elements from dstIndex to the end of the destination array.", nameof(length));
var handle = GCHandle.Alloc(dst, GCHandleType.Pinned);
var addr = handle.AddrOfPinnedObject();
UnsafeUtility.MemCpy(
(byte*)addr + dstIndex * UnsafeUtility.SizeOf<T>(),
(byte*)src.m_Buffer + srcIndex * UnsafeUtility.SizeOf<T>(),
length * UnsafeUtility.SizeOf<T>());
handle.Free();
}
}
/// <summary>
/// DebuggerTypeProxy for <see cref="NativeArray{T}"/>
/// </summary>
internal sealed class NativeArrayDebugView<T> where T : struct
{
NativeArray<T> m_Array;
public NativeArrayDebugView(NativeArray<T> array)
{
m_Array = array;
}
public T[] Items => m_Array.ToArray();
}
}
namespace Unity.Collections.LowLevel.Unsafe
{
public static class NativeArrayUnsafeUtility
{
public static AtomicSafetyHandle GetAtomicSafetyHandle<T>(NativeArray<T> array) where T : struct
{
return array.m_Safety;
}
public static void SetAtomicSafetyHandle<T>(ref NativeArray<T> array, AtomicSafetyHandle safety) where T : struct
{
array.m_Safety = safety;
}
/// Internal method used typically by other systems to provide a view on them.
/// The caller is still the owner of the data.
public static unsafe NativeArray<T> ConvertExistingDataToNativeArray<T>(void* dataPointer, int length, Allocator allocator) where T : struct
{
if (length < 0)
throw new ArgumentOutOfRangeException(nameof(length), "Length must be >= 0");
NativeArray<T>.IsUnmanagedAndThrow();
var totalSize = UnsafeUtility.SizeOf<T>() * (long)length;
// Make sure we cannot allocate more than int.MaxValue (2,147,483,647 bytes)
// because the underlying UnsafeUtility.Malloc is expecting a int.
// TODO: change UnsafeUtility.Malloc to accept a UIntPtr length instead to match C++ API
if (totalSize > int.MaxValue)
throw new ArgumentOutOfRangeException(nameof(length), $"Length * sizeof(T) cannot exceed {int.MaxValue} bytes");
var newArray = new NativeArray<T>
{
m_Buffer = dataPointer,
m_Length = length,
m_AllocatorLabel = allocator,
m_MinIndex = 0,
m_MaxIndex = length - 1,
};
return newArray;
}
public static unsafe void* GetUnsafePtr<T>(this NativeArray<T> nativeArray) where T : struct
{
AtomicSafetyHandle.CheckWriteAndThrow(nativeArray.m_Safety);
return nativeArray.m_Buffer;
}
public static unsafe void* GetUnsafeReadOnlyPtr<T>(this NativeArray<T> nativeArray) where T : struct
{
AtomicSafetyHandle.CheckReadAndThrow(nativeArray.m_Safety);
return nativeArray.m_Buffer;
}
public static unsafe void* GetUnsafeBufferPointerWithoutChecks<T>(NativeArray<T> nativeArray) where T : struct
{
return nativeArray.m_Buffer;
}
}
}