using System.Runtime.InteropServices;
using Brutal.Pointers;
using Brutal.Pointers.Extensions;
namespace Brutal.Memory;
///
/// Memory management utilities for unmanaged memory operations.
///
public static class Mem
{
#region Byte
///
/// Creates a new allocation for a specified number of bytes
///
public static unsafe Ptr Alloc(ByteSize byteSize)
{
return NativeMemory.Alloc((nuint)byteSize);
}
///
/// Creates a new allocation for a specified number of elements of a specified size
///
public static unsafe Ptr Alloc(ElementCount count, ByteSize stride)
{
return NativeMemory.Alloc((nuint)count, (nuint)stride);
}
///
/// Creates a new zeroed allocation for a specified number of bytes
///
public static unsafe Ptr AllocZeroed(ByteSize byteSize)
{
return NativeMemory.AllocZeroed((nuint)byteSize);
}
///
/// Creates a new zeroed allocation for a specified number of elements of a specified size
///
public static unsafe Ptr AllocZeroed(ElementCount count, ByteSize stride)
{
return NativeMemory.AllocZeroed((nuint)count, (nuint)stride);
}
///
/// Copies a specified number of bytes from source to destination
///
public static unsafe void Copy(Ptr source, Ptr destination, ByteSize byteSize)
{
NativeMemory.Copy(source, destination, (nuint)byteSize);
}
///
/// Fills a specified number of bytes at destination with value
///
public static unsafe void Fill(Ptr destination, ByteSize byteSize, byte value)
{
NativeMemory.Fill(destination, (nuint)byteSize, value);
}
///
/// Clears a specified number of bytes at destination
///
public static unsafe void Clear(Ptr destination, ByteSize byteSize)
{
NativeMemory.Clear(destination, (nuint)byteSize);
}
///
/// Reallocates the given pointer to hold a specified number of bytes
///
public static unsafe Ptr Realloc(Ptr source, ByteSize byteSize)
{
return NativeMemory.Realloc(source, (nuint)byteSize);
}
#endregion
///
/// Creates a new allocation for a single instance of T and copies val into it
///
public static Ptr AllocAndCopy(T val)
where T : unmanaged
{
var ptr = Alloc();
ptr.Value = val;
return ptr;
}
///
/// Creates a new allocation for a single instance of T
///
public static Ptr Alloc()
where T : unmanaged
{
return Alloc(ByteSize.Of()).Cast();
}
///
/// Creates a new allocation for elementCount instances of T and returns it as a span
///
public static Span Alloc(ElementCount count)
where T : unmanaged
{
return Alloc(count, ByteSize.Of()).AsSpan(count);
}
///
/// Creates a new zeroed allocation for a single instance of T
///
public static Ptr AllocZeroed()
where T : unmanaged
{
return AllocZeroed(ByteSize.Of()).Cast();
}
///
/// Creates a new zeroed allocation for elementCount instances of T and returns it as a span
///
public static Span AllocZeroed(ElementCount count)
where T : unmanaged
{
return AllocZeroed(count, ByteSize.Of()).AsSpan(count);
}
///
/// Reallocates the given pointer to hold numElements instances of T
///
public static Ptr Realloc(Ptr source, ElementCount count)
where T : unmanaged
{
return Realloc(source, ByteSize.Of(count)).Cast();
}
public static void Clear(Ptr source, ElementCount count)
where T : unmanaged
{
Clear(source, ByteSize.Of(count));
}
public static unsafe void Clear(ReadOnlySpan span)
where T : unmanaged
{
fixed (T* pSpan = span)
Clear(pSpan, ByteSize.Of(span));
}
///
/// Frees a previously allocated pointer
///
public static unsafe void Free(Ptr ptr)
{
NativeMemory.Free(ptr);
}
///
/// Frees a previously allocated span
///
public static unsafe void Free(Span ptr)
where T : unmanaged
{
NativeMemory.Free(ptr.AsPointer());
}
///
/// Copies elementCount elements of type T from source to destination
///
public static void Copy(Ptr source, Ptr destination, ElementCount count)
where T : unmanaged
{
Copy(source.AsSpan(count), destination.AsSpan(count));
}
///
/// Creates a new allocation for values.Length instances of T, copies the values into it, and returns it as a span
///
public static Span AllocAndCopy(ReadOnlySpan values)
where T : unmanaged
{
var memory = Alloc(values.Length);
values.CopyTo(memory);
return memory;
}
///
/// Creates a new allocation for values.Length instances of T, copies the values into it, and returns it as a span
///
public static Span AllocAndCopy(Span values)
where T : unmanaged
{
return AllocAndCopy((ReadOnlySpan)values);
}
///
/// Copies the elements of source to destination
///
/// If the destination span is not long enough to hold the source span
public static unsafe void Copy(ReadOnlySpan source, Span destination)
where T : unmanaged
{
if (source.Length > destination.Length)
throw new ArgumentException("Destination span is not long enough to hold the source span.");
fixed (T* pSource = source)
fixed (T* pDestination = destination)
{
NativeMemory.Copy(pSource, pDestination, (nuint)ByteSize.Of(source.Length));
}
}
///
/// Copies the elements of source to destination
///
/// If the destination span is not long enough to hold the source span
public static void Copy(Span source, Span destination)
where T : unmanaged
{
Copy((ReadOnlySpan)source, destination);
}
}