Improve memory safety and alignment handling
Some checks failed
Publish NuGet Packages / publish (pull_request) Failing after 1m5s

- Updated `.gitignore` to include `.vscode/` and clarified comments.
- Introduced `SafeHandle` for managing memory alignment and safe access.
- Refactored `UnsafeArray<T>` to add bounds checking and alignment logic.
- Added `IUnsafeHashCollection<T>` for specialized hash-based collections.
- Refactored `UnsafeHashMap<TKey, TValue>` and `UnsafeHashSet<T>` to use `HashMapHelper<TKey>` with alignment support.
- Made `UnsafeSlotMap<T>` methods `readonly` for immutability.
- Enhanced `HashMapHelper<TKey>` with alignment-aware buffer management and validation.
- Updated benchmarks to use `UnsafeArray<Vector256<int>>` and added capacity checks.
- Incremented assembly version to `1.1.3` in `Misaki.HighPerformance.LowLevel.csproj`.
- Updated `Program.cs` to run `CollectionBenchmark` and demonstrate safe disposal handling.
This commit is contained in:
2025-11-18 01:25:40 +09:00
parent 57725369f9
commit c0a0861897
13 changed files with 309 additions and 140 deletions

View File

@@ -0,0 +1,31 @@
namespace Misaki.HighPerformance.LowLevel.Buffer;
public unsafe struct SafeHandle
{
private const nuint _ALIGNMENT = 16u;
public int valid;
public static nuint GetAlignWithHeader(nuint baseAlign)
{
return Math.Max(_ALIGNMENT, baseAlign);
}
public static nuint GetPaddedHeaderSize(nuint baseAlign)
{
var headerBaseSize = (nuint)sizeof(SafeHandle);
var dataAlignment = Math.Max(_ALIGNMENT, baseAlign);
return (headerBaseSize + (dataAlignment - 1u)) & ~(dataAlignment - 1u);
}
public static SafeHandle* GetSafeHandle(void* ptr, nuint baseAlign)
{
if (ptr == null)
{
return null;
}
var alignedHeaderSize = GetPaddedHeaderSize(baseAlign);
return (SafeHandle*)((byte*)ptr - alignedHeaderSize);
}
}