public Stream MapView(MapAccess access, long offset, IntPtr size) { if (!IsOpen) { throw new ObjectDisposedException("MMF already closed"); } IntPtr baseAddress = IntPtr.Zero; baseAddress = Win32MapApis.MapViewOfFile( _hMap, (int)access, (int)((offset >> 32) & 0xFFFFFFFF), (int)(offset & 0xFFFFFFFF), size ); if (baseAddress == IntPtr.Zero) { throw new FileMapIOException(Marshal.GetHRForLastWin32Error()); } // Find out what MapProtection to use // based on the MapAccess flags... MapProtection protection; if (access == MapAccess.FileMapRead) { protection = MapProtection.PageReadOnly; } else { protection = MapProtection.PageReadWrite; } return(new MapViewStream(baseAddress, size.ToInt64(), protection)); }
public void Flush(IntPtr viewBaseAddr) { // Throws OverflowException if (a) this is a 32-bit platform AND (b) size is out of bounds (ie. int bounds) with respect to this platform IntPtr flushLength = new IntPtr(MaxSize); Win32MapApis.FlushViewOfFile(viewBaseAddr, flushLength); }
public override void Flush() { if (!IsOpen) throw new ObjectDisposedException("Stream is closed"); // flush the view but leave the buffer intact // FIX: get rid of cast Win32MapApis.FlushViewOfFile(_base, (int)_length); }
protected virtual void Dispose(bool disposing) { if (IsOpen) Win32MapApis.CloseHandle(_hMap); _hMap = NULL_HANDLE; if (disposing) GC.SuppressFinalize(this); }
/// <summary> /// Open an existing named File Mapping object /// </summary> /// <param name="access">desired access to the map</param> /// <param name="name">name of object</param> /// <returns>The memory mapped file instance</returns> public static MemoryMappedFile Open(MapAccess access, String name) { MemoryMappedFile map = new MemoryMappedFile(); map._hMap = Win32MapApis.OpenFileMapping((int)access, false, name); if (map._hMap == NULL_HANDLE) throw new FileMapIOException(Marshal.GetHRForLastWin32Error()); return map; }
new protected virtual void Dispose(bool disposing) { if (IsOpen) { Flush(); // FIX: eliminate cast Win32MapApis.UnmapViewOfFile(_base); IsOpen = false; } if (disposing) GC.SuppressFinalize(this); }
Create(string fileName, MapProtection protection, long maxSize, String name) { MemoryMappedFile map = new MemoryMappedFile(); // open file first IntPtr hFile = INVALID_HANDLE_VALUE; if (fileName != null) { // determine file access needed // we'll always need generic read access int desiredAccess = GENERIC_READ; if ((protection == MapProtection.PageReadWrite) || (protection == MapProtection.PageWriteCopy)) { desiredAccess |= GENERIC_WRITE; } // open or create the file // if it doesn't exist, it gets created hFile = Win32MapApis.CreateFile( fileName, desiredAccess, 0, IntPtr.Zero, OPEN_ALWAYS, 0, IntPtr.Zero ); if (hFile == INVALID_HANDLE_VALUE) { throw new FileMapIOException(Marshal.GetHRForLastWin32Error()); } // FIX: Is support neede for zero-length files!?! } map._hMap = Win32MapApis.CreateFileMapping( hFile, IntPtr.Zero, (int)protection, (int)((maxSize >> 32) & 0xFFFFFFFF), (int)(maxSize & 0xFFFFFFFF), name ); // close file handle, we don't need it if (hFile != INVALID_HANDLE_VALUE) { Win32MapApis.CloseHandle(hFile); } if (map._hMap == NULL_HANDLE) { throw new FileMapIOException(Marshal.GetHRForLastWin32Error()); } return(map); }
/// <summary> /// Open an existing named File Mapping object /// </summary> /// <param name="access">desired access to the map</param> /// <param name="name">name of object</param> /// <returns>The memory mapped file instance</returns> public static MemoryMappedFile Open(MapAccess access, String name) { MemoryMappedFile map = new MemoryMappedFile { _hMap = Win32MapApis.OpenFileMapping((int)access, false, name) }; if (map._hMap == NULL_HANDLE) { throw new FileMapIOException(Marshal.GetHRForLastWin32Error()); } map._maxSize = -1; // debug unknown return(map); }
/// <summary> /// Open an existing named File Mapping object /// </summary> /// <param name="access">desired access to the map</param> /// <param name="name">name of object</param> /// <returns>The memory mapped file instance</returns> public static MemoryMappedFile Open(MapAccess access, String name) { MemoryMappedFile map = new MemoryMappedFile(); // Debug.Log("mem map start"); map._hMap = Win32MapApis.OpenFileMapping((int)access, false, name); if (map._hMap == NULL_HANDLE) { // Debug.Log("mem map doesnt exist"); return(null); } //throw new FileMapIOException ( Marshal.GetHRForLastWin32Error() ); return(map); }
protected virtual void Dispose(bool disposing) { // //UnityEngine.Debug.Log("dispose mem map1"); if (IsOpen) { Flush(); // FIX: eliminate cast Win32MapApis.UnmapViewOfFile(_base); //UnityEngine.Debug.Log("Dispose isOpen = false"); IsOpen = false; } if (disposing) { GC.SuppressFinalize(this); } }
public IntPtr MapView(MapAccess access, long offset, long size) { if (!IsOpen) { throw new ObjectDisposedException("Winterdom.IO.FileMap.MemoryMappedFile.MapView - MMF already closed"); } // Throws OverflowException if (a) this is a 32-bit platform AND (b) size is out of bounds (ie. int bounds) with respect to this platform IntPtr mapSize = new IntPtr(size); IntPtr baseAddress = Win32MapApis.MapViewOfFile( _hMap, (int)access, (int)((offset >> 32) & 0xFFFFFFFF), (int)(offset & 0xFFFFFFFF), mapSize ); if (baseAddress == IntPtr.Zero) { throw new FileMapIOException(Marshal.GetHRForLastWin32Error()); } return(baseAddress); }
public void UnMapView(IntPtr mapBaseAddr) { Win32MapApis.UnmapViewOfFile(mapBaseAddr); }
Create(string fileName, MapProtection protection, long maxSize, String name) { MemoryMappedFile map = new MemoryMappedFile(); if (!map.Is64bit && maxSize > uint.MaxValue) { throw new ConstraintException("32bit systems support max size of 4gb."); } // open file first IntPtr hFile = INVALID_HANDLE_VALUE; if (!string.IsNullOrEmpty(fileName)) { if (maxSize == 0) { if (!File.Exists(fileName)) { throw new Exception(string.Format("Winterdom.IO.FileMap.MemoryMappedFile.Create - \"{0}\" does not exist ==> Unable to map entire file", fileName)); } FileInfo backingFileInfo = new FileInfo(fileName); maxSize = backingFileInfo.Length; if (maxSize == 0) { throw new Exception(string.Format("Winterdom.IO.FileMap.MemoryMappedFile.Create - \"{0}\" is zero bytes ==> Unable to map entire file", fileName)); } } // determine file access needed // we'll always need generic read access int desiredAccess = GENERIC_READ; if ((protection == MapProtection.PageReadWrite) || (protection == MapProtection.PageWriteCopy)) { desiredAccess |= GENERIC_WRITE; } // open or create the file // if it doesn't exist, it gets created hFile = Win32MapApis.CreateFile( fileName, desiredAccess, 0, IntPtr.Zero, OPEN_ALWAYS, 0, IntPtr.Zero ); if (hFile == INVALID_HANDLE_VALUE) { throw new FileMapIOException(Marshal.GetHRForLastWin32Error()); } //SafeFileHandle handle = new SafeFileHandle(hFile,true); //NTFS.Sparse.SparseFile.MarkSparse(handle); map._fileName = fileName; } map._hMap = Win32MapApis.CreateFileMapping( hFile, IntPtr.Zero, (int)protection, (int)((maxSize >> 32) & 0xFFFFFFFF), (int)(maxSize & 0xFFFFFFFF), name ); // close file handle, we don't need it if (hFile != INVALID_HANDLE_VALUE) { Win32MapApis.CloseHandle(hFile); } if (map._hMap == NULL_HANDLE) { throw new FileMapIOException(Marshal.GetHRForLastWin32Error()); } map._protection = protection; map._maxSize = maxSize; return(map); }