SOURCE DUMP v1

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

View file

@ -0,0 +1,62 @@
using System.Runtime.InteropServices;
using Brutal.Attributes;
namespace Brutal.Pointers.Extensions;
/// <summary>
/// Extensions for Span and ReadOnlySpan as pointers
/// </summary>
public static class PointerExtensions
{
/// <summary>
/// Return the underlying pointer of span. Note this may be unsafe if the span refers to memory not allocated on the
/// native memory, as they may be relocated at any time due to generational Garbage Collections. Please review usage
/// to ensure only fixed native memory pointers are returned.
/// </summary>
/// <param name="span">Span of native memory</param>
/// <returns>Unsafe pointer to block of memory</returns>
public static unsafe T* AsPointer<T>(this Span<T> span)
where T : unmanaged
{
return (T*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(span));
}
/// <summary>
/// Return the underlying pointer of span. Note this may be unsafe if the span refers to memory not allocated on the
/// native memory, as they may be relocated at any time due to generational Garbage Collections. Please review usage
/// to ensure only fixed native memory pointers are returned.
/// </summary>
/// <param name="span">ReadOnlySpan of native memory</param>
/// <returns>Unsafe pointer to block of memory</returns>
public static unsafe T* AsPointer<T>(this ReadOnlySpan<T> span)
where T : unmanaged
{
return (T*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(span));
}
/// <summary>
/// Return the underlying pointer of span. Note this may be unsafe if the span refers to memory not allocated on the
/// native memory, as they may be relocated at any time due to generational Garbage Collections. Please review usage
/// to ensure only fixed native memory pointers are returned.
/// </summary>
/// <param name="span">Span of native memory</param>
/// <returns>Unsafe pointer to block of memory</returns>
public static unsafe Ptr<T> AsPtr<T>(this Span<T> span)
where T : unmanaged
{
return (T*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(span));
}
/// <summary>
/// Return the underlying pointer of span. Note this may be unsafe if the span refers to memory not allocated on the
/// native memory, as they may be relocated at any time due to generational Garbage Collections. Please review usage
/// to ensure only fixed native memory pointers are returned.
/// </summary>
/// <param name="span">ReadOnlySpan of native memory</param>
/// <returns>Unsafe pointer to block of memory</returns>
public static unsafe Ptr<T> AsPtr<T>([Fixed] this ReadOnlySpan<T> span)
where T : unmanaged
{
return (T*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(span));
}
}

View file

@ -0,0 +1,103 @@
using Brutal.Attributes;
namespace Brutal.Pointers;
/// <summary>
/// A wrapper for an untyped pointer.
/// </summary>
public readonly unsafe struct Ptr(void* pointer) : INullable, IEquatable<Ptr>
{
/// <summary>
/// The raw void pointer.
/// </summary>
public void* Pointer => pointer;
/// <summary>
/// Returns a new <see cref="Ptr"/>, offset by the given byte count.
/// </summary>
public Ptr Offset(ByteSize byteOffset)
{
return new Ptr((byte*)Pointer + byteOffset);
}
/// <summary>
/// Converts the pointer to a Span of the given type and length.
/// </summary>
[return: Fixed]
public Span<TValueType> AsSpan<TValueType>(ElementCount length)
{
return new Span<TValueType>(Pointer, length);
}
/// <summary>
/// Casts the pointer to a typed pointer.
/// </summary>
public Ptr<TCastType> Cast<TCastType>()
where TCastType : unmanaged
{
return new Ptr<TCastType>((TCastType*)Pointer);
}
/// <inheritdoc />
public bool IsNull()
{
return Pointer == null;
}
/// <summary>
/// Implicit conversion from void* to <see cref="Ptr"/>
/// </summary>
public static implicit operator Ptr(void* pointer)
{
return new Ptr(pointer);
}
/// <summary>
/// Implicit conversion from <see cref="Ptr"/> to void*.
/// </summary>
public static implicit operator void*(Ptr ptr)
{
return ptr.Pointer;
}
/// <summary>
/// Explicit conversion from nint to <see cref="Ptr"/>.
/// </summary>
public static explicit operator Ptr(nint ptr)
{
return new Ptr((void*)ptr);
}
/// <summary>
/// Explicit cast to nint.
/// </summary>
public static explicit operator nint(Ptr ptr)
{
return (nint)ptr.Pointer;
}
public bool Equals(Ptr other)
{
return Pointer == other.Pointer;
}
public override bool Equals(object? obj)
{
return obj is Ptr other && Equals(other);
}
public override int GetHashCode()
{
return ((nint)Pointer).GetHashCode();
}
public static bool operator ==(Ptr left, Ptr right)
{
return left.Equals(right);
}
public static bool operator !=(Ptr left, Ptr right)
{
return !left.Equals(right);
}
}

View file

@ -0,0 +1,130 @@
using Brutal.Attributes;
using Brutal.Pointers.Extensions;
namespace Brutal.Pointers;
/// <summary>
/// A wrapper for a typed pointer.
/// </summary>
public readonly unsafe struct Ptr<TValueType>(TValueType* pointer) : INullable
where TValueType : unmanaged
{
/// <summary>
/// The raw pointer of type <typeparamref name="TValueType"/>.
/// </summary>
public TValueType* Pointer => pointer;
/// <summary>
/// A reference to the first value at the pointer address.
/// </summary>
public ref TValueType Value => ref *Pointer;
/// <summary>
/// Returns a new <see cref="Ptr{T}"/>, offset by the given byte count.
/// </summary>
/// <param name="count"></param>
/// <returns></returns>
public Ptr<TValueType> Offset(ElementCount count)
{
return new Ptr<TValueType>(Pointer + count);
}
/// <summary>
/// Converts the pointer to a <see cref="Span{T}"/> of the given length.
/// </summary>
[return: Fixed]
public Span<TValueType> AsSpan(ElementCount length)
{
return new Span<TValueType>(Pointer, length);
}
/// <summary>
/// Converts the pointer to an untyped <see cref="Ptr"/>.
/// </summary>
public Ptr AsPtr()
{
return new Ptr(Pointer);
}
/// <summary>
/// Cast this pointer to a different type.
/// </summary>
/// <typeparam name="TCastType"></typeparam>
/// <returns></returns>
public Ptr<TCastType> Cast<TCastType>()
where TCastType : unmanaged
{
return new Ptr<TCastType>((TCastType*)Pointer);
}
/// <inheritdoc />
public bool IsNull()
{
return Pointer == null;
}
/// <summary>
/// Gets a reference to the value at the given index.
/// </summary>
public ref TValueType this[int index] => ref Pointer[index];
/// <summary>
/// Implicit conversion from a <see cref="Ptr{T}"/> to an untyped <see cref="Ptr"/>.
/// </summary>
public static implicit operator Ptr(Ptr<TValueType> ptr)
{
return ptr.AsPtr();
}
/// <summary>
/// Implicit conversion from a typed pointer to a <see cref="Ptr{T}"/>.
/// </summary>
public static implicit operator Ptr<TValueType>(TValueType* pointer)
{
return new Ptr<TValueType>(pointer);
}
/// <summary>
/// Implicit conversion from a <see cref="Ptr{T}"/> to <typeparamref name="TValueType"/>*.
/// </summary>
/// <param name="ptr"></param>
/// <returns></returns>
public static implicit operator TValueType*(Ptr<TValueType> ptr)
{
return ptr.Pointer;
}
/// <summary>
/// Implicit conversion from a <see cref="Ptr{T}"/> to void pointer.
/// </summary>
public static implicit operator void*(Ptr<TValueType> ptr)
{
return ptr.Pointer;
}
public static implicit operator Ptr<TValueType>([Fixed] ReadOnlySpan<TValueType> span)
{
return span.AsPtr();
}
public static implicit operator Ptr<TValueType>([Fixed] Span<TValueType> span)
{
return span.AsPtr();
}
/// <summary>
/// Explicit cast from nint to Ptr.
/// </summary>
public static explicit operator Ptr<TValueType>(nint ptr)
{
return new Ptr<TValueType>((TValueType*)ptr);
}
/// <summary>
/// Explicit cast to nint.
/// </summary>
public static explicit operator nint(Ptr<TValueType> ptr)
{
return (nint)ptr.Pointer;
}
}

View file

@ -0,0 +1,90 @@
using Brutal.Attributes;
namespace Brutal.Pointers;
/// <summary>
/// Wraps a pointer to a pointer.
/// </summary>
public readonly unsafe struct PtrPtr(void** pointerPointer) : INullable
{
/// <summary>
/// The pointer-pointer.
/// </summary>
public void** Pointer => pointerPointer;
/// <summary>
/// Convert the pointer to a span of pointers.
/// </summary>
[return: Fixed]
public Span<Ptr> AsSpan(ElementCount length)
{
// Ptr is equivalent to void*,
// So we can cast void** to Ptr*,
// Or so we will hope
return new Span<Ptr>((Ptr*)Pointer, length);
}
/// <summary>
/// Convert the pointer to a span of pointers.
/// </summary>
public Span<Ptr<T>> AsSpan<T>(ElementCount length)
where T : unmanaged
{
// Ptr<T> is equivalent to T*,
// So we can cast void** to Ptr<T>,
// Or so we will hope
return new Span<Ptr<T>>((Ptr<T>*)Pointer, length);
}
/// <inheritdoc />
public bool IsNull()
{
return Pointer == null;
}
/// <summary>
/// Get the value of the pointer as a reference.
/// </summary>
public ref void* Value => ref *Pointer;
/// <summary>
/// Access the pointer at the given index.
/// </summary>
public Ptr this[int index]
{
get => Pointer[index];
set => Pointer[index] = value.Pointer;
}
/// <summary>
/// Implicit conversion from void** to PtrPtr.
/// </summary>
public static implicit operator PtrPtr(void** address)
{
return new PtrPtr(address);
}
/// <summary>
/// Implicit conversion from PtrPtr to void**
/// </summary>
public static implicit operator void**(PtrPtr ptr)
{
return ptr.Pointer;
}
/// <summary>
/// Explicit conversion from nint to PtrPtr.
/// </summary>
public static explicit operator PtrPtr(nint ptr)
{
return new PtrPtr((void**)ptr);
}
/// <summary>
/// Explicit cast to nint.
/// </summary>
public static explicit operator nint(PtrPtr ptr)
{
return (nint)ptr.Pointer;
}
}

View file

@ -0,0 +1,84 @@
namespace Brutal.Pointers;
/// <summary>
/// Wrapper for a pointer to a pointer of type TValueType.
/// </summary>
public readonly unsafe struct PtrPtr<TValueType>(TValueType** address) : INullable
where TValueType : unmanaged
{
/// <summary>
/// The pointer-pointer.
/// </summary>
public TValueType** Pointer => address;
/// <summary>
/// Convert the pointer to a span of pointers of type TValueType.
/// </summary>
public Span<Ptr<TValueType>> AsSpan(ElementCount length)
{
// Ptr<TValueType> is equivalent to TValueType*,
// So we can cast TValueType** to Ptr<TValueType>*,
// Or so we will hope
return new Span<Ptr<TValueType>>((Ptr<TValueType>*)Pointer, length);
}
/// <inheritdoc />
public bool IsNull()
{
return Pointer == null;
}
/// <summary>
/// Get the value of the pointer as a reference.
/// </summary>
public ref TValueType* Value => ref *Pointer;
/// <summary>
/// Access the pointer at the given index.
/// </summary>
public Ptr<TValueType> this[int index]
{
get => Pointer[index];
set => Pointer[index] = value.Pointer;
}
/// <summary>
/// Implicit conversion from void** to PtrPtr.
/// </summary>
public static implicit operator PtrPtr<TValueType>(TValueType** address)
{
return new PtrPtr<TValueType>(address);
}
/// <summary>
/// Implicit conversion from PtrPtr to void**.
/// </summary>
public static implicit operator void**(PtrPtr<TValueType> ptr)
{
return (void**)ptr.Pointer;
}
/// <summary>
/// Implicit conversion from PtrPtr to TValueType**
/// </summary>
public static implicit operator TValueType**(PtrPtr<TValueType> ptr)
{
return ptr.Pointer;
}
/// <summary>
/// Explicit conversion from nint to PtrPtr.
/// </summary>
public static explicit operator PtrPtr<TValueType>(nint ptr)
{
return new PtrPtr<TValueType>((TValueType**)ptr);
}
/// <summary>
/// Explicit cast to nint.
/// </summary>
public static explicit operator nint(PtrPtr<TValueType> ptr)
{
return (nint)ptr.Pointer;
}
}