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:
@@ -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>
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Authors>Misaki</Authors>
|
||||
<AssemblyVersion>1.6.6</AssemblyVersion>
|
||||
<AssemblyVersion>1.6.7</AssemblyVersion>
|
||||
<Version>$(AssemblyVersion)</Version>
|
||||
<PackageProjectUrl>https://git.personalnas.com/Misaki/Misaki.HighPerformance.git</PackageProjectUrl>
|
||||
<RepositoryUrl>https://git.personalnas.com/Misaki/Misaki.HighPerformance.git</RepositoryUrl>
|
||||
|
||||
@@ -10,14 +10,33 @@ var opts = new AllocationManagerInitOpts
|
||||
FreeListConcurrencyLevel = 1
|
||||
};
|
||||
|
||||
AllocationManager.Initialize(opts);
|
||||
var pool = new MemoryPool<VirtualStack, VirtualStack.CreationOpts>(new VirtualStack.CreationOpts
|
||||
{
|
||||
reserveCapacity = 1024 * 1024
|
||||
});
|
||||
|
||||
var arr = new UnsafeArray<int>(10, Allocator.Persistent);
|
||||
var arrcpy = arr;
|
||||
var arr = new UnsafeArray<int>(1000, pool.AllocationHandle);
|
||||
|
||||
Test(in pool);
|
||||
arr.Dispose();
|
||||
arrcpy.Dispose();
|
||||
|
||||
Console.WriteLine(arr.IsCreated);
|
||||
Console.WriteLine(arrcpy.IsCreated);
|
||||
Console.WriteLine("Done.");
|
||||
|
||||
AllocationManager.Dispose();
|
||||
void Test(ref readonly MemoryPool<VirtualStack, VirtualStack.CreationOpts> pool)
|
||||
{
|
||||
using var arr = new UnsafeArray<int>(1000, pool.AllocationHandle);
|
||||
if (true)
|
||||
{
|
||||
using var arr1 = new UnsafeArray<int>(1000, pool.AllocationHandle);
|
||||
}
|
||||
|
||||
using var arr3 = Test2(in pool);
|
||||
using var arr2 = new UnsafeArray<int>(1000, pool.AllocationHandle);
|
||||
}
|
||||
|
||||
UnsafeArray<int> Test2(ref readonly MemoryPool<VirtualStack, VirtualStack.CreationOpts> pool)
|
||||
{
|
||||
using var arr = new UnsafeArray<int>(1000, pool.AllocationHandle);
|
||||
var arr1 = new UnsafeArray<int>(1000, pool.AllocationHandle);
|
||||
return arr1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user