Skip to content

Commit

Permalink
Updated NormFile.Name to use stackalloc
Browse files Browse the repository at this point in the history
  • Loading branch information
mullerj committed Aug 25, 2024
1 parent bc8ab6b commit 441502c
Showing 1 changed file with 7 additions and 16 deletions.
23 changes: 7 additions & 16 deletions src/dotnet/src/Mil.Navy.Nrl.Norm/NormFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,19 @@ internal NormFile(long handle) : base(handle)
/// The name of the file.
/// </summary>
/// <exception cref="IOException">Thrown when failed to get file name.</exception>
public string Name
public unsafe string Name
{
get
{
var buffer = new byte[FILENAME_MAX];
var bufferHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
string? name;
var buffer = stackalloc byte[FILENAME_MAX];
var bufferPtr = (nint)buffer;

try
if (!NormFileGetName(_handle, bufferPtr, FILENAME_MAX))
{
var bufferPtr = bufferHandle.AddrOfPinnedObject();
if (!NormFileGetName(_handle, bufferPtr, FILENAME_MAX))
{
throw new IOException("Failed to get file name");
}
name = Marshal.PtrToStringAnsi(bufferPtr);
}
finally
{
bufferHandle.Free();
throw new IOException("Failed to get file name");
}

var name = Marshal.PtrToStringAnsi(bufferPtr);

return name ?? string.Empty;
}
}
Expand Down

0 comments on commit 441502c

Please sign in to comment.