diff --git a/src/XenoAtom.Allocators.Tests/BasicTests.cs b/src/XenoAtom.Allocators.Tests/BasicTests.cs index 3a82863..08be0c4 100644 --- a/src/XenoAtom.Allocators.Tests/BasicTests.cs +++ b/src/XenoAtom.Allocators.Tests/BasicTests.cs @@ -97,6 +97,8 @@ public async Task TestAllocate3() // Free allocation 1 tlsf.Free(allocation1); + Assert.ThrowsException(() => tlsf.Free(allocation1)); + Assert.AreEqual(2U, tlsf.Chunks[0].UsedBlockCount); Assert.AreEqual(2U, tlsf.Chunks[0].FreeBlockCount); diff --git a/src/XenoAtom.Allocators/TlsfAllocator.cs b/src/XenoAtom.Allocators/TlsfAllocator.cs index 5f6d0e8..e46142c 100644 --- a/src/XenoAtom.Allocators/TlsfAllocator.cs +++ b/src/XenoAtom.Allocators/TlsfAllocator.cs @@ -179,8 +179,15 @@ public TlsfAllocation Allocate(uint size) public void Free(TlsfAllocation allocation) { int blockIndex = (int)allocation.BlockIndex; + if ((uint)blockIndex >= (uint)_blockCount) + { + throw new ArgumentException($"The block index {blockIndex} is out of range", nameof(allocation)); + } ref var block = ref GetBlockAt(blockIndex); - Debug.Assert(block.IsUsed); + if (!block.IsUsed) + { + throw new ArgumentException($"The block at index {blockIndex} is already free", nameof(allocation)); + } block.IsUsed = false; // Update statistics for the chunk