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)); }
/// <summary> /// Create a <see cref="NativeArray2D{T}" />. /// </summary> /// <param name="columnCount">The number of columns (X).</param> /// <param name="rowCount">The number of rows (Y).</param> /// <param name="allocator">The <see cref="Unity.Collections.Allocator" /> type to use.</param> /// <param name="nativeArrayOptions">Should the memory be cleared on allocation?</param> public NativeArray2D(int columnCount, int rowCount, Allocator allocator, NativeArrayOptions nativeArrayOptions) { ColumnCount = columnCount; RowCount = rowCount; Array = new NativeArray <T>(rowCount * columnCount, allocator, nativeArrayOptions); }
// ReSharper enable MemberCanBePrivate.Global /// <summary> /// Create a new <see cref="NativeSimpleQueue{T}" />. /// </summary> /// <param name="capacity">The maximum number of items allowed in the <see cref="NativeSimpleQueue{T}" /></param> /// <param name="allocator">The <see cref="Unity.Collections.Allocator" /> type to use.</param> /// <param name="nativeArrayOptions">Should the memory be cleared on allocation?</param> public NativeSimpleQueue(int capacity, Allocator allocator, NativeArrayOptions nativeArrayOptions) { Array = new NativeArray <T>(capacity, allocator, nativeArrayOptions); FirstIndex = 0; EndIndex = 0; Count = 0; }
/// <summary> /// Resize the capacity of the <see cref="NativeSimpleQueue{T}" />. /// </summary> /// <param name="capacity">The desired capacity for the <see cref="NativeSimpleQueue{T}" /></param> /// <param name="allocator">The <see cref="Unity.Collections.Allocator" /> type to use.</param> /// <param name="nativeArrayOptions">Should the memory be cleared on allocation?</param> private void SetCapacity(int capacity, Allocator allocator, NativeArrayOptions nativeArrayOptions) { NativeArray <T> newArray = new NativeArray <T>(capacity, allocator, nativeArrayOptions); if (Count > 0) { if (FirstIndex < EndIndex) { for (int i = 0; i < Count; i++) { newArray[i] = Array[FirstIndex + i]; } } else { for (int i = 0; i < Array.Length - FirstIndex; i++) { newArray[i] = Array[FirstIndex + i]; } for (int i = 0; i < EndIndex; i++) { newArray[Array.Length - FirstIndex + i] = Array[i]; } } } Array.Dispose(); Array = newArray; FirstIndex = 0; EndIndex = Count == capacity ? 0 : Count; }
/// <summary> /// Reallocate the dense and sparse arrays with additional capacity. /// </summary> /// <param name="extraCapacity">How many indices to expand the dense and sparse arrays by.</param> /// <param name="versionArray">Array containing version numbers to check against.</param> /// <param name="allocator">The <see cref="Unity.Collections.Allocator" /> type to use.</param> /// <param name="nativeArrayOptions">Should the memory be cleared on allocation?</param> public void Expand(int extraCapacity, ref NativeArray <ulong> versionArray, Allocator allocator, NativeArrayOptions nativeArrayOptions) { int currentCapacity = SparseArray.Length; int newCapacity = currentCapacity + extraCapacity; NativeArray <int> newSparseArray = new NativeArray <int>(newCapacity, allocator, nativeArrayOptions); NativeSlice <int> newSparseArraySlice = new NativeSlice <int>(newSparseArray, 0, currentCapacity); newSparseArraySlice.CopyFrom(SparseArray); SparseArray = newSparseArray; NativeArray <int> newDenseArray = new NativeArray <int>(newCapacity, allocator, nativeArrayOptions); NativeSlice <int> newDenseArraySlice = new NativeSlice <int>(newDenseArray, 0, currentCapacity); newDenseArraySlice.CopyFrom(DenseArray); DenseArray = newDenseArray; NativeArray <ulong> newVersionArray = new NativeArray <ulong>(newCapacity, allocator, nativeArrayOptions); NativeSlice <ulong> newVersionArraySlice = new NativeSlice <ulong>(newVersionArray, 0, currentCapacity); newVersionArraySlice.CopyFrom(versionArray); versionArray = newVersionArray; for (int i = currentCapacity; i < newCapacity; i++) { DenseArray[i] = -1; // Set new dense indices as unclaimed. SparseArray[i] = i + 1; // Build the free list chain. } }
public NativeArray2D(int x, int y, Allocator allocator, NativeArrayOptions options = NativeArrayOptions.ClearMemory) { Internal = new NativeArray <T>(x * y, allocator); _ptr = Internal.GetUnsafePtr(); YLength = y; XLength = x; }
public NativeArray(int length, Allocator allocator, NativeArrayOptions options = NativeArrayOptions.ClearMemory) { Allocate(length, allocator, out this); if ((options & NativeArrayOptions.ClearMemory) == NativeArrayOptions.ClearMemory) { UnsafeUtility.MemClear(m_Buffer, (long)Length * (long)UnsafeUtility.SizeOf <T>()); } }
/// <summary> /// Create a <see cref="NativeUniformArray3D{T}" /> with a uniform dimensional length. /// </summary> /// <remarks></remarks> /// <param name="stride">X length, Y length and Z length will all be set to this value.</param> /// <param name="allocator">The <see cref="Unity.Collections.Allocator" /> type to use.</param> /// <param name="nativeArrayOptions">Should the memory be cleared on allocation?</param> public NativeUniformArray3D(int stride, Allocator allocator, NativeArrayOptions nativeArrayOptions) { Stride = stride; _strideSquared = stride * stride; Length = stride * stride * stride; Array = new NativeArray <T>(Length, allocator, nativeArrayOptions); }
public NativeArray <T> NewNativeArray <T>(int length, Allocator allocator, NativeArrayOptions options = NativeArrayOptions.ClearMemory) where T : struct { var res = new NativeArray <T>(length, allocator, options); unsafe { slots.Add(new NativeArraySlot { hash = (long)NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(res), lastWrite = default,
/// <summary> /// Changes the list length, resizing if necessary. /// </summary> /// <param name="length">The new length of the list.</param> /// <param name="options">Memory should be cleared on allocation or left uninitialized.</param> public void Resize(int length, NativeArrayOptions options) { #if ENABLE_UNITY_COLLECTIONS_CHECKS AtomicSafetyHandle.CheckWriteAndBumpSecondaryVersion(m_Safety); AtomicSafetyHandle.CheckReadAndThrow(m_Safety); #endif m_ListData->Resize(UnsafeUtility.SizeOf <T>(), UnsafeUtility.AlignOf <T>(), length, options); }
/// <summary> /// Constructs a new reference container using the specified 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> /// <param name="options">A member of the /// [Unity.Collections.NativeArrayOptions](https://docs.unity3d.com/ScriptReference/Unity.Collections.NativeArrayOptions.html) enumeration.</param> public NativeReference(Allocator allocator, NativeArrayOptions options = NativeArrayOptions.ClearMemory) { Allocate(allocator, out this); if (options == NativeArrayOptions.ClearMemory) { UnsafeUtility.MemClear(m_Data, UnsafeUtility.SizeOf <T>()); } }
/// <summary> /// Shrink the capacity of the <see cref="NativeSimpleQueue{T}" /> to fit its contents. /// </summary> /// <param name="allocator">The <see cref="Unity.Collections.Allocator" /> type to use.</param> /// <param name="nativeArrayOptions">Should the memory be cleared on allocation?</param> public void TrimExcess(Allocator allocator, NativeArrayOptions nativeArrayOptions) { int threshold = (int)(Array.Length * 0.9); if (Count < threshold) { SetCapacity(Count, allocator, nativeArrayOptions); } }
public unsafe NativeArrayNoLeakDetection(int length, Allocator allocator, NativeArrayOptions options = NativeArrayOptions.ClearMemory) { Allocate(length, allocator, out this); if ((options & NativeArrayOptions.ClearMemory) != NativeArrayOptions.ClearMemory) { return; } UnsafeUtility.MemClear(m_Buffer, (long)Length * (long)UnsafeUtility.SizeOf <T>()); }
public NativeArray2D(int length0, int length1, Allocator allocator, NativeArrayOptions options = NativeArrayOptions.ClearMemory) { Allocate(length0, length1, allocator, out this); if (options == NativeArrayOptions.ClearMemory) { UnsafeUtility.MemClear(m_Buffer, m_Length * UnsafeUtility.SizeOf <T>()); } }
public NativeArray3D(int x, int y, int z, Allocator allocator, NativeArrayOptions options = NativeArrayOptions.ClearMemory) { Internal = new NativeArray <T>(x * y * z, allocator); IsDisposed = false; _xLength = x; _yLength = y; _zLength = z; _yzLength = y * z; }
public MeshProxyArray(int length, Allocator allocator, NativeArrayOptions options = NativeArrayOptions.ClearMemory) { Allocate(length, allocator, out this); if ((options & NativeArrayOptions.ClearMemory) != NativeArrayOptions.ClearMemory) { return; } UnsafeUtility.MemClear(this.m_Meshes, (long)this.Length * (long)UnsafeUtility.SizeOf <MeshProxyElement>()); }
public NativeIntArray(int length, Allocator allocator, NativeArrayOptions options = NativeArrayOptions.ClearMemory) { Allocate(length, allocator, out this); // Set the memory block to 0 if requested. if ((options & NativeArrayOptions.ClearMemory) == NativeArrayOptions.ClearMemory) { UnsafeUtility.MemClear(m_Buffer, (long)length * UnsafeUtility.SizeOf <int>()); } }
public NativeMinHeap(int capacity, Allocator allocator, NativeArrayOptions options = NativeArrayOptions.ClearMemory) { // Allocate the buffer Allocate(capacity, allocator, out this); // Clear the buffer if passed as argument if ((options & NativeArrayOptions.ClearMemory) == NativeArrayOptions.ClearMemory) { UnsafeUtility.MemClear(m_Buffer, (long)capacity * UnsafeUtility.SizeOf <NativeMinHeapNode <TValue, TPriority> >()); } }
public static void ResizeNativeArray <T>(ref NativeArray <T> array, int length, NativeArrayOptions option = NativeArrayOptions.UninitializedMemory) where T : struct { if (array.Length < length) { if (array.IsCreated) { array.Dispose(); } array = new NativeArray <T>(length, Allocator.Persistent, option); } }
public NativeArray(int length, Allocator allocator, NativeArrayOptions options = NativeArrayOptions.ClearMemory) { NativeArray <T> .Allocate(length, allocator, out this); bool flag = (options & NativeArrayOptions.ClearMemory) == NativeArrayOptions.ClearMemory; if (flag) { UnsafeUtility.MemClear(this.m_Buffer, (long)this.Length * (long)UnsafeUtility.SizeOf <T>()); } }
/// <summary> /// Constructs a new list using the specified type of memory allocation. /// </summary> /// <param name="initialCapacity">The initial capacity of the list. If the list grows larger than its capacity, /// the internal array is copied to a new, larger array.</param> /// <param name="allocator">A member of the /// [Unity.Collections.Allocator](https://docs.unity3d.com/ScriptReference/Unity.Collections.Allocator.html) enumeration.</param> /// <param name="options">Memory should be cleared on allocation or left uninitialized.</param> /// <remarks>The list initially has a capacity of one. To avoid reallocating memory for the list, specify /// sufficient capacity up front.</remarks> public unsafe UnsafePtrList(int initialCapacity, Allocator allocator, NativeArrayOptions options = NativeArrayOptions.UninitializedMemory) { Ptr = null; length = 0; capacity = 0; Allocator = AllocatorManager.None; var sizeOf = IntPtr.Size; this.ListData() = new UnsafeList(sizeOf, sizeOf, initialCapacity, allocator, options); }
/// <summary> /// Construct a new 2D Native Array with the given row and column count, and given allocator. /// </summary> /// <param name="columnCount"></param> /// <param name="rowCount"></param> /// <param name="allocator"></param> public NativeArray2D(int columnCount, int rowCount, Allocator allocator, NativeArrayOptions options = NativeArrayOptions.ClearMemory) { m_data = new NativeArray <T>(columnCount * rowCount, allocator, options); #if ENABLE_UNITY_COLLECTIONS_CHECKS m_safety = NativeArrayUnsafeUtility.GetAtomicSafetyHandle(m_data); #endif m_columns = columnCount; m_rows = rowCount; }
/************************************************************************************************************************/ #region Initialisation /************************************************************************************************************************/ /// <summary> /// Allocates room for a specified number of properties to be filled by /// <see cref="InitialiseProperty(int, Transform, Type, string)"/>. /// </summary> public AnimatedProperty(IAnimancerComponent animancer, int propertyCount, NativeArrayOptions options = NativeArrayOptions.ClearMemory) { _Properties = new NativeArray <PropertyStreamHandle>(propertyCount, Allocator.Persistent, options); _Values = new NativeArray <TValue>(propertyCount, Allocator.Persistent); CreateJob(); var playable = animancer.Playable; CreatePlayable(playable); playable.Disposables.Add(this); }
NativeBitArray(int numBits, Allocator allocator, NativeArrayOptions options, int disposeSentinelStackDepth) { CheckAllocator(allocator); #if ENABLE_UNITY_COLLECTIONS_CHECKS 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_BitArray = new UnsafeBitArray(numBits, allocator, options); }
/// <summary> /// Constructs a new container with the specified initial capacity and type of memory allocation. /// </summary> /// <param name="numBits">Number of bits.</param> /// <param name="allocator">A member of the /// [Unity.Collections.Allocator](https://docs.unity3d.com/ScriptReference/Unity.Collections.Allocator.html) enumeration.</param> /// <param name="options">Memory should be cleared on allocation or left uninitialized.</param> public UnsafeBitArray(int numBits, Allocator allocator, NativeArrayOptions options = NativeArrayOptions.ClearMemory) { Allocator = allocator; var sizeInBytes = Bitwise.AlignUp(numBits, 64) / 8; Ptr = (ulong *)UnsafeUtility.Malloc(sizeInBytes, 16, allocator); Length = sizeInBytes * 8; if (options == NativeArrayOptions.ClearMemory) { UnsafeUtility.MemClear(Ptr, sizeInBytes); } }
/// <summary> /// Create a <see cref="NativeSparseSet" /> with an <paramref name="initialCapacity" />. /// </summary> /// <param name="initialCapacity">The initial capacity of the sparse and dense int arrays.</param> /// <param name="allocator">The <see cref="Unity.Collections.Allocator" /> type to use.</param> /// <param name="nativeArrayOptions">Should the memory be cleared on allocation?</param> public NativeSparseSet(int initialCapacity, Allocator allocator, NativeArrayOptions nativeArrayOptions) { ++initialCapacity; DenseArray = new NativeArray <int>(initialCapacity, allocator, nativeArrayOptions); SparseArray = new NativeArray <int>(initialCapacity, allocator, nativeArrayOptions); Count = 0; FreeIndex = 0; for (int i = 0; i < initialCapacity; i++) { DenseArray[i] = -1; SparseArray[i] = i + 1; } }
/// <summary> /// Constructs a new container with the specified initial capacity and type of memory allocation. /// </summary> /// <param name="numBits">Number of bits.</param> /// <param name="allocator">A member of the /// [Unity.Collections.Allocator](https://docs.unity3d.com/ScriptReference/Unity.Collections.Allocator.html) enumeration.</param> /// <param name="options">Memory should be cleared on allocation or left uninitialized.</param> /// <note>Allocated number of bits will be aligned-up to closest 64-bits. For example, passing 1 as numBits will create BitArray that's /// 64-bit (8 bytes) long.</note> public UnsafeBitArray(int numBits, Allocator allocator, NativeArrayOptions options = NativeArrayOptions.ClearMemory) { CheckAllocator(allocator); Allocator = allocator; var sizeInBytes = Bitwise.AlignUp(numBits, 64) / 8; Ptr = (ulong *)Memory.Unmanaged.Allocate(sizeInBytes, 16, allocator); Length = numBits; if (options == NativeArrayOptions.ClearMemory) { UnsafeUtility.MemClear(Ptr, sizeInBytes); } }
/// <summary> /// Constructs a new container with the specified capacity and type of memory allocation. /// </summary> /// <param name="capacity">Container capacity.</param> /// <param name="allocator">A member of the /// [Unity.Collections.Allocator](https://docs.unity3d.com/ScriptReference/Unity.Collections.Allocator.html) enumeration.</param> /// <param name="options">Memory should be cleared on allocation or left uninitialized.</param> public UnsafeRingQueue(int capacity, Allocator allocator, NativeArrayOptions options = NativeArrayOptions.ClearMemory) { capacity += 1; Allocator = allocator; Control = new RingControl(capacity); var sizeInBytes = capacity * UnsafeUtility.SizeOf <T>(); Ptr = (T *)UnsafeUtility.Malloc(sizeInBytes, 16, allocator); if (options == NativeArrayOptions.ClearMemory) { UnsafeUtility.MemClear(Ptr, sizeInBytes); } }
/************************************************************************************************************************/ private void Awake() { // Create the job and initialise all its arrays. // They are all Persistent because we want them to last for the full lifetime of the job. // Most of them can use UninitializedMemory which is faster because we will be immediately filling them. // But the velocities will use the default ClearMemory to make sure all the values start at zero. // Since we are about to use these values several times, we can shorten the following lines a bit by using constants: const Allocator Persistent = Allocator.Persistent; const NativeArrayOptions UninitializedMemory = NativeArrayOptions.UninitializedMemory; var job = new DampingJob() { jointHandles = new NativeArray <TransformStreamHandle>(_BoneCount, Persistent, UninitializedMemory), localPositions = new NativeArray <Vector3>(_BoneCount, Persistent, UninitializedMemory), localRotations = new NativeArray <Quaternion>(_BoneCount, Persistent, UninitializedMemory), positions = new NativeArray <Vector3>(_BoneCount, Persistent, UninitializedMemory), velocities = new NativeArray <Vector3>(_BoneCount, Persistent), }; // Initialise the contents of the arrays for each bone. var animator = _Animancer.Animator; var bone = _EndBone; for (int i = _BoneCount - 1; i >= 0; i--) { job.jointHandles[i] = animator.BindStreamTransform(bone); job.localPositions[i] = bone.localPosition; job.localRotations[i] = bone.localRotation; job.positions[i] = bone.position; bone = bone.parent; } job.rootHandle = animator.BindStreamTransform(bone); // Add the job to Animancer's output. _Animancer.Playable.InsertOutputJob(job); // Make sure Animancer disposes the Native Arrays when it is destroyed so we don't leak memory. // If we were writing our own job rather than just using the sample, we could have it implement the // IDisposable interface to dispose its arrays so that we would only have to call ...Add(_Job); here. _Animancer.Playable.Disposables.Add(job.jointHandles); _Animancer.Playable.Disposables.Add(job.localPositions); _Animancer.Playable.Disposables.Add(job.localRotations); _Animancer.Playable.Disposables.Add(job.positions); _Animancer.Playable.Disposables.Add(job.velocities); }
/// <summary> /// Add an <paramref name="item" /> to the <see cref="NativeSimpleQueue{T}" /> at its end.. /// </summary> /// <param name="item">The typed <see cref="object" /> to add.</param> /// <param name="allocator">The <see cref="Unity.Collections.Allocator" /> type to use.</param> /// <param name="nativeArrayOptions">Should the memory be cleared on allocation?</param> public void Enqueue(T item, Allocator allocator, NativeArrayOptions nativeArrayOptions) { if (Count == Array.Length) { int newCapacity = Array.Length * 2; if (newCapacity < Array.Length + MinimumGrow) { newCapacity = Array.Length + MinimumGrow; } SetCapacity(newCapacity, allocator, nativeArrayOptions); } Array[EndIndex] = item; EndIndex = (EndIndex + 1) % Array.Length; Count++; }