public void Flush() { CheckDisposed(); foreach (var area in areas) { if (!Win32FileMapping.FlushViewOfFile(area.Address, new IntPtr(area.Size))) { throw new Win32Exception(); } } fs.Flush(true); }
public void Dispose() { if (isDisposed) { return; } isDisposed = true; foreach (var a in this.areas) { Win32FileMapping.UnmapViewOfFile(a.Address); a.Mmf.Dispose(); } fs.Dispose(); areas.Clear(); }
public void Grow(long bytesToGrow) { CheckDisposed(); if (bytesToGrow <= 0 || bytesToGrow % AllocationGranularity != 0) { throw new ArgumentException("The growth must be a multiple of 64Kb and greater than zero"); } long offset = fs.Length; fs.SetLength(fs.Length + bytesToGrow); var mmf = MemoryMappedFile.CreateFromFile(fs, null, fs.Length, MemoryMappedFileAccess.ReadWrite, null, HandleInheritability.None, true); uint *offsetPointer = (uint *)&offset; var lastArea = areas[areas.Count - 1]; byte *desiredAddress = lastArea.Address + lastArea.Size; var address = Win32FileMapping.MapViewOfFileEx(mmf.SafeMemoryMappedFileHandle.DangerousGetHandle(), Win32FileMapping.FileMapAccess.Read | Win32FileMapping.FileMapAccess.Write, offsetPointer[1], offsetPointer[0], new UIntPtr((ulong)bytesToGrow), desiredAddress); if (address == null) { address = Win32FileMapping.MapViewOfFileEx(mmf.SafeMemoryMappedFileHandle.DangerousGetHandle(), Win32FileMapping.FileMapAccess.Read | Win32FileMapping.FileMapAccess.Write, offsetPointer[1], offsetPointer[0], new UIntPtr((ulong)bytesToGrow), null); } if (address == null) { throw new Win32Exception(); } var area = new MemoryMappedArea { Address = address, Mmf = mmf, Size = bytesToGrow }; areas.Add(area); if (desiredAddress != address) { offsets = offsets.Add(offset); addresses = addresses.Add(address); } }
private void CreateFirstArea() { var mmf = MemoryMappedFile.CreateFromFile(fs, null, fs.Length, MemoryMappedFileAccess.ReadWrite, null, HandleInheritability.None, true); var address = Win32FileMapping.MapViewOfFileEx(mmf.SafeMemoryMappedFileHandle.DangerousGetHandle(), Win32FileMapping.FileMapAccess.Read | Win32FileMapping.FileMapAccess.Write, 0, 0, new UIntPtr((ulong)fs.Length), null); if (address == null) { throw new Win32Exception(); } var area = new MemoryMappedArea { Address = address, Mmf = mmf, Size = fs.Length }; areas.Add(area); addresses = new byte *[] { address }; offsets = new long[] { 0 }; }