public int[] Allocate(int blockCount) { if (blockCount <= 0) { throw new ArgumentOutOfRangeException(nameof(blockCount)); } var allocatedFromIndexBlocks = new int[0]; lock (lockObject) { var allocatedFromIndexBlockCount = Math.Min(ReleasedBlockCount, blockCount); if (allocatedFromIndexBlockCount > 0) { allocatedFromIndexBlocks = new int[allocatedFromIndexBlockCount]; var position = ReleasedBlockCount - allocatedFromIndexBlockCount; blockStream.Read(position, allocatedFromIndexBlocks); blockStream.Write(position, new int[allocatedFromIndexBlockCount]); ReleasedBlockCount -= allocatedFromIndexBlockCount; } var leftBlocks = blockCount - allocatedFromIndexBlockCount; if (leftBlocks > 0) { var blocks = storage.Extend(leftBlocks); return(allocatedFromIndexBlocks.Concat(blocks).ToArray()); } } EraseBlocks(allocatedFromIndexBlocks); return(allocatedFromIndexBlocks); }