Class BorrowedMemorySource
- Namespace
- CStructSharp.Memory
- Assembly
- CStructSharp.dll
A non-owning read-only view of caller-managed memory at an unsigned base address.
public sealed class BorrowedMemorySource : IMemorySource
- Inheritance
-
BorrowedMemorySource
- Implements
- Inherited Members
Remarks
The caller keeps the backing memory alive and stable for every operation and any cache lifetime. Read-only access does not freeze a mutable array. Generation is constant; this source creates no snapshot. Reads charge requested bytes once as a leaf source, independently of any earlier capture operation.
Constructors
BorrowedMemorySource(string, ReadOnlyMemory<byte>, ulong, int?)
Borrows a readable prefix without copying or taking ownership.
public BorrowedMemorySource(string id, ReadOnlyMemory<byte> bytes, ulong baseAddress = 0, int? readableLength = null)
Parameters
idstringDiagnostic source label.
bytesReadOnlyMemory<byte>Caller-owned memory; an unreadable suffix is excluded by readableLength.
baseAddressulongUnsigned coordinate of the first buffer byte.
readableLengthint?Readable prefix in bytes; null means the whole memory.
Exceptions
- ArgumentException
The label is empty.
- ArgumentOutOfRangeException
The prefix is outside the memory or its addressed range wraps.
Properties
BaseAddress
Gets the unsigned source coordinate corresponding to buffer offset zero.
public ulong BaseAddress { get; }
Property Value
Generation
Gets zero; externally managed changes are not observable through a generation counter.
public long Generation { get; }
Property Value
Id
Gets a stable label for diagnostics and error messages. Object identity, not this label, distinguishes address spaces.
public string Id { get; }
Property Value
Length
Gets the readable prefix length in bytes.
public int Length { get; }
Property Value
Methods
Read(ulong, Span<byte>, MemoryAccessContext)
Copies up to destination.Length bytes starting at address and returns the count copied.
public int Read(ulong address, Span<byte> destination, MemoryAccessContext context)
Parameters
addressulongUnsigned address in this source's own coordinate system (file offset, virtual address, or whatever the source defines).
destinationSpan<byte>Caller-owned buffer, valid only for the duration of the call.
contextMemoryAccessContextShared operation budget; leaf sources charge bytes, translation adapters charge zero-byte requests.
Returns
- int
The number of bytes copied, never greater than the buffer length.
Remarks
Zero means the bytes are unavailable; a finite view turns that into a MissingBytes failure rather than treating it as end of file. An implementation may also throw a structured MemoryAccessException, for example Unmapped for a hole it can name.