Exemplo n.º 1
0
 public void Dispose()
 {
     for (var i = m_blocks.Length - 1; i >= 0; --i)
     {
         AllocatorManager.Free(m_handle, (void *)m_blocks[i]);
     }
     m_allocations.Dispose();
     m_blocks.Dispose();
 }
Exemplo n.º 2
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.º 3
0
 public void Dispose()
 {
     if (ptr != null)
     {
         AllocatorManager.Free(allocator, ptr);
         allocator = Allocator.Invalid;
         ptr       = null;
         length    = 0;
         UnsafeList.Destroy(emptyIndices);
     }
 }
        /// <summary>
        /// Destroys list.
        /// </summary>
        /// <param name="listData">Container to destroy.</param>
        public static void Destroy(UnsafePtrList *listData)
        {
            UnsafeList.CheckNull(listData);
            var allocator = listData->ListData().Allocator.Value == AllocatorManager.Invalid.Value
                ? AllocatorManager.Persistent
                : listData->ListData().Allocator
            ;

            listData->Dispose();
            AllocatorManager.Free(allocator, listData);
        }
Exemplo n.º 5
0
        static public ClearToValueAllocator New <T>(byte ClearValue, ref T parent) where T : unmanaged, AllocatorManager.IAllocator
        {
            var temp = new ClearToValueAllocator();

            temp.m_parent     = parent.Handle;
            temp.m_clearValue = ClearValue;
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            AllocatorManager.Register(ref temp);
            parent.Handle.AddChildAllocator(temp.m_handle);
#endif
            return(temp);
        }
Exemplo n.º 6
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.º 7
0
                static void *CustomResize(void *oldPointer, long oldCount, long newCount, Allocator allocator, long size, int align)
                {
                    AllocatorManager.Block block = default;
                    block.Range.Allocator = new AllocatorManager.AllocatorHandle {
                        Index = (ushort)allocator
                    };
                    block.Range.Items    = (int)newCount;
                    block.Range.Pointer  = (IntPtr)oldPointer;
                    block.BytesPerItem   = (int)size;
                    block.Alignment      = align;
                    block.AllocatedItems = (int)oldCount;
                    var error = AllocatorManager.Try(ref block);

                    CheckFailedToAllocate(error);
                    return((void *)block.Range.Pointer);
                }
Exemplo n.º 8
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.º 9
0
 public async Task <Sequence> GetNext(String key)
 {
     if (String.IsNullOrEmpty(key))
     {
         throw new HttpRequestException("The key should not be NULL!");
     }
     else
     {
         using (var dbFactory = new DbContextFactory(this._env.EnvironmentName))
             using (var allocatorMng = new AllocatorManager(dbFactory))
             {
                 var seq = allocatorMng.GetNextVal(key, this.getValProvider);
                 base._logger.Trace($"{key} Get {seq.Value}");
                 return(seq);
             }
     }
 }
Exemplo n.º 10
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.º 11
0
 public async Task <HttpResponseMessage> Create([FromBody] HiLo hilo)
 {
     if (hilo == null)
     {
         return(new HttpResponseMessage(HttpStatusCode.BadRequest));
     }
     else
     {
         using (var dbFactory = new DbContextFactory(this._env.EnvironmentName))
             using (var allocatorMng = new AllocatorManager(dbFactory))
             {
                 bool isKeyExist = false;
                 allocatorMng.CreateHiLoInstance(hilo, out isKeyExist);
             }
         // this.CreateHiLo(hilo);
         return(new HttpResponseMessage(HttpStatusCode.Created));
     }
 }
Exemplo n.º 12
0
        public unsafe int Try(ref AllocatorManager.Block block)
        {
            var temp = block.Range.Allocator;

            block.Range.Allocator = m_parent;
            var error = AllocatorManager.Try(ref block);

            block.Range.Allocator = temp;
            if (error != 0)
            {
                return(error);
            }
            if (block.Range.Pointer != IntPtr.Zero)                                           // if we allocated or reallocated...
            {
                UnsafeUtility.MemSet((void *)block.Range.Pointer, m_clearValue, block.Bytes); // clear to a value.
            }
            return(0);
        }
Exemplo n.º 13
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();
    }
Exemplo n.º 15
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();
    }
    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.º 17
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.º 18
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.º 19
0
        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.º 20
0
        public void Free(void *pointer)
        {
            if (pointer == null)
            {
                return;
            }
            var blocks = m_allocations.Length; // how many blocks have we allocated?

            for (var i = blocks - 1; i >= 0; --i)
            {
                var block = (byte *)m_blocks[i];                        // get a pointer to the block.
                if (pointer >= block && pointer < block + ms_BlockSize) // is the pointer we want to free in this block?
                {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
                    if (m_allocations.Ptr[i] <= 0) // if that block has no allocations, we can't proceed
                    {
                        throw new ArgumentException($"Cannot free this pointer from BlockAllocator: no more allocations to free in its block.");
                    }
#endif
                    if (--m_allocations.Ptr[i] == 0) // if this was the last allocation in the block,
                    {
                        if (i == blocks - 1)         // if it's the last block,
                        {
                            m_nextByteOffset = 0;    // just forget that we allocated anything from it, but keep it for later allocations
                        }
                        else
                        {
                            AllocatorManager.Free(m_handle, (void *)m_blocks[i]); // delete the block
                            m_blocks.RemoveAtSwapBack(i);                         // and forget we ever saw it
                            m_allocations.RemoveAtSwapBack(i);                    // and your dog toto, too
                        }
                    }
                    return;
                }
            }
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            throw new ArgumentException($"Cannot free this pointer from BlockAllocator: can't be found in any block.");
#endif
        }
Exemplo n.º 21
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();
    }
Exemplo n.º 22
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.º 23
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();
    }
    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.º 25
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);
        }