feat(memory): refactor allocation and add new queue
Refactored memory management by removing safety checks and introducing `MemoryHandle` for centralized tracking. Simplified allocation logic across allocators and enhanced `Dispose` methods for better resource cleanup. Added `UnsafeChunkedQueue<T>`, a lock-free, dynamically resizing queue with chunk-based memory management, supporting parallel producers and consumers. Updated unit tests to validate new queue functionality and ensure compatibility with refactored memory logic. Incremented assembly version to 1.6.12. BREAKING CHANGE: Removed `#if MHP_ENABLE_SAFETY_CHECKS` blocks, altering memory validation behavior.
This commit is contained in:
@@ -2,7 +2,7 @@ using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace Misaki.HighPerformance.LowLevel.Buffer;
|
||||
|
||||
public readonly struct MemoryHandle : IEquatable<MemoryHandle>
|
||||
public readonly struct MemoryHandle : IDisposable, IEquatable<MemoryHandle>
|
||||
{
|
||||
public readonly int ID
|
||||
{
|
||||
@@ -16,12 +16,25 @@ public readonly struct MemoryHandle : IEquatable<MemoryHandle>
|
||||
|
||||
public static readonly MemoryHandle Invalid = default;
|
||||
|
||||
public bool IsValid => AllocationManager.ContainsAllocation(this);
|
||||
public bool IsInvalid => !IsValid;
|
||||
|
||||
public MemoryHandle(int id, int generation)
|
||||
{
|
||||
ID = id + 1;
|
||||
Generation = generation + 1;
|
||||
}
|
||||
|
||||
public unsafe static MemoryHandle Create(void* address, nuint size)
|
||||
{
|
||||
return AllocationManager.AddAllocation(address, size);
|
||||
}
|
||||
|
||||
public unsafe void Update(void* newAddress, nuint newSize)
|
||||
{
|
||||
AllocationManager.UpdateAllocation(this, newAddress, newSize);
|
||||
}
|
||||
|
||||
public bool Equals(MemoryHandle other)
|
||||
{
|
||||
return ID == other.ID && Generation == other.Generation;
|
||||
@@ -42,6 +55,11 @@ public readonly struct MemoryHandle : IEquatable<MemoryHandle>
|
||||
return $"MemoryHandle(Id: {ID}, Generation: {Generation})";
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
AllocationManager.RemoveAllocation(this);
|
||||
}
|
||||
|
||||
public static bool operator ==(MemoryHandle left, MemoryHandle right)
|
||||
{
|
||||
return left.Equals(right);
|
||||
@@ -61,7 +79,7 @@ public readonly unsafe struct AllocationHandle
|
||||
/// <summary>
|
||||
/// Gets a pointer to the state instance associated with this allocation handle.
|
||||
/// </summary>
|
||||
public void* State
|
||||
public required void* State
|
||||
{
|
||||
get; init;
|
||||
}
|
||||
@@ -69,7 +87,7 @@ public readonly unsafe struct AllocationHandle
|
||||
/// <summary>
|
||||
/// Gets a function pointer for allocating memory.
|
||||
/// </summary>
|
||||
public AllocFunc Alloc
|
||||
public required AllocFunc Alloc
|
||||
{
|
||||
get; init;
|
||||
}
|
||||
@@ -77,7 +95,7 @@ public readonly unsafe struct AllocationHandle
|
||||
/// <summary>
|
||||
/// Gets a function pointer for reallocating memory.
|
||||
/// </summary>
|
||||
public ReallocFunc Realloc
|
||||
public required ReallocFunc Realloc
|
||||
{
|
||||
get; init;
|
||||
}
|
||||
@@ -85,15 +103,7 @@ public readonly unsafe struct AllocationHandle
|
||||
/// <summary>
|
||||
/// Gets a function pointer for freeing allocated memory.
|
||||
/// </summary>
|
||||
public FreeFunc Free
|
||||
{
|
||||
get; init;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a function pointer for validating a memory handle.
|
||||
/// </summary>
|
||||
public IsValidFunc IsValid
|
||||
public required FreeFunc Free
|
||||
{
|
||||
get; init;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user