feat(buffer): improve Free safety and refactor tests

Stack and VirtualStack Free methods now check pointer bounds and update offsets only when appropriate, improving safety. Program.cs refactored to use MemoryPool with VirtualStack and new test methods. Assembly version bumped to 1.6.7.
This commit is contained in:
2026-04-02 20:49:17 +09:00
parent 2b438660a1
commit 8d5ed30c5d
4 changed files with 47 additions and 10 deletions

View File

@@ -1,3 +1,4 @@
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace Misaki.HighPerformance.LowLevel.Buffer;
@@ -141,8 +142,16 @@ public unsafe partial struct Stack : IMemoryAllocator<Stack, Stack.CreationOpts>
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void Free(void* ptr)
public void Free(void* ptr)
{
if (ptr < _buffer && ptr >= _buffer + _size)
{
Debug.Fail("Attempting to free a pointer that is out of bounds of the current stack allocation.");
return; // Pointer is out of bounds, ignore
}
var offset = (nuint)((byte*)ptr - _buffer);
_offset = offset < _offset ? offset : _offset;
}
/// <summary>

View File

@@ -1,4 +1,5 @@
using Misaki.HighPerformance.LowLevel.Utilities;
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace Misaki.HighPerformance.LowLevel.Buffer;
@@ -161,8 +162,16 @@ public unsafe struct VirtualStack : IMemoryAllocator<VirtualStack, VirtualStack.
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void Free(void* ptr)
public void Free(void* ptr)
{
if (ptr < _baseAddress && ptr >= _baseAddress + _committedSize)
{
Debug.Fail("Attempting to free a pointer that is out of bounds of the current stack allocation.");
return; // Pointer is out of bounds, ignore
}
var offset = (nuint)((byte*)ptr - _baseAddress);
_allocatedOffset = offset < _allocatedOffset ? offset : _allocatedOffset;
}
public void Dispose()