feat(allocator): add unified Reallocate to all allocators
Introduce a unified Reallocate method to all memory allocator types (Arena, Stack, FreeList, VirtualArena, VirtualStack, DynamicArena) and require it in the IMemoryAllocator interface. This enables efficient resizing of memory blocks, with fast-path optimizations for stack-like allocators. Update AllocationManager and MemoryPool to use the new Reallocate method, simplifying and optimizing memory resizing logic. Add public properties for buffer pointers, sizes, and offsets to allocator structs for easier diagnostics. Set FreeList's default concurrency level to 1 and make its allocation method return null on dispose instead of throwing. Clean up vector types for formatting, fix UnsafeList's RemoveRangeSwapBack logic, and simplify RemoveAtSwapBack. Simplify Program.cs to only run SPMDBenchmark. Add new unit tests for FixedString, UnsafeList, UnsafeHashMap, and UnsafeHashSet. Apply minor test code cleanups for consistency in TestUnsafeQueue. BREAKING CHANGE: IMemoryAllocator now requires a Reallocate method, and allocator APIs have changed accordingly.
This commit is contained in:
@@ -26,6 +26,10 @@ public unsafe struct Arena : IMemoryAllocator<Arena, Arena.CreationOptions>
|
||||
[FieldOffset(16)]
|
||||
private nuint _offset;
|
||||
|
||||
public readonly byte* Buffer => _buffer;
|
||||
public readonly nuint Size => _size;
|
||||
public readonly nuint Offset => _offset;
|
||||
|
||||
public Arena(nuint size)
|
||||
{
|
||||
ArgumentOutOfRangeException.ThrowIfNegative(size);
|
||||
@@ -55,7 +59,7 @@ public unsafe struct Arena : IMemoryAllocator<Arena, Arena.CreationOptions>
|
||||
{
|
||||
if (_buffer == null)
|
||||
{
|
||||
throw new ObjectDisposedException(nameof(Arena));
|
||||
return null;
|
||||
}
|
||||
|
||||
if (size == 0)
|
||||
@@ -92,6 +96,47 @@ public unsafe struct Arena : IMemoryAllocator<Arena, Arena.CreationOptions>
|
||||
return ptr;
|
||||
}
|
||||
|
||||
public void* Reallocate(void* ptr, nuint oldSize, nuint newSize, nuint alignment, AllocationOption allocationOption)
|
||||
{
|
||||
if (_buffer == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (ptr == null)
|
||||
{
|
||||
return Allocate(newSize, alignment, allocationOption);
|
||||
}
|
||||
|
||||
var additionalSize = newSize - oldSize;
|
||||
var currentOffset = Volatile.Read(ref _offset);
|
||||
|
||||
if ((byte*)ptr + oldSize == _buffer + currentOffset)
|
||||
{
|
||||
if (currentOffset + additionalSize <= _size)
|
||||
{
|
||||
if (Interlocked.CompareExchange(ref _offset, currentOffset + additionalSize, currentOffset) == currentOffset)
|
||||
{
|
||||
if (allocationOption.HasFlag(AllocationOption.Clear) && additionalSize > 0)
|
||||
{
|
||||
MemClear((byte*)ptr + oldSize, additionalSize);
|
||||
}
|
||||
|
||||
return ptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var newPtr = Allocate(newSize, alignment, allocationOption);
|
||||
if (newPtr == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
MemCpy(newPtr, ptr, Math.Min(oldSize, newSize));
|
||||
return newPtr;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly void Free(void* ptr)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user