private static unsafe void Allocate(int length, Allocator allocator, out NativeArrayBurst <T> array) { long totalSize = (long)((ulong)UnsafeUtility.SizeOf <T>() * (ulong)length); #if COLLECTIONS_CHECKS // Native allocation is only valid for Temp, Job and Persistent if (allocator <= Allocator.None) { throw new ArgumentException("Allocator must be Temp, TempJob or Persistent", "allocator"); } if (length < 0) { throw new ArgumentOutOfRangeException("length", "Length must be >= 0"); } if (!UnsafeUtility.IsBlittable <T>()) { throw new ArgumentException(string.Format("{0} used in NativeArrayBurst<{0}> must be blittable", typeof(T))); } #endif array.m_Buffer = UnsafeUtility.Malloc(totalSize, UnsafeUtility.AlignOf <T>(), allocator); UnsafeUtility.MemClear(array.m_Buffer, totalSize); array.m_Length = length; array.m_AllocatorLabel = allocator; #if COLLECTIONS_CHECKS array.m_MinIndex = 0; array.m_MaxIndex = length - 1; DisposeSentinel.Create(out array.m_Safety, out array.m_DisposeSentinel, 1, allocator); #endif }
public unsafe NativeSliceBurst(NativeArrayBurst <T> array, int start, int length) { #if COLLECTIONS_CHECKS if (start < 0) { throw new ArgumentOutOfRangeException(nameof(start), string.Format("Slice start {0} < 0.", (object)start)); } if (length < 0) { throw new ArgumentOutOfRangeException(nameof(length), string.Format("Slice length {0} < 0.", (object)length)); } if (start + length > array.Length) { throw new ArgumentException(string.Format("Slice start + length ({0}) range must be <= array.Length ({1})", (object)(start + length), (object)array.Length)); } if ((array.m_MinIndex != 0 || array.m_MaxIndex != array.m_Length - 1) && (start < array.m_MinIndex || array.m_MaxIndex < start || array.m_MaxIndex < start + length - 1)) { throw new ArgumentException("Slice may not be used on a restricted range array", nameof(array)); } this.m_MinIndex = 0; this.m_MaxIndex = length - 1; this.m_Safety = array.m_Safety; #endif this.m_Stride = UnsafeUtility.SizeOf <T>(); this.m_Buffer = (byte *)((IntPtr)array.m_Buffer + this.m_Stride * start); this.m_Length = length; }
public NativeArrayBurst(T[] array, Allocator allocator) { if (array == null) { throw new ArgumentNullException(nameof(array)); } NativeArrayBurst <T> .Allocate(array.Length, allocator, out this); NativeArrayBurst <T> .Copy(array, this); }
public static void Copy(T[] src, NativeArrayBurst <T> dst) { #if COLLECTIONS_CHECKS AtomicSafetyHandle.CheckWriteAndThrow(dst.m_Safety); if (src.Length != dst.Length) { throw new ArgumentException("source and destination length must be the same"); } #endif NativeArrayBurst <T> .Copy(src, 0, dst, 0, src.Length); }
public static ref T GetRefRead <T>(this NativeArrayBurst <T> array, int index) where T : struct { CheckArray(index, array.Length); unsafe { var ptr = array.GetUnsafePtr(); #if UNITY_2020_1_OR_NEWER return(ref UnsafeUtility.ArrayElementAsRef <T>(ptr, index)); #else throw new Exception("UnsafeUtility.ArrayElementAsRef"); #endif } }
public static ref T GetRefRead <T>(this NativeArrayBurst <T> array, int index) where T : struct { if (index < 0 || index >= array.Length) { throw new ArgumentOutOfRangeException(nameof(index)); } unsafe { var ptr = array.GetUnsafePtr(); #if UNITY_2020_1_OR_NEWER return(ref UnsafeUtility.ArrayElementAsRef <T>(ptr, index)); #else throw new Exception("UnsafeUtility.ArrayElementAsRef"); #endif } }
public static void Copy( T[] src, int srcIndex, NativeArrayBurst <T> dst, int dstIndex, int length) { #if COLLECTIONS_CHECKS AtomicSafetyHandle.CheckWriteAndThrow(dst.m_Safety); #endif 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 && (uint)src.Length > 0U) { 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 gcHandle = System.Runtime.InteropServices.GCHandle.Alloc(src, System.Runtime.InteropServices.GCHandleType.Pinned); IntPtr num = gcHandle.AddrOfPinnedObject(); UnsafeUtility.MemCpy((void *)((IntPtr)dst.m_Buffer + dstIndex * UnsafeUtility.SizeOf <T>()), (void *)((IntPtr)(void *)num + srcIndex * UnsafeUtility.SizeOf <T>()), (long)(length * UnsafeUtility.SizeOf <T>())); gcHandle.Free(); }
public NativeSliceBurst(NativeArrayBurst <T> array, int start) : this(array, start, array.Length - start) { }
public NativeSliceBurst(NativeArrayBurst <T> array) : this(array, 0, array.Length) { }
public NativeArrayBurst(int length, Allocator allocator) { NativeArrayBurst <T> .Allocate(length, allocator, out this); }
public NativeArrayBurst(NativeArrayBurst <T> array, Allocator allocator) { NativeArrayBurst <T> .Allocate(array.Length, allocator, out this); NativeArrayUtils.Copy(array, ref this); }
public NativeArrayBurstDebugView(NativeArrayBurst <T> array) { m_Array = array; }
public static void Copy(T[] src, NativeArrayBurst <T> dst, int length) => NativeArrayBurst <T> .Copy(src, 0, dst, 0, length);