Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cdac] Pass a delegate into Target #105043

Merged
merged 3 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/native/managed/cdacreader/src/Entrypoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal static class Entrypoints
private static unsafe int Init(ulong descriptor, delegate* unmanaged<ulong, byte*, uint, void*, int> readFromTarget, void* readContext, IntPtr* handle)
{
// TODO: [cdac] Better error code/details
if (!Target.TryCreate(descriptor, readFromTarget, readContext, out Target? target))
if (!Target.TryCreate(descriptor, (address, buffer, bufferLength) => readFromTarget(address, buffer, bufferLength, readContext), out Target? target))
return -1;

GCHandle gcHandle = GCHandle.Alloc(target);
Expand Down
21 changes: 7 additions & 14 deletions src/native/managed/cdacreader/src/Target.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,11 @@ private readonly struct Configuration
internal Contracts.Registry Contracts { get; }
internal DataCache ProcessedData { get; }

public static bool TryCreate(ulong contractDescriptor, delegate* unmanaged<ulong, byte*, uint, void*, int> readFromTarget, void* readContext, out Target? target)
public delegate int ReadFromTargetDelegate(ulong address, byte* buffer, uint bytesToRead);
jkoritzinsky marked this conversation as resolved.
Show resolved Hide resolved

public static bool TryCreate(ulong contractDescriptor, ReadFromTargetDelegate readFromTarget, out Target? target)
{
Reader reader = new Reader(readFromTarget, readContext);
Reader reader = new Reader(readFromTarget);
if (TryReadContractDescriptor(contractDescriptor, reader, out Configuration config, out ContractDescriptorParser.ContractDescriptor? descriptor, out TargetPointer[] pointerData))
{
target = new Target(config, descriptor!, pointerData, reader);
Expand Down Expand Up @@ -418,26 +420,17 @@ public bool TryGet<T>(ulong address, [NotNullWhen(true)] out T? data)
}
}

private sealed class Reader
private struct Reader(Target.ReadFromTargetDelegate readFromTarget)
{
private readonly delegate* unmanaged<ulong, byte*, uint, void*, int> _readFromTarget;
private readonly void* _context;

public Reader(delegate* unmanaged<ulong, byte*, uint, void*, int> readFromTarget, void* context)
{
_readFromTarget = readFromTarget;
_context = context;
}

public int ReadFromTarget(ulong address, Span<byte> buffer)
{
fixed (byte* bufferPtr = buffer)
{
return _readFromTarget(address, bufferPtr, (uint)buffer.Length, _context);
return readFromTarget(address, bufferPtr, (uint)buffer.Length);
}
}

public int ReadFromTarget(ulong address, byte* buffer, uint bytesToRead)
=> _readFromTarget(address, buffer, bytesToRead, _context);
=> readFromTarget(address, buffer, bytesToRead);
}
}
5 changes: 2 additions & 3 deletions src/native/managed/cdacreader/tests/MockMemorySpace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,10 @@ public static ReadContext CreateContext(ReadOnlySpan<byte> descriptor, ReadOnlyS

public static bool TryCreateTarget(ReadContext* context, out Target? target)
{
return Target.TryCreate(ContractDescriptorAddr, &ReadFromTarget, context, out target);
return Target.TryCreate(ContractDescriptorAddr, (address, buffer, length) => ReadFromTarget(address, buffer, length, context), out target);
}

[UnmanagedCallersOnly]
private static int ReadFromTarget(ulong address, byte* buffer, uint length, void* context)
private static int ReadFromTarget(ulong address, byte* buffer, uint length, ReadContext* context)
{
ReadContext* readContext = (ReadContext*)context;
var span = new Span<byte>(buffer, (int)length);
Expand Down
Loading