internal bool TryRemoveChildAllocator(AllocatorHandle handle, int childAllocatorIndex) { if (!NeedsUseAfterFreeTracking()) { return(false); } if (childAllocatorIndex == InvalidChildAllocatorIndex) { return(false); } childAllocatorIndex = math.min(childAllocatorIndex, ChildAllocators.Length - 1); while (childAllocatorIndex >= 0) { unsafe { var allocatorHandle = ChildAllocators.Ptr + childAllocatorIndex; if (AreTheSame(*allocatorHandle, handle)) { ChildAllocators.RemoveAtSwapBack(childAllocatorIndex); return(true); } } --childAllocatorIndex; } return(false); }
internal static bool AreTheSame(AllocatorHandle a, AllocatorHandle b) { if (a.Index != b.Index) { return(false); } if (a.Version != b.Version) { return(false); } return(true); }
internal int AddChildAllocator(AllocatorHandle handle) { if (!NeedsUseAfterFreeTracking()) { return(InvalidChildAllocatorIndex); } var result = ChildAllocators.Length; ChildAllocators.Add(handle); handle.Parent = this; handle.IndexInParent = result; return(result); }
/// <summary> /// /// </summary> /// <param name="handle"></param> /// <param name="itemSizeInBytes"></param> /// <param name="alignmentInBytes"></param> /// <param name="items"></param> /// <returns></returns> public unsafe static void *Allocate(AllocatorHandle handle, int itemSizeInBytes, int alignmentInBytes, int items = 1) { Block block = default; block.Range.Allocator = handle; block.Range.Items = items; block.Range.Pointer = IntPtr.Zero; block.BytesPerItem = itemSizeInBytes; block.Alignment = alignmentInBytes; var error = Try(ref block); CheckFailedToAllocate(error); return((void *)block.Range.Pointer); }
public unsafe static void *Allocate(AllocatorHandle handle, int itemSizeInBytes, int alignmentInBytes, int items = 1) { Block block = default; block.Range.Allocator = handle; block.Range.Items = items; block.Range.Pointer = IntPtr.Zero; block.BytesPerItem = itemSizeInBytes; block.Alignment = alignmentInBytes; var error = Try(ref block); if (error != 0) { throw new ArgumentException("failed to allocate"); } return((void *)block.Range.Pointer); }
/// <summary> /// /// </summary> /// <param name="handle"></param> /// <param name="pointer"></param> /// <param name="itemSizeInBytes"></param> /// <param name="alignmentInBytes"></param> /// <param name="items"></param> public unsafe static void Free(AllocatorHandle handle, void *pointer, int itemSizeInBytes, int alignmentInBytes, int items = 1) { if (pointer == null) { return; } Block block = default; block.Range.Allocator = handle; block.Range.Items = 0; block.Range.Pointer = (IntPtr)pointer; block.BytesPerItem = itemSizeInBytes; block.Alignment = alignmentInBytes; var error = Try(ref block); CheckFailedToFree(error); }
public unsafe static void Free(AllocatorHandle handle, void *pointer, int itemSizeInBytes, int alignmentInBytes, int items = 1) { if (pointer == null) { return; } Block block = default; block.Range.Allocator = handle; block.Range.Items = 0; block.Range.Pointer = (IntPtr)pointer; block.BytesPerItem = itemSizeInBytes; block.Alignment = alignmentInBytes; var error = Try(ref block); if (error != 0) { throw new ArgumentException("failed to free"); } }
internal void InvalidateDependents() { if (!NeedsUseAfterFreeTracking()) { return; } for (var i = 0; i < ChildSafetyHandles.Length; ++i) { unsafe { AtomicSafetyHandle *handle = ChildSafetyHandles.Ptr + i; if (CheckExists(*handle)) { AtomicSafetyHandle.Release(*handle); } } } ChildSafetyHandles.Clear(); if (Parent.IsValid) { Parent.TryRemoveChildAllocator(this, IndexInParent); } Parent = default; IndexInParent = InvalidChildAllocatorIndex; for (var i = 0; i < ChildAllocators.Length; ++i) { unsafe { AllocatorHandle *handle = (AllocatorHandle *)ChildAllocators.Ptr + i; if (handle->IsValid) { handle->Unregister(); } } } ChildAllocators.Clear(); }
/// <summary> /// /// </summary> /// <param name="handle"></param> /// <param name="pointer"></param> public unsafe static void Free(AllocatorHandle handle, void *pointer) { Free(handle, pointer, 1, 1, 1); }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="handle"></param> /// <param name="items"></param> /// <returns></returns> public unsafe static T *Allocate <T>(AllocatorHandle handle, int items = 1) where T : unmanaged { return((T *)Allocate(handle, UnsafeUtility.SizeOf <T>(), UnsafeUtility.AlignOf <T>(), items)); }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="handle"></param> /// <param name="pointer"></param> /// <param name="items"></param> public unsafe static void Free <T>(AllocatorHandle handle, T *pointer, int items = 1) where T : unmanaged { Free(handle, pointer, UnsafeUtility.SizeOf <T>(), UnsafeUtility.AlignOf <T>(), items); }