feat(memory): refactor allocation and add new queue

Refactored memory management by removing safety checks and introducing `MemoryHandle` for centralized tracking. Simplified allocation logic across allocators and enhanced `Dispose` methods for better resource cleanup.

Added `UnsafeChunkedQueue<T>`, a lock-free, dynamically resizing queue with chunk-based memory management, supporting parallel producers and consumers.

Updated unit tests to validate new queue functionality and ensure compatibility with refactored memory logic. Incremented assembly version to 1.6.12.

BREAKING CHANGE: Removed `#if MHP_ENABLE_SAFETY_CHECKS` blocks, altering memory validation behavior.
This commit is contained in:
2026-04-10 14:44:48 +09:00
parent dea8de60d0
commit a0deadc363
25 changed files with 647 additions and 456 deletions

View File

@@ -33,9 +33,6 @@ public unsafe partial struct Stack : IMemoryAllocator<Stack, Stack.CreationOptio
_allocator = allocator;
_handle = handle;
_originalOffset = allocator->_offset;
#if MHP_ENABLE_SAFETY_CHECKS
_allocator->_activeScopeCount++;
#endif
}
public void Dispose()
@@ -43,9 +40,6 @@ public unsafe partial struct Stack : IMemoryAllocator<Stack, Stack.CreationOptio
if (_allocator != null)
{
_allocator->_offset = _allocator->_offset > _originalOffset ? _originalOffset : _allocator->_offset;
#if MHP_ENABLE_SAFETY_CHECKS
_allocator->_activeScopeCount--;
#endif
}
}
}
@@ -53,9 +47,6 @@ public unsafe partial struct Stack : IMemoryAllocator<Stack, Stack.CreationOptio
private byte* _buffer;
private nuint _size;
private nuint _offset;
#if MHP_ENABLE_SAFETY_CHECKS
private uint _activeScopeCount;
#endif
public readonly byte* Buffer => _buffer;
public readonly nuint Size => _size;
@@ -72,9 +63,6 @@ public unsafe partial struct Stack : IMemoryAllocator<Stack, Stack.CreationOptio
_buffer = (byte*)Malloc(size);
_size = size;
_offset = 0;
#if MHP_ENABLE_SAFETY_CHECKS
_activeScopeCount = 0;
#endif
}
/// <summary>
@@ -101,13 +89,6 @@ public unsafe partial struct Stack : IMemoryAllocator<Stack, Stack.CreationOptio
/// there is insufficient space in the buffer.</returns>
public void* Allocate(nuint size, nuint alignment, AllocationOption allocationOption = AllocationOption.None)
{
#if MHP_ENABLE_SAFETY_CHECKS
if (_activeScopeCount == 0)
{
throw new InvalidOperationException("Allocations can only be made within an active memory scope.");
}
#endif
if (size == 0)
{
return null;
@@ -197,10 +178,12 @@ public unsafe partial struct Stack : IMemoryAllocator<Stack, Stack.CreationOptio
return;
}
MemoryUtility.Free(_buffer);
var ptr = _buffer;
_buffer = null;
_size = 0;
_offset = 0;
_size = 0;
MemoryUtility.Free(ptr);
}
}