Files
Misaki.HighPerformance/Misaki.HighPerformance.LowLevel/Buffer/MemoryPool.cs
Misaki 28e921c48d feat(buffer)!: refactor allocators to use MemoryPool<T>
Refactor memory allocation system to use generic MemoryPool<TAllocator, TOpts> for arena, stack, and free list allocators, replacing custom allocator structs. Introduce MemoryBlock as a safer, more robust replacement for UnTypedArray. Improve thread safety, safety checks, and documentation. Reorder and clarify Allocator enum. Add comprehensive unit tests for all allocators and pointer assertion utilities. Update project to enable safety checks in Debug builds. Remove obsolete interfaces and ensure consistent deallocation with MemoryUtility.Free.

BREAKING CHANGE: Custom allocator structs are removed and replaced with MemoryPool-based abstraction. UnTypedArray is replaced by MemoryBlock. Allocator enum order and semantics are changed. Public API changes may require code updates.
2026-04-04 19:24:02 +09:00

99 lines
2.7 KiB
C#

using Misaki.HighPerformance.LowLevel.Utilities;
using System.Runtime.CompilerServices;
namespace Misaki.HighPerformance.LowLevel.Buffer;
public unsafe struct MemoryPool<TAllocator, TOpts> : IDisposable
where TAllocator : unmanaged, IMemoryAllocator<TAllocator, TOpts>
{
private TAllocator* _pAllocator;
private AllocationHandle _allocationHandle;
public readonly ref TAllocator Allocator => ref Unsafe.AsRef<TAllocator>(_pAllocator);
public readonly AllocationHandle AllocationHandle => _allocationHandle;
public MemoryPool(in TOpts opts)
{
_pAllocator = (TAllocator*)Malloc((nuint)sizeof(TAllocator));
*_pAllocator = TAllocator.Create(opts);
_allocationHandle = new AllocationHandle
{
State = _pAllocator,
Alloc = &Allocate,
Realloc = &Reallocate,
Free = &Free,
IsValid = null
};
}
private static void* Allocate(void* pAllocator, nuint size, nuint alignment, AllocationOption allocationOption
#if MHP_ENABLE_SAFETY_CHECKS
, MemoryHandle* pHandle
#endif
)
{
var ptr = ((TAllocator*)pAllocator)->Allocate(size, alignment, allocationOption);
#if MHP_ENABLE_SAFETY_CHECKS
if (ptr != null)
{
*pHandle = AllocationManager.AddAllocation(ptr, size);
}
#endif
return ptr;
}
private static void* Reallocate(void* pAllocator, void* ptr, nuint oldSize, nuint newSize, nuint alignment, AllocationOption allocationOption
#if MHP_ENABLE_SAFETY_CHECKS
, MemoryHandle* pHandle
#endif
)
{
var newPtr = ((TAllocator*)pAllocator)->Reallocate(ptr, oldSize, newSize, alignment, allocationOption);
#if MHP_ENABLE_SAFETY_CHECKS
if (ptr == null && newPtr != null)
{
*pHandle = AllocationManager.AddAllocation(newPtr, newSize);
}
else
{
if (newPtr == null)
{
AllocationManager.RemoveAllocation(*pHandle);
}
else
{
AllocationManager.UpdateAllocation(*pHandle, newPtr, newSize);
}
}
#endif
return newPtr;
}
private static void Free(void* pAllocator, void* ptr
#if MHP_ENABLE_SAFETY_CHECKS
, MemoryHandle handle
#endif
)
{
((TAllocator*)pAllocator)->Free(ptr);
#if MHP_ENABLE_SAFETY_CHECKS
AllocationManager.RemoveAllocation(handle);
#endif
}
public void Dispose()
{
if (_pAllocator == null)
{
return;
}
_pAllocator->Dispose();
MemoryUtility.Free(_pAllocator);
_pAllocator = null;
_allocationHandle = default;
}
}