Пример #1
0
 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);
 }
Пример #2
0
 internal static bool AreTheSame(AllocatorHandle a, AllocatorHandle b)
 {
     if (a.Index != b.Index)
     {
         return(false);
     }
     if (a.Version != b.Version)
     {
         return(false);
     }
     return(true);
 }
Пример #3
0
            internal int AddChildAllocator(AllocatorHandle handle)
            {
                if (!NeedsUseAfterFreeTracking())
                {
                    return(InvalidChildAllocatorIndex);
                }
                var result = ChildAllocators.Length;

                ChildAllocators.Add(handle);
                handle.Parent        = this;
                handle.IndexInParent = result;
                return(result);
            }
Пример #4
0
        /// <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);
        }
Пример #5
0
        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);
        }
Пример #6
0
        /// <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);
        }
Пример #7
0
        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");
            }
        }
Пример #8
0
 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();
 }
Пример #9
0
 /// <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);
 }
Пример #10
0
 /// <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));
 }
Пример #11
0
 /// <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);
 }