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 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 }
/// <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 }
unsafe NativeList(int capacity, Allocator i_label, int stackDepth) { //@TODO: Find out why this is needed? capacity = Math.Max(1, capacity); var totalSize = UnsafeUtility.SizeOf <T>() * (long)capacity; #if ENABLE_UNITY_COLLECTIONS_CHECKS // Native allocation is only valid for Temp, Job and Persistent. if (i_label <= Allocator.None) { throw new ArgumentException("Allocator must be Temp, TempJob or Persistent", nameof(i_label)); } if (capacity < 0) { throw new ArgumentOutOfRangeException(nameof(capacity), "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(capacity), $"Capacity * sizeof(T) cannot exceed {int.MaxValue} bytes"); } #endif m_Allocator = i_label; m_ListData = (NativeListData *)UnsafeUtility.Malloc(UnsafeUtility.SizeOf <NativeListData>(), UnsafeUtility.AlignOf <NativeListData>(), m_Allocator); m_ListData->buffer = UnsafeUtility.Malloc(totalSize, UnsafeUtility.AlignOf <T>(), m_Allocator); m_ListData->length = 0; m_ListData->capacity = capacity; #if ENABLE_UNITY_COLLECTIONS_CHECKS DisposeSentinel.Create(out m_Safety, out m_DisposeSentinel, stackDepth, m_Allocator); #endif }
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 }