unsafe private NativeList(int capacity, Allocator i_label, int stackDepth) { #if ENABLE_UNITY_COLLECTIONS_CHECKS if (!UnsafeUtility.IsBlittable <T>()) { throw new ArgumentException(string.Format("{0} used in NativeList<{0}> must be blittable", typeof(T))); } #endif NativeListData *data = (NativeListData *)UnsafeUtility.Malloc(sizeof(NativeListData), UnsafeUtility.AlignOf <NativeListData>(), i_label); int elementSize = UnsafeUtility.SizeOf <T> (); //@TODO: Find out why this is needed? capacity = Math.Max(1, capacity); data->buffer = UnsafeUtility.Malloc(capacity * elementSize, UnsafeUtility.AlignOf <T>(), i_label); data->length = 0; data->capacity = capacity; m_ListData = data; m_AllocatorLabel = i_label; #if ENABLE_UNITY_COLLECTIONS_CHECKS DisposeSentinel.Create(out m_Safety, out m_DisposeSentinel, stackDepth); #endif }
public unsafe static void Execute(ref T data, IntPtr listDataPtr, IntPtr bufferRangePatchData, ref JobRanges ranges, int jobIndex) { NativeListData * ptr = (NativeListData *)((void *)listDataPtr); NativeParticleData nativeParticleData; ParticleSystem.CopyManagedJobData(ptr->system, out nativeParticleData); ParticleSystemJobData particleSystemJobData = new ParticleSystemJobData(ref nativeParticleData); while (true) { int num; int num2; bool flag = !JobsUtility.GetWorkStealingRange(ref ranges, jobIndex, out num, out num2); if (flag) { break; } JobsUtility.PatchBufferMinMaxRanges(bufferRangePatchData, UnsafeUtility.AddressOf <T>(ref data), num, num2 - num); for (int i = num; i < num2; i++) { data.Execute(particleSystemJobData, i); } } AtomicSafetyHandle.CheckDeallocateAndThrow(particleSystemJobData.m_Safety); AtomicSafetyHandle.Release(particleSystemJobData.m_Safety); }
public NativeListImpl(int capacity, Allocator allocatorLabel) #endif { #if ENABLE_UNITY_COLLECTIONS_CHECKS this.sentinel = sentinel; m_ListData = null; if (!UnsafeUtility.IsBlittable <T>()) { this.sentinel.Dispose(); throw new ArgumentException(string.Format("{0} used in NativeList<{0}> must be blittable", typeof(T))); } #endif m_MemoryAllocator = default(TMemManager); m_ListData = (NativeListData *)m_MemoryAllocator.Init(UnsafeUtility.SizeOf <NativeListData>(), UnsafeUtility.AlignOf <NativeListData>(), allocatorLabel); var elementSize = UnsafeUtility.SizeOf <T>(); //@TODO: Find out why this is needed? capacity = Math.Max(1, capacity); m_ListData->buffer = UnsafeUtility.Malloc(capacity * elementSize, UnsafeUtility.AlignOf <T>(), allocatorLabel); m_ListData->length = 0; m_ListData->capacity = capacity; }
/// <summary> /// Disposes of this list and deallocates its memory immediately. /// </summary> public void Dispose() { #if ENABLE_UNITY_COLLECTIONS_CHECKS DisposeSentinel.Dispose(ref m_Safety, ref m_DisposeSentinel); #endif NativeListUnsafeUtility.DeallocateList(m_ListData, m_Allocator); m_ListData = null; }
public unsafe static void DeallocateList(void *buffer, Allocator allocation) { NativeListData *data = (NativeListData *)buffer; UnsafeUtility.Free(data->buffer, allocation); data->buffer = null; UnsafeUtility.Free(buffer, allocation); }
public static unsafe void *GetUnsafePtr <T>(this NativeList <T> nativeList) where T : struct { #if ENABLE_UNITY_COLLECTIONS_CHECKS AtomicSafetyHandle.CheckWriteAndThrow(nativeList.m_Safety); #endif NativeListData *data = (NativeListData *)nativeList.m_ListData; return(data->buffer); }
public NativeList(int count, int capacity, Allocator alloc) { data = (NativeListData *)UnsafeUtility.Malloc(sizeof(NativeListData), 16, alloc); data->count = count; data->capacity = capacity; data->allocator = alloc; data->ptr = UnsafeUtility.Malloc(sizeof(T) * capacity, 16, alloc); }
unsafe public void Dispose() { #if ENABLE_UNITY_COLLECTIONS_CHECKS DisposeSentinel.Dispose(m_Safety, ref m_DisposeSentinel); #endif NativeListData.DeallocateList(m_Buffer, m_AllocatorLabel); m_Buffer = null; }
public NativeList_Int(int count, int capacity, Allocator alloc) { isCreated = true; data = MUnsafeUtility.Malloc <NativeListData>(sizeof(NativeListData), alloc); data->count = count; data->capacity = capacity; data->allocator = alloc; data->ptr = MUnsafeUtility.Malloc <int>(sizeof(int) * capacity, alloc); }
public NativeList(int capacity, Allocator alloc) { isCreated = true; capacity = Mathf.Max(capacity, 1); data = MUnsafeUtility.Malloc <NativeListData>(sizeof(NativeListData), alloc); data->count = 0; data->capacity = capacity; data->allocator = alloc; data->ptr = MUnsafeUtility.Malloc <T>(sizeof(T) * capacity, alloc); }
public unsafe void ResizeUninitialized(int length) { #if ENABLE_UNITY_COLLECTIONS_CHECKS AtomicSafetyHandle.CheckWriteAndThrow(m_Safety); #endif Capacity = math.max(length, Capacity); NativeListData *data = (NativeListData *)m_ListData; data->length = length; }
public unsafe static void Execute(ref T data, IntPtr listDataPtr, IntPtr unusedPtr, ref JobRanges ranges, int jobIndex) { NativeListData * ptr = (NativeListData *)((void *)listDataPtr); NativeParticleData nativeParticleData; ParticleSystem.CopyManagedJobData(ptr->system, out nativeParticleData); ParticleSystemJobData particleSystemJobData = new ParticleSystemJobData(ref nativeParticleData); data.Execute(particleSystemJobData); AtomicSafetyHandle.CheckDeallocateAndThrow(particleSystemJobData.m_Safety); AtomicSafetyHandle.Release(particleSystemJobData.m_Safety); }
public NativeList(int capacity = 10) { if (capacity < 1) { capacity = 1; } data = (NativeListData *)NativeMemory.Alloc(NativeListData.SIZE, NativeMemory.NativeMemoryType.RawList); data->structSize = Marshal.SizeOf <NativeListData>(); data->capacity = capacity; data->length = 0; data->arrayPtr = NativeMemory.Alloc(data->structSize * data->capacity, NativeMemory.NativeMemoryType.RawList); }
unsafe public void RemoveAtSwapBack(int index) { NativeListData *data = m_ListData; #if ENABLE_UNITY_COLLECTIONS_CHECKS AtomicSafetyHandle.CheckWriteAndBumpSecondaryVersion(m_Safety); #endif int newLength = Length - 1; this[index] = this[newLength]; data->length = newLength; }
public NativeList(int count, Allocator alloc, T defaultValue) { data = (NativeListData *)UnsafeUtility.Malloc(sizeof(NativeListData), 16, alloc); data->count = count; data->capacity = count; data->allocator = alloc; data->ptr = UnsafeUtility.Malloc(sizeof(T) * count, 16, alloc); T *add = (T *)data->ptr; for (int i = 0; i < count; ++i) { add[i] = defaultValue; } }
public NativeList_Int(int count, Allocator alloc, int defaultValue) { isCreated = true; data = MUnsafeUtility.Malloc <NativeListData>(sizeof(NativeListData), alloc); data->count = count; data->capacity = count; data->allocator = alloc; data->ptr = MUnsafeUtility.Malloc <int>(sizeof(int) * count, alloc); int *add = (int *)data->ptr; for (int i = 0; i < count; ++i) { add[i] = defaultValue; } }
public NativeList_ulong(int count, Allocator alloc, ulong defaultValue) { isCreated = true; data = MUnsafeUtility.Malloc <NativeListData>(sizeof(NativeListData), alloc); data->count = count; data->capacity = count; data->allocator = alloc; data->ptr = MUnsafeUtility.Malloc <ulong>(sizeof(ulong) * count, alloc); ulong *add = (ulong *)data->ptr; for (int i = 0; i < count; ++i) { add[i] = defaultValue; } }
internal static void DeallocateList(NativeListData *data, Allocator allocator) { if (data != null) { UnsafeUtility.Free(data->buffer, allocator); #if ENABLE_UNITY_COLLECTIONS_CHECKS data->buffer = (void *)0xDEADF00D; #endif UnsafeUtility.Free(data, allocator); } #if ENABLE_UNITY_COLLECTIONS_CHECKS else { throw new Exception("NativeList has yet to be allocated or has been dealocated!"); } #endif }
unsafe public void Add(T element) { NativeListData *data = m_ListData; #if ENABLE_UNITY_COLLECTIONS_CHECKS AtomicSafetyHandle.CheckWriteAndBumpSecondaryVersion(m_Safety); #endif if (data->length >= data->capacity) { Capacity = data->length + data->capacity * 2; } int length = data->length; data->length = length + 1; this[length] = element; }
//@TODO: Test for AddRange unsafe public void AddRange(NativeArray <T> elements) { NativeListData *data = m_ListData; #if ENABLE_UNITY_COLLECTIONS_CHECKS AtomicSafetyHandle.CheckWriteAndBumpSecondaryVersion(m_Safety); #endif if (data->length + elements.Length > data->capacity) { Capacity = data->length + elements.Length * 2; } int sizeOf = UnsafeUtility.SizeOf <T> (); UnsafeUtility.MemCpy((byte *)data->buffer + data->length * sizeOf, elements.GetUnsafePtr(), sizeOf * elements.Length); data->length += elements.Length; }
/// <summary> /// Safely disposes of this list and deallocates its memory when the jobs that use it have completed. /// </summary> /// <remarks>You can call this function dispose of the list immediately after scheduling the job. Pass /// the [JobHandle](https://docs.unity3d.com/ScriptReference/Unity.Jobs.JobHandle.html) returned by /// the [Job.Schedule](https://docs.unity3d.com/ScriptReference/Unity.Jobs.IJobExtensions.Schedule.html) /// method using the `jobHandle` parameter so the job scheduler can dispose the list after all jobs /// using it have run.</remarks> /// <param name="jobHandle">The job handle or handles for any scheduled jobs that use this list.</param> /// <returns>A new job handle containing the prior handles as well as the handle for the job that deletes /// the list.</returns> public JobHandle Dispose(JobHandle jobHandle) { // [DeallocateOnJobCompletion] is not supported, but we want the deallocation to happen in a thread. // DisposeSentinel needs to be cleared on main thread. // AtomicSafetyHandle can be destroyed after the job was scheduled (Job scheduling will check that no jobs are writing to the container) #if ENABLE_UNITY_COLLECTIONS_CHECKS DisposeSentinel.Clear(ref m_DisposeSentinel); #endif var jobData = new DisposeListJob { List = this }; jobHandle = jobData.Schedule(jobHandle); m_ListData = null; #if ENABLE_UNITY_COLLECTIONS_CHECKS AtomicSafetyHandle.Release(m_Safety); #endif return(jobHandle); }
unsafe NativeBuffer(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"); } IsBlittableAndThrow(); // 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; m_ListData->stride = UnsafeUtility.SizeOf <T>(); #if ENABLE_UNITY_COLLECTIONS_CHECKS DisposeSentinel.Create(out m_Safety, out m_DisposeSentinel, stackDepth, m_Allocator); #endif }
public void Dispose() { #if ENABLE_UNITY_COLLECTIONS_CHECKS DisposeSentinel.Dispose(ref m_Safety, ref m_DisposeSentinel); #endif if (m_ListData != null) { UnsafeUtility.Free(m_ListData->buffer, m_Allocator); #if ENABLE_UNITY_COLLECTIONS_CHECKS m_ListData->buffer = (void *)0xDEADF00D; #endif UnsafeUtility.Free(m_ListData, m_Allocator); m_ListData = null; } #if ENABLE_UNITY_COLLECTIONS_CHECKS else { throw new Exception("NativeList has yet to be allocated or has been dealocated!"); } #endif }