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