Ejemplo n.º 1
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();
    }
Ejemplo n.º 2
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();
    }
Ejemplo n.º 3
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();
    }
Ejemplo n.º 4
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();
    }