Table of Contents

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

id string

Diagnostic source label.

bytes ReadOnlyMemory<byte>

Caller-owned memory; an unreadable suffix is excluded by readableLength.

baseAddress ulong

Unsigned coordinate of the first buffer byte.

readableLength int?

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

ulong

Generation

Gets zero; externally managed changes are not observable through a generation counter.

public long Generation { get; }

Property Value

long

Id

Gets a stable label for diagnostics and error messages. Object identity, not this label, distinguishes address spaces.

public string Id { get; }

Property Value

string

Length

Gets the readable prefix length in bytes.

public int Length { get; }

Property Value

int

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

address ulong

Unsigned address in this source's own coordinate system (file offset, virtual address, or whatever the source defines).

destination Span<byte>

Caller-owned buffer, valid only for the duration of the call.

context MemoryAccessContext

Shared 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.