Skip to content

Commit

Permalink
Implemented try/finally blocks for calls to Marshal
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Sol committed Jan 16, 2025
1 parent cb28554 commit d13c67a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 55 deletions.
62 changes: 32 additions & 30 deletions mzLib/Readers/timsTOF/FrameProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,44 +183,46 @@ internal double GetOneOverK0(double zeroIndexedScanNumberMedian)
/// Note: different threads must not read scans from the same storage handle
/// concurrently.
/// </summary>
internal unsafe static uint[] GetScanRawData(UInt64 fileHandle, long frameId, UInt32 numScans, Object fileLock)
internal static uint[] GetScanRawData(UInt64 fileHandle, long frameId, UInt32 numScans, Object fileLock)
{
int bufferSize = _defaultBufferSize;
// buffer expansion loop
while (true)
{
IntPtr pData = Marshal.AllocHGlobal(bufferSize * Marshal.SizeOf<Int32>());
uint outputLength;

lock ( fileLock )
try
{
outputLength = tims_read_scans_v2(
fileHandle,
frameId,
scan_begin: 0,
scan_end: numScans,
buffer: pData,
length: (uint)(bufferSize * 4));
uint outputLength;

lock (fileLock)
{
outputLength = tims_read_scans_v2(
fileHandle,
frameId,
scan_begin: 0,
scan_end: numScans,
buffer: pData,
length: (uint)(bufferSize * 4));
}

if (4 * bufferSize > outputLength)
{
var dataArray = new uint[bufferSize];
CopyToManaged(pData, dataArray, 0, bufferSize);
Marshal.FreeHGlobal(pData);

return dataArray;
}

if (outputLength > 16777216) // Arbitrary 16 mb frame limit
{
throw new Exception("Maximum frame size exceeded");
}

// Increase buffer size if necessary
bufferSize = ((int)outputLength / 4) + 1;
}

if (4 * bufferSize > outputLength)
{
var dataArray = new uint[bufferSize];
CopyToManaged(pData, dataArray, 0, bufferSize);
Marshal.FreeHGlobal(pData);

return dataArray;
}

if (outputLength > 16777216) // Arbitrary 16 mb frame limit
{
throw new Exception("Maximum frame size exceeded");
}

// Increase buffer size if necessary
bufferSize = ((int)outputLength / 4) + 1;

Marshal.FreeHGlobal(pData);
finally{ Marshal.FreeHGlobal(pData); }
}
}

Expand Down
53 changes: 28 additions & 25 deletions mzLib/Readers/timsTOF/TimsConversion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,37 +49,40 @@ internal unsafe double[] DoTransformation(UInt64 fileHandle, long frameId, doubl
fixed (double* inputPtr = &input[0])
{
IntPtr outPtr = Marshal.AllocHGlobal(input.Length * Marshal.SizeOf<double>());
lock (_fileLock)
try
{
switch (function)
lock (_fileLock)
{
case ConversionFunctions.IndexToMz:
tims_index_to_mz(fileHandle, frameId, inputPtr, (double*)outPtr, (UInt32)input.Length);
break;
case ConversionFunctions.MzToIndex:
tims_mz_to_index(fileHandle, frameId, inputPtr, (double*)outPtr, (UInt32)input.Length);
break;
case ConversionFunctions.ScanToOneOverK0:
tims_scannum_to_oneoverk0(fileHandle, frameId, inputPtr, (double*)outPtr, (UInt32)input.Length);
break;
case ConversionFunctions.OneOverK0ToScan:
tims_oneoverk0_to_scannum(fileHandle, frameId, inputPtr, (double*)outPtr, (UInt32)input.Length);
break;
case ConversionFunctions.ScanToVoltage:
tims_scannum_to_voltage(fileHandle, frameId, inputPtr, (double*)outPtr, (UInt32)input.Length);
break;
case ConversionFunctions.VoltageToScan:
tims_voltage_to_scannum(fileHandle, frameId, inputPtr, (double*)outPtr, (UInt32)input.Length);
break;
default:
break;
switch (function)
{
case ConversionFunctions.IndexToMz:
tims_index_to_mz(fileHandle, frameId, inputPtr, (double*)outPtr, (UInt32)input.Length);
break;
case ConversionFunctions.MzToIndex:
tims_mz_to_index(fileHandle, frameId, inputPtr, (double*)outPtr, (UInt32)input.Length);
break;
case ConversionFunctions.ScanToOneOverK0:
tims_scannum_to_oneoverk0(fileHandle, frameId, inputPtr, (double*)outPtr, (UInt32)input.Length);
break;
case ConversionFunctions.OneOverK0ToScan:
tims_oneoverk0_to_scannum(fileHandle, frameId, inputPtr, (double*)outPtr, (UInt32)input.Length);
break;
case ConversionFunctions.ScanToVoltage:
tims_scannum_to_voltage(fileHandle, frameId, inputPtr, (double*)outPtr, (UInt32)input.Length);
break;
case ConversionFunctions.VoltageToScan:
tims_voltage_to_scannum(fileHandle, frameId, inputPtr, (double*)outPtr, (UInt32)input.Length);
break;
default:
break;

}
}

Marshal.Copy(outPtr, transformedValues, 0, input.Length);
}
Marshal.Copy(outPtr, transformedValues, 0, input.Length);
Marshal.FreeHGlobal(outPtr);
finally { Marshal.FreeHGlobal(outPtr); }
}

return transformedValues;
}

Expand Down

0 comments on commit d13c67a

Please sign in to comment.