public static void DeallocateHashSet(NativeHashSetData *data, Allocator allocator) { UnsafeUtility.Free(data->values, allocator); data->values = null; data->buckets = null; data->next = null; UnsafeUtility.Free(data, allocator); }
public void Dispose() { #if ENABLE_UNITY_COLLECTIONS_CHECKS AtomicSafetyHandle.CheckDeallocateAndThrow(m_Safety); DisposeSentinel.Dispose(ref m_Safety, ref m_DisposeSentinel); #endif NativeHashSetData.DeallocateHashSet(buffer, allocator); buffer = null; }
/// <summary> /// Safely disposes of this container and deallocates its memory when the jobs that use it have completed. /// </summary> /// <remarks>You can call this function dispose of the container 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 container after all jobs /// using it have run.</remarks> /// <param name="jobHandle">The job handle or handles for any scheduled jobs that use this container.</param> /// <returns>A new job handle containing the prior handles as well as the handle for the job that deletes /// the container.</returns> public JobHandle Dispose(JobHandle inputDeps) { #if ENABLE_UNITY_COLLECTIONS_CHECKS // [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). DisposeSentinel.Clear(ref m_DisposeSentinel); #endif var jobHandle = new DisposeJob { Container = this }.Schedule(inputDeps); #if ENABLE_UNITY_COLLECTIONS_CHECKS AtomicSafetyHandle.Release(m_Safety); #endif buffer = null; return(jobHandle); }
public static void AllocateHashSet <T>( int capacity, Allocator label, out NativeHashSetData *buffer) where T : struct { #if ENABLE_UNITY_COLLECTIONS_CHECKS if (!UnsafeUtility.IsBlittable <T>()) { throw new ArgumentException($"{typeof(T)} used in NativeHashSet<{typeof(T)}> must be blittable"); } #endif var data = (NativeHashSetData *)UnsafeUtility.Malloc( sizeof(NativeHashSetData), UnsafeUtility.AlignOf <NativeHashSetData>(), label); int bucketCapacity = math.ceilpow2(capacity * 2); data->valueCapacity = capacity; data->bucketCapacityMask = bucketCapacity - 1; int nextOffset, bucketOffset; int totalSize = CalculateDataSize <T>(capacity, bucketCapacity, out nextOffset, out bucketOffset); data->values = (byte *)UnsafeUtility.Malloc(totalSize, JobsUtility.CacheLineSize, label); data->next = data->values + nextOffset; data->buckets = data->values + bucketOffset; buffer = data; }
void Deallocate() { NativeHashSetData.DeallocateHashSet(buffer, allocator); buffer = null; }