feat(memory): add generic IMemoryAllocator and MemoryPool

Refactored Arena, DynamicArena, FreeList, and Stack to implement the new IMemoryAllocator<TSelf, TOpts> interface, standardizing allocation and free methods. Introduced MemoryPool<T, TOpts> for allocator lifetime management and AllocationHandle access. Updated Program.cs to use MemoryPool, replacing AllocationManager. Minor project file updates included.
This commit is contained in:
2026-03-18 21:19:51 +09:00
parent 641b459997
commit faf87953a3
8 changed files with 150 additions and 21 deletions

View File

@@ -7,8 +7,18 @@ namespace Misaki.HighPerformance.LowLevel.Buffer;
/// A memory management structure that allocates and resets memory blocks with specified alignment. /// A memory management structure that allocates and resets memory blocks with specified alignment.
/// </summary> /// </summary>
[StructLayout(LayoutKind.Explicit, Size = 64)] // Cache line aligned to prevent false sharing [StructLayout(LayoutKind.Explicit, Size = 64)] // Cache line aligned to prevent false sharing
public unsafe struct Arena : IDisposable public unsafe struct Arena : IMemoryAllocator<Arena, Arena.CreateOptions>
{ {
public struct CreateOptions
{
public nuint size;
}
public static Arena Create(in CreateOptions opts)
{
return new Arena(opts.size);
}
[FieldOffset(0)] [FieldOffset(0)]
private byte* _buffer; private byte* _buffer;
[FieldOffset(8)] [FieldOffset(8)]
@@ -82,6 +92,11 @@ public unsafe struct Arena : IDisposable
return ptr; return ptr;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void Free(void* ptr)
{
}
/// <summary> /// <summary>
/// Resets the arena. /// Resets the arena.
/// </summary> /// </summary>

View File

@@ -6,8 +6,18 @@ namespace Misaki.HighPerformance.LowLevel.Buffer;
/// A dynamic memory management structure that automatically grows by creating linked arenas when more space is needed. /// A dynamic memory management structure that automatically grows by creating linked arenas when more space is needed.
/// </summary> /// </summary>
[StructLayout(LayoutKind.Explicit, Size = 128)] [StructLayout(LayoutKind.Explicit, Size = 128)]
public unsafe struct DynamicArena : IDisposable public unsafe struct DynamicArena : IMemoryAllocator<DynamicArena, DynamicArena.CreateOptions>
{ {
public struct CreateOptions
{
public uint initialSize;
}
public static DynamicArena Create(in CreateOptions options)
{
return new DynamicArena(options.initialSize);
}
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
private struct ArenaNode private struct ArenaNode
{ {
@@ -133,6 +143,10 @@ public unsafe struct DynamicArena : IDisposable
return result; return result;
} }
public readonly void Free(void* ptr)
{
}
/// <summary> /// <summary>
/// Resets all arenas in the chain. /// Resets all arenas in the chain.
/// </summary> /// </summary>

View File

@@ -7,8 +7,20 @@ namespace Misaki.HighPerformance.LowLevel.Buffer;
/// A variable-size allocator that uses per-thread caches for the hot path and a remote-free queue for cross-thread deallocation. /// A variable-size allocator that uses per-thread caches for the hot path and a remote-free queue for cross-thread deallocation.
/// </summary> /// </summary>
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
public unsafe struct FreeList : IDisposable public unsafe struct FreeList : IMemoryAllocator<FreeList, FreeList.CreationOpts>
{ {
public struct CreationOpts
{
public nuint alignment;
public nuint chunkSize;
public int maxConcurrencyLevel;
}
public static FreeList Create(in CreationOpts opts)
{
return new FreeList(opts.alignment, opts.chunkSize, opts.maxConcurrencyLevel);
}
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
private struct FreeNode private struct FreeNode
{ {

View File

@@ -109,3 +109,12 @@ public interface IAllocator
get; get;
} }
} }
public unsafe interface IMemoryAllocator<TSelf, TOpts> : IDisposable
where TSelf : unmanaged, IMemoryAllocator<TSelf, TOpts>
{
static abstract TSelf Create(in TOpts opts);
void* Allocate(nuint size, nuint alignment, AllocationOption option = AllocationOption.None);
void Free(void* ptr);
}

View File

@@ -0,0 +1,74 @@
using Misaki.HighPerformance.LowLevel.Utilities;
namespace Misaki.HighPerformance.LowLevel.Buffer;
public unsafe struct MemoryPool<T, TOpts> : IDisposable
where T : unmanaged, IMemoryAllocator<T, TOpts>
{
private T* _pAllocator;
private AllocationHandle _allocationHandle;
public readonly ref T Allocator => ref *_pAllocator;
public readonly AllocationHandle AllocationHandle => _allocationHandle;
public MemoryPool(in TOpts opts)
{
_pAllocator = (T*)Malloc((nuint)sizeof(T));
*_pAllocator = T.Create(opts);
_allocationHandle = new AllocationHandle
{
State = _pAllocator,
Alloc = &Allocate,
Realloc = &Reallocate,
Free = &Free,
IsValid = null
};
}
private static void* Allocate(void* pAllocator, nuint size, nuint alignment, AllocationOption allocationOption, MemoryHandle* pHandle)
{
return ((T*)pAllocator)->Allocate(size, alignment, allocationOption);
}
private static void* Reallocate(void* pAllocator, void* ptr, nuint oldSize, nuint newSize, nuint alignment, AllocationOption allocationOption, MemoryHandle* pHandle)
{
if (ptr == null)
{
return Allocate(pAllocator, newSize, alignment, allocationOption, pHandle);
}
MemoryHandle newHandle;
var newPtr = Allocate(pAllocator, newSize, alignment, allocationOption, &newHandle);
if (newPtr == null)
{
return null;
}
MemCpy(newPtr, ptr, Math.Min(oldSize, newSize));
Free(pAllocator, ptr, *pHandle);
*pHandle = newHandle;
return newPtr;
}
private static void Free(void* pAllocator, void* ptr, MemoryHandle handle)
{
((T*)pAllocator)->Free(ptr);
}
public void Dispose()
{
if (_pAllocator == null)
{
return;
}
_pAllocator->Dispose();
MemoryUtility.Free(_pAllocator);
_pAllocator = null;
_allocationHandle = default;
}
}

View File

@@ -1,3 +1,4 @@
using Misaki.HighPerformance.LowLevel.Utilities;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
namespace Misaki.HighPerformance.LowLevel.Buffer; namespace Misaki.HighPerformance.LowLevel.Buffer;
@@ -18,7 +19,7 @@ public unsafe partial struct Stack
for (var i = 0; i < s_stackCount; i++) for (var i = 0; i < s_stackCount; i++)
{ {
Free(s_pStackBuffers[i]); MemoryUtility.Free(s_pStackBuffers[i]);
} }
} }
} }
@@ -28,8 +29,18 @@ public unsafe partial struct Stack
/// blocks within a preallocated buffer. /// blocks within a preallocated buffer.
/// </summary> /// </summary>
/// <remarks>This is not a thread-safe implementation.</remarks> /// <remarks>This is not a thread-safe implementation.</remarks>
public unsafe partial struct Stack : IDisposable public unsafe partial struct Stack : IMemoryAllocator<Stack, Stack.CreationOpts>
{ {
public struct CreationOpts
{
public nuint size;
}
public static Stack Create(in CreationOpts opts)
{
return new Stack(opts.size);
}
private const nuint _DEFAULT_SIZE = 1024 * 1024; // 1MB private const nuint _DEFAULT_SIZE = 1024 * 1024; // 1MB
public readonly ref struct Scope : IDisposable public readonly ref struct Scope : IDisposable
@@ -211,6 +222,10 @@ public unsafe partial struct Stack : IDisposable
return ptr; return ptr;
} }
public readonly void Free(void* ptr)
{
}
/// <summary> /// <summary>
/// Resets the internal offset to its initial position. /// Resets the internal offset to its initial position.
/// </summary> /// </summary>

View File

@@ -1,15 +1,5 @@
using BenchmarkDotNet.Running;
using Misaki.HighPerformance.LowLevel.Buffer; using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.LowLevel.Collections; using Misaki.HighPerformance.LowLevel.Collections;
using Misaki.HighPerformance.LowLevel.Utilities;
using Misaki.HighPerformance.Mathematics.SPMD;
using Misaki.HighPerformance.Test.Benchmark;
using Misaki.HighPerformance.Test.Jobs;
using System.Dynamic;
using System.Runtime.CompilerServices;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
//BenchmarkRunner.Run<SPMDBenchmark>(); //BenchmarkRunner.Run<SPMDBenchmark>();
//var hashMap = new UnsafeHashMap<int, int>(10, Misaki.HighPerformance.LowLevel.Buffer.Allocator.Persistent); //var hashMap = new UnsafeHashMap<int, int>(10, Misaki.HighPerformance.LowLevel.Buffer.Allocator.Persistent);
@@ -42,13 +32,13 @@ using System.Text.Json.Serialization;
// } // }
//} //}
AllocationManager.Initialize(1024 * 1024 * 1024, 1); using var pool = new MemoryPool<Stack, Stack.CreationOpts>(new Stack.CreationOpts() { size = 1024 * 1024 });
using var scope = pool.Allocator.CreateScope(pool.AllocationHandle);
// Should be undefined or throw exception because AllocationManager does not initialized. var arr = new UnsafeArray<int>(1000, scope.AllocationHandle);
var arr = new UnsafeArray<int>(1000, Allocator.FreeList); for (var i = 0; i < arr.Length; i++)
for (int i = 0; i < arr.Length; i++)
{ {
Console.WriteLine(arr[i]); Console.WriteLine(arr[i]);
} }
arr.Dispose(); arr.Dispose();
AllocationManager.Dispose();