feat(buffer)!: refactor allocators to use MemoryPool<T>

Refactor memory allocation system to use generic MemoryPool<TAllocator, TOpts> for arena, stack, and free list allocators, replacing custom allocator structs. Introduce MemoryBlock as a safer, more robust replacement for UnTypedArray. Improve thread safety, safety checks, and documentation. Reorder and clarify Allocator enum. Add comprehensive unit tests for all allocators and pointer assertion utilities. Update project to enable safety checks in Debug builds. Remove obsolete interfaces and ensure consistent deallocation with MemoryUtility.Free.

BREAKING CHANGE: Custom allocator structs are removed and replaced with MemoryPool-based abstraction. UnTypedArray is replaced by MemoryBlock. Allocator enum order and semantics are changed. Public API changes may require code updates.
This commit is contained in:
2026-04-04 19:24:02 +09:00
parent 208e1aa975
commit 28e921c48d
18 changed files with 1284 additions and 505 deletions

View File

@@ -1,7 +1,5 @@
#if MHP_ENABLE_SAFETY_CHECKS
using Misaki.HighPerformance.Collections;
using System.Collections.Concurrent;
#endif
using System.Diagnostics;
using System.Runtime.CompilerServices;
@@ -52,6 +50,16 @@ public readonly struct AllocationManagerInitOpts
get; init;
}
public nuint FreeListChunkSize
{
get; init;
}
public nuint FreeListDefaultAlignment
{
get; init;
}
public int FreeListConcurrencyLevel
{
get; init;
@@ -61,6 +69,8 @@ public readonly struct AllocationManagerInitOpts
{
ArenaCapacity = 1024 * 1024 * 1024, // 1 GB
StackCapacity = 16 * 1024 * 1024, // 16 MB per thread
FreeListChunkSize = 64 * 1024 * 1024,
FreeListDefaultAlignment = 8,
FreeListConcurrencyLevel = Environment.ProcessorCount
};
}
@@ -71,93 +81,6 @@ public readonly struct AllocationManagerInitOpts
/// </summary>
public static unsafe class AllocationManager
{
private struct ArenaAllocator : IAllocator, IDisposable
{
private const int _ARENA_MAGIC_ID = -3941029;
private VirtualArena _arena;
private int _currentTick;
private AllocationHandle _handle;
public readonly AllocationHandle Handle => _handle;
public readonly int CurrentTick => _currentTick;
public void Init(nuint capacity)
{
_arena = new VirtualArena(capacity);
_handle = new AllocationHandle
{
State = Unsafe.AsPointer(ref this),
Alloc = &Allocate,
Realloc = &Reallocate,
Free = null,
#if MHP_ENABLE_SAFETY_CHECKS
IsValid = &IsValid
#else
IsValid = null
#endif
};
_currentTick = 0;
}
private static void* Allocate(void* instance, nuint size, nuint alignment, AllocationOption allocationOption
#if MHP_ENABLE_SAFETY_CHECKS
, MemoryHandle* pHandle
#endif
)
{
var selfPtr = (ArenaAllocator*)instance;
var ptr = selfPtr->_arena.Allocate(size, alignment, allocationOption);
if (ptr == null)
{
#if MHP_ENABLE_SAFETY_CHECKS
*pHandle = MemoryHandle.Invalid;
#endif
return null;
}
#if MHP_ENABLE_SAFETY_CHECKS
*pHandle = new MemoryHandle(_ARENA_MAGIC_ID, selfPtr->_currentTick);
#endif
return ptr;
}
private static void* Reallocate(void* instance, void* ptr, nuint oldSize, nuint newSize, nuint alignment, AllocationOption allocationOption
#if MHP_ENABLE_SAFETY_CHECKS
, MemoryHandle* pHandle
#endif
)
{
var selfPtr = (ArenaAllocator*)instance;
var newPtr = selfPtr->_arena.Reallocate(ptr, oldSize, newSize, alignment, allocationOption);
#if MHP_ENABLE_SAFETY_CHECKS
*pHandle = new MemoryHandle(_ARENA_MAGIC_ID, selfPtr->_currentTick);
#endif
return newPtr;
}
#if MHP_ENABLE_SAFETY_CHECKS
private static bool IsValid(void* instance, MemoryHandle handle)
{
var selfPtr = (ArenaAllocator*)instance;
return handle.ID == _ARENA_MAGIC_ID && handle.Generation == selfPtr->_currentTick;
}
#endif
public void Reset()
{
_arena.Reset();
_currentTick++;
}
public void Dispose()
{
_arena.Dispose();
}
}
private struct HeapAllocator : IAllocator
{
private AllocationHandle _handle;
@@ -186,11 +109,24 @@ public static unsafe class AllocationManager
#endif
)
{
return HeapAlloc(size, alignment, allocationOption
var ptr = AlignedAlloc(size, alignment);
if (ptr == null)
{
#if MHP_ENABLE_SAFETY_CHECKS
, pHandle
*pHandle = MemoryHandle.Invalid;
#endif
);
return null;
}
if (allocationOption.HasFlag(AllocationOption.Clear))
{
MemClear(ptr, size);
}
#if MHP_ENABLE_SAFETY_CHECKS
*pHandle = AddAllocation(ptr, size);
#endif
return ptr;
}
private static void* Reallocate(void* _, void* ptr, nuint oldSize, nuint newSize, nuint alignment, AllocationOption allocationOption
@@ -199,39 +135,38 @@ public static unsafe class AllocationManager
#endif
)
{
if (ptr == null)
{
return Allocate(null, newSize, alignment, allocationOption
#if MHP_ENABLE_SAFETY_CHECKS
, pHandle
#endif
);
}
var newPtr = AlignedRealloc(ptr, newSize, alignment);
#if MHP_ENABLE_SAFETY_CHECKS
MemoryHandle newHandle;
if (ptr == null && newPtr != null)
{
AddAllocation(newPtr, newSize);
}
else
{
if (newPtr == null)
{
RemoveAllocation(*pHandle);
}
else
{
UpdateAllocation(*pHandle, newPtr, newSize);
}
}
#endif
var newPtr = HeapAlloc(newSize, alignment, allocationOption
#if MHP_ENABLE_SAFETY_CHECKS
, &newHandle
#endif
);
if (newPtr == null)
{
return null;
}
MemCpy(newPtr, ptr, Math.Min(oldSize, newSize));
HeapFree(ptr
#if MHP_ENABLE_SAFETY_CHECKS
, *pHandle
#endif
);
if (allocationOption.HasFlag(AllocationOption.Clear) && newSize > oldSize)
{
var offset = (byte*)newPtr + oldSize;
var clearSize = newSize - oldSize;
MemClear(offset, clearSize);
}
#if MHP_ENABLE_SAFETY_CHECKS
*pHandle = newHandle;
#endif
return newPtr;
}
@@ -241,11 +176,10 @@ public static unsafe class AllocationManager
#endif
)
{
HeapFree(ptr
AlignedFree(ptr);
#if MHP_ENABLE_SAFETY_CHECKS
, handle
RemoveAllocation(handle);
#endif
);
}
#if MHP_ENABLE_SAFETY_CHECKS
@@ -256,231 +190,13 @@ public static unsafe class AllocationManager
#endif
}
private struct StackAllocator : IAllocator, IDisposable
{
private const int _STACK_MAGIC_ID = -6843541;
private static MemoryPool<VirtualArena, VirtualArena.CreationOptions> s_arenaAllocator;
private static MemoryPool<FreeList, FreeList.CreationOptions> s_freeListAllocator;
private static void** s_pStackBuffers = null;
private static int s_stackCount = 0;
private static int s_stackCapacity = 0;
private static readonly SpinLock s_locker = new SpinLock(false);
[ThreadStatic]
private static MemoryPool<VirtualStack, VirtualStack.CreationOptions> t_stackAllocator;
[ThreadStatic]
private static VirtualStack s_stack;
private AllocationHandle _handle;
public readonly AllocationHandle Handle => _handle;
public void Init()
{
_handle = new AllocationHandle
{
State = Unsafe.AsPointer(ref this),
Alloc = &Allocate,
Realloc = &Reallocate,
Free = null,
#if MHP_ENABLE_SAFETY_CHECKS
IsValid = &IsValid
#else
IsValid = null
#endif
};
}
private static void EnsureInitialize()
{
if (s_stack.Buffer == null)
{
s_stack = new VirtualStack(s_threadLocalStackDefaultSize);
var token = false;
try
{
s_locker.Enter(ref token);
if (s_pStackBuffers == null)
{
s_pStackBuffers = (void**)Malloc((nuint)(sizeof(void*) * Environment.ProcessorCount));
s_stackCapacity = Environment.ProcessorCount;
}
if (s_stackCount >= s_stackCapacity)
{
var pOld = s_pStackBuffers;
var newCapacity = s_stackCapacity * 2;
var pNew = (void**)Realloc(pOld, (nuint)(sizeof(void*) * newCapacity));
s_pStackBuffers = pNew;
s_stackCapacity = newCapacity;
}
s_pStackBuffers[s_stackCount] = s_stack.Buffer;
s_stackCount++;
}
finally
{
if (token)
{
s_locker.Exit();
}
}
}
}
private static void* Allocate(void* instance, nuint size, nuint alignment, AllocationOption allocationOption
#if MHP_ENABLE_SAFETY_CHECKS
, MemoryHandle* pHandle
#endif
)
{
EnsureInitialize();
var ptr = s_stack.Allocate(size, alignment, allocationOption);
if (ptr == null)
{
#if MHP_ENABLE_SAFETY_CHECKS
*pHandle = MemoryHandle.Invalid;
#endif
return null;
}
#if MHP_ENABLE_SAFETY_CHECKS
*pHandle = new MemoryHandle(_STACK_MAGIC_ID, (int)s_stack.Offset);
#endif
return ptr;
}
private static void* Reallocate(void* instance, void* ptr, nuint oldSize, nuint newSize, nuint alignment, AllocationOption allocationOption
#if MHP_ENABLE_SAFETY_CHECKS
, MemoryHandle* pHandle
#endif
)
{
EnsureInitialize();
var newPtr = s_stack.Reallocate(ptr, oldSize, newSize, alignment, allocationOption);
#if MHP_ENABLE_SAFETY_CHECKS
*pHandle = new MemoryHandle(_STACK_MAGIC_ID, (int)s_stack.Offset);
#endif
return newPtr;
}
#if MHP_ENABLE_SAFETY_CHECKS
private static bool IsValid(void* instance, MemoryHandle handle)
{
return handle.ID == _STACK_MAGIC_ID && handle.Generation <= (int)s_stack.Offset;
}
#endif
public static VirtualStack.Scope CreateScope(StackAllocator* pSelf)
{
EnsureInitialize();
return s_stack.CreateScope(pSelf->_handle);
}
public readonly void Dispose()
{
if (s_pStackBuffers == null)
{
return;
}
for (var i = 0; i < s_stackCount; i++)
{
Munmap(s_pStackBuffers[i], s_threadLocalStackDefaultSize);
}
}
}
private struct FreeListAllocator : IAllocator, IDisposable
{
private FreeList _freeList;
private AllocationHandle _handle;
public readonly AllocationHandle Handle => _handle;
public void Init(int concurrencyLevel)
{
_freeList = new FreeList(8, 64 * 1024, concurrencyLevel);
_handle = new AllocationHandle
{
State = Unsafe.AsPointer(ref this),
Alloc = &Allocate,
Realloc = &Reallocate,
Free = &Free,
#if MHP_ENABLE_SAFETY_CHECKS
IsValid = &IsValid
#else
IsValid = null
#endif
};
}
private static void* Allocate(void* instance, nuint size, nuint alignment, AllocationOption allocationOption
#if MHP_ENABLE_SAFETY_CHECKS
, MemoryHandle* pHandle
#endif
)
{
var selfPtr = (FreeListAllocator*)instance;
var ptr = selfPtr->_freeList.Allocate(size, alignment, allocationOption);
if (ptr == null)
{
return null;
}
#if MHP_ENABLE_SAFETY_CHECKS
*pHandle = AddAllocation(ptr, size);
#endif
return ptr;
}
private static void* Reallocate(void* instance, void* ptr, nuint oldSize, nuint newSize, nuint alignment, AllocationOption allocationOption
#if MHP_ENABLE_SAFETY_CHECKS
, MemoryHandle* pHandle
#endif
)
{
var selfPtr = (FreeListAllocator*)instance;
var newPtr = selfPtr->_freeList.Reallocate(ptr, oldSize, newSize, alignment, allocationOption);
#if MHP_ENABLE_SAFETY_CHECKS
RemoveAllocation(*pHandle);
*pHandle = AddAllocation(newPtr, newSize);
#endif
return newPtr;
}
#if MHP_ENABLE_SAFETY_CHECKS
private static bool IsValid(void* instance, MemoryHandle handle)
{
return ContainsAllocation(handle);
}
#endif
private static void Free(void* instance, void* ptr
#if MHP_ENABLE_SAFETY_CHECKS
, MemoryHandle handle
#endif
)
{
var selfPtr = (FreeListAllocator*)instance;
selfPtr->_freeList.Free(ptr);
#if MHP_ENABLE_SAFETY_CHECKS
RemoveAllocation(handle);
#endif
}
public void Dispose()
{
_freeList.Dispose();
}
}
private static ArenaAllocator* s_pArenaAllocator;
private static HeapAllocator* s_pHeapAllocator;
private static StackAllocator* s_pStackAllocator;
private static FreeListAllocator* s_pFreeListAllocator;
#if MHP_ENABLE_SAFETY_CHECKS
private static ConcurrentSlotMap<AllocationInfo> s_allocations = null!;
@@ -497,7 +213,55 @@ public static unsafe class AllocationManager
#endif
private static volatile bool s_initialized;
private static nuint s_threadLocalStackDefaultSize;
private static nuint s_threadLocalStackSize;
private static readonly SpinLock s_stackLocker = new SpinLock(false);
private static VirtualStack** s_ppStack;
private static int s_ppStackCount;
private static int s_ppStackCapacity;
private static void EnsureThreadLocalStackInitialize()
{
if (Unsafe.IsNullRef(ref t_stackAllocator.Allocator))
{
t_stackAllocator = new MemoryPool<VirtualStack, VirtualStack.CreationOptions>(new VirtualStack.CreationOptions
{
reserveCapacity = s_threadLocalStackSize
});
var token = false;
try
{
s_stackLocker.Enter(ref token);
if (s_ppStack == null)
{
s_ppStack = (VirtualStack**)Malloc((nuint)(sizeof(VirtualStack*) * Environment.ProcessorCount));
s_ppStackCapacity = Environment.ProcessorCount;
}
if (s_ppStackCount >= s_ppStackCapacity)
{
var pOld = s_ppStack;
var newCapacity = s_ppStackCapacity * 2;
var pNew = (VirtualStack**)Realloc(pOld, (nuint)(sizeof(VirtualStack*) * newCapacity));
s_ppStack = pNew;
s_ppStackCapacity = newCapacity;
}
s_ppStack[s_ppStackCount] = (VirtualStack*)Unsafe.AsPointer(ref t_stackAllocator.Allocator);
var test = s_ppStack[s_ppStackCount];
s_ppStackCount++;
}
finally
{
if (token)
{
s_stackLocker.Exit();
}
}
}
}
public static void Initialize(AllocationManagerInitOpts opts)
{
@@ -510,19 +274,22 @@ public static unsafe class AllocationManager
s_allocations = new ConcurrentSlotMap<AllocationInfo>(256);
#endif
var ptr = (byte*)Malloc((nuint)(sizeof(ArenaAllocator) + sizeof(HeapAllocator) + sizeof(StackAllocator) + sizeof(FreeListAllocator)));
s_arenaAllocator = new MemoryPool<VirtualArena, VirtualArena.CreationOptions>(new VirtualArena.CreationOptions
{
reserveCapacity = opts.ArenaCapacity
});
s_pArenaAllocator = (ArenaAllocator*)ptr;
s_pHeapAllocator = (HeapAllocator*)(ptr + sizeof(ArenaAllocator));
s_pStackAllocator = (StackAllocator*)(ptr + sizeof(ArenaAllocator) + sizeof(HeapAllocator));
s_pFreeListAllocator = (FreeListAllocator*)(ptr + sizeof(ArenaAllocator) + sizeof(HeapAllocator) + sizeof(StackAllocator));
s_freeListAllocator = new MemoryPool<FreeList, FreeList.CreationOptions>(new FreeList.CreationOptions
{
alignment = opts.FreeListDefaultAlignment,
chunkSize = opts.FreeListChunkSize,
maxConcurrencyLevel = opts.FreeListConcurrencyLevel
});
s_pArenaAllocator->Init(opts.ArenaCapacity);
s_pHeapAllocator = (HeapAllocator*)Malloc((nuint)(sizeof(HeapAllocator)));
s_pHeapAllocator->Init();
s_pStackAllocator->Init();
s_pFreeListAllocator->Init(opts.FreeListConcurrencyLevel);
s_threadLocalStackDefaultSize = opts.StackCapacity;
s_threadLocalStackSize = opts.StackCapacity;
s_initialized = true;
}
@@ -540,91 +307,13 @@ public static unsafe class AllocationManager
return allocator switch
{
Allocator.Temp => s_pArenaAllocator->Handle,
Allocator.Temp => s_arenaAllocator.AllocationHandle,
Allocator.Persistent => s_pHeapAllocator->Handle,
Allocator.FreeList => s_pFreeListAllocator->Handle,
Allocator.FreeList => s_freeListAllocator.AllocationHandle,
_ => throw new ArgumentException("Target allocator type does not support custom allocation.", nameof(allocator)),
};
}
/// <summary>
/// Allocates a block of memory from the heap with the specified newSize and alignment, using the given allocation options.
/// </summary>
/// <param name="size">The number of bytes to allocate. Must be greater than zero.</param>
/// <param name="alignment">The alignment, in bytes, for the allocated memory block. Must be a power of two.</param>
/// <param name="allocationOption">An optional set of flags that control allocation behavior, such as whether the memory should be cleared or
/// tracked. The default is <see cref="AllocationOption.None"/>.</param>
/// <returns>A pointer to the beginning of the allocated memory block.</returns>
/// <exception cref="OutOfMemoryException">Thrown if the allocation fails.</exception>
public static void* HeapAlloc(nuint size, nuint alignment, AllocationOption allocationOption
#if MHP_ENABLE_SAFETY_CHECKS
, MemoryHandle* pHandle
#endif
)
{
Debug.Assert(s_initialized, "AllocationManager is not initialized.");
var ptr = AlignedAlloc(size, alignment);
if (ptr == null)
{
#if MHP_ENABLE_SAFETY_CHECKS
*pHandle = MemoryHandle.Invalid;
#endif
return null;
}
if (allocationOption.HasFlag(AllocationOption.Clear))
{
MemClear(ptr, size);
}
#if MHP_ENABLE_SAFETY_CHECKS
*pHandle = AddAllocation(ptr, size);
#endif
return ptr;
}
/// <summary>
/// Releases a block of unmanaged memory previously allocated by the heap allocator.
/// </summary>
/// <param name="ptr">A pointer to the memory block to be freed. The pointer must have been returned by a compatible heap allocation
/// method and must not be null.</param>
/// <param name="handle">The handle representing the memory allocation to free. The handle must be valid and previously allocated.</param>
public static void HeapFree(void* ptr
#if MHP_ENABLE_SAFETY_CHECKS
, MemoryHandle handle
#endif
)
{
Debug.Assert(s_initialized, "AllocationManager is not initialized.");
AlignedFree(ptr);
#if MHP_ENABLE_SAFETY_CHECKS
RemoveAllocation(handle);
#endif
}
/// <summary>
/// Releases a block of unmanaged memory previously allocated by the heap allocator.
/// </summary>
/// <remarks>
/// No ops when MHP_ENABLE_SAFETY_CHECKS is disabled, as we cannot fetch the allocation info from the handle to get the pointer to free.
/// </remarks>
/// <param name="handle">The handle representing the memory allocation to free. The handle must be valid and previously allocated.</param>
public static void HeapFree(MemoryHandle handle)
{
#if MHP_ENABLE_SAFETY_CHECKS
Debug.Assert(s_initialized, "AllocationManager is not initialized.");
if (TryGetAllocation(handle, out var info))
{
HeapFree((void*)info.Address, handle);
}
#endif
// No-op when safety checks are disabled, as we cannot fetch the allocation info from the handle.
}
/// <summary>
/// Resets the temporary memory allocator, clearing all allocated memory.
/// </summary>
@@ -632,7 +321,7 @@ public static unsafe class AllocationManager
public static void ResetTempAllocator()
{
Debug.Assert(s_initialized, "AllocationManager is not initialized.");
s_pArenaAllocator->Reset();
s_arenaAllocator.Allocator.Reset();
}
/// <summary>
@@ -643,7 +332,9 @@ public static unsafe class AllocationManager
public static VirtualStack.Scope CreateStackScope()
{
Debug.Assert(s_initialized, "AllocationManager is not initialized.");
return StackAllocator.CreateScope(s_pStackAllocator);
EnsureThreadLocalStackInitialize();
return t_stackAllocator.Allocator.CreateScope(t_stackAllocator.AllocationHandle);
}
/// <summary>
@@ -677,6 +368,24 @@ public static unsafe class AllocationManager
#endif
}
public static void UpdateAllocation(MemoryHandle handle, void* newPtr, nuint newSize)
{
#if MHP_ENABLE_SAFETY_CHECKS
Debug.Assert(s_initialized, "AllocationManager is not initialized.");
if (s_allocations.TryGetElement(handle.ID, handle.Generation, out var oldInfo))
{
var newInfo = oldInfo with
{
Address = (IntPtr)newPtr,
Size = newSize
};
s_allocations.UpdateElement(handle.ID, handle.Generation, newInfo);
}
#endif
}
/// <summary>
/// Removes the memory allocation associated with the specified handle.
/// </summary>
@@ -749,7 +458,7 @@ public static unsafe class AllocationManager
{
#if MHP_ENABLE_SAFETY_CHECKS
Debug.Assert(s_initialized, "AllocationManager is not initialized.");
nuint total = 0;
foreach (var allocation in s_allocations)
{
@@ -781,10 +490,29 @@ public static unsafe class AllocationManager
}
#endif
s_pArenaAllocator->Dispose();
s_pStackAllocator->Dispose();
s_pFreeListAllocator->Dispose();
s_arenaAllocator.Dispose();
s_freeListAllocator.Dispose();
Free(s_pArenaAllocator);
if (s_ppStack != null)
{
for (var i = 0; i < s_ppStackCount; i++)
{
var pStack = s_ppStack[i];
if (pStack != null)
{
pStack->Dispose();
Free(pStack);
}
}
Free(s_ppStack);
s_ppStack = null;
}
if (s_pHeapAllocator != null)
{
Free(s_pHeapAllocator);
s_pHeapAllocator = null;
}
}
}

View File

@@ -16,17 +16,20 @@ public enum AllocationOption : byte
public enum Allocator : byte
{
// Make the first allocator as invalid because we don't want to user create a default collection without passing any parameters
/// <summary>
/// The invalid allocator. This value is reserved and should not be used for actual memory allocations. It can be used to indicate an uninitialized or invalid state in allocation scenarios.
/// </summary>
Invalid,
/// <summary>
/// Allocator for temporary allocations. Allocations are automatically released after use automatically.
/// </summary>
Temp,
/// <summary>
/// Allocator for persistent allocations using a free list. Allocations are not automatically released after use, but can be reused to reduce fragmentation, system call and improve performance.
/// </summary>
FreeList,
/// <summary>
/// Allocator for persistent allocations. Allocations are not automatically released after use.
/// </summary>
Persistent,
/// <summary>
/// Allocator for persistent allocations using a free list. Allocations are not automatically released after use, but can be reused to reduce fragmentation and improve performance.
/// </summary>
FreeList
}

View File

@@ -1,3 +1,4 @@
using Misaki.HighPerformance.LowLevel.Utilities;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@@ -158,7 +159,7 @@ public unsafe struct Arena : IMemoryAllocator<Arena, Arena.CreationOptions>
return;
}
Free(_buffer);
MemoryUtility.Free(_buffer);
_buffer = null;
_size = 0;

View File

@@ -1,3 +1,4 @@
using Misaki.HighPerformance.LowLevel.Utilities;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@@ -187,7 +188,7 @@ public unsafe struct DynamicArena : IMemoryAllocator<DynamicArena, DynamicArena.
{
var next = current->next;
current->arena.Dispose();
Free(current);
MemoryUtility.Free(current);
current = next;
}

View File

@@ -1,3 +1,4 @@
using Misaki.HighPerformance.LowLevel.Utilities;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@@ -728,13 +729,13 @@ public unsafe struct FreeList : IMemoryAllocator<FreeList, FreeList.CreationOpti
if (_caches != null)
{
Free(_caches);
MemoryUtility.Free(_caches);
_caches = null;
}
if (_instanceId != null)
{
Free(_instanceId);
MemoryUtility.Free(_instanceId);
_instanceId = null;
}

View File

@@ -0,0 +1,318 @@
using Misaki.HighPerformance.LowLevel.Utilities;
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace Misaki.HighPerformance.LowLevel.Buffer;
public unsafe struct MemoryBlock : IDisposable
{
private void* _buffer;
private nuint _size;
private nuint _alignment;
private MemoryHandle _memoryHandle;
private AllocationHandle _allocationHandle;
public readonly nuint Size => _size;
public readonly nuint Alignment => _alignment;
public readonly bool IsCreated
{
get
{
#if MHP_ENABLE_SAFETY_CHECKS
if (_buffer != null)
{
if (_allocationHandle.IsValid != null)
{
return _allocationHandle.IsValid(_allocationHandle.State, _memoryHandle);
}
else
{
return true;
}
}
return false;
#else
return _buffer != null;
#endif
}
}
public MemoryBlock()
: this(0, 0, Allocator.Invalid)
{
}
public MemoryBlock(nuint size, nuint alignment, AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None)
{
ArgumentOutOfRangeException.ThrowIfNegative(size);
if (handle.Alloc == null)
{
throw new InvalidOperationException("Target allocation handle does not support allocation.");
}
#if MHP_ENABLE_SAFETY_CHECKS
MemoryHandle memHandle;
#endif
_buffer = 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;
}
/// <summary>
/// Initializes a new instance of UnsafeArray with a specified number of elements and an allocation type.
/// </summary>
/// <param name="count">Specifies the number of elements to allocate in the array, which must be greater than zero.</param>
/// <param name="allocator">Specifies the allocator to use for memory allocation, which determines the memory management strategy.</param>
/// <param name="allocationOption">Determines how the memory should be allocated.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the specified number of elements is less than or equal to zero.</exception>
public MemoryBlock(nuint size, nuint alignment, Allocator allocator, AllocationOption allocationOption = AllocationOption.None)
: this(size, alignment, AllocationManager.GetAllocationHandle(allocator), allocationOption)
{
}
/// <summary>
/// Initializes an UnsafeArray with a pointer to a buffer and a count of elements. This does not copy the data.
/// </summary>
/// <param name="buffer">A pointer to the memory location that holds the elements of the array.</param>
/// <param name="count">The total size of the data.</param>
/// <remarks>
/// When using this constructor, the user is responsible for managing the memory pointed to by the buffer.
/// Disposing of the UnsafeArray does not free the memory and only release the reference. The memory should be freed manually when no longer needed.
/// Use <see cref="UnsafeArray(int, Allocator, AllocationOption)"/> constructor and <see cref="MemCpy(void*, void*, nuint)"/> if you are not sure what you are doing.
/// </remarks>
public MemoryBlock(void* buffer, uint size)
{
_buffer = buffer;
_size = size;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly ref T GetElementAt<T>(nuint index)
where T : unmanaged
{
#if MHP_ENABLE_SAFETY_CHECKS
if (index * (uint)sizeof(T) >= _size)
{
throw new IndexOutOfRangeException($"Index {index} is out of range for collection of size {_size / (uint)sizeof(T)}.");
}
#endif
return ref UnsafeUtility.ReadArrayElementRef<T>(_buffer, index);
}
/// <inheritdoc/>
public void Resize(uint newSize, AllocationOption option = AllocationOption.None)
{
if (newSize == _size)
{
return;
}
#if MHP_ENABLE_SAFETY_CHECKS
var memHandle = _memoryHandle;
#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/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void Clear()
{
MemClear(_buffer, _size);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void* GetUnsafePtr()
{
return _buffer;
}
/// <summary>
/// Copies elements from an untyped source collection to a destination span of a specific type.
/// </summary>
/// <typeparam name="T">Specifies the type of elements in the destination span, which must be unmanaged.</typeparam>
/// <param name="destination">The typed span where the data will be copied to.</param>
/// <exception cref="ArgumentException">Thrown when the source collection size exceeds the destination span capacity.</exception>
public readonly void CopyTo<C, T>(Span<T> destination)
where T : unmanaged
{
var destSize = (uint)destination.Length * (uint)sizeof(T);
if (_size > destSize)
{
throw new ArgumentException("Source collection is larger than the destination span.");
}
fixed (T* pDest = destination)
{
MemCpy(pDest, _buffer, _size);
}
}
/// <summary>
/// Copies a range of bytes from an untyped source collection to a destination span, interpreting the bytes as elements of type T.
/// </summary>
/// <typeparam name="T">Specifies the type of elements in the destination span, which must be unmanaged.</typeparam>
/// <param name="destination">The typed span where the elements will be placed.</param>
/// <param name="sourceOffset">The byte offset in the source collection from which to start copying.</param>
/// <param name="destinationIndex">The element index in the destination span where copying will begin.</param>
/// <param name="length">The number of elements of type T to copy.</param>
/// <exception cref="ArgumentException">Thrown when the specified range exceeds the bounds of the source collection or destination span.</exception>
public readonly void CopyTo<T>(Span<T> destination, uint sourceOffset, uint destinationIndex, uint length)
where T : unmanaged
{
var sizeOfElement = (uint)sizeof(T);
if (sourceOffset + (length * sizeOfElement) > _size || destinationIndex + length > destination.Length)
{
throw new ArgumentException("Source collection or destination span is too small for the specified range.");
}
fixed (T* pDest = destination)
{
MemCpy(pDest + destinationIndex, (byte*)_buffer + sourceOffset, length * sizeOfElement);
}
}
/// <summary>
/// Copies elements from a typed source span to an untyped destination collection.
/// </summary>
/// <typeparam name="T">Specifies the type of elements in the source span, which must be unmanaged.</typeparam>
/// <param name="source">The typed span containing the elements to be copied.</param>
/// <exception cref="ArgumentException">Thrown when the destination collection is smaller than the source span data size.</exception>
public readonly void CopyFrom<T>(ReadOnlySpan<T> source)
where T : unmanaged
{
var sourceSize = (uint)(source.Length * sizeof(T));
if (_size < sourceSize)
{
throw new ArgumentException("Destination collection is smaller than the source span.");
}
fixed (T* pSrc = source)
{
MemCpy(_buffer, pSrc, sourceSize);
}
}
/// <summary>
/// Copies a range of elements from a typed source span to an untyped destination collection at a specified byte offset.
/// </summary>
/// <typeparam name="T">Specifies the type of elements in the source span, which must be unmanaged.</typeparam>
/// <param name="source">The typed span containing the elements to be copied.</param>
/// <param name="sourceIndex">The starting element index in the source span from which to begin copying.</param>
/// <param name="destinationOffset">The byte offset in the destination collection where the data will be placed.</param>
/// <param name="length">The number of elements to copy from the source span.</param>
/// <exception cref="ArgumentException">Thrown when the specified range exceeds the bounds of the source span or destination collection.</exception>
public readonly void CopyFrom<T>(ReadOnlySpan<T> source, uint sourceIndex, uint destinationOffset, uint length)
where T : unmanaged
{
var sizeOfElement = (uint)sizeof(T);
if (sourceIndex + length > source.Length || destinationOffset + (length * sizeOfElement) > _size)
{
throw new ArgumentException("Source span or destination collection is too small for the specified range.");
}
fixed (T* pSrc = source)
{
MemCpy((byte*)_buffer + destinationOffset, pSrc + sourceIndex, length * sizeOfElement);
}
}
/// <summary>
/// Converts into a Span for efficient memory access.
/// </summary>
/// <returns>A <see cref="Span{T}"/> that provides a view over the elements of the UnsafeCollection.</returns>
public readonly Span<T> AsSpan<T>()
where T : unmanaged
{
Debug.Assert(_size % (uint)sizeof(T) == 0, "The size of the collection must be a multiple of the size of the element type.");
return new Span<T>(_buffer, (int)_size / sizeof(T));
}
/// <summary>
/// Creates a span over a contiguous region of elements in the specified unsafe collection, starting at the given index and covering the specified number of elements.
/// </summary>
/// <param name="start">The zero-based index of the first element in the collection to include in the span. Must be greater than or equal to zero and less than the number of elements in the collection.</param>
/// <param name="length">The number of elements to include in the span. Must be greater than or equal to zero and the range defined by
/// <paramref name="start"/> and <paramref name="length"/> must not exceed the bounds of the collection.</param>
/// <returns>A <see cref="Span{T}"/> representing the specified region of the collection.</returns>
public readonly Span<T> AsSpan<T>(int start, int length)
where T : unmanaged
{
if (start < 0 || length < 0 || (nuint)(start + length) * (nuint)sizeof(T) > _size)
{
throw new ArgumentOutOfRangeException(nameof(start), "The specified range is out of bounds of the collection.");
}
return new Span<T>((T*)_buffer + start, length);
}
/// <inheritdoc/>
public void Dispose()
{
if (!IsCreated)
{
#if DEBUG
if (_buffer == null)
{
return;
}
var message = "The UnTypedArray is not created or already disposed.";
#if MHP_ENABLE_STACKTRACE
var stackTrace = new StackTrace(1, true);
var sb = new System.Text.StringBuilder();
foreach (var frame in stackTrace.GetFrames())
{
var fileName = frame?.GetFileName();
if (frame != null)
{
var methodInfo = DiagnosticMethodInfo.Create(frame);
sb.AppendLine($"File: {fileName}, Type: {methodInfo?.DeclaringTypeName}, Method: {methodInfo?.Name}, Line: {frame.GetFileLineNumber()}");
}
}
message += Environment.NewLine + sb.ToString();
#endif
Debug.WriteLine(message);
#endif
return;
}
if (_allocationHandle.Free != null)
{
_allocationHandle.Free(_allocationHandle.State, _buffer
#if MHP_ENABLE_SAFETY_CHECKS
, _memoryHandle
#endif
);
}
_buffer = null;
_size = 0;
_alignment = 0;
}
}

View File

@@ -1,20 +1,21 @@
using Misaki.HighPerformance.LowLevel.Utilities;
using System.Runtime.CompilerServices;
namespace Misaki.HighPerformance.LowLevel.Buffer;
public unsafe struct MemoryPool<T, TOpts> : IDisposable
where T : unmanaged, IMemoryAllocator<T, TOpts>
public unsafe struct MemoryPool<TAllocator, TOpts> : IDisposable
where TAllocator : unmanaged, IMemoryAllocator<TAllocator, TOpts>
{
private T* _pAllocator;
private TAllocator* _pAllocator;
private AllocationHandle _allocationHandle;
public readonly ref T Allocator => ref *_pAllocator;
public readonly ref TAllocator Allocator => ref Unsafe.AsRef<TAllocator>(_pAllocator);
public readonly AllocationHandle AllocationHandle => _allocationHandle;
public MemoryPool(in TOpts opts)
{
_pAllocator = (T*)Malloc((nuint)sizeof(T));
*_pAllocator = T.Create(opts);
_pAllocator = (TAllocator*)Malloc((nuint)sizeof(TAllocator));
*_pAllocator = TAllocator.Create(opts);
_allocationHandle = new AllocationHandle
{
@@ -32,7 +33,14 @@ public unsafe struct MemoryPool<T, TOpts> : IDisposable
#endif
)
{
return ((T*)pAllocator)->Allocate(size, alignment, allocationOption);
var ptr = ((TAllocator*)pAllocator)->Allocate(size, alignment, allocationOption);
#if MHP_ENABLE_SAFETY_CHECKS
if (ptr != null)
{
*pHandle = AllocationManager.AddAllocation(ptr, size);
}
#endif
return ptr;
}
private static void* Reallocate(void* pAllocator, void* ptr, nuint oldSize, nuint newSize, nuint alignment, AllocationOption allocationOption
@@ -41,7 +49,25 @@ public unsafe struct MemoryPool<T, TOpts> : IDisposable
#endif
)
{
return ((T*)pAllocator)->Reallocate(ptr, oldSize, newSize, alignment, allocationOption);
var newPtr = ((TAllocator*)pAllocator)->Reallocate(ptr, oldSize, newSize, alignment, allocationOption);
#if MHP_ENABLE_SAFETY_CHECKS
if (ptr == null && newPtr != null)
{
*pHandle = AllocationManager.AddAllocation(newPtr, newSize);
}
else
{
if (newPtr == null)
{
AllocationManager.RemoveAllocation(*pHandle);
}
else
{
AllocationManager.UpdateAllocation(*pHandle, newPtr, newSize);
}
}
#endif
return newPtr;
}
private static void Free(void* pAllocator, void* ptr
@@ -50,7 +76,10 @@ public unsafe struct MemoryPool<T, TOpts> : IDisposable
#endif
)
{
((T*)pAllocator)->Free(ptr);
((TAllocator*)pAllocator)->Free(ptr);
#if MHP_ENABLE_SAFETY_CHECKS
AllocationManager.RemoveAllocation(handle);
#endif
}
public void Dispose()

View File

@@ -1,3 +1,4 @@
using Misaki.HighPerformance.LowLevel.Utilities;
using System.Collections;
using System.Runtime.CompilerServices;
@@ -197,7 +198,7 @@ public unsafe partial struct Stack : IMemoryAllocator<Stack, Stack.CreationOptio
return;
}
Free(_buffer);
MemoryUtility.Free(_buffer);
_buffer = null;
_size = 0;

View File

@@ -83,6 +83,12 @@ public unsafe struct VirtualStack : IMemoryAllocator<VirtualStack, VirtualStack.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Scope CreateScope(AllocationHandle handle)
{
#if MHP_ENABLE_SAFETY_CHECKS
if (_baseAddress == null)
{
throw new InvalidOperationException("Allocator must be initialized before creating a scope.");
}
#endif
return new Scope((VirtualStack*)Unsafe.AsPointer(ref this), handle);
}
@@ -151,6 +157,13 @@ public unsafe struct VirtualStack : IMemoryAllocator<VirtualStack, VirtualStack.
public void* Reallocate(void* ptr, nuint oldSize, nuint newSize, nuint alignment, AllocationOption allocationOption)
{
#if MHP_ENABLE_SAFETY_CHECKS
if (_activeScopeCount == 0)
{
throw new InvalidOperationException("Allocations can only be made within an active memory scope.");
}
#endif
if (_baseAddress == null)
{
return null;
@@ -193,6 +206,8 @@ public unsafe struct VirtualStack : IMemoryAllocator<VirtualStack, VirtualStack.
{
MemClear(_baseAddress + _allocatedOffset - diff, diff);
}
return ptr;
}
var newPtr = Allocate(newSize, alignment, allocationOption);