Improve memory safety and alignment handling
Some checks failed
Publish NuGet Packages / publish (pull_request) Failing after 1m5s
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:
31
Misaki.HighPerformance.LowLevel/Buffer/SafeHandle.cs
Normal file
31
Misaki.HighPerformance.LowLevel/Buffer/SafeHandle.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user