Add TLSF allocator and refactor allocation API

- 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.
This commit is contained in:
2026-05-05 22:13:58 +09:00
parent 627c1da928
commit d3e497c7d8
14 changed files with 303 additions and 114 deletions

View File

@@ -1,3 +1,4 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
namespace Misaki.HighPerformance.LowLevel.Buffer;
@@ -97,35 +98,39 @@ public readonly unsafe struct AllocationHandle
public static AllocationHandle Persistent => AllocationManager.s_pHeapAllocator->Handle;
/// <summary>
/// Gets a pointer to the state instance associated with this allocation handle.
/// Allocator for persistent allocations using a Two-Level Segregated Fit (TLSF) algorithm. Allocations are not automatically released after use, but can be reused to reduce fragmentation, system call and improve performance.
/// </summary>
public required void* State
public static AllocationHandle TLSF => AllocationManager.s_pTLSFAllocator->Handle;
private readonly void* _state;
private readonly AllocFunc _alloc;
private readonly ReallocFunc _realloc;
private readonly FreeFunc _free;
public AllocationHandle(void* state, AllocFunc alloc, ReallocFunc realloc, FreeFunc free)
{
get; init;
_state = state;
_alloc = alloc;
_realloc = realloc;
_free = free;
}
/// <summary>
/// Gets a function pointer for allocating memory.
/// </summary>
public required AllocFunc Alloc
public void* Alloc(nuint size, nuint alignment, AllocationOption option = AllocationOption.None)
{
get; init;
Debug.Assert(_alloc != null);
return _alloc(_state, size, alignment, option);
}
/// <summary>
/// Gets a function pointer for reallocating memory.
/// </summary>
public required ReallocFunc Realloc
public void* Realloc(void* ptr, nuint oldSize, nuint newSize, nuint alignment, AllocationOption allocationOption = AllocationOption.None)
{
get; init;
Debug.Assert(_realloc != null);
return _realloc(_state, ptr, oldSize, newSize, alignment, allocationOption);
}
/// <summary>
/// Gets a function pointer for freeing allocated memory.
/// </summary>
public required FreeFunc Free
public void Free(void* ptr)
{
get; init;
Debug.Assert(_free != null);
_free(_state, ptr);
}
}