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();
    }
Exemplo n.º 3
0
    public void CustomAllocatorNativeListWorksWithoutHandles()
    {
        AllocatorManager.Initialize();
        var allocator = AllocatorManager.Persistent;
        var list      = NativeList <byte> .New(100, ref allocator);

        list.Dispose(ref allocator);
        AllocatorManager.Shutdown();
    }
Exemplo n.º 4
0
    public void CustomAllocatorNativeListThrowsWhenAllocatorIsWrong()
    {
        AllocatorManager.Initialize();
        var allocator0 = AllocatorManager.Persistent;
        var allocator1 = AllocatorManager.TempJob;
        var list       = NativeList <byte> .New(100, ref allocator0);

        Assert.Throws <ArgumentOutOfRangeException>(() =>
        {
            list.Dispose(ref allocator1);
        });
        list.Dispose(ref allocator0);
        AllocatorManager.Shutdown();
    }
Exemplo n.º 5
0
    public void ReleasingChildHandlesWorks()
    {
        AllocatorManager.Initialize();
        var origin    = AllocatorManager.Persistent;
        var storage   = origin.AllocateBlock(default(byte), 100000); // allocate a block of bytes from Malloc.Persistent
        var allocator = new AllocatorManager.StackAllocator(storage);
        var list      = NativeList <int> .New(10, ref allocator);

        list.Add(0);         // put something in the list, so it'll have a size for later
        allocator.Dispose(); // ok to tear down the storage that the stack allocator used, too.
        Assert.Throws <ObjectDisposedException>(
            () => {
            list[0] = 0; // we haven't disposed this list, but it was released automatically already. so this is an error.
        });
        storage.Dispose();
        AllocatorManager.Shutdown();
    }
Exemplo n.º 6
0
    public void AllocatesAndFreesFromMono()
    {
        AllocatorManager.Initialize();
        const int kLength = 100;

        for (int i = 0; i < kLength; ++i)
        {
            using (var block = AllocatorManager.Persistent.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);
                Assert.AreEqual(AllocatorManager.Persistent.Value, block.Range.Allocator.Value);
            }
        }
        AllocatorManager.Shutdown();
    }
Exemplo n.º 7
0
    public void AllocatorVersioningWorks()
    {
        AllocatorManager.Initialize();
        var origin  = AllocatorManager.Persistent;
        var storage = origin.AllocateBlock(default(byte), 100000); // allocate a block of bytes from Malloc.Persistent

        for (var i = 1; i <= 3; ++i)
        {
            var allocator  = new AllocatorManager.StackAllocator(storage);
            var oldIndex   = allocator.Handle.Index;
            var oldVersion = allocator.Handle.Version;
            allocator.Dispose();
            var newVersion = AllocatorManager.SharedStatics.Version.Ref.Data.ElementAt(oldIndex);
            Assert.AreEqual(oldVersion + 1, newVersion);
        }
        storage.Dispose();
        AllocatorManager.Shutdown();
    }
    public void AllocatesAndFreesFromBurst()
    {
        AllocatorManager.Initialize();

        const int kLength = 100;

        using (var blocks = new NativeArray <AllocatorManager.Block>(kLength, Allocator.Persistent))
        {
            var allocateJob = new AllocateJob();
            allocateJob.m_blocks = blocks;
            allocateJob.Schedule(kLength, 1).Complete();

            for (int i = 0; i < kLength; ++i)
            {
                var block = allocateJob.m_blocks[i];
                if (i != 0)
                {
                    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);
                Assert.AreEqual(AllocatorManager.Persistent.Value, block.Range.Allocator.Value);
            }

            var freeJob = new FreeJob();
            freeJob.m_blocks = blocks;
            freeJob.Schedule(kLength, 1).Complete();

            for (int i = 0; i < kLength; ++i)
            {
                var block = allocateJob.m_blocks[i];
                Assert.AreEqual(IntPtr.Zero, block.Range.Pointer);
                Assert.AreEqual(0, block.Range.Items);
                Assert.AreEqual(UnsafeUtility.SizeOf <int>(), block.BytesPerItem);
                Assert.AreEqual(UnsafeUtility.AlignOf <int>(), block.Alignment);
                Assert.AreEqual(AllocatorManager.Persistent.Value, block.Range.Allocator.Value);
            }
        }

        AllocatorManager.Shutdown();
    }
    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();
    }
Exemplo n.º 10
0
    public void SlabAllocatorWorks()
    {
        var SlabSizeInBytes = 256;
        var SlabSizeInInts  = SlabSizeInBytes / sizeof(int);
        var Slabs           = 256;

        AllocatorManager.Initialize();
        var origin         = AllocatorManager.Persistent;
        var backingStorage = origin.AllocateBlock(default(byte), Slabs * SlabSizeInBytes); // allocate a block of bytes from Malloc.Persistent
        var allocator      = new AllocatorManager.SlabAllocator(backingStorage, SlabSizeInBytes, Slabs * SlabSizeInBytes);

        var block0 = allocator.AllocateBlock(default(int), 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, allocator.Occupied[0]);

        var block1 = allocator.AllocateBlock(default(int), 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, allocator.Occupied[0]);

        allocator.FreeBlock(ref block0);
        Assert.AreEqual(2, allocator.Occupied[0]);
        allocator.FreeBlock(ref block1);
        Assert.AreEqual(0, allocator.Occupied[0]);

        Assert.Throws <ArgumentException>(() =>
        {
            allocator.AllocateBlock(default(int), 65);
        });

        allocator.Dispose();
        backingStorage.Dispose();
        AllocatorManager.Shutdown();
    }
Exemplo n.º 11
0
    public void AllocatesAndFreesFromMono()
    {
        AllocatorManager.Initialize();
        const int kLength = 100;

        for (int i = 0; i < kLength; ++i)
        {
            var allocator = AllocatorManager.Persistent;
            var block     = allocator.AllocateBlock(default(int), i);
            if (i != 0)
            {
                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);
            Assert.AreEqual(AllocatorManager.Persistent.Value, block.Range.Allocator.Value);
            allocator.FreeBlock(ref block);
        }
        AllocatorManager.Shutdown();
    }
Exemplo n.º 12
0
    public void StackAllocatorWorks()
    {
        AllocatorManager.Initialize();
        var       origin         = AllocatorManager.Persistent;
        var       backingStorage = origin.AllocateBlock(default(byte), 100000); // allocate a block of bytes from Malloc.Persistent
        var       allocator      = new AllocatorManager.StackAllocator(backingStorage);
        const int kLength        = 100;

        for (int i = 1; i < kLength; ++i)
        {
            var block = allocator.AllocateBlock(default(int), 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);
            allocator.FreeBlock(ref block);
        }
        allocator.Dispose();
        backingStorage.Dispose();
        AllocatorManager.Shutdown();
    }
Exemplo n.º 13
0
    public unsafe void ReleasingChildAllocatorsWorks()
    {
        AllocatorManager.Initialize();

        var origin        = AllocatorManager.Persistent;
        var parentStorage = origin.AllocateBlock(default(byte), 100000);        // allocate a block of bytes from Malloc.Persistent
        var parent        = new AllocatorManager.StackAllocator(parentStorage); // and make a stack allocator from it

        var childStorage = parent.AllocateBlock(default(byte), 10000);          // allocate some space from the parent
        var child        = new AllocatorManager.StackAllocator(childStorage);   // and make a stack allocator from it

        parent.Dispose();                                                       // tear down the parent allocator

        Assert.Throws <ArgumentException>(() =>
        {
            child.Allocate(default(byte), 1000); // try to allocate from the child - it should fail.
        });

        parentStorage.Dispose();

        AllocatorManager.Shutdown();
    }
Exemplo n.º 14
0
    public void CustomAllocatorUnsafeListWorks()
    {
        AllocatorManager.Initialize();
        var parent    = AllocatorManager.Persistent;
        var allocator = ClearToValueAllocator.New(0xFE, ref parent);

        allocator.Register();
        for (byte ClearValue = 0; ClearValue < 0xF; ++ClearValue)
        {
            allocator.m_clearValue = ClearValue;
            var       unsafelist = new UnsafeList <byte>(1, allocator.Handle);
            const int kLength    = 100;
            unsafelist.Resize(kLength);
            for (int i = 0; i < kLength; ++i)
            {
                Assert.AreEqual(ClearValue, unsafelist[i]);
            }
            unsafelist.Dispose();
        }
        allocator.Dispose();
        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();
    }
Exemplo n.º 16
0
    public void UserDefinedAllocatorWorks()
    {
        AllocatorManager.Initialize();
        var parent    = AllocatorManager.Persistent;
        var allocator = ClearToValueAllocator.New(0, ref parent);

        for (byte ClearValue = 0; ClearValue < 0xF; ++ClearValue)
        {
            allocator.m_clearValue = ClearValue;
            const int kLength = 100;
            for (int i = 1; i < kLength; ++i)
            {
                var block = allocator.AllocateBlock(default(int), 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);
                allocator.FreeBlock(ref block);
            }
        }
        allocator.Dispose();
        AllocatorManager.Shutdown();
    }