using System.Diagnostics.CodeAnalysis; namespace Misaki.HighPerformance.LowLevel.Buffer; public readonly struct MemoryHandle : IEquatable { public readonly int ID { get => field - 1; } public readonly int Generation { get => field - 1; } public static readonly MemoryHandle Invalid = default; public MemoryHandle(int id, int generation) { ID = id + 1; Generation = generation + 1; } public bool Equals(MemoryHandle other) { return ID == other.ID && Generation == other.Generation; } public override bool Equals([NotNullWhen(true)] object? obj) { return obj is MemoryHandle other && Equals(other); } public override int GetHashCode() { return ID ^ Generation; } public override string? ToString() { return $"MemoryHandle(Id: {ID}, Generation: {Generation})"; } public static bool operator ==(MemoryHandle left, MemoryHandle right) { return left.Equals(right); } public static bool operator !=(MemoryHandle left, MemoryHandle right) { return !(left == right); } } /// /// A structure that encapsulates function pointers for memory allocation operations. /// public readonly unsafe struct AllocationHandle { /// /// Gets a pointer to the state instance associated with this allocation handle. /// public void* State { get; init; } /// /// Gets a function pointer for allocating memory. /// public AllocFunc Alloc { get; init; } /// /// Gets a function pointer for reallocating memory. /// public ReallocFunc Realloc { get; init; } /// /// Gets a function pointer for freeing allocated memory. /// public FreeFunc Free { get; init; } /// /// Gets a function pointer for validating a memory handle. /// public IsValidFunc IsValid { get; init; } } /// /// Represents an state interface for managing memory allocations. /// /// /// The state must be pined to a specific memory region. /// Otherwise the reference of the , may become invalid and lead to undefined behavior. /// public interface IAllocator { /// /// Gets a reference to the allocation handle associated with this state. /// AllocationHandle Handle { get; } } public unsafe interface IMemoryAllocator : IDisposable where TSelf : unmanaged, IMemoryAllocator { static abstract TSelf Create(in TOpts opts); void* Allocate(nuint size, nuint alignment, AllocationOption option = AllocationOption.None); void* Reallocate(void* ptr, nuint oldSize, nuint newSize, nuint alignment, AllocationOption allocationOption = AllocationOption.None); void Free(void* ptr); }