public void Save(PrimitiveWriter writer)
{
var globalStopwatch = Stopwatch.StartNew();
var realSegmentsCount = 0;
// magic
writer.Write(Magic);
// saving size of the memory segment
writer.Write(SegmentSize);
// saving size of the memory
writer.Write(size);
byte[][] outputBuffers = new byte[segments.Length][];
int[] outputLengths = new int[segments.Length];
Parallel.For(0, segments.Length, i =>
{
if(segments[i] == IntPtr.Zero)
{
return;
}
Interlocked.Increment(ref realSegmentsCount);
var compressedBuffer = new byte[LZ4Codec.MaximumOutputLength(SegmentSize)];
var length = LZ4Codec.Encode64(segments[i], compressedBuffer, SegmentSize);
outputBuffers[i] = compressedBuffer;
outputLengths[i] = length;
});
for(var i = 0; i < segments.Length; i++)
{
if(segments[i] == IntPtr.Zero)
{
writer.Write(false);
continue;
}
writer.Write(true);
writer.Write(outputLengths[i]);
writer.Write(outputBuffers[i], 0, outputLengths[i]);
}
this.NoisyLog(string.Format("{0} segments saved to stream, of which {1} had contents.", segments.Length, realSegmentsCount));
globalStopwatch.Stop();
this.NoisyLog("Memory serialization ended in {0}s.", Misc.NormalizeDecimal(globalStopwatch.Elapsed.TotalSeconds));
}