- Introduced TLSF allocator with thread-safe wrapper and integrated into AllocationManager. - Extended AllocationManagerDesc for TLSF config; made properties settable. - Refactored AllocationHandle to encapsulate function pointers and state, replacing direct field access with methods. - Updated all memory-related structs to use new AllocationHandle API. - Added ReplaceIfZeros utility to MemoryUtility. - Improved IndexOfNullByte performance. - Minor fix in MemoryLeakException output order. - FreeList now uses a fixed 64KB refill budget. - Bumped version to 1.6.21; removed MHP_ENABLE_STACKTRACE from Debug. - Updated Program.cs to test TLSF allocator and manage allocation lifecycle.
54 lines
1.6 KiB
C#
54 lines
1.6 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)
|
|
{
|
|
var allocator = TAllocator.Create(opts);
|
|
|
|
_pAllocator = (TAllocator*)Malloc((nuint)sizeof(TAllocator));
|
|
*_pAllocator = allocator;
|
|
|
|
_allocationHandle = new AllocationHandle(_pAllocator, &Allocate, &Reallocate, &Free);
|
|
}
|
|
|
|
private static void* Allocate(void* pAllocator, nuint size, nuint alignment, AllocationOption allocationOption)
|
|
{
|
|
return ((TAllocator*)pAllocator)->Allocate(size, alignment, allocationOption);
|
|
}
|
|
|
|
private static void* Reallocate(void* pAllocator, void* ptr, nuint oldSize, nuint newSize, nuint alignment, AllocationOption allocationOption)
|
|
{
|
|
return ((TAllocator*)pAllocator)->Reallocate(ptr, oldSize, newSize, alignment, allocationOption);
|
|
}
|
|
|
|
private static void Free(void* pAllocator, void* ptr)
|
|
{
|
|
((TAllocator*)pAllocator)->Free(ptr);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if (_pAllocator == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_pAllocator->Dispose();
|
|
|
|
MemoryUtility.Free(_pAllocator);
|
|
|
|
_pAllocator = null;
|
|
_allocationHandle = default;
|
|
}
|
|
} |