Class StreamMemorySource
- Namespace
- CStructSharp.Memory
- Assembly
- CStructSharp.dll
A read-only source over a caller-owned seekable stream, typically a capture file, whose addresses are file offsets.
public sealed class StreamMemorySource : IMemorySource
- Inheritance
-
StreamMemorySource
- Implements
- Inherited Members
Remarks
This adapter lets a large capture be analyzed without loading it into memory. Its addresses are positions in the stream, so they must fit a signed Position; a MappedMemorySource above it can still expose those bytes at addresses beyond MaxValue, because translation happens before the stream is touched.
Each read seeks to the requested offset and restores the previous position in a finally block, so code
that shares the stream for other purposes sees it unchanged. The adapter's lock serializes calls made through
this object only; direct use of the stream by other code still needs the caller's own synchronization.
The adapter cannot observe changes made to the file by other programs, so Generation is a constant supplied at construction rather than a real change counter. Keep the file stable while analyzing or caching it, and dispose the stream yourself after every non-owning view has finished with it.
Constructors
StreamMemorySource(string, Stream, long)
Wraps a readable, seekable stream without taking ownership of it.
public StreamMemorySource(string id, Stream stream, long generation = 0)
Parameters
idstringDiagnostic label; source objects, not labels, distinguish address spaces.
streamStreamCaller-owned stream that supports reading and seeking.
generationlongConstant snapshot label reported as the generation; the adapter cannot detect external changes.
Properties
Generation
Gets the constant snapshot label supplied at construction; external file changes are not detected.
public long Generation { get; }
Property Value
Id
Gets the diagnostic label for this source.
public string Id { 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.