Esempio n. 1
0
        NativeListArray(int initialListCapacity, Allocator allocator, int disposeSentinelStackDepth)
        {
            var totalSize = UnsafeUtility.SizeOf <T>() * (long)initialListCapacity;

#if ENABLE_UNITY_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", nameof(allocator));
            }
            if (initialListCapacity < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(initialListCapacity), "InitialListCapacity must be >= 0");
            }


            CollectionHelper.CheckIsUnmanaged <T>();

            if (totalSize > int.MaxValue)
            {
                throw new ArgumentOutOfRangeException(nameof(initialListCapacity), $"InitialListCapacity * sizeof(T) cannot exceed {int.MaxValue} bytes");
            }

            DisposeSentinel.Create(out m_Safety, out m_DisposeSentinel, disposeSentinelStackDepth, allocator);
#endif
            m_Array = UnsafeListArray.Create(allocator);
            m_Array->InitialListCapacity = initialListCapacity;

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            AtomicSafetyHandle.SetBumpSecondaryVersionOnScheduleWrite(m_Safety, true);
#endif
        }
Esempio n. 2
0
 internal static void IsBlittableAndThrow <TKey, TValue>()
     where TKey : struct
     where TValue : struct
 {
     CollectionHelper.CheckIsUnmanaged <TKey>();
     CollectionHelper.CheckIsUnmanaged <TValue>();
 }
Esempio n. 3
0
        internal NativeMappedAggregator(int capacity, Allocator allocator, TAggregator shell = default)
        {
            CollectionHelper.CheckIsUnmanaged <TKey>();
            CollectionHelper.CheckIsUnmanaged <TData>();
            mAllocator = allocator;
            // Bucket size is bigger to reduce collisions
            UnsafeHashMapData.AllocateHashMap <TKey, TData>(capacity, capacity << 1, allocator, out mBuffer);
            UnsafeHashMapBase <TKey, TData> .Clear(mBuffer);

            mShell = shell;
        }
Esempio n. 4
0
        /// <summary>
        /// Constructs a new queue with type of memory allocation.
        /// </summary>
        /// <param name="allocator">A member of the
        /// [Unity.Collections.Allocator](https://docs.unity3d.com/ScriptReference/Unity.Collections.Allocator.html) enumeration.</param>
        public NativeQueue(Allocator allocator)
        {
            CollectionHelper.CheckIsUnmanaged <T>();

            m_QueuePool      = NativeQueueBlockPool.QueueBlockPool;
            m_AllocatorLabel = allocator;

            NativeQueueData.AllocateQueue <T>(allocator, out m_Buffer);

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            DisposeSentinel.Create(out m_Safety, out m_DisposeSentinel, 0, allocator);
#endif
        }
Esempio n. 5
0
        public NativeArrayFullSOA(int length, Allocator label, int stackDepth)
        {
            CollectionHelper.CheckIsUnmanaged <T>();
            if (!ms_CachedLayout.IsCreated)
            {
                ms_CachedLayout = new StructLayoutData4(typeof(T));
            }

            m_Base      = (byte *)UnsafeUtility.Malloc(4 * length * ms_CachedLayout.FieldCount, StructLayoutData4.ChunkSizeBytes, label);
            m_Length    = length;
            m_Allocator = label;

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            DisposeSentinel.Create(out m_Safety, out m_DisposeSentinel, stackDepth, label);
#endif
        }
Esempio n. 6
0
        /// <summary>
        /// Constructs a new queue with type of memory allocation.
        /// </summary>
        /// <param name="allocator">A member of the
        /// [Unity.Collections.Allocator](https://docs.unity3d.com/ScriptReference/Unity.Collections.Allocator.html) enumeration.</param>
        public NativeQueue(Allocator allocator)
        {
            CollectionHelper.CheckIsUnmanaged <T>();

            m_QueuePool      = NativeQueueBlockPool.QueueBlockPool;
            m_AllocatorLabel = allocator;

            NativeQueueData.AllocateQueue <T>(allocator, out m_Buffer);

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            DisposeSentinel.Create(out m_Safety, out m_DisposeSentinel, 0, allocator);

            if (s_staticSafetyId.Data == 0)
            {
                CreateStaticSafetyId();
            }
            AtomicSafetyHandle.SetStaticSafetyId(ref m_Safety, s_staticSafetyId.Data);
#endif
        }
Esempio n. 7
0
        private NativeBinaryHeap(int initialCapacity, Allocator mAllocator, int disposeSentinelStackDepth)
        {
            var totalSize = UnsafeUtility.SizeOf <T>() * (long)initialCapacity;

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            // Native allocation is only valid for Temp, Job and Persistent.
            if (mAllocator <= Allocator.None)
            {
                throw new ArgumentException("Allocator must be Temp, TempJob or Persistent", nameof(mAllocator));
            }
            if (initialCapacity < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(initialCapacity), "Capacity must be >= 0");
            }

            CollectionHelper.CheckIsUnmanaged <T>();

            // 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(initialCapacity), $"Capacity * sizeof(T) cannot exceed {int.MaxValue} bytes");
            }

            DisposeSentinel.Create(out m_Safety, out m_DisposeSentinel, disposeSentinelStackDepth, mAllocator);
#if UNITY_2020_1_OR_NEWER
            if (s_staticSafetyId.Data == 0)
            {
                CreateStaticSafetyId();
            }
            AtomicSafetyHandle.SetStaticSafetyId(ref m_Safety, s_staticSafetyId.Data);
#endif
#endif
            m_ListData = UnsafeList.Create(UnsafeUtility.SizeOf <T>(), UnsafeUtility.AlignOf <T>(), initialCapacity, mAllocator);

            m_AllocatorLabel = mAllocator;

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            AtomicSafetyHandle.SetBumpSecondaryVersionOnScheduleWrite(m_Safety, true);
#endif
        }
        private NativeQuadTree(int capacity, Allocator allocator, int disposeSentinelStackDepth)
        {
            var totalSize = UnsafeUtility.SizeOf(typeof(NativeQuadTreeNode)) * (long)capacity;

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            if (allocator <= Allocator.None)
            {
                throw new ArgumentException("Allocator must be Temp, TempJob or Persistent", nameof(allocator));
            }
            if (capacity < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(capacity), "Capacity must be >= 0");
            }

            CollectionHelper.CheckIsUnmanaged <NativeQuadTreeNode>();

            if (totalSize > int.MaxValue)
            {
                throw new ArgumentOutOfRangeException(nameof(capacity), $"Capacity * sizeof(T) cannot exceed {int.MaxValue} bytes");
            }

            DisposeSentinel.Create(out m_Safety, out m_DisposeSentinel, disposeSentinelStackDepth, allocator);
#if UNITY_2020_1_OR_NEWER
            if (s_staticSafetyId.Data == 0)
            {
                CreateStaticSafetyId();
            }
            AtomicSafetyHandle.SetStaticSafetyId(ref m_Safety, s_staticSafetyId.Data);
#endif
#endif
            m_ListData       = UnsafeList.Create(UnsafeUtility.SizeOf <NativeQuadTreeNode>(), UnsafeUtility.AlignOf <NativeQuadTreeNode>(), capacity, allocator);
            m_SequenceId     = UnsafeList.Create(UnsafeUtility.SizeOf <int>(), UnsafeUtility.AlignOf <int>(), 0, allocator);
            m_AllocatorLabel = allocator;
            _eps             = 0;
            _resolution      = 1;
            _min             = int2.zero;
            _max             = int2.zero;

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            AtomicSafetyHandle.SetBumpSecondaryVersionOnScheduleWrite(m_Safety, true);
#endif
        }
        AABB               bounds; // NOTE: Currently assuming uniform

        /// <summary>
        /// Create a new Octree.
        /// - Ensure the bounds are not way bigger than needed, otherwise the buckets are very off. Probably best to calculate bounds
        /// - The higher the depth, the larger the overhead, it especially goes up at a depth of 7/8
        /// </summary>
        public NativeOctree_Original(AABB bounds, Allocator allocator = Allocator.Temp, int maxDepth = 6, short maxLeafElements = 16,
                                     int initialElementsCapacity      = 256
                                     ) : this()
        {
            this.bounds          = bounds;
            this.maxDepth        = maxDepth;
            this.maxLeafElements = maxLeafElements;
            elementsCount        = 0;

            if (maxDepth > 8)
            {
                // Currently no support for higher depths, the morton code lookup tables would have to support it
                throw new InvalidOperationException();
            }

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            CollectionHelper.CheckIsUnmanaged <T>();
            DisposeSentinel.Create(out safetyHandle, out disposeSentinel, 1, allocator);
#endif

            // Allocate memory for every depth, the nodes on all depths are stored in a single continuous array
            var totalSize = LookupTables.DepthSizeLookup[maxDepth + 1];

            lookup = UnsafeList.Create(UnsafeUtility.SizeOf <int>(),
                                       UnsafeUtility.AlignOf <int>(),
                                       totalSize,
                                       allocator,
                                       NativeArrayOptions.ClearMemory);

            nodes = UnsafeList.Create(UnsafeUtility.SizeOf <OctNode>(),
                                      UnsafeUtility.AlignOf <OctNode>(),
                                      totalSize,
                                      allocator,
                                      NativeArrayOptions.ClearMemory);

            elements = UnsafeList.Create(UnsafeUtility.SizeOf <OctElement <T> >(),
                                         UnsafeUtility.AlignOf <OctElement <T> >(),
                                         initialElementsCapacity,
                                         allocator);
        }
        NativeListArray(int initialListCapacity, Allocator allocator, int disposeSentinelStackDepth)
        {
            var totalSize = UnsafeUtility.SizeOf <T>() * (long)initialListCapacity;

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            // Native allocation is only valid for Temp, Job and Persistent.
            CheckAllocator(allocator);
            CheckArgPositive(initialListCapacity);


            CollectionHelper.CheckIsUnmanaged <T>();
            CheckArgInRange(totalSize, int.MaxValue);

            DisposeSentinel.Create(out m_Safety, out m_DisposeSentinel, disposeSentinelStackDepth, allocator);
#endif
            m_Array = UnsafeListArray.Create(allocator);
            m_Array->InitialListCapacity = initialListCapacity;

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            AtomicSafetyHandle.SetBumpSecondaryVersionOnScheduleWrite(m_Safety, true);
#endif
        }