Skip to content

Commit

Permalink
Switched DataEnqueue to use pointer arithmetic
Browse files Browse the repository at this point in the history
  • Loading branch information
mullerj committed Jun 11, 2024
1 parent 472014e commit 6f92da8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 51 deletions.
1 change: 1 addition & 0 deletions src/dotnet/src/Mil.Navy.Nrl.Norm/Mil.Navy.Nrl.Norm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>1.0.0</Version>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

</Project>
35 changes: 0 additions & 35 deletions src/dotnet/src/Mil.Navy.Nrl.Norm/NormApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -544,41 +544,6 @@ public struct NormEvent
[DllImport(NORM_LIBRARY)]
public static extern long NormDataEnqueue(long sessionHandle, nint dataPtr, int dataLen, nint infoPtr, int infoLen);

/// <summary>
/// This function enqueues a segment of application memory space for transmission within the specified NORM sessionHandle.
/// </summary>
/// <param name="sessionHandle">Used to identify application in the NormSession.</param>
/// <param name="data">The data parameter must be a managed buffer to be transmitted.</param>
/// <param name="dataLen">The dataLen parameter indicates the quantity of data to transmit.</param>
/// <param name="info">The optional info and infoLen parameters
/// are used to associate NORM_INFO content with the sent transport object. The maximum allowed infoLen
/// corresponds to the segmentSize used in the prior call to NormStartSender(). The use and interpretation of the
/// NORM_INFO content is left to the application's discretion.</param>
/// <param name="infoLen">The optional info and infoLen parameters
/// are used to associate NORM_INFO content with the sent transport object. The maximum allowed infoLen
/// corresponds to the segmentSize used in the prior call to NormStartSender(). The use and interpretation of the
/// NORM_INFO content is left to the application's discretion</param>
/// <returns>A NormObjectHandle is returned which the application may use in other NORM API calls as needed.</returns>
public static long NormDataEnqueue(long sessionHandle, byte[] data, int dataLen, byte[]? info, int infoLen)
{
long objectHandle;
var dataHandle = GCHandle.Alloc(data, GCHandleType.Pinned);
var infoHandle = GCHandle.Alloc(info, GCHandleType.Pinned);

try
{
var dataPtr = dataHandle.AddrOfPinnedObject();
var infoPtr = infoHandle.AddrOfPinnedObject();
objectHandle = NormDataEnqueue(sessionHandle, dataPtr, dataLen, infoPtr, infoLen);
}
finally
{
dataHandle.Free();
infoHandle.Free();
}
return objectHandle;
}

/// <summary>
/// This function allows the application to resend (or reset transmission of) a NORM_OBJECT_FILE or NORM_OBJECT_DATA
/// transmit object that was previously enqueued for the indicated sessionHandle.
Expand Down
43 changes: 27 additions & 16 deletions src/dotnet/src/Mil.Navy.Nrl.Norm/NormSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -526,23 +526,34 @@ public NormData DataEnqueue(byte[] dataBuffer, int dataOffset, int dataLength, b
{
throw new ArgumentOutOfRangeException(nameof(dataLength), "The data length is out of range");
}
var dataBytes = dataBuffer.Skip(dataOffset).Take(dataLength).ToArray();
byte[]? infoBytes;
if (info != null)
{
infoBytes = info.Skip(infoOffset).Take(infoLength).ToArray();
}
else
unsafe
{
infoBytes = null;
infoLength = 0;
}
var objectHandle = NormDataEnqueue(_handle, dataBytes, dataLength, infoBytes, infoLength);
if (objectHandle == NormObject.NORM_OBJECT_INVALID)
{
throw new IOException("Failed to enqueue data");
}
return new NormData(objectHandle);
fixed (byte* dataPointer = &dataBuffer[0])
{
byte* dataPointerWithOffset = dataPointer + dataOffset;

byte* infoPointerWithOffset;

if (info != null)
{
fixed (byte* infoPointer = &info[0])
{
infoPointerWithOffset = infoPointer + infoOffset;
}
}
else
{
infoPointerWithOffset = null;
infoLength = 0;
}
var objectHandle = NormDataEnqueue(_handle, (IntPtr)dataPointerWithOffset, dataLength, (IntPtr)infoPointerWithOffset, infoLength);
if (objectHandle == NormObject.NORM_OBJECT_INVALID)
{
throw new IOException("Failed to enqueue data");
}
return new NormData(objectHandle);
}
}
}

/// <summary>
Expand Down

0 comments on commit 6f92da8

Please sign in to comment.