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

@@ -8,7 +8,7 @@ public unsafe interface IUnsafeCollection : IDisposable
/// Indicates whether the object has been created. Returns true if the object is created, otherwise false.
/// </summary>
/// <remarks>
/// If ENABLE_DEBUG_LAYER is not defined, this property will only check if the underlying pointer is not null, which may not be sufficient to determine if the collection is fully initialized and ready for use.
/// If MHP_ENABLE_STACKTRACE is not defined, this property will only check if the underlying pointer is not null, which may not be sufficient to determine if the collection is fully initialized and ready for use.
/// </remarks>
bool IsCreated
{

View File

@@ -77,7 +77,7 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
private readonly int _sizeOfTValue;
private readonly int _log2MinGrowth;
#if ENABLE_DEBUG_LAYER
#if MHP_ENABLE_SAFETY_CHECKS
private MemoryHandle _memoryHandle;
#endif
private AllocationHandle _allocationHandle;
@@ -96,7 +96,7 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
{
get
{
#if ENABLE_DEBUG_LAYER
#if MHP_ENABLE_SAFETY_CHECKS
if (_buffer != null)
{
if (_allocationHandle.IsValid != null)
@@ -161,7 +161,7 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Conditional("ENABLE_SAFETY_CHECKS")]
[Conditional("MHP_ENABLE_SAFETY_CHECKS")]
private readonly void ThrowIfNotCreated()
{
if (!IsCreated)
@@ -256,11 +256,11 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
throw new InvalidOperationException("Target allocation handle does not support allocation.");
}
#if ENABLE_DEBUG_LAYER
#if MHP_ENABLE_SAFETY_CHECKS
MemoryHandle memHandle;
#endif
var buf = (byte*)_allocationHandle.Alloc(_allocationHandle.State, (uint)totalSize, (nuint)_alignment, allocationOption
#if ENABLE_DEBUG_LAYER
#if MHP_ENABLE_SAFETY_CHECKS
, &memHandle
#endif
);
@@ -269,7 +269,7 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
_keys = (TKey*)(_buffer + keyOffset);
_next = (int*)(_buffer + nextOffset);
_buckets = (int*)(_buffer + bucketOffset);
#if ENABLE_DEBUG_LAYER
#if MHP_ENABLE_SAFETY_CHECKS
_memoryHandle = memHandle;
#endif
}
@@ -284,7 +284,7 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
var oldNext = _next;
var oldBuckets = _buckets;
var oldBucketCapacity = _bucketCapacity;
#if ENABLE_DEBUG_LAYER
#if MHP_ENABLE_SAFETY_CHECKS
var oldMemoryHandle = _memoryHandle;
#endif
@@ -306,7 +306,7 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
if (_allocationHandle.Free != null)
{
_allocationHandle.Free(_allocationHandle.State, oldBuffer
#if ENABLE_DEBUG_LAYER
#if MHP_ENABLE_SAFETY_CHECKS
, oldMemoryHandle
#endif
);
@@ -725,7 +725,7 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
if (_allocationHandle.Free != null)
{
_allocationHandle.Free(_allocationHandle.State, _buffer
#if ENABLE_DEBUG_LAYER
#if MHP_ENABLE_SAFETY_CHECKS
, _memoryHandle
#endif
);

View File

@@ -96,7 +96,7 @@ public readonly unsafe struct ReadOnlyUnsafeCollection<T> : IEnumerable<T>
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Conditional("ENABLE_SAFETY_CHECKS")]
[Conditional("MHP_ENABLE_SAFETY_CHECKS")]
private readonly void CheckIndexBounds(int index)
{
if (index >= _count)

View File

@@ -21,6 +21,7 @@ public unsafe struct UnTypedArray : IUnTypedCollection
{
get
{
#if MHP_ENABLE_SAFETY_CHECKS
if (_buffer != null)
{
if (_allocationHandle.IsValid != null)
@@ -34,6 +35,9 @@ public unsafe struct UnTypedArray : IUnTypedCollection
}
return false;
#else
return _buffer != null;
#endif
}
}
@@ -57,12 +61,20 @@ public unsafe struct UnTypedArray : IUnTypedCollection
throw new InvalidOperationException("Target allocation handle does not support allocation.");
}
#if MHP_ENABLE_SAFETY_CHECKS
MemoryHandle memHandle;
_buffer = handle.Alloc(_allocationHandle.State, size, alignment, allocationOption, &memHandle);
#endif
var buff = handle.Alloc(handle.State, size, alignment, allocationOption
#if MHP_ENABLE_SAFETY_CHECKS
, &memHandle
#endif
);
_size = size;
_alignment = alignment;
#if MHP_ENABLE_SAFETY_CHECKS
_memoryHandle = memHandle;
#endif
_allocationHandle = handle;
}
@@ -109,10 +121,18 @@ public unsafe struct UnTypedArray : IUnTypedCollection
return;
}
#if MHP_ENABLE_SAFETY_CHECKS
var memHandle = _memoryHandle;
_buffer = _allocationHandle.Realloc(_allocationHandle.State, _buffer, _size, newSize, _alignment, option, &memHandle);
#endif
_buffer = _allocationHandle.Realloc(_allocationHandle.State, _buffer, _size, newSize, _alignment, option
#if MHP_ENABLE_SAFETY_CHECKS
, &memHandle
#endif
);
_size = newSize;
#if MHP_ENABLE_SAFETY_CHECKS
_memoryHandle = memHandle;
#endif
}
/// <inheritdoc/>
@@ -257,7 +277,11 @@ public unsafe struct UnTypedArray : IUnTypedCollection
if (_allocationHandle.Free != null)
{
_allocationHandle.Free(_allocationHandle.State, _buffer, _memoryHandle);
_allocationHandle.Free(_allocationHandle.State, _buffer
#if MHP_ENABLE_SAFETY_CHECKS
, _memoryHandle
#endif
);
}
_buffer = null;

View File

@@ -76,7 +76,7 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
private T* _buffer;
private int _count;
#if ENABLE_DEBUG_LAYER
#if MHP_ENABLE_SAFETY_CHECKS
private MemoryHandle _memoryHandle;
#endif
private AllocationHandle _allocationHandle;
@@ -108,7 +108,7 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
{
get
{
#if ENABLE_DEBUG_LAYER
#if MHP_ENABLE_SAFETY_CHECKS
if (_buffer != null)
{
if (_allocationHandle.IsValid != null)
@@ -167,17 +167,17 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
throw new InvalidOperationException("Target allocation handle does not support allocation.");
}
#if ENABLE_DEBUG_LAYER
#if MHP_ENABLE_SAFETY_CHECKS
MemoryHandle memHandle;
#endif
var buff = handle.Alloc(handle.State, (nuint)(count * sizeof(T)), AlignOf<T>(), allocationOption
#if ENABLE_DEBUG_LAYER
#if MHP_ENABLE_SAFETY_CHECKS
, &memHandle
#endif
);
_buffer = (T*)buff;
#if ENABLE_DEBUG_LAYER
#if MHP_ENABLE_SAFETY_CHECKS
_memoryHandle = memHandle;
#endif
_allocationHandle = handle;
@@ -213,7 +213,7 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Conditional("ENABLE_SAFETY_CHECKS")]
[Conditional("MHP_ENABLE_SAFETY_CHECKS")]
private readonly void ThrowIfNotCreated()
{
if (!IsCreated)
@@ -223,7 +223,7 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Conditional("ENABLE_SAFETY_CHECKS")]
[Conditional("MHP_ENABLE_SAFETY_CHECKS")]
private readonly void CheckIndexBounds(int index)
{
ThrowIfNotCreated();
@@ -258,16 +258,16 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
return;
}
#if ENABLE_DEBUG_LAYER
#if MHP_ENABLE_SAFETY_CHECKS
MemoryHandle memHandle = _memoryHandle;
#endif
var elemSize = SizeOf<T>();
_buffer = (T*)_allocationHandle.Realloc(_allocationHandle.State, _buffer, (nuint)Count * elemSize, (nuint)newSize * elemSize, AlignOf<T>(), option
#if ENABLE_DEBUG_LAYER
#if MHP_ENABLE_SAFETY_CHECKS
, &memHandle
#endif
);
#if ENABLE_DEBUG_LAYER
#if MHP_ENABLE_SAFETY_CHECKS
_memoryHandle = memHandle;
#endif
_count = newSize;
@@ -424,7 +424,7 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
if (_allocationHandle.Free != null)
{
_allocationHandle.Free(_allocationHandle.State, _buffer
#if ENABLE_DEBUG_LAYER
#if MHP_ENABLE_SAFETY_CHECKS
, _memoryHandle
#endif
);

View File

@@ -207,14 +207,14 @@ public unsafe struct UnsafeList<T> : IUnsafeCollection<T>
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Conditional("ENABLE_SAFETY_CHECKS")]
[Conditional("MHP_ENABLE_SAFETY_CHECKS")]
private readonly void CheckNoResizeCapacity(int count)
{
CheckNoResizeCapacity(count, Count);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Conditional("ENABLE_SAFETY_CHECKS")]
[Conditional("MHP_ENABLE_SAFETY_CHECKS")]
private readonly void CheckNoResizeCapacity(int index, int count)
{
if (index + count > Capacity)