private void Dispose(bool disposing, bool takeLock = true) { bool lockTaken = false; if (takeLock) { Monitor.Enter(syncObject, ref lockTaken); } try { if (IsDisposed) { return; } IsDisposed = true; var exceptions = new List <Exception>(); foreach (var a in areas) { try { if (Win32FileMapping.UnmapViewOfFile(a.Address) == false) { throw new Win32Exception(); } } catch (Exception ex) { exceptions.Add(ex); } if (disposing) { try { a.Mmf.Dispose(); } catch (Exception ex) { exceptions.Add(ex); } } } if (disposing && exceptions.Count > 0) { throw new AggregateException(exceptions); } } finally { if (lockTaken) { Monitor.Exit(syncObject); } } if (disposing) { GC.SuppressFinalize(this); } }
internal void Flush() { lock (syncObject) { CheckDisposed(); foreach (var area in areas) { if (!Win32FileMapping.FlushViewOfFile(area.Address, new IntPtr(area.Size))) { throw new Win32Exception(); } } } }
internal static MemoryMapping Grow(long bytesToGrow, MemoryMapping mapping) { if (bytesToGrow <= 0 || bytesToGrow % Constants.AllocationGranularity != 0) { throw new ArgumentException("The growth must be a multiple of 64Kb and greater than zero"); } long offset = mapping.fileStream.Length; mapping.fileStream.SetLength(mapping.fileStream.Length + bytesToGrow); var mmf = MemoryMappedFile.CreateFromFile(mapping.fileStream, null, mapping.fileStream.Length, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, true); uint *offsetPointer = (uint *)&offset; var lastArea = mapping.areas[mapping.areas.Count - 1]; byte *desiredAddress = lastArea.Address + lastArea.Size; ulong bytesToMap = (ulong)bytesToGrow; var address = Win32FileMapping.MapViewOfFileEx(mmf.SafeMemoryMappedFileHandle.DangerousGetHandle(), Win32FileMapping.FileMapAccess.Read | Win32FileMapping.FileMapAccess.Write, offsetPointer[1], offsetPointer[0], new UIntPtr(bytesToMap), desiredAddress); if (address == null) { bytesToMap = (ulong)mapping.fileStream.Length; address = Win32FileMapping.MapViewOfFileEx(mmf.SafeMemoryMappedFileHandle.DangerousGetHandle(), Win32FileMapping.FileMapAccess.Read | Win32FileMapping.FileMapAccess.Write, 0, 0, new UIntPtr(bytesToMap), null); if (address == null) { throw new Win32Exception(); } mapping = new MemoryMapping() { baseAddress = address, fileStream = mapping.fileStream, refCount = 1 }; } var area = new MemoryMappedArea { Address = address, Mmf = mmf, Size = (long)bytesToMap }; mapping.areas.Add(area); return(mapping); }
internal static MemoryMapping Create(FileStream fs, long bytesToMap = 0) { var mmf = MemoryMappedFile.CreateFromFile(fs, null, fs.Length, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, true); var address = Win32FileMapping.MapViewOfFileEx(mmf.SafeMemoryMappedFileHandle.DangerousGetHandle(), Win32FileMapping.FileMapAccess.Read | Win32FileMapping.FileMapAccess.Write, 0, 0, new UIntPtr((ulong)bytesToMap), null); if (address == null) { throw new Win32Exception(); } var mapping = new MemoryMapping() { refCount = 1, fileStream = fs, baseAddress = address }; mapping.areas.Add(new MemoryMappedArea(mmf, address, fs.Length)); return(mapping); }