Updated NormFile.Name to use stackalloc (#37)

pull/85/head
mullerj 2024-08-25 13:25:13 -04:00 committed by GitHub
parent bc8ab6b8d0
commit f04faca08a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 7 additions and 16 deletions

View File

@ -25,27 +25,18 @@ namespace Mil.Navy.Nrl.Norm
/// The name of the file. /// The name of the file.
/// </summary> /// </summary>
/// <exception cref="IOException">Thrown when failed to get file name.</exception> /// <exception cref="IOException">Thrown when failed to get file name.</exception>
public string Name public unsafe string Name
{ {
get get
{ {
var buffer = new byte[FILENAME_MAX]; var buffer = stackalloc byte[FILENAME_MAX];
var bufferHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned); var bufferPtr = (nint)buffer;
string? name;
try
{
var bufferPtr = bufferHandle.AddrOfPinnedObject();
if (!NormFileGetName(_handle, bufferPtr, FILENAME_MAX)) if (!NormFileGetName(_handle, bufferPtr, FILENAME_MAX))
{ {
throw new IOException("Failed to get file name"); throw new IOException("Failed to get file name");
} }
name = Marshal.PtrToStringAnsi(bufferPtr); var name = Marshal.PtrToStringAnsi(bufferPtr);
}
finally
{
bufferHandle.Free();
}
return name ?? string.Empty; return name ?? string.Empty;
} }