Files
UnmanagedMMU/UnmangedMMU/Allocators/DefaultUnmanagedAllocator.cs

39 lines
1.1 KiB
C#

using System.Runtime.InteropServices;
namespace UnmanagedMMU.Allocators
{
/// <summary>
/// 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)
{
return NativeMemory.AlignedAlloc(size, 16);
}
/// <inheritdoc/>
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);
}
}
}