Skip to content

Commit 40b8c67

Browse files
authored
Add Span<byte> overloads for get/set_raw_bytes (#467)
This is anticipated to be part 1 of a series of performance-oriented changes around the native interop.
1 parent ea2a269 commit 40b8c67

3 files changed

Lines changed: 28 additions & 4 deletions

File tree

src/libplctag.NativeImport/NativeMethods.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,9 @@ static NativeMethods()
257257

258258

259259
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_get_raw_bytes), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
260-
public static extern int plc_tag_get_raw_bytes(Int32 tag_id, int start_offset, [Out] byte[] buffer, int buffer_length);
260+
public unsafe static extern int plc_tag_get_raw_bytes(Int32 tag_id, int start_offset, byte* buffer, int buffer_length);
261261

262262
[DllImport(DLL_NAME, EntryPoint = nameof(plc_tag_set_raw_bytes), CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
263-
public static extern int plc_tag_set_raw_bytes(Int32 tag_id, int start_offset, [In] byte[] buffer, int buffer_length);
263+
public unsafe static extern int plc_tag_set_raw_bytes(Int32 tag_id, int start_offset, byte* buffer, int buffer_length);
264264
}
265265
}

src/libplctag.NativeImport/libplctag.NativeImport.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@
3333
<PublishRepositoryUrl>true</PublishRepositoryUrl>
3434
<EmbedUntrackedSources>true</EmbedUntrackedSources>
3535
<DebugType>embedded</DebugType>
36+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
3637
</PropertyGroup>
3738

3839
<ItemGroup>
3940
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All"/>
41+
<PackageReference Include="System.Memory" Version="4.6.0" />
4042
</ItemGroup>
4143

4244
<ItemGroup>

src/libplctag.NativeImport/plctag.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,12 +295,34 @@ public static int plc_tag_get_string_total_length(Int32 tag_id, int string_start
295295

296296
public static int plc_tag_get_raw_bytes(Int32 tag_id, int start_offset, byte[] buffer, int buffer_length)
297297
{
298-
return NativeMethods.plc_tag_get_raw_bytes(tag_id, start_offset, buffer, buffer_length);
298+
return plc_tag_get_raw_bytes(tag_id, start_offset, buffer.AsSpan(0, buffer_length));
299+
}
300+
301+
public static int plc_tag_get_raw_bytes(Int32 tag_id, int start_offset, Span<byte> buffer)
302+
{
303+
unsafe
304+
{
305+
fixed (byte* ptr = buffer)
306+
{
307+
return NativeMethods.plc_tag_get_raw_bytes(tag_id, start_offset, ptr, buffer.Length);
308+
}
309+
}
299310
}
300311

301312
public static int plc_tag_set_raw_bytes(Int32 tag_id, int start_offset, byte[] buffer, int buffer_length)
302313
{
303-
return NativeMethods.plc_tag_set_raw_bytes(tag_id, start_offset, buffer, buffer_length);
314+
return plc_tag_set_raw_bytes(tag_id, start_offset, new ReadOnlySpan<byte>(buffer, 0, buffer_length));
315+
}
316+
317+
public static int plc_tag_set_raw_bytes(Int32 tag_id, int start_offset, ReadOnlySpan<byte> buffer)
318+
{
319+
unsafe
320+
{
321+
fixed (byte* ptr = buffer)
322+
{
323+
return NativeMethods.plc_tag_set_raw_bytes(tag_id, start_offset, ptr, buffer.Length);
324+
}
325+
}
304326
}
305327

306328

0 commit comments

Comments
 (0)