After reading multiple post on SO, I need to pre-allocate my memory buffer on the C# side, pass it to the native side so when the function returns the value in the memory buffer will be properly filled (this avoid multiple copies of the data).
See:
Which does basically:
[DllImport("NativePlugin", CallingConvention = CallingConvention.Cdecl)]
private static extern void fillArrayNative(IntPtr data, int count, out int outValue);
public unsafe void getFillArrayNative(float[] outArray, int count, out int outValue)
{
//Pin Memory
fixed (float* p = outArray)
{
fillArrayNative((IntPtr)p, count, out outValue);
}
}
How can I make sure this function will be thread-safe ? The point here is that the same buffer outArray
should be used as I iterate over each files and populate the outArray
.