Beispiel #1
0
        private NativeRoutines(int initialCapacity, Allocator allocator, int disposeSentinelStackDepth)
        {
            var totalSize = (long)sizeof(T) * initialCapacity;

#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 (totalSize > int.MaxValue)
            {
                throw new ArgumentOutOfRangeException($"Capacity has exceeded {int.MaxValue.ToString()} bytes");
            }

            DisposeSentinel.Create(out m_Safety, out m_DisposeSentinel, disposeSentinelStackDepth, allocator);
#endif
            m_ListData = UnsafeList.Create(UnsafeUtility.SizeOf <T>(), UnsafeUtility.AlignOf <T>(), initialCapacity, allocator);

            m_AllocatorLabel = allocator;

#if UNITY_2019_3_OR_NEWER && ENABLE_UNITY_COLLECTIONS_CHECKS
            AtomicSafetyHandle.SetBumpSecondaryVersionOnScheduleWrite(m_Safety, true);
#endif
        }
        HashedVertices(int vertexCapacity, int chainedIndicesCapacity, Allocator allocator, int disposeSentinelStackDepth)
        {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            // Native allocation is only valid for Temp, Job and Persistent.
            CheckAllocator(allocator);
            CheckArgPositive(chainedIndicesCapacity);

            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
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            DisposeSentinel.Create(out m_Safety, out m_DisposeSentinel, 0, allocator);
#endif
            m_AllocatorLabel = allocator;
            var hashTableMemSize = (ushort)(kHashTableSize + 1) * UnsafeUtility.SizeOf <ushort>();
            m_HashTable = UnsafeUtility.Malloc(hashTableMemSize, UnsafeUtility.AlignOf <ushort>(), m_AllocatorLabel);
            UnsafeUtility.MemClear(m_HashTable, hashTableMemSize);

            m_Vertices = UnsafeList <float3> .Create(vertexCapacity, allocator);

            m_ChainedIndices = UnsafeList <ushort> .Create(chainedIndicesCapacity, allocator);

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            AtomicSafetyHandle.SetBumpSecondaryVersionOnScheduleWrite(m_Safety, true);
#endif
        }
        void Initialize<U>(int initialCapacity, ref U allocator, int disposeSentinelStackDepth) where U : unmanaged, AllocatorManager.IAllocator
        {
            var totalSize = UnsafeUtility.SizeOf<T>() * (long)initialCapacity;
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            CheckAllocator((Allocator)allocator.Handle.Value);
            CheckInitialCapacity(initialCapacity);
            CollectionHelper.CheckIsUnmanaged<T>();
            CheckTotalSize(initialCapacity, totalSize);

            DisposeSentinel.Create(out m_Safety, out m_DisposeSentinel, disposeSentinelStackDepth, (Allocator)allocator.Handle.Value);
            if (s_staticSafetyId.Data == 0)
            {
                CreateStaticSafetyId();
            }
            AtomicSafetyHandle.SetStaticSafetyId(ref m_Safety, s_staticSafetyId.Data);

            m_SafetyIndexHint = (allocator.Handle).AddSafetyHandle(m_Safety);
            if(m_SafetyIndexHint != AllocatorManager.AllocatorHandle.InvalidChildSafetyHandleIndex)
                DisposeSentinel.Clear(ref m_DisposeSentinel);
#endif
            m_ListData = UnsafeList.Create(UnsafeUtility.SizeOf<T>(), UnsafeUtility.AlignOf<T>(), initialCapacity, ref allocator);
            m_DeprecatedAllocator = (Allocator)allocator.Handle.Value;

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            AtomicSafetyHandle.SetBumpSecondaryVersionOnScheduleWrite(m_Safety, true);
#endif
        }
Beispiel #4
0
        public NativePriorityQueue(int initialCapacity, Allocator allocator)
        {
            var totalSize = UnsafeUtility.SizeOf <Node>() * (long)initialCapacity;

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            if (allocator <= Allocator.None)
            {
                throw new ArgumentException("Allocator must be Temp, TempJob, or Persistent", "allocator");
            }
            if (initialCapacity < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(initialCapacity), $"{nameof(initialCapacity)} must be >= 0");
            }
            if (!UnsafeUtility.IsBlittable <T>())
            {
                throw new ArgumentException($"{typeof(T)} used in {nameof(NativePriorityQueue<T>)} must be blittable");
            }

            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, 0, allocator);
            AtomicSafetyHandle.SetBumpSecondaryVersionOnScheduleWrite(m_Safety, true);
#endif

            int nodeSize  = UnsafeUtility.SizeOf <Node>();
            int nodeAlign = UnsafeUtility.AlignOf <Node>();
            _nodes = UnsafeList.Create(nodeSize, nodeAlign, initialCapacity, allocator);
            _nodes->Add <Node>(default);
        }
Beispiel #5
0
        NativeList(int initialCapacity, Allocator allocator, 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 (allocator <= Allocator.None)
            {
                throw new ArgumentException("Allocator must be Temp, TempJob or Persistent", nameof(allocator));
            }
            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, allocator);
#endif
            m_ListData            = UnsafeList.Create(UnsafeUtility.SizeOf <T>(), UnsafeUtility.AlignOf <T>(), initialCapacity, allocator);
            m_DeprecatedAllocator = allocator;

#if UNITY_2019_3_OR_NEWER && ENABLE_UNITY_COLLECTIONS_CHECKS
            AtomicSafetyHandle.SetBumpSecondaryVersionOnScheduleWrite(m_Safety, true);
#endif
        }
        public static DynamicBlobAssetBatch *Allocate(Allocator allocator)
        {
            var batch = (DynamicBlobAssetBatch *)UnsafeUtility.Malloc(sizeof(DynamicBlobAssetBatch), UnsafeUtility.AlignOf <DynamicBlobAssetBatch>(), allocator);

            batch->m_Allocator  = allocator;
            batch->m_BlobAssets = UnsafeList.Create(sizeof(BlobAssetPtr), UnsafeUtility.AlignOf <BlobAssetPtr>(), 1, allocator);
            return(batch);
        }
Beispiel #7
0
        public UnsafeList *InitializeIndex(int index, int sizeOf, int alignOf, int capacity, NativeArrayOptions options = NativeArrayOptions.UninitializedMemory)
        {
            CheckIndexInRange(index, Length);
            var ptr = UnsafeList.Create(sizeOf, alignOf, capacity, Allocator, options);

            Ptr[index] = ptr;
            return(ptr);
        }
        public static DynamicBlobAssetBatch *Allocate(Allocator allocator)
        {
            var batch = (DynamicBlobAssetBatch *)Memory.Unmanaged.Allocate(sizeof(DynamicBlobAssetBatch), UnsafeUtility.AlignOf <DynamicBlobAssetBatch>(), allocator);

            batch->m_FramesToRetainBlobAssets = 1;
            batch->m_Allocator  = allocator;
            batch->m_BlobAssets = UnsafeList.Create(sizeof(BlobAssetPtr), UnsafeUtility.AlignOf <BlobAssetPtr>(), 1, allocator);
            return(batch);
        }
        HashedVertices(int vertexCapacity, int chainedIndicesCapacity, Allocator allocator = Allocator.Persistent)
        {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            DisposeSentinel.Create(out m_Safety, out m_DisposeSentinel, 0, allocator);
#endif
            m_AllocatorLabel = allocator;
            var hashTableMemSize = (ushort)(kHashTableSize + 1) * UnsafeUtility.SizeOf <ushort>();
            m_HashTable = UnsafeUtility.Malloc(hashTableMemSize, UnsafeUtility.AlignOf <ushort>(), m_AllocatorLabel);
            UnsafeUtility.MemClear(m_HashTable, hashTableMemSize);

            m_Vertices       = UnsafeList.Create(UnsafeUtility.SizeOf <float3>(), UnsafeUtility.AlignOf <float3>(), vertexCapacity, allocator);
            m_ChainedIndices = UnsafeList.Create(UnsafeUtility.SizeOf <ushort>(), UnsafeUtility.AlignOf <ushort>(), chainedIndicesCapacity, allocator);
        }
        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);
        }
Beispiel #12
0
        /// <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(Aabb bounds, Allocator allocator = Allocator.Temp, int maxDepth = 5, short maxLeafElements = 16, int initialElementsCapacity = 256)         //: this()
        {
            Data = (UnsafeOctree *)UnsafeUtility.Malloc(sizeof(UnsafeOctree), UnsafeUtility.AlignOf <UnsafeOctree>(), allocator);
            UnsafeUtility.MemClear(Data, sizeof(UnsafeOctree));

            Data->RootBounds         = bounds;
            Data->MaxDepth           = maxDepth;
            Data->MaxElementsPerLeaf = maxLeafElements;
            Data->Allocator          = allocator;

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

            var totalSize = LookupTables.DepthSizeLookup[maxDepth + 1];

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

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

            Data->Elements = UnsafeList.Create(UnsafeUtility.SizeOf <OctElement <T> >(),
                                               UnsafeUtility.AlignOf <OctElement <T> >(),
                                               initialElementsCapacity,
                                               allocator);

            Data->Quads = UnsafeList.Create(UnsafeUtility.SizeOf <QuadGroup>(),
                                            UnsafeUtility.AlignOf <QuadGroup>(),
                                            totalSize, // todo, allocate after counting used branches.
                                            allocator);
        }
            public static Connection Create(uint id)
            {
                Connection connection;

                connection.Id = id;
                connection.m_IncomingMessages = UnsafeList.Create
                                                (
                    UnsafeUtility.SizeOf <NetworkingMessage>(),
                    UnsafeUtility.AlignOf <NetworkingMessage>(),
                    8,
                    Allocator.Persistent,
                    NativeArrayOptions.ClearMemory
                                                );
                connection.m_IncomingEvents = UnsafeList.Create
                                              (
                    UnsafeUtility.SizeOf <NetworkEvent.Type>(),
                    UnsafeUtility.AlignOf <NetworkEvent.Type>(),
                    8,
                    Allocator.Persistent,
                    NativeArrayOptions.ClearMemory
                                              );
                return(connection);
            }
Beispiel #14
0
        NativeList(int initialCapacity, Allocator allocator, int disposeSentinelStackDepth)
        {
            var totalSize = UnsafeUtility.SizeOf <T>() * (long)initialCapacity;

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            CheckAllocator(allocator);
            CheckInitialCapacity(initialCapacity);
            CollectionHelper.CheckIsUnmanaged <T>();
            CheckTotalSize(initialCapacity, totalSize);

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

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            AtomicSafetyHandle.SetBumpSecondaryVersionOnScheduleWrite(m_Safety, true);
#endif
        }
Beispiel #15
0
        public static UnsafeNodesList *Create(int length, Allocator allocator, NativeArrayOptions options = NativeArrayOptions.UninitializedMemory)
        {
            var handle = new AllocatorManager.AllocatorHandle {
                Value = (int)allocator
            };
            UnsafeNodesList *listData = AllocatorManager.Allocate <UnsafeNodesList>(handle);

            UnsafeUtility.MemClear(listData, UnsafeUtility.SizeOf <UnsafeNodesList>());

            listData->allocator    = allocator;
            listData->emptyIndices = UnsafeList.Create(UnsafeUtility.SizeOf <int>(), UnsafeUtility.AlignOf <int>(), length, allocator);

            if (length != 0)
            {
                listData->Resize(length);
            }

            if (options == NativeArrayOptions.ClearMemory && listData->ptr != null)
            {
                UnsafeUtility.MemClear(listData->ptr, listData->length * UnsafeUtility.SizeOf <int>());
            }

            return(listData);
        }
Beispiel #16
0
            public static Connection Create(Peer peer)
            {
                Connection connection;

                connection.m_PeerPtr    = peer.NativeData;
                connection.Id           = peer.ID;
                connection.m_DataStream = UnsafeList.Create
                                          (
                    UnsafeUtility.SizeOf <byte>(),
                    UnsafeUtility.AlignOf <byte>(),
                    4096,
                    Allocator.Persistent,
                    NativeArrayOptions.ClearMemory
                                          );
                connection.m_IncomingEvents = UnsafeList.Create
                                              (
                    UnsafeUtility.SizeOf <DriverEvent>(),
                    UnsafeUtility.AlignOf <DriverEvent>(),
                    8,
                    Allocator.Persistent,
                    NativeArrayOptions.ClearMemory
                                              );
                return(connection);
            }
Beispiel #17
0
 public List(int initialCapacity, Allocator allocator)
 {
     _list      = UnsafeList.Create(UnsafeUtility.SizeOf <T>(), UnsafeUtility.AlignOf <T>(), initialCapacity, allocator);
     _allocator = allocator;
 }
Beispiel #18
0
 public UnsafeListContainer(int initialCapacity, int sizeOf, int alignOf, Allocator allocator)
 {
     m_ListData  = UnsafeList.Create(sizeOf, alignOf, initialCapacity, allocator);
     m_Allocator = allocator;
 }