Esempio n. 1
0
        public static unsafe void Copy <T>(NativeSpan src, int srcIndex, T[] dst, int dstIndex, int length) where T : struct
        {
            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 NativeArray2.");
            }
            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 NativeArray2.", 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));
            }
            GCHandle gcHandle = GCHandle.Alloc((object)dst, GCHandleType.Pinned);

            UnsafeUtility.MemCpy((void *)((IntPtr)(void *)gcHandle.AddrOfPinnedObject() + (dstIndex * UnsafeUtility.SizeOf <T>())), (void *)((IntPtr)src.m_Buffer + (srcIndex * UnsafeUtility.SizeOf <T>())), (long)(length * UnsafeUtility.SizeOf <T>()));
            gcHandle.Free();
        }
Esempio n. 2
0
        private NativeFasterDictionaryData *Allocate(int size, Allocator allocator, NativeArrayOptions options = NativeArrayOptions.ClearMemory)
        {
            var layout = CalculateLayout(size);

            var ptr = UnsafeUtility.Malloc(layout.AllocationBytes, UnsafeUtility.AlignOf <int>(), allocator);

            if (options == NativeArrayOptions.ClearMemory)
            {
                UnsafeUtility.MemClear(ptr, layout.AllocationBytes);
            }

            var header = new NativeFasterDictionaryData
            {
                //BaseAddress = ptr,
                FreeValueCellIndex = 0,
                Collisions         = 0,
                Capacity           = size,
                Layout             = layout,
                Nodes   = NativeSpan.Assign <Node>((IntPtr)ptr + layout.NodesOffset, layout.NodesCount),
                Values  = NativeSpan.Assign <TValue>((IntPtr)ptr + layout.ValuesOffset, layout.ValuesCount),
                Buckets = NativeSpan.Assign <int>((IntPtr)ptr + layout.BucketsOffset, layout.BucketsCount),
            };

            UnsafeUtility.CopyStructureToPtr(ref header, ptr);
            return(CastPtr <NativeFasterDictionaryData>(ptr, 0));
        }
Esempio n. 3
0
        public static unsafe void Copy <T>(NativeSpan src, int srcIndex, NativeArray <T> dst, int dstIndex, int length) where T : struct
        {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            AtomicSafetyHandle.CheckWriteAndThrow(NativeArrayUnsafeUtility.GetAtomicSafetyHandle(dst));
#endif
            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 NativeArray2.");
            }
            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 NativeArray2.");
            }
            if (srcIndex + length > src.Length)
            {
                throw new ArgumentException("length is greater than the number of elements from srcIndex to the end of the source NativeArray2.", 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 NativeArray2.", nameof(length));
            }
            UnsafeUtility.MemCpy((void *)((IntPtr)NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(dst) + (dstIndex * UnsafeUtility.SizeOf <T>())), (void *)((IntPtr)src.m_Buffer + (srcIndex * UnsafeUtility.SizeOf <T>())), (long)(length * UnsafeUtility.SizeOf <T>()));
        }
Esempio n. 4
0
 public static void Copy <T>(T[] src, NativeSpan dst) where T : struct
 {
     if (src.Length != dst.Length)
     {
         throw new ArgumentException("source and destination length must be the same");
     }
     Copy(src, 0, dst, 0, src.Length);
 }
Esempio n. 5
0
        public static void Copy <T>(NativeSpan src, NativeArray <T> dst) where T : struct
        {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            AtomicSafetyHandle.CheckWriteAndThrow(NativeArrayUnsafeUtility.GetAtomicSafetyHandle(dst));
#endif
            if (src.Length != dst.Length)
            {
                throw new ArgumentException("source and destination length must be the same");
            }

            Copy(src, 0, dst, 0, src.Length);
        }
Esempio n. 6
0
 public static unsafe void Copy <T>(NativeSpan src, int srcIndex, NativeSpan dst, int dstIndex, int length) where T : struct
 {
     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 NativeArray2.");
     }
     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 NativeArray2.");
     }
     if (srcIndex + length > src.Length)
     {
         throw new ArgumentException("length is greater than the number of elements from srcIndex to the end of the source NativeArray2.", 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 NativeArray2.", nameof(length));
     }
     UnsafeUtility.MemCpy((void *)((IntPtr)dst.m_Buffer + (dstIndex * UnsafeUtility.SizeOf <T>())), (void *)((IntPtr)src.m_Buffer + (srcIndex * UnsafeUtility.SizeOf <T>())), (long)(length * UnsafeUtility.SizeOf <T>()));
 }
Esempio n. 7
0
 public static void Copy <T>(NativeSpan src, T[] dst, int length) where T : struct
 {
     Copy(src, 0, dst, 0, length);
 }
Esempio n. 8
0
 public void CopyTo <T>(NativeSpan buffer) where T : struct
 {
     Copy <T>(this, buffer);
 }