public void CustomAllocatorUnsafeListWorks()
    {
        var customhandle = new AllocatorManager.AllocatorHandle {
            Value = AllocatorManager.FirstUserIndex
        };

        AllocatorManager.Initialize();
        using (var installation = new AllocatorManager.AllocatorInstallation <ClearToValueAllocator>(customhandle))
        {
            installation.Allocator.budgetInBytes = 100;
            for (byte ClearValue = 0; ClearValue < 0xF; ++ClearValue)
            {
                installation.Allocator.ClearValue = ClearValue;
                using (var unsafelist = new UnsafeList <byte>(1, customhandle))
                {
                    const int kLength = 100;
                    unsafelist.Resize(kLength);
                    for (int i = 0; i < kLength; ++i)
                    {
                        Assert.AreEqual(ClearValue, unsafelist[i]);
                    }
                }
            }
        }
        AllocatorManager.Shutdown();
    }
    public void UserDefinedAllocatorWorks()
    {
        var customhandle = new AllocatorManager.AllocatorHandle {
            Value = AllocatorManager.FirstUserIndex
        };

        AllocatorManager.Initialize();
        using (var installation = new AllocatorManager.AllocatorInstallation <ClearToValueAllocator>(customhandle))
        {
            installation.Allocator.budgetInBytes = 100;
            for (byte ClearValue = 0; ClearValue < 0xF; ++ClearValue)
            {
                installation.Allocator.ClearValue = ClearValue;
                const int kLength = 100;
                for (int i = 1; i < kLength; ++i)
                {
                    using (var block = customhandle.Allocate <int>(Items: i))
                    {
                        Assert.AreNotEqual(IntPtr.Zero, block.Range.Pointer);
                        Assert.AreEqual(i, block.Range.Items);
                        Assert.AreEqual(UnsafeUtility.SizeOf <int>(), block.BytesPerItem);
                        Assert.AreEqual(UnsafeUtility.AlignOf <int>(), block.Alignment);

                        unsafe
                        {
                            Assert.AreEqual(ClearValue, *(byte *)block.Range.Pointer);
                        }
                    }
                }
            }
        }
        AllocatorManager.Shutdown();
    }
Example #3
0
            /// <summary>
            /// Constructs an allocator.
            /// </summary>
            /// <param name="budgetInBytes">Budget of the allocator in bytes.</param>
            /// <param name="bufferSizeInBytes">Size of each buffer to be allocated in bytes.</param>
            /// <param name="handle">An AllocatorHandle to use for internal bookkeeping structures.</param>
            /// <exception cref="InvalidOperationException">Thrown if the allocator cannot reserve the address range required for the given budget.</exception>
            public BufferAllocator(int budgetInBytes, int bufferSizeInBytes, AllocatorManager.AllocatorHandle handle)
            {
                BufferSizeInBytes = bufferSizeInBytes;

                // Reserve the entire budget's worth of address space. The reserved space may be larger than the budget
                // due to page sizes.
                var pageCount = VirtualMemoryUtility.BytesToPageCount((uint)budgetInBytes, VirtualMemoryUtility.DefaultPageSizeInBytes);
                BaselibErrorState errorState;

                ReservedRange = VirtualMemoryUtility.ReserveAddressSpace(pageCount, VirtualMemoryUtility.DefaultPageSizeInBytes, out errorState);

#if ENABLE_UNITY_COLLECTIONS_CHECKS
                if (!errorState.Success)
                {
                    throw new InvalidOperationException($"Failed to reserve address range for {budgetInBytes} bytes");
                }
#endif

                // Init a free list of blocks.
                MaxBufferCount = (int)VirtualMemoryUtility.BytesToPageCount((uint)budgetInBytes, (uint)bufferSizeInBytes);
                FreeList       = new UnsafeIntList(MaxBufferCount, handle);

                for (int i = MaxBufferCount - 1; i >= 0; --i)
                {
                    FreeList.Add(i);
                }
            }
 /// <summary>
 /// Constructs list as view into memory.
 /// </summary>
 /// <param name="ptr"></param>
 /// <param name="length"></param>
 public unsafe UnsafePtrList(void **ptr, int length)
 {
     Ptr           = ptr;
     this.length   = length;
     this.capacity = length;
     Allocator     = AllocatorManager.None;
 }
        public BlockAllocator(AllocatorManager.AllocatorHandle handle, int budgetInBytes)
        {
            m_handle         = handle;
            m_nextByteOffset = 0;
            var blocks = (budgetInBytes + ms_BlockSize - 1) >> ms_Log2BlockSize;

            m_blocks      = new UnsafePtrList(blocks, handle);
            m_allocations = new UnsafeIntList(blocks, handle);
        }
        /// <summary>
        /// Constructs a new list using the specified 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>
        /// <remarks>The list initially has a capacity of one. To avoid reallocating memory for the list, specify
        /// sufficient capacity up front.</remarks>
        public unsafe UnsafePtrList(int initialCapacity, Allocator allocator, NativeArrayOptions options = NativeArrayOptions.UninitializedMemory)
        {
            Ptr       = null;
            length    = 0;
            capacity  = 0;
            Allocator = AllocatorManager.None;

            var sizeOf = IntPtr.Size;

            this.ListData() = new UnsafeList(sizeOf, sizeOf, initialCapacity, allocator, options);
        }
Example #7
0
        public BlockAllocator(AllocatorManager.AllocatorHandle handle, int budgetInBytes)
        {
            m_bufferAllocator = new BufferAllocator(budgetInBytes, ms_BlockSize, handle);
            m_nextPtr         = 0;
            var blocks = (budgetInBytes + ms_BlockSize - 1) >> ms_Log2BlockSize;

            m_allocations = new UnsafeIntList(blocks, handle);

            for (int i = 0; i < blocks; ++i)
            {
                m_allocations.Add(0);
            }

            m_currentBlockIndex = -1;
        }
    public void SlabAllocatorWorks()
    {
        var SlabSizeInBytes = 256;
        var SlabSizeInInts  = SlabSizeInBytes / sizeof(int);
        var Slabs           = 256;
        var customhandle    = new AllocatorManager.AllocatorHandle {
            Value = AllocatorManager.FirstUserIndex
        };

        AllocatorManager.Initialize();
        using (var storage = AllocatorManager.Persistent.Allocate <byte>(Items: Slabs *SlabSizeInBytes))                         // allocate a block of bytes from Malloc.Persistent
            using (var installation = new AllocatorManager.AllocatorInstallation <AllocatorManager.SlabAllocator>(customhandle)) // and make a slab allocator from it
            {
                installation.Allocator = new AllocatorManager.SlabAllocator(storage, SlabSizeInBytes, Slabs * SlabSizeInBytes);

                var block0 = customhandle.Allocate <int>(Items: SlabSizeInInts);
                Assert.AreNotEqual(IntPtr.Zero, block0.Range.Pointer);
                Assert.AreEqual(SlabSizeInInts, block0.Range.Items);
                Assert.AreEqual(UnsafeUtility.SizeOf <int>(), block0.BytesPerItem);
                Assert.AreEqual(UnsafeUtility.AlignOf <int>(), block0.Alignment);
                Assert.AreEqual(1, installation.Allocator.Occupied[0]);

                var block1 = customhandle.Allocate <int>(Items: SlabSizeInInts - 1);
                Assert.AreNotEqual(IntPtr.Zero, block1.Range.Pointer);
                Assert.AreEqual(SlabSizeInInts - 1, block1.Range.Items);
                Assert.AreEqual(UnsafeUtility.SizeOf <int>(), block1.BytesPerItem);
                Assert.AreEqual(UnsafeUtility.AlignOf <int>(), block1.Alignment);
                Assert.AreEqual(3, installation.Allocator.Occupied[0]);

                block0.Dispose();
                Assert.AreEqual(2, installation.Allocator.Occupied[0]);
                block1.Dispose();
                Assert.AreEqual(0, installation.Allocator.Occupied[0]);

                Assert.Throws <ArgumentException>(() =>
                {
                    customhandle.Allocate <int>(Items: 65);
                });
            }
        AllocatorManager.Shutdown();
    }
    public void StackAllocatorWorks()
    {
        var customhandle = new AllocatorManager.AllocatorHandle {
            Value = AllocatorManager.FirstUserIndex
        };

        AllocatorManager.Initialize();
        using (var storage = AllocatorManager.Persistent.Allocate <byte>(100000))                                                 // allocate a block of bytes from Malloc.Persistent
            using (var installation = new AllocatorManager.AllocatorInstallation <AllocatorManager.StackAllocator>(customhandle)) // and make a stack allocator from it
            {
                installation.Allocator.m_storage = storage;                                                                       // install the block into the allocator
                const int kLength = 100;
                for (int i = 1; i < kLength; ++i)
                {
                    var block = customhandle.Allocate <int>(Items: i);
                    Assert.AreNotEqual(IntPtr.Zero, block.Range.Pointer);
                    Assert.AreEqual(i, block.Range.Items);
                    Assert.AreEqual(UnsafeUtility.SizeOf <int>(), block.BytesPerItem);
                    Assert.AreEqual(UnsafeUtility.AlignOf <int>(), block.Alignment);
                }
            }
        AllocatorManager.Shutdown();
    }
Example #10
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);
        }
Example #11
0
 public unsafe UnsafeEntityQueryDataPtrList(int initialCapacity, Allocator allocator, NativeArrayOptions options = NativeArrayOptions.UninitializedMemory)
 {
     Ptr = null; Length = 0; Capacity = 0; Allocator = AllocatorManager.None; this.ListData() = new UnsafePtrList(initialCapacity, allocator, options);
 }
Example #12
0
 public unsafe UnsafeEntityQueryDataPtrList(EntityQueryData **ptr, int length)
 {
     Ptr = null; Length = 0; Capacity = 0; Allocator = AllocatorManager.Invalid; this.ListData() = new UnsafePtrList((void **)ptr, length);
 }
Example #13
0
 public unsafe UnsafeArchetypePtrList(Archetype **ptr, int length)
 {
     Ptr = null; Length = 0; Capacity = 0; Allocator = AllocatorManager.Invalid; this.ListData() = new UnsafePtrList((void **)ptr, length);
 }
Example #14
0
 public unsafe UnsafeIntList(int initialCapacity, Allocator allocator, NativeArrayOptions options = NativeArrayOptions.UninitializedMemory)
 {
     Ptr = null; Length = 0; Capacity = 0; Allocator = AllocatorManager.None; this.ListData() = new UnsafeList(UnsafeUtility.SizeOf <int>(), UnsafeUtility.AlignOf <int>(), initialCapacity, allocator, options);
 }