Files
Misaki.HighPerformance/Misaki.HighPerformance.LowLevel/Buffer/MemoryPool.cs
Misaki 69b054e81d feat(lowlevel): add VirtualStack, update allocators, docs
Introduce VirtualStack allocator, refactor memory management to use virtual memory stacks, and update documentation.

Added VirtualStack as a new stack allocator using virtual memory, replaced Stack with VirtualStack in allocation manager and related APIs, and updated TempJobAllocator to use VirtualArena. Introduced AllocationManagerInitOpts for allocator configuration. Replaced ENABLE_COLLECTION_CHECKS with ENABLE_SAFETY_CHECKS for safety checks. Removed Result.cs and updated project files and examples. Added comprehensive README files for all major packages and improved root documentation.

BREAKING CHANGE: Stack allocator replaced by VirtualStack; TempJobAllocator and AllocationManager initialization signatures changed; Result types removed.
2026-03-19 15:38:23 +09:00

72 lines
2.0 KiB
C#

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);
}
var newPtr = Allocate(pAllocator, newSize, alignment, allocationOption, pHandle);
if (newPtr == null)
{
return null;
}
MemCpy(newPtr, ptr, Math.Min(oldSize, newSize));
Free(pAllocator, ptr, *pHandle);
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;
}
}