diff --git a/src/DotNext.Tests/Buffers/UnmanagedMemoryPoolTests.cs b/src/DotNext.Tests/Buffers/UnmanagedMemoryPoolTests.cs index 608348f6c..337ec1a3a 100644 --- a/src/DotNext.Tests/Buffers/UnmanagedMemoryPoolTests.cs +++ b/src/DotNext.Tests/Buffers/UnmanagedMemoryPoolTests.cs @@ -113,7 +113,7 @@ public static void SliceTest() [Fact] public static void Allocation() { - using var manager = UnmanagedMemory.Allocate(2); + using var manager = UnmanagedMemory.AllocateZeroed(2); Equal(2, manager.Length); Equal(sizeof(long) * 2U, manager.Size); @@ -130,7 +130,7 @@ public static void Allocation() [Fact] public static void Pooling() { - using var pool = new UnmanagedMemoryPool(10) { TrackAllocations = true }; + using var pool = new UnmanagedMemoryPool(10) { TrackAllocations = true, AllocateZeroedMemory = true }; using var manager = pool.Rent(2); Equal(2, manager.Memory.Length); diff --git a/src/DotNext.Unsafe/Buffers/UnmanagedMemoryPool.cs b/src/DotNext.Unsafe/Buffers/UnmanagedMemoryPool.cs index acb8a2e5b..f3f2d1085 100644 --- a/src/DotNext.Unsafe/Buffers/UnmanagedMemoryPool.cs +++ b/src/DotNext.Unsafe/Buffers/UnmanagedMemoryPool.cs @@ -91,12 +91,16 @@ private void AddTracking(IUnmanagedMemory owner) public override IMemoryOwner Rent(int length = -1) { ArgumentOutOfRangeException.ThrowIfGreaterThan(length, MaxBufferSize); + ArgumentOutOfRangeException.ThrowIfZero(length); + + if (length < 0) + length = defaultBufferSize; PoolingUnmanagedMemoryOwner result; unsafe { - result = new PoolingUnmanagedMemoryOwner(Math.Max(length, defaultBufferSize), allocator) + result = new PoolingUnmanagedMemoryOwner(length, allocator) { OnDisposed = removeMemory, };