Exemple #1
0
    public void AllocatesFromJob()
    {
        const int kLengthInWords = 10;
        const int kLengthInBits  = kLengthInWords * 64;
        var       storage        = new NativeList <long>(kLengthInWords, Allocator.Persistent);

        storage.Length = kLengthInWords;

        for (int i = 0; i < kLengthInWords; ++i)
        {
            Assert.AreEqual(0L, storage[i]);
        }

        var allocateJob = new AllocateJob();

        allocateJob.m_storage = storage;
        allocateJob.Schedule(kLengthInBits, 1).Complete();

        for (int i = 0; i < kLengthInWords; ++i)
        {
            Assert.AreEqual(~0L, storage[i]);
        }

        storage.Dispose();
    }
    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();
    }