Skip to content

Commit 03ab101

Browse files
committed
Use generic with IBinaryInteger constraint
1 parent c7bc9aa commit 03ab101

File tree

1 file changed

+36
-32
lines changed

1 file changed

+36
-32
lines changed

src/native/managed/cdacreader/src/Target.cs

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Buffers.Binary;
66
using System.Collections.Generic;
7+
using System.Numerics;
78

89
namespace Microsoft.Diagnostics.DataContractReader;
910

@@ -166,19 +167,7 @@ public byte ReadUInt8(ulong address)
166167
}
167168

168169
public bool TryReadUInt8(ulong address, out byte value)
169-
=> TryReadUInt8(address, _reader, out value);
170-
171-
private static bool TryReadUInt8(ulong address, Reader reader, out byte value)
172-
{
173-
value = 0;
174-
fixed (byte* ptr = &value)
175-
{
176-
if (reader.ReadFromTarget(address, ptr, 1) < 0)
177-
return false;
178-
}
179-
180-
return true;
181-
}
170+
=> TryRead(address, isUnsigned: true, out value);
182171

183172
public uint ReadUInt32(ulong address)
184173
{
@@ -189,21 +178,38 @@ public uint ReadUInt32(ulong address)
189178
}
190179

191180
public bool TryReadUInt32(ulong address, out uint value)
192-
=> TryReadUInt32(address, _config.IsLittleEndian, _reader, out value);
181+
=> TryRead(address, isUnsigned: true, out value);
193182

194183
private static bool TryReadUInt32(ulong address, bool isLittleEndian, Reader reader, out uint value)
184+
=> TryRead(address, isLittleEndian, isUnsigned: true, reader, out value);
185+
186+
public ulong ReadUInt64(ulong address)
195187
{
196-
value = 0;
188+
if (!TryReadUInt64(address, out ulong value))
189+
throw new InvalidOperationException($"Failed to read uint32 at 0x{address:x8}.");
197190

198-
Span<byte> buffer = stackalloc byte[sizeof(uint)];
191+
return value;
192+
}
193+
194+
public bool TryReadUInt64(ulong address, out ulong value)
195+
=> TryRead(address, isUnsigned: true, out value);
196+
197+
private static bool TryReadUInt64(ulong address, bool isLittleEndian, Reader reader, out ulong value)
198+
=> TryRead(address, isLittleEndian, isUnsigned: true, reader, out value);
199+
200+
private bool TryRead<T>(ulong address, bool isUnsigned, out T value) where T : unmanaged, IBinaryInteger<T>
201+
=> TryRead(address, _config.IsLittleEndian, isUnsigned, _reader, out value);
202+
203+
private static bool TryRead<T>(ulong address, bool isLittleEndian, bool isUnsigned, Reader reader, out T value) where T : unmanaged, IBinaryInteger<T>
204+
{
205+
value = default;
206+
Span<byte> buffer = stackalloc byte[sizeof(T)];
199207
if (reader.ReadFromTarget(address, buffer) < 0)
200208
return false;
201209

202-
value = isLittleEndian
203-
? BinaryPrimitives.ReadUInt32LittleEndian(buffer)
204-
: BinaryPrimitives.ReadUInt32BigEndian(buffer);
205-
206-
return true;
210+
return isLittleEndian
211+
? T.TryReadLittleEndian(buffer, isUnsigned, out value)
212+
: T.TryReadBigEndian(buffer, isUnsigned, out value);
207213
}
208214

209215
public TargetPointer ReadPointer(ulong address)
@@ -225,22 +231,20 @@ private static bool TryReadPointer(ulong address, Configuration config, Reader r
225231
if (reader.ReadFromTarget(address, buffer) < 0)
226232
return false;
227233

228-
if (config.PointerSize == sizeof(uint))
234+
if (config.PointerSize == sizeof(uint)
235+
&& TryReadUInt32(address, config.IsLittleEndian, reader, out uint value32))
229236
{
230-
pointer = new TargetPointer(
231-
config.IsLittleEndian
232-
? BinaryPrimitives.ReadUInt32LittleEndian(buffer)
233-
: BinaryPrimitives.ReadUInt32BigEndian(buffer));
237+
pointer = new TargetPointer(value32);
238+
return true;
234239
}
235-
else if (config.PointerSize == sizeof(ulong))
240+
else if (config.PointerSize == sizeof(ulong)
241+
&& TryReadUInt64(address, config.IsLittleEndian, reader, out ulong value64))
236242
{
237-
pointer = new TargetPointer(
238-
config.IsLittleEndian
239-
? BinaryPrimitives.ReadUInt64LittleEndian(buffer)
240-
: BinaryPrimitives.ReadUInt64BigEndian(buffer));
243+
pointer = new TargetPointer(value64);
244+
return true;
241245
}
242246

243-
return true;
247+
return false;
244248
}
245249

246250
public byte ReadGlobalUInt8(string name)

0 commit comments

Comments
 (0)