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
1086 lines (889 loc) · 36.9 KB
/
Copy pathNativeArray.cs
File metadata and controls
1086 lines (889 loc) · 36.9 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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 System.Runtime.CompilerServices;
using Unity.Burst;
using Unity.Jobs;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine;
using UnityEngine.Bindings;
using UnityEngine.Internal;
using UnityEngine.Scripting;
namespace Unity.Collections
{
public enum NativeArrayOptions
{
UninitializedMemory = 0,
ClearMemory = 1
}
[StructLayout(LayoutKind.Sequential)]
[NativeContainer]
[NativeContainerSupportsMinMaxWriteRestriction]
[NativeContainerSupportsDeallocateOnJobCompletion]
[NativeContainerSupportsDeferredConvertListToArray]
[DebuggerDisplay("Length = {m_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;
internal unsafe ref DisposeSentinel.Dummy m_DisposeSentinel
{
get
{
void* pointer = UnsafeUtility.Malloc(sizeof(DisposeSentinel.Dummy), 8, Allocator.Temp);
return ref UnsafeUtility.AsRef<DisposeSentinel.Dummy>(pointer);
}
}
// TODO: Use SharedStatic for burst compatible static id once we have typehash intrinsic for unity in burst 1.6.5 and 1.7.0
static int s_staticSafetyId;
[BurstDiscard]
static void InitStaticSafetyId(ref AtomicSafetyHandle handle)
{
if (s_staticSafetyId == 0)
s_staticSafetyId = AtomicSafetyHandle.NewStaticSafetyId<NativeArray<T>>();
AtomicSafetyHandle.SetStaticSafetyId(ref handle, s_staticSafetyId);
}
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)
{
AtomicSafetyHandle.CheckReadAndThrow(array.m_Safety);
Allocate(array.Length, allocator, out this);
Copy(array, 0, this, 0, array.Length);
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
static void CheckAllocateArguments(int length, Allocator allocator)
{
// 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));
// NativeArray constructor does not support custom allocator
if (allocator >= Allocator.FirstUserIndex)
throw new ArgumentException("Use CollectionHelper.CreateNativeArray in com.unity.collections package for custom allocator", nameof(allocator));
if (length < 0)
throw new ArgumentOutOfRangeException(nameof(length), "Length must be >= 0");
}
static void Allocate(int length, Allocator allocator, out NativeArray<T> array)
{
long totalSize = UnsafeUtility.SizeOf<T>() * (long)length;
CheckAllocateArguments(length, allocator);
array = default(NativeArray<T>);
IsUnmanagedAndThrow();
array.m_Buffer = UnsafeUtility.MallocTracked(totalSize, UnsafeUtility.AlignOf<T>(), allocator, 0);
array.m_Length = length;
array.m_AllocatorLabel = allocator;
array.m_MinIndex = 0;
array.m_MaxIndex = length - 1;
AtomicSafetyHandle.CreateHandle(out array.m_Safety, allocator);
InitStaticSafetyId(ref array.m_Safety);
InitNestedNativeContainer(array.m_Safety);
}
public int Length
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return m_Length;
}
}
internal static void InitNestedNativeContainer(AtomicSafetyHandle handle)
{
if (UnsafeUtility.IsNativeContainerType<T>())
{
AtomicSafetyHandle.SetNestedContainer(handle, true);
}
}
// NativeArray is not constrained to unmanaged so it must be checked
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
[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")]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void CheckElementReadAccess(int index)
{
if (index < m_MinIndex || index > m_MaxIndex)
FailOutOfRangeError(index);
AtomicSafetyHandle.CheckReadAndThrow(m_Safety);
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void CheckElementWriteAccess(int index)
{
if (index < m_MinIndex || index > m_MaxIndex)
FailOutOfRangeError(index);
AtomicSafetyHandle.CheckWriteAndThrow(m_Safety);
}
public T this[int index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
CheckElementReadAccess(index);
return UnsafeUtility.ReadArrayElement<T>(m_Buffer, index);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[WriteAccessRequired]
set
{
CheckElementWriteAccess(index);
UnsafeUtility.WriteArrayElement(m_Buffer, index, value);
}
}
public bool IsCreated
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => m_Buffer != null;
}
[WriteAccessRequired]
public void Dispose()
{
if (m_AllocatorLabel != Allocator.None
&& !AtomicSafetyHandle.IsDefaultValue(m_Safety))
{
AtomicSafetyHandle.CheckExistsAndThrow(m_Safety);
}
if (!IsCreated)
{
return;
}
if (m_AllocatorLabel == Allocator.Invalid)
{
throw new InvalidOperationException("The NativeArray can not be Disposed because it was not allocated with a valid allocator.");
}
if (m_AllocatorLabel >= Allocator.FirstUserIndex)
{
throw new InvalidOperationException("The NativeArray can not be Disposed because it was allocated with a custom allocator, use CollectionHelper.Dispose in com.unity.collections package.");
}
if (m_AllocatorLabel > Allocator.None)
{
AtomicSafetyHandle.DisposeHandle(ref m_Safety);
UnsafeUtility.FreeTracked(m_Buffer, m_AllocatorLabel);
m_AllocatorLabel = Allocator.Invalid;
}
m_Buffer = null;
}
public JobHandle Dispose(JobHandle inputDeps)
{
if (m_AllocatorLabel != Allocator.None
&& !AtomicSafetyHandle.IsDefaultValue(m_Safety))
{
AtomicSafetyHandle.CheckExistsAndThrow(m_Safety);
}
if (!IsCreated)
{
return inputDeps;
}
if (m_AllocatorLabel >= Allocator.FirstUserIndex)
{
throw new InvalidOperationException("The NativeArray can not be Disposed because it was allocated with a custom allocator, use CollectionHelper.Dispose in com.unity.collections package.");
}
if (m_AllocatorLabel > Allocator.None)
{
// [DeallocateOnJobCompletion] is not supported, but we want the deallocation
// to happen in a thread. DisposeSentinel needs to be cleared on main thread.
// AtomicSafetyHandle can be destroyed after the job was scheduled (Job scheduling
// will check that no jobs are writing to the container).
var jobHandle = new NativeArrayDisposeJob { Data = new NativeArrayDispose { m_Buffer = m_Buffer, m_AllocatorLabel = m_AllocatorLabel, m_Safety = m_Safety } }.Schedule(inputDeps);
AtomicSafetyHandle.Release(m_Safety);
m_Buffer = null;
m_AllocatorLabel = Allocator.Invalid;
return jobHandle;
}
m_Buffer = null;
return inputDeps;
}
[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;
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
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;
T value;
public Enumerator(ref NativeArray<T> array)
{
m_Array = array;
m_Index = -1;
value = default;
}
public void Dispose()
{
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext()
{
m_Index++;
if (m_Index < m_Array.m_Length)
{
AtomicSafetyHandle.CheckReadAndThrow(m_Array.m_Safety);
value = UnsafeUtility.ReadArrayElement<T>(m_Array.m_Buffer, m_Index);
return true;
}
value = default;
return false;
}
public void Reset()
{
m_Index = -1;
}
// Let NativeArray indexer check for out of range.
public T Current
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return value;
}
}
object IEnumerator.Current
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return 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)
{
CheckCopyLengths(src.Length, dst.Length);
CopySafe(src, 0, dst, 0, src.Length);
}
public static void Copy(ReadOnly src, NativeArray<T> dst)
{
CheckCopyLengths(src.Length, dst.Length);
CopySafe(src, 0, dst, 0, src.Length);
}
public static void Copy(T[] src, NativeArray<T> dst)
{
CheckCopyLengths(src.Length, dst.Length);
CopySafe(src, 0, dst, 0, src.Length);
}
public static void Copy(NativeArray<T> src, T[] dst)
{
CheckCopyLengths(src.Length, dst.Length);
CopySafe(src, 0, dst, 0, src.Length);
}
public static void Copy(ReadOnly src, T[] dst)
{
CheckCopyLengths(src.Length, dst.Length);
CopySafe(src, 0, dst, 0, src.Length);
}
public static void Copy(NativeArray<T> src, NativeArray<T> dst, int length)
{
CopySafe(src, 0, dst, 0, length);
}
public static void Copy(ReadOnly src, NativeArray<T> dst, int length)
{
CopySafe(src, 0, dst, 0, length);
}
public static void Copy(T[] src, NativeArray<T> dst, int length)
{
CopySafe(src, 0, dst, 0, length);
}
public static void Copy(NativeArray<T> src, T[] dst, int length)
{
CopySafe(src, 0, dst, 0, length);
}
public static void Copy(ReadOnly src, T[] dst, int length)
{
CopySafe(src, 0, dst, 0, length);
}
public static void Copy(NativeArray<T> src, int srcIndex, NativeArray<T> dst, int dstIndex, int length)
{
CopySafe(src, srcIndex, dst, dstIndex, length);
}
public static void Copy(ReadOnly src, int srcIndex, NativeArray<T> dst, int dstIndex, int length)
{
CopySafe(src, srcIndex, dst, dstIndex, length);
}
public static void Copy(T[] src, int srcIndex, NativeArray<T> dst, int dstIndex, int length)
{
CopySafe(src, srcIndex, dst, dstIndex, length);
}
public static void Copy(NativeArray<T> src, int srcIndex, T[] dst, int dstIndex, int length)
{
CopySafe(src, srcIndex, dst, dstIndex, length);
}
public static void Copy(ReadOnly src, int srcIndex, T[] dst, int dstIndex, int length)
{
CopySafe(src, srcIndex, dst, dstIndex, length);
}
static void CopySafe(NativeArray<T> src, int srcIndex, NativeArray<T> dst, int dstIndex, int length)
{
AtomicSafetyHandle.CheckReadAndThrow(src.m_Safety);
AtomicSafetyHandle.CheckWriteAndThrow(dst.m_Safety);
CheckCopyArguments(src.Length, srcIndex, dst.Length, dstIndex, length);
UnsafeUtility.MemCpy(
(byte*)dst.m_Buffer + dstIndex * UnsafeUtility.SizeOf<T>(),
(byte*)src.m_Buffer + srcIndex * UnsafeUtility.SizeOf<T>(),
length * UnsafeUtility.SizeOf<T>());
}
static void CopySafe(ReadOnly src, int srcIndex, NativeArray<T> dst, int dstIndex, int length)
{
AtomicSafetyHandle.CheckReadAndThrow(src.m_Safety);
AtomicSafetyHandle.CheckWriteAndThrow(dst.m_Safety);
CheckCopyArguments(src.Length, srcIndex, dst.Length, dstIndex, length);
UnsafeUtility.MemCpy(
(byte*)dst.m_Buffer + dstIndex * UnsafeUtility.SizeOf<T>(),
(byte*)src.m_Buffer + srcIndex * UnsafeUtility.SizeOf<T>(),
length * UnsafeUtility.SizeOf<T>());
}
static void CopySafe(T[] src, int srcIndex, NativeArray<T> dst, int dstIndex, int length)
{
AtomicSafetyHandle.CheckWriteAndThrow(dst.m_Safety);
CheckCopyPtr(src);
CheckCopyArguments(src.Length, srcIndex, dst.Length, dstIndex, 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();
}
static void CopySafe(NativeArray<T> src, int srcIndex, T[] dst, int dstIndex, int length)
{
AtomicSafetyHandle.CheckReadAndThrow(src.m_Safety);
CheckCopyPtr(dst);
CheckCopyArguments(src.Length, srcIndex, dst.Length, dstIndex, 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();
}
static void CopySafe(ReadOnly src, int srcIndex, T[] dst, int dstIndex, int length)
{
AtomicSafetyHandle.CheckReadAndThrow(src.m_Safety);
CheckCopyPtr(dst);
CheckCopyArguments(src.Length, srcIndex, dst.Length, dstIndex, 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();
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
static void CheckCopyPtr(T[] ptr)
{
if (ptr == null)
throw new ArgumentNullException(nameof(ptr));
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
static void CheckCopyLengths(int srcLength, int dstLength)
{
if (srcLength != dstLength)
throw new ArgumentException("source and destination length must be the same");
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
static void CheckCopyArguments(int srcLength, int srcIndex, int dstLength, int dstIndex, int length)
{
if (length < 0)
throw new ArgumentOutOfRangeException(nameof(length), "length must be equal or greater than zero.");
if (srcIndex < 0 || srcIndex > srcLength || (srcIndex == srcLength && srcLength > 0))
throw new ArgumentOutOfRangeException(nameof(srcIndex), "srcIndex is outside the range of valid indexes for the source NativeArray.");
if (dstIndex < 0 || dstIndex > dstLength || (dstIndex == dstLength && dstLength > 0))
throw new ArgumentOutOfRangeException(nameof(dstIndex), "dstIndex is outside the range of valid indexes for the destination NativeArray.");
if (srcIndex + length > srcLength)
throw new ArgumentException("length is greater than the number of elements from srcIndex to the end of the source NativeArray.", nameof(length));
if (srcIndex + length < 0)
throw new ArgumentException("srcIndex + length causes an integer overflow");
if (dstIndex + length > dstLength)
throw new ArgumentException("length is greater than the number of elements from dstIndex to the end of the destination NativeArray.", nameof(length));
if (dstIndex + length < 0)
throw new ArgumentException("dstIndex + length causes an integer overflow");
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
void CheckReinterpretLoadRange<U>(int sourceIndex) where U : struct
{
long tsize = UnsafeUtility.SizeOf<T>();
AtomicSafetyHandle.CheckReadAndThrow(m_Safety);
long usize = UnsafeUtility.SizeOf<U>();
long byteSize = Length * tsize;
long firstByte = sourceIndex * tsize;
long lastByte = firstByte + usize;
if (firstByte < 0 || lastByte > byteSize)
throw new ArgumentOutOfRangeException(nameof(sourceIndex), "loaded byte range must fall inside container bounds");
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
void CheckReinterpretStoreRange<U>(int destIndex) where U : struct
{
long tsize = UnsafeUtility.SizeOf<T>();
AtomicSafetyHandle.CheckWriteAndThrow(m_Safety);
long usize = UnsafeUtility.SizeOf<U>();
long byteSize = Length * tsize;
long firstByte = destIndex * tsize;
long lastByte = firstByte + usize;
if (firstByte < 0 || lastByte > byteSize)
throw new ArgumentOutOfRangeException(nameof(destIndex), "stored byte range must fall inside container bounds");
}
public U ReinterpretLoad<U>(int sourceIndex) where U : struct
{
CheckReinterpretLoadRange<U>(sourceIndex);
byte* src_ptr = ((byte*)m_Buffer) + ((long)UnsafeUtility.SizeOf<T>()) * sourceIndex;
return UnsafeUtility.ReadArrayElement<U>(src_ptr, 0);
}
public void ReinterpretStore<U>(int destIndex, U data) where U : struct
{
CheckReinterpretStoreRange<U>(destIndex);
byte* dst_ptr = ((byte*)m_Buffer) + ((long)UnsafeUtility.SizeOf<T>()) * destIndex;
UnsafeUtility.WriteArrayElement<U>(dst_ptr, 0, data);
}
private NativeArray<U> InternalReinterpret<U>(int length) where U : struct
{
var result = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray<U>(m_Buffer, length, m_AllocatorLabel);
NativeArrayUnsafeUtility.SetAtomicSafetyHandle(ref result, m_Safety);
return result;
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
static void CheckReinterpretSize<U>() where U : struct
{
if (UnsafeUtility.SizeOf<T>() != UnsafeUtility.SizeOf<U>())
{
throw new InvalidOperationException($"Types {typeof(T)} and {typeof(U)} are different sizes - direct reinterpretation is not possible. If this is what you intended, use Reinterpret(<type size>)");
}
}
public NativeArray<U> Reinterpret<U>() where U : struct
{
CheckReinterpretSize<U>();
return InternalReinterpret<U>(Length);
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
void CheckReinterpretSize<U>(long tSize, long uSize, int expectedTypeSize, long byteLen, long uLen)
{
if (tSize != expectedTypeSize)
{
throw new InvalidOperationException($"Type {typeof(T)} was expected to be {expectedTypeSize} but is {tSize} bytes");
}
if (uLen * uSize != byteLen)
{
throw new InvalidOperationException($"Types {typeof(T)} (array length {Length}) and {typeof(U)} cannot be aliased due to size constraints. The size of the types and lengths involved must line up.");
}
}
public NativeArray<U> Reinterpret<U>(int expectedTypeSize) where U : struct
{
long tSize = UnsafeUtility.SizeOf<T>();
long uSize = UnsafeUtility.SizeOf<U>();
long byteLen = ((long)Length) * tSize;
long uLen = byteLen / uSize;
CheckReinterpretSize<U>(tSize, uSize, expectedTypeSize, byteLen, uLen);
return InternalReinterpret<U>((int)uLen);
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
void CheckGetSubArrayArguments(int start, int length)
{
if (start < 0)
{
throw new ArgumentOutOfRangeException(nameof(start), "start must be >= 0");
}
if (start + length > Length)
{
throw new ArgumentOutOfRangeException(nameof(length), $"sub array range {start}-{start + length - 1} is outside the range of the native array 0-{Length - 1}");
}
if (start + length < 0)
{
throw new ArgumentException($"sub array range {start}-{start + length - 1} caused an integer overflow and is outside the range of the native array 0-{Length - 1}");
}
}
public NativeArray<T> GetSubArray(int start, int length)
{
CheckGetSubArrayArguments(start, length);
var result = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray<T>(((byte*)m_Buffer) + ((long)UnsafeUtility.SizeOf<T>()) * start, length, Allocator.None);
NativeArrayUnsafeUtility.SetAtomicSafetyHandle(ref result, m_Safety);
return result;
}
public ReadOnly AsReadOnly()
{
return new ReadOnly(m_Buffer, m_Length, ref m_Safety);
}
[StructLayout(LayoutKind.Sequential)]
[NativeContainer]
[NativeContainerIsReadOnly]
[DebuggerDisplay("Length = {Length}")]
[DebuggerTypeProxy(typeof(NativeArrayReadOnlyDebugView<>))]
public struct ReadOnly : IEnumerable<T>
{
[NativeDisableUnsafePtrRestriction]
internal void* m_Buffer;
internal int m_Length;
internal AtomicSafetyHandle m_Safety;
internal ReadOnly(void* buffer, int length, ref AtomicSafetyHandle safety)
{
m_Buffer = buffer;
m_Length = length;
m_Safety = safety;
}
public int Length
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return m_Length;
}
}
public void CopyTo(T[] array) => Copy(this, array);
public void CopyTo(NativeArray<T> array) => Copy(this, array);
public T[] ToArray()
{
var array = new T[m_Length];
Copy(this, array, m_Length);
return array;
}
public NativeArray<U>.ReadOnly Reinterpret<U>() where U : struct
{
CheckReinterpretSize<U>();
return new NativeArray<U>.ReadOnly(m_Buffer, m_Length, ref m_Safety);
}
public T this[int index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
CheckElementReadAccess(index);
return UnsafeUtility.ReadArrayElement<T>(m_Buffer, index);
}
}
// This method does not copy T, but returns a readonly T.
// It is marked as unsafe because the value returned by this method can become invalid at any time, for example, if the container was disposed.
public ref readonly T UnsafeElementAt(int index)
{
CheckElementReadAccess(index);
return ref UnsafeUtility.ArrayElementAsRef<T>(m_Buffer, index);
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void CheckElementReadAccess(int index)
{
if ((uint)index >= (uint)m_Length)
{
throw new IndexOutOfRangeException($"Index {index} is out of range (must be between 0 and {m_Length-1}).");
}
AtomicSafetyHandle.CheckReadAndThrow(m_Safety);
}
public bool IsCreated
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => m_Buffer != null;
}
[ExcludeFromDocs]
public struct Enumerator : IEnumerator<T>
{
ReadOnly m_Array;
int m_Index;
T value;
public Enumerator(in ReadOnly array)
{
m_Array = array;
m_Index = -1;
value = default;
}
public void Dispose()
{
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext()
{
m_Index++;
if (m_Index < m_Array.m_Length)
{
AtomicSafetyHandle.CheckReadAndThrow(m_Array.m_Safety);
value = UnsafeUtility.ReadArrayElement<T>(m_Array.m_Buffer, m_Index);
return true;
}
value = default;
return false;
}
public void Reset()
{
m_Index = -1;
}
// Let NativeArray indexer check for out of range.
public T Current
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => value;
}
object IEnumerator.Current => Current;
}
public Enumerator GetEnumerator()
{
return new Enumerator(this);
}
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public readonly ReadOnlySpan<T> AsReadOnlySpan()
{
AtomicSafetyHandle.CheckReadAndThrow(m_Safety);
return new ReadOnlySpan<T>(m_Buffer, m_Length);
}
public static implicit operator ReadOnlySpan<T>(in ReadOnly source)
{
return source.AsReadOnlySpan();
}
}
[WriteAccessRequired]
public readonly Span<T> AsSpan()
{
AtomicSafetyHandle.CheckWriteAndThrow(m_Safety);
return new Span<T>(m_Buffer, m_Length);
}
public readonly ReadOnlySpan<T> AsReadOnlySpan()
{
AtomicSafetyHandle.CheckReadAndThrow(m_Safety);
return new ReadOnlySpan<T>(m_Buffer, m_Length);
}
public static implicit operator Span<T>(in NativeArray<T> source)
{
return source.AsSpan();
}
public static implicit operator ReadOnlySpan<T>(in NativeArray<T> source)
{
return source.AsReadOnlySpan();
}
}
[NativeContainer]
internal unsafe struct NativeArrayDispose
{
[NativeDisableUnsafePtrRestriction]
internal void* m_Buffer;
internal Allocator m_AllocatorLabel;
internal AtomicSafetyHandle m_Safety;
public void Dispose()
{
UnsafeUtility.FreeTracked(m_Buffer, m_AllocatorLabel);
}
}
// [BurstCompile] - can't use attribute since it's inside com.unity.Burst.
[NativeClass(null)]
internal struct NativeArrayDisposeJob : IJob
{
internal NativeArrayDispose Data;
public void Execute()
{
Data.Dispose();
}
[RequiredByNativeCode]
internal static void RegisterNativeArrayDisposeJobReflectionData()
{
// Necessary so we may schedule NativeArrayDisposeJob from
// burst compiled codepaths
IJobExtensions.EarlyJobInit<NativeArrayDisposeJob>();
}
}
/// <summary>
/// DebuggerTypeProxy for <see cref="NativeArray{T}"/>
/// </summary>
internal unsafe sealed class NativeArrayDebugView<T> where T : struct
{
NativeArray<T> m_Array;
public NativeArrayDebugView(NativeArray<T> array)
{
m_Array = array;
}
public T[] Items
{
get
{
if (!m_Array.IsCreated)
{
return default;
}
// Trying to avoid safety checks, so that container can be read in debugger if it's safety handle
// is in write-only mode.
var length = m_Array.m_Length;
var dst = new T[length];
var handle = GCHandle.Alloc(dst, GCHandleType.Pinned);
var addr = handle.AddrOfPinnedObject();
UnsafeUtility.MemCpy((void*)addr, m_Array.m_Buffer, length * UnsafeUtility.SizeOf<T>());
handle.Free();
return dst;
}
}
}
/// <summary>
/// DebuggerTypeProxy for <see cref="NativeArray{T}.ReadOnly"/>
/// </summary>
internal sealed class NativeArrayReadOnlyDebugView<T> where T : struct
{
NativeArray<T>.ReadOnly m_Array;