Improved SegmentedPool with the introduction of handles and alignment (#1)

This commit is contained in:
Jim
2026-03-20 17:33:30 +00:00
parent dfbdf905fe
commit 8bf297a244
15 changed files with 2788 additions and 674 deletions

View File

@@ -3,14 +3,36 @@
namespace UnmanagedMMU.Allocators
{
/// <summary>
/// Wrapper class around <see cref="NativeMemory.Alloc(nuint)"/> and <see cref="NativeMemory.Free(void*)"/>.
/// Wrapper class around <see cref="NativeMemory.AlignedAlloc(nuint, nuint)"/> and <see cref="NativeMemory.AlignedFree(void*)"/>.
/// </summary>
internal sealed unsafe class DefaultUnmanagedAllocator : IUnmanagedAllocator
{
/// <inheritdoc/>
public void* Alloc(nuint size) => NativeMemory.Alloc(size);
public void* Alloc(nuint size)
{
return NativeMemory.AlignedAlloc(size, 16);
}
/// <inheritdoc/>
public void Free(void* ptr) => NativeMemory.Free(ptr);
public void* AllocAligned(nuint size, nuint alignment)
{
if (!((alignment & (alignment - 1)) == 0 && alignment > 0))
{
throw new ArgumentException("Alignment must be a power of 2.", nameof(alignment));
}
return NativeMemory.AlignedAlloc(size, alignment);
}
/// <inheritdoc/>
public void Free(void* ptr)
{
NativeMemory.Free(ptr);
}
/// <inheritdoc/>
public void FreeAligned(void* ptr, nuint alignment = 0)
{
NativeMemory.AlignedFree(ptr);
}
}
}