This commit is contained in:
Rian Drake 2026-10-15 14:09:11 +13:00
commit b578433e37
3 changed files with 784842 additions and 0 deletions

219
Brutal.Memory/Mem.cs Normal file
View file

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

View file

@ -0,0 +1,28 @@
namespace Brutal.Memory;
/// <summary>
/// Interface for an object that has a size (Eg: a buffer)
/// </summary>
public interface ISize
{
/// <summary>
/// Size of the object's internals
/// </summary>
public int Size { get; }
}
/// <summary>
/// Interface for an object that has a 2D size (Eg: a texture)
/// </summary>
public interface ISize2D
{
/// <summary>
/// Width of the object
/// </summary>
public int Width { get; }
/// <summary>
/// Height of the object
/// </summary>
public int Height { get; }
}

141
Brutal.Memory/PtrOwner.cs Normal file
View file

@ -0,0 +1,141 @@
using Brutal.Attributes;
using Brutal.Pointers;
using Brutal.Pointers.Extensions;
namespace Brutal.Memory;
/// <summary>
/// A variant of Ptr that owns the memory and frees it automatically when disposed or finalized
/// </summary>
/// <typeparam name="TValueType">The struct type we want to create unmanaged memory pointing to</typeparam>
public class PtrOwner<TValueType> : IDisposable, INullable
where TValueType : unmanaged
{
/// <summary>
/// The owned pointer.
/// </summary>
public Ptr<TValueType> Ptr { get; private set; }
/// <summary>
/// Get the value of the pointer as a reference
/// </summary>
public ref TValueType Value => ref Ptr.Value;
public PtrOwner(TValueType value)
{
Ptr = Mem.AllocAndCopy(value);
}
public PtrOwner(Ptr<TValueType> ptr)
{
Ptr = ptr;
}
/// <summary>
/// Dispose resources, freeing the memory and preventing further use.
/// </summary>
/// <remarks>
/// ONLY call this if you are sure you don't need the PtrOwner anymore.
/// </remarks>
public void Dispose()
{
Mem.Free(Ptr);
Ptr = null;
GC.SuppressFinalize(this);
}
/// <summary>
/// A finalizer that frees the memory, in case the object was not disposed.
/// </summary>
~PtrOwner()
{
Mem.Free(Ptr);
}
/// <inheritdoc />
public bool IsNull()
{
return Ptr.IsNull();
}
/// <summary>
/// Returns a new <see cref="Ptr{TValueType}"/> that points to the same memory as this one, but with an offset in elements of <typeparamref name="TValueType"/>.
/// </summary>
public Ptr<TValueType> Offset(int count)
{
return Ptr.Offset(count);
}
/// <summary>
/// Convert the pointer to a span of type TValueType with the given length.
/// </summary>
[return: Fixed]
public Span<TValueType> AsSpan(int length)
{
return Ptr.AsSpan(length);
}
/// <summary>
/// Gets a reference to the element at the specified index.
/// </summary>
public ref TValueType this[int index] => ref Ptr[index];
/// <summary>
/// Swap the pointer with another one.
/// </summary>
/// <param name="newPointer"></param>
/// <returns></returns>
public Ptr<TValueType> Swap(Ptr<TValueType> newPointer)
{
var oldPointer = Ptr;
Ptr = newPointer;
return oldPointer;
}
/// <summary>
/// Release the pointer and return it to the caller.
/// </summary>
/// <remarks>
/// If you don't need the PtrOwner anymore, call Dispose() instead.
/// </remarks>
public Ptr<TValueType> Release()
{
return Swap(null);
}
/// <summary>
/// Frees any existing pointer and resets it to the given value.
/// </summary>
/// <remarks>
/// This PtrOwner will now own the memory of the given value.
/// </remarks>
public void Reset(Ptr<TValueType> newValue = default)
{
Release();
Swap(newValue);
}
/// <summary>
/// Implicit cast to <see cref="Ptr{TValueType}"/>.
/// </summary>
public static implicit operator Ptr<TValueType>(PtrOwner<TValueType>? inPtrOwner)
{
return inPtrOwner?.Ptr ?? null;
}
/// <summary>
/// Implicit cast to <typeparamref name="TValueType"/>*.
/// </summary>
public static unsafe implicit operator TValueType*(PtrOwner<TValueType>? inPtrOwner)
{
return inPtrOwner?.Ptr ?? null;
}
/// <summary>
/// Explicit cast to nint.
/// </summary>
public static explicit operator nint(PtrOwner<TValueType> ptr)
{
return (nint)ptr.Ptr;
}
}