Exemplo n.º 1
0
        public byte *Allocate(int bytesToAllocate, int alignment)
        {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            if (bytesToAllocate > ms_BlockSize)
            {
                throw new ArgumentException($"Cannot allocate more than {ms_BlockSize} in BlockAllocator. Requested: {bytesToAllocate}");
            }
#endif
            var nextByteOffsetAligned = (m_nextByteOffset + alignment - 1) & ~(alignment - 1);
            var nextBlockSize         = nextByteOffsetAligned + bytesToAllocate;
            if (m_blocks.Length == 0 || nextBlockSize > ms_BlockSize)
            {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
                if (m_blocks.Length == m_blocks.Capacity)
                {
                    throw new ArgumentException($"Cannot exceed budget of {ms_BudgetInBytes} in BlockAllocator.");
                }
#endif
                // Allocate a fresh block of memory
                m_blocks.Add((byte *)AllocatorManager.Allocate(m_handle, sizeof(byte), ms_BlockAlignment, ms_BlockSize));
                m_allocations.Add(0);
                nextByteOffsetAligned = 0;
            }
            var blockIndex = m_blocks.Length - 1;

            var pointer = (byte *)m_blocks[blockIndex] + nextByteOffsetAligned;
            m_nextByteOffset = nextByteOffsetAligned + bytesToAllocate;
            m_allocations.Ptr[blockIndex]++;
            return(pointer);
        }
Exemplo n.º 2
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;
        }
        /// <summary>
        /// Creates a new list with the specified initial capacity and 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>
        /// <returns>New initialized container.</returns>
        public static UnsafePtrList *Create(int initialCapacity, Allocator allocator, NativeArrayOptions options = NativeArrayOptions.UninitializedMemory)
        {
            UnsafePtrList *listData = AllocatorManager.Allocate <UnsafePtrList>(allocator);

            *listData = new UnsafePtrList(initialCapacity, allocator, options);
            return(listData);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="ptr"></param>
        /// <param name="length"></param>
        /// <returns>New initialized container.</returns>
        public static UnsafePtrList *Create(void **ptr, int length)
        {
            UnsafePtrList *listData = AllocatorManager.Allocate <UnsafePtrList>(AllocatorManager.Persistent);

            *listData = new UnsafePtrList(ptr, length);
            return(listData);
        }
        public byte *Allocate(int bytesToAllocate, int alignment)
        {
            CheckAllocationToLarge(bytesToAllocate);
            var nextByteOffsetAligned = (m_nextByteOffset + alignment - 1) & ~(alignment - 1);
            var nextBlockSize         = nextByteOffsetAligned + bytesToAllocate;

            if (m_blocks.Length == 0 || nextBlockSize > ms_BlockSize)
            {
                CheckExceededBudget();
                // Allocate a fresh block of memory
                m_blocks.Add(AllocatorManager.Allocate(m_handle, sizeof(byte), ms_BlockAlignment, ms_BlockSize));
                m_allocations.Add(0);
                nextByteOffsetAligned = 0;
            }
            var blockIndex = m_blocks.Length - 1;

            var pointer = (byte *)m_blocks[blockIndex] + nextByteOffsetAligned;

            m_nextByteOffset = nextByteOffsetAligned + bytesToAllocate;
            m_allocations.Ptr[blockIndex]++;
            return(pointer);
        }
Exemplo n.º 6
0
        public static UnsafeNodesList *Create(int length, Allocator allocator, NativeArrayOptions options = NativeArrayOptions.UninitializedMemory)
        {
            var handle = new AllocatorManager.AllocatorHandle {
                Value = (int)allocator
            };
            UnsafeNodesList *listData = AllocatorManager.Allocate <UnsafeNodesList>(handle);

            UnsafeUtility.MemClear(listData, UnsafeUtility.SizeOf <UnsafeNodesList>());

            listData->allocator    = allocator;
            listData->emptyIndices = UnsafeList.Create(UnsafeUtility.SizeOf <int>(), UnsafeUtility.AlignOf <int>(), length, allocator);

            if (length != 0)
            {
                listData->Resize(length);
            }

            if (options == NativeArrayOptions.ClearMemory && listData->ptr != null)
            {
                UnsafeUtility.MemClear(listData->ptr, listData->length * UnsafeUtility.SizeOf <int>());
            }

            return(listData);
        }