public unsafe long? AllocateSpace(uint size)
{
if (size == 0)
return null;
long oldSize, newSize;
// This is thread-safe, but because we bump the DataLength without
// making any effort to ensure the data in the region is valid,
// other threads may attempt to read it and find random garbage
// there.
// On the bright side, MSDN claims that unused regions in a mapped
// file are always zeroes, and this seems to be true so far. Given
// this, most of the time you just need a 'this data is valid' bit
// tucked away to protect yourself from reading uninitialized data.
using (var header = AccessHeader()) {
newSize = Interlocked.Add(ref header.Ptr->DataLength, size);
oldSize = newSize - size;
}
EnsureCapacity(newSize + HeaderSize);
return oldSize;
}