diff --git a/Brutal.Memory/Allocators/NativeStackAllocator.cs b/Brutal.Memory/Allocators/NativeStackAllocator.cs new file mode 100644 index 0000000..fb8df0a --- /dev/null +++ b/Brutal.Memory/Allocators/NativeStackAllocator.cs @@ -0,0 +1,109 @@ +using System; +using System.Runtime.InteropServices; + +using Brutal; +using Brutal.Pointers; + +namespace Brutal.Memory; + +public unsafe class NativeStackAllocator +{ + private readonly Ptr _pointer; + private readonly ByteSize _capacity; + private ByteSize _reserved; + + public ByteSize Capacity => _capacity; + public ByteSize Reserved => _reserved; + public ByteSize Remaining => _capacity - _reserved; + public Span Span => new(_pointer, ((int)_capacity)); + + public NativeStackAllocator(Ptr pointer, ByteSize capacity) + { + if (pointer.IsNull()) + { + throw new ArgumentNullException(nameof(pointer)); + } + + _pointer = pointer; + _capacity = capacity; + _reserved = ByteSize.Zero; + } + + public unsafe Ptr Rent(ByteSize bytes) + { + if (_reserved + bytes > _capacity) + { + throw new OutOfMemoryException("Stack allocator is full"); + } + + Ptr result = _pointer.Offset(_reserved); + _reserved += bytes; + return result; + } + + public Ptr Rent(ElementCount count) where T : unmanaged + { + int alignment = TypeAlign.Value; + + nuint currentAddr = (nuint)_pointer.Offset(_reserved).Pointer; + nuint alignedAddr = (currentAddr + (nuint)(alignment - 1)) & ~(nuint)(alignment - 1); + ByteSize padding = ByteSize.Of((int)(alignedAddr - currentAddr)); + + ByteSize dataBytes = ByteSize.Of(count); + ByteSize totalBytes = padding + dataBytes; + + if (_reserved + totalBytes > _capacity) + { + throw new OutOfMemoryException("Stack allocator is full"); + } + + Ptr alignedPtr = _pointer.Offset(_reserved + padding); + _reserved += totalBytes; + + return alignedPtr.Cast(); + } + + public Scope RentScoped(ElementCount count) where T : unmanaged + { + ByteSize marker = _reserved; + Ptr ptr = Rent(count); + return new Scope(this, marker, ptr.AsSpan(count)); + } + + public void Reset() => _reserved = ByteSize.Zero; + + public void Rollback(ByteSize marker) + { + if (marker > _reserved) + { + throw new ArgumentOutOfRangeException(nameof(marker), "Cannot rollback to a future offset"); + } + + _reserved = marker; + } + + public readonly ref struct Scope : IDisposable where T : unmanaged + { + private readonly NativeStackAllocator _allocator; + private readonly ByteSize _marker; + + public readonly Span Span; + + public int Length => Span.Length; + public ref T this[int index] => ref Span[index]; + + internal Scope(NativeStackAllocator allocator, ByteSize marker, Span span) + { + _allocator = allocator; + _marker = marker; + Span = span; + } + + public void Dispose() => _allocator.Rollback(_marker); + + public Span.Enumerator GetEnumerator() => Span.GetEnumerator(); + + public static implicit operator Span(Scope scope) => scope.Span; + public static implicit operator ReadOnlySpan(Scope scope) => scope.Span; + } +} \ No newline at end of file diff --git a/Brutal.Memory/TypeAlign.cs b/Brutal.Memory/TypeAlign.cs new file mode 100644 index 0000000..a04bef5 --- /dev/null +++ b/Brutal.Memory/TypeAlign.cs @@ -0,0 +1,36 @@ +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +namespace Brutal.Memory; + +/// +/// Provides the natural memory alignment requirement for an unmanaged type . +/// Serves as the C# equivalent of C++ alignof(T). +/// +/// The unmanaged type whose alignment is to be queried. +public static unsafe class TypeAlign where T : unmanaged +{ + /// + /// Probe struct to measure padding added by the runtime between fields. + /// Explicitly sequential to prevent field reordering optimizations. + /// + [StructLayout(LayoutKind.Sequential)] + private struct Probe + { + public byte Byte; + public T Value; + } + + /// + /// The natural alignment of type in bytes. + /// Guaranteed to be a power of two (1, 2, 4, 8, or 16). + /// + public static readonly int Value = ComputeAlignment(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static int ComputeAlignment() + { + Probe probe = default; + return (int)((byte*)&probe.Value - (byte*)&probe); + } +} \ No newline at end of file