示例#1
0
        private void Resize(int newLength)
        {
            void *newPointer = null;

            if (newLength > 0)
            {
                newPointer = AllocatorManager.Allocate(allocator, UnsafeUtility.SizeOf <Node>(), UnsafeUtility.AlignOf <Node>(), newLength);

                if (length > 0)
                {
                    var itemsToCopy = math.min(length, newLength);
                    var bytesToCopy = itemsToCopy * UnsafeUtility.SizeOf <Node>();
                    UnsafeUtility.MemCpy(newPointer, ptr, bytesToCopy);
                }
            }

            AllocatorManager.Free(allocator, ptr);

            for (int i = newLength - 1; i >= length; i--)
            {
                emptyIndices->Add(i);
            }

            ptr    = newPointer;
            length = newLength;
        }
示例#2
0
        public static void Destroy(UnsafeNodesList *listData)
        {
            var allocator = listData->allocator;

            listData->Dispose();
            AllocatorManager.Free(allocator, listData);
        }
 public void Dispose()
 {
     for (var i = m_blocks.Length - 1; i >= 0; --i)
     {
         AllocatorManager.Free(m_handle, (void *)m_blocks[i]);
     }
     m_allocations.Dispose();
     m_blocks.Dispose();
 }
示例#4
0
 public void Dispose()
 {
     if (ptr != null)
     {
         AllocatorManager.Free(allocator, ptr);
         allocator = Allocator.Invalid;
         ptr       = null;
         length    = 0;
         UnsafeList.Destroy(emptyIndices);
     }
 }
        /// <summary>
        /// Destroys list.
        /// </summary>
        /// <param name="listData">Container to destroy.</param>
        public static void Destroy(UnsafePtrList *listData)
        {
            UnsafeList.CheckNull(listData);
            var allocator = listData->ListData().Allocator.Value == AllocatorManager.Invalid.Value
                ? AllocatorManager.Persistent
                : listData->ListData().Allocator
            ;

            listData->Dispose();
            AllocatorManager.Free(allocator, listData);
        }
        public void Free(void *pointer)
        {
            if (pointer == null)
            {
                return;
            }
            var blocks = m_allocations.Length; // how many blocks have we allocated?

            for (var i = blocks - 1; i >= 0; --i)
            {
                var block = (byte *)m_blocks[i];                        // get a pointer to the block.
                if (pointer >= block && pointer < block + ms_BlockSize) // is the pointer we want to free in this block?
                {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
                    if (m_allocations.Ptr[i] <= 0) // if that block has no allocations, we can't proceed
                    {
                        throw new ArgumentException($"Cannot free this pointer from BlockAllocator: no more allocations to free in its block.");
                    }
#endif
                    if (--m_allocations.Ptr[i] == 0) // if this was the last allocation in the block,
                    {
                        if (i == blocks - 1)         // if it's the last block,
                        {
                            m_nextByteOffset = 0;    // just forget that we allocated anything from it, but keep it for later allocations
                        }
                        else
                        {
                            AllocatorManager.Free(m_handle, (void *)m_blocks[i]); // delete the block
                            m_blocks.RemoveAtSwapBack(i);                         // and forget we ever saw it
                            m_allocations.RemoveAtSwapBack(i);                    // and your dog toto, too
                        }
                    }
                    return;
                }
            }
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            throw new ArgumentException($"Cannot free this pointer from BlockAllocator: can't be found in any block.");
#endif
        }