Thursday, April 09, 2015

Malloc in BinaryReader and BinaryWriter

In my efforts to write a malloc-free network library for Unity3D, I discovered that BinaryReader and BinaryWriter perform malloc operations when reading/writing floats and doubles.

It seems this is the case, because C# does not allow bit shifting operations on these types, which is used to turn these types into 4 or 8 bytes.

I ended up using this code to convert these types without using the builtin BitConverter (which performs a malloc!)


float[] FLOAT = new float[1];

public float ReadFloat ()
{
   Buffer.BlockCopy(buffer, (int)readStream.Position, FLOAT, 0, 4);
   readStream.Position += 4;
   return FLOAT[0];
}

public void Write (float value)
{
   FLOAT[0] = value;
   Buffer.BlockCopy(FLOAT, 0, buffer, (int)writeStream.Position, 4);
   writeStream.Position += 4;
}


Popular Posts