feat(core)!: refactor safety/debug defines, remove FreeList in JobSchedular

Refactor to use MHP_ENABLE_SAFETY_CHECKS, MHP_ENABLE_STACKTRACE, and MHP_ENABLE_MIMALLOC for feature toggling. Remove FreeList allocator in JobSchedular and debug-layer code, simplifying memory management. Improve memory leak detection and reporting, update memory allocation API, and guard all safety/debug features with new defines. Update csproj files, README, and code samples to match new API and toggles. Fix and improve collection types for correct behavior with and without safety checks. The codebase is now more modular and easier to configure for different build environments.

BREAKING CHANGE: Old defines (ENABLE_SAFETY_CHECKS, ENABLE_DEBUG_LAYER, ENABLE_MIMALLOC) are replaced with MHP_* equivalents. FreeList allocator and related debug features are removed. Some APIs and behaviors have changed for safety/debug configuration.
This commit is contained in:
2026-03-30 15:21:09 +09:00
parent 8231d6df60
commit aae8e2826f
27 changed files with 310 additions and 573 deletions

View File

@@ -1,9 +1,6 @@
using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.LowLevel.Utilities;
using System.Runtime.CompilerServices;
namespace Misaki.HighPerformance.Jobs;
#if false
public unsafe struct TempJobAllocator : IAllocator, IDisposable
{
private const int _FRAME_LATENCY = 4;
@@ -23,7 +20,7 @@ public unsafe struct TempJobAllocator : IAllocator, IDisposable
{
var memoryHandle = default(MemoryHandle);
_pArena = (VirtualArena*)AllocationManager.HeapAlloc((nuint)(sizeof(VirtualArena) * _FRAME_LATENCY), MemoryUtility.AlignOf<VirtualArena>(), AllocationOption.Clear, &memoryHandle);
_pArena = (VirtualArena*)MemoryUtility.Malloc((nuint)(sizeof(VirtualArena) * _FRAME_LATENCY));
_currentFrameCount = 0;
_currentFrameIndex = 0;
_memoryHandle = memoryHandle;
@@ -40,31 +37,51 @@ public unsafe struct TempJobAllocator : IAllocator, IDisposable
Alloc = &Allocate,
Realloc = &Reallocate,
Free = &Free,
#if ENABLE_SAFETY_CHECKS
IsValid = &IsValid,
#else
IsValid = null,
#endif
};
}
private static void* Allocate(void* instance, nuint size, nuint alignment, AllocationOption allocationOption, MemoryHandle* pHandle)
private static void* Allocate(void* instance, nuint size, nuint alignment, AllocationOption allocationOption
#if ENABLE_SAFETY_CHECKS
, MemoryHandle* pHandle
#endif
)
{
var pSelf = (TempJobAllocator*)instance;
var pCurrentArena = pSelf->_pArena + pSelf->_currentFrameIndex;
var ptr = pCurrentArena->Allocate(size, alignment, allocationOption);
if (ptr == null)
{
#if ENABLE_SAFETY_CHECKS
*pHandle = MemoryHandle.Invalid;
#endif
return null;
}
Interlocked.Increment(ref pSelf->_allocationsPerFrame[pSelf->_currentFrameIndex]);
#if ENABLE_SAFETY_CHECKS
*pHandle = new MemoryHandle(_MAGIC_ID, pSelf->_currentFrameCount);
#endif
return ptr;
}
private static void* Reallocate(void* instance, void* ptr, nuint oldSize, nuint newSize, nuint alignment, AllocationOption allocationOption, MemoryHandle* pHandle)
private static void* Reallocate(void* instance, void* ptr, nuint oldSize, nuint newSize, nuint alignment, AllocationOption allocationOption
#if ENABLE_SAFETY_CHECKS
, MemoryHandle* pHandle
#endif
)
{
if (ptr == null)
{
return Allocate(instance, newSize, alignment, allocationOption, pHandle);
return Allocate(instance, newSize, alignment, allocationOption
#if ENABLE_SAFETY_CHECKS
, pHandle
#endif
);
}
var pSelf = (TempJobAllocator*)instance;
@@ -80,18 +97,23 @@ public unsafe struct TempJobAllocator : IAllocator, IDisposable
return newPtr;
}
private static void Free(void* instance, void* ptr, MemoryHandle handle)
private static void Free(void* instance, void* ptr
#if ENABLE_SAFETY_CHECKS
, MemoryHandle handle
#endif
)
{
// The arena allocator does not free individual blocks, as it manages memory in chunks.
var pSelf = (TempJobAllocator*)instance;
Interlocked.Decrement(ref pSelf->_allocationsPerFrame[pSelf->_currentFrameIndex]);
}
#if ENABLE_SAFETY_CHECKS
private static bool IsValid(void* instance, MemoryHandle handle)
{
var pSelf = (TempJobAllocator*)instance;
return handle.ID == _MAGIC_ID && handle.Generation > pSelf->_currentFrameCount - _FRAME_LATENCY;
}
#endif
public int AdvanceFrame()
{
@@ -115,3 +137,4 @@ public unsafe struct TempJobAllocator : IAllocator, IDisposable
AllocationManager.HeapFree(_pArena, _memoryHandle);
}
}
#endif