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:
@@ -7,8 +7,18 @@ namespace Misaki.HighPerformance.LowLevel.Buffer;
|
||||
/// A memory management structure that allocates and resets memory blocks with specified alignment.
|
||||
/// </summary>
|
||||
[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)]
|
||||
private byte* _buffer;
|
||||
[FieldOffset(8)]
|
||||
@@ -82,6 +92,11 @@ public unsafe struct Arena : IDisposable
|
||||
return ptr;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly void Free(void* ptr)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resets the arena.
|
||||
/// </summary>
|
||||
|
||||
@@ -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.
|
||||
/// </summary>
|
||||
[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)]
|
||||
private struct ArenaNode
|
||||
{
|
||||
@@ -133,6 +143,10 @@ public unsafe struct DynamicArena : IDisposable
|
||||
return result;
|
||||
}
|
||||
|
||||
public readonly void Free(void* ptr)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resets all arenas in the chain.
|
||||
/// </summary>
|
||||
|
||||
@@ -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.
|
||||
/// </summary>
|
||||
[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)]
|
||||
private struct FreeNode
|
||||
{
|
||||
|
||||
@@ -109,3 +109,12 @@ public interface IAllocator
|
||||
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);
|
||||
}
|
||||
74
Misaki.HighPerformance.LowLevel/Buffer/MemoryPool.cs
Normal file
74
Misaki.HighPerformance.LowLevel/Buffer/MemoryPool.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
using Misaki.HighPerformance.LowLevel.Utilities;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Misaki.HighPerformance.LowLevel.Buffer;
|
||||
@@ -18,7 +19,7 @@ public unsafe partial struct Stack
|
||||
|
||||
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.
|
||||
/// </summary>
|
||||
/// <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
|
||||
|
||||
public readonly ref struct Scope : IDisposable
|
||||
@@ -211,6 +222,10 @@ public unsafe partial struct Stack : IDisposable
|
||||
return ptr;
|
||||
}
|
||||
|
||||
public readonly void Free(void* ptr)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resets the internal offset to its initial position.
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user