public Stream CreateViewStream(long offset, long size, MemoryMappedFileAccess access) { IDisposable context = Acquire(); DisposableStream result = new DisposableStream(_mmf.CreateViewStream(offset, size, access)); result.AfterDispose.Add(context); return result; }
private static unsafe SafeMemoryMappedFileHandle CreateCore( FileStream fileStream, string mapName, HandleInheritability inheritability, MemoryMappedFileAccess access, MemoryMappedFileOptions options, long capacity) { if (mapName != null) { // Named maps are not supported in our Unix implementation. We could support named maps on Linux using // shared memory segments (shmget/shmat/shmdt/shmctl/etc.), but that doesn't work on OSX by default due // to very low default limits on OSX for the size of such objects; it also doesn't support behaviors // like copy-on-write or the ability to control handle inheritability, and reliably cleaning them up // relies on some non-conforming behaviors around shared memory IDs remaining valid even after they've // been marked for deletion (IPC_RMID). We could also support named maps using the current implementation // by not unlinking after creating the backing store, but then the backing stores would remain around // and accessible even after process exit, with no good way to appropriately clean them up. // (File-backed maps may still be used for cross-process communication.) throw CreateNamedMapsNotSupportedException(); } bool ownsFileStream = false; if (fileStream != null) { // This map is backed by a file. Make sure the file's size is increased to be // at least as big as the requested capacity of the map. if (fileStream.Length < capacity) { try { fileStream.SetLength(capacity); } catch (ArgumentException exc) { // If the capacity is too large, we'll get an ArgumentException from SetLength, // but on Windows this same condition is represented by an IOException. throw new IOException(exc.Message, exc); } } } else { // This map is backed by memory-only. With files, multiple views over the same map // will end up being able to share data through the same file-based backing store; // for anonymous maps, we need a similar backing store, or else multiple views would logically // each be their own map and wouldn't share any data. To achieve this, we create a backing object // (either memory or on disk, depending on the system) and use its file descriptor as the file handle. // However, we only do this when the permission is more than read-only. We can't change the size // of an object that has read-only permissions, but we also don't need to worry about sharing // views over a read-only, anonymous, memory-backed map, because the data will never change, so all views // will always see zero and can't change that. In that case, we just use the built-in anonymous support of // the map by leaving fileStream as null. Interop.libc.MemoryMappedProtections protections = MemoryMappedView.GetProtections(access, forVerification: false); if ((protections & Interop.libc.MemoryMappedProtections.PROT_WRITE) != 0 && capacity > 0) { ownsFileStream = true; fileStream = CreateSharedBackingObject(protections, capacity); } } return new SafeMemoryMappedFileHandle(fileStream, ownsFileStream, inheritability, access, options, capacity); }
unsafe void CreateStreamPosix (FileStream file, long offset, long size, MemoryMappedFileAccess access) { long fsize = file.Length; if (size == 0 || size > fsize) size = fsize; int offset_diff; MemoryMappedFile.MapPosix (file, offset, size, access, out mmap_addr, out offset_diff, out mmap_size); FileAccess faccess; switch (access) { case MemoryMappedFileAccess.ReadWrite: faccess = FileAccess.ReadWrite; break; case MemoryMappedFileAccess.Read: faccess = FileAccess.Read; break; case MemoryMappedFileAccess.Write: faccess = FileAccess.Write; break; default: throw new NotImplementedException ("access mode " + access + " not supported."); } Initialize ((byte*)mmap_addr + offset_diff, size, size, faccess); }
internal MemoryMappedViewAccessor (FileStream file, long offset, long size, MemoryMappedFileAccess access) { monitor = new Object (); if (Environment.OSVersion.Platform < PlatformID.Unix) throw new NotImplementedException ("Not implemented on windows."); else CreatePosix (file, offset, size, access); }
/// <summary>Performs basic verification on a map.</summary> /// <param name="mmf">The map.</param> /// <param name="expectedCapacity">The capacity that was specified to create the map.</param> /// <param name="expectedAccess">The access specified to create the map.</param> /// <param name="expectedInheritability">The inheritability specified to create the map.</param> protected static void ValidateMemoryMappedFile(MemoryMappedFile mmf, long expectedCapacity, MemoryMappedFileAccess expectedAccess = MemoryMappedFileAccess.ReadWrite, HandleInheritability expectedInheritability = HandleInheritability.None) { // Validate that we got a MemoryMappedFile object and that its handle is valid Assert.NotNull(mmf); Assert.NotNull(mmf.SafeMemoryMappedFileHandle); Assert.Same(mmf.SafeMemoryMappedFileHandle, mmf.SafeMemoryMappedFileHandle); Assert.False(mmf.SafeMemoryMappedFileHandle.IsClosed); Assert.False(mmf.SafeMemoryMappedFileHandle.IsInvalid); AssertInheritability(mmf.SafeMemoryMappedFileHandle, expectedInheritability); // Create and validate one or more views from the map if (IsReadable(expectedAccess) && IsWritable(expectedAccess)) { CreateAndValidateViews(mmf, expectedCapacity, MemoryMappedFileAccess.Read); CreateAndValidateViews(mmf, expectedCapacity, MemoryMappedFileAccess.Write); CreateAndValidateViews(mmf, expectedCapacity, MemoryMappedFileAccess.ReadWrite); } else if (IsWritable(expectedAccess)) { CreateAndValidateViews(mmf, expectedCapacity, MemoryMappedFileAccess.Write); } else if (IsReadable(expectedAccess)) { CreateAndValidateViews(mmf, expectedCapacity, MemoryMappedFileAccess.Read); } else { Assert.Throws<UnauthorizedAccessException>(() => mmf.CreateViewAccessor(0, expectedCapacity, MemoryMappedFileAccess.Read)); Assert.Throws<UnauthorizedAccessException>(() => mmf.CreateViewAccessor(0, expectedCapacity, MemoryMappedFileAccess.Write)); Assert.Throws<UnauthorizedAccessException>(() => mmf.CreateViewAccessor(0, expectedCapacity, MemoryMappedFileAccess.ReadWrite)); } }
private static SafeMemoryMappedFileHandle CreateCore( SafeFileHandle fileHandle, string mapName, HandleInheritability inheritability, MemoryMappedFileAccess access, MemoryMappedFileOptions options, long capacity) { Interop.SECURITY_ATTRIBUTES secAttrs = GetSecAttrs(inheritability); // split the long into two ints int capacityLow = unchecked((int)(capacity & 0x00000000FFFFFFFFL)); int capacityHigh = unchecked((int)(capacity >> 32)); SafeMemoryMappedFileHandle handle = fileHandle != null ? Interop.mincore.CreateFileMapping(fileHandle, ref secAttrs, GetPageAccess(access) | (int)options, capacityHigh, capacityLow, mapName) : Interop.mincore.CreateFileMapping(Interop.INVALID_HANDLE_VALUE, ref secAttrs, GetPageAccess(access) | (int)options, capacityHigh, capacityLow, mapName); int errorCode = Marshal.GetLastWin32Error(); if (!handle.IsInvalid) { if (errorCode == Interop.ERROR_ALREADY_EXISTS) { handle.Dispose(); throw Win32Marshal.GetExceptionForWin32Error(errorCode); } } else if (handle.IsInvalid) { throw Win32Marshal.GetExceptionForWin32Error(errorCode); } return handle; }
private static SafeMemoryMappedFileHandle CreateCore( FileStream fileStream, string mapName, HandleInheritability inheritability, MemoryMappedFileAccess access, MemoryMappedFileOptions options, long capacity) { SafeFileHandle fileHandle = fileStream != null ? fileStream.SafeFileHandle : null; Interop.Kernel32.SECURITY_ATTRIBUTES secAttrs = GetSecAttrs(inheritability); SafeMemoryMappedFileHandle handle = fileHandle != null ? Interop.CreateFileMapping(fileHandle, ref secAttrs, GetPageAccess(access) | (int)options, capacity, mapName) : Interop.CreateFileMapping(INVALID_HANDLE_VALUE, ref secAttrs, GetPageAccess(access) | (int)options, capacity, mapName); int errorCode = Marshal.GetLastWin32Error(); if (!handle.IsInvalid) { if (errorCode == Interop.Errors.ERROR_ALREADY_EXISTS) { handle.Dispose(); throw Win32Marshal.GetExceptionForWin32Error(errorCode); } } else // handle.IsInvalid { handle.Dispose(); throw Win32Marshal.GetExceptionForWin32Error(errorCode); } return handle; }
private MemoryMappedView(SafeMemoryMappedViewHandle viewHandle, long pointerOffset, long size, MemoryMappedFileAccess access) { this.m_viewHandle = viewHandle; this.m_pointerOffset = pointerOffset; this.m_size = size; this.m_access = access; }
private unsafe MemoryMappedView(SafeMemoryMappedViewHandle viewHandle, long pointerOffset, long size, MemoryMappedFileAccess access) { _viewHandle = viewHandle; _pointerOffset = pointerOffset; _size = size; _access = access; }
public MemoryMappedFileCommunicator(MemoryMappedFile rpMemoryMappedFile, long rpOffset, long rpSize, MemoryMappedFileAccess rpAccess) { r_MemoryMappedFile = rpMemoryMappedFile; r_ViewAccessor = rpMemoryMappedFile.CreateViewAccessor(rpOffset, rpSize, rpAccess); ReadPosition = -1; r_WritePosition = -1; }
private unsafe MemoryMappedView(SafeMemoryMappedViewHandle viewHandle, Int64 pointerOffset, Int64 size, MemoryMappedFileAccess access) { m_viewHandle = viewHandle; m_pointerOffset = pointerOffset; m_size = size; m_access = access; }
internal MemoryMappedViewStream (int fd, long offset, long size, MemoryMappedFileAccess access) { this.fd = fd; monitor = new Object (); if (MonoUtil.IsUnix) CreateStreamPosix (fd, offset, size, access); else throw new NotImplementedException ("Not implemented on windows."); }
internal MemoryMappedViewAccessor (int file_handle, long offset, long size, MemoryMappedFileAccess access) { this.file_handle = file_handle; if (MonoUtil.IsUnix) CreatePosix (offset, size, access); else throw new NotImplementedException ("Not implemented on windows."); }
public IMemoryMappedFile CreateFromFile( string path, FileMode mode, string mapName, long capacity, MemoryMappedFileAccess access) { return new MemoryMappedFileWrapper(MemoryMappedFile.CreateFromFile(path, mode, mapName, capacity, access)); }
unsafe void Create (IntPtr handle, long offset, long size, MemoryMappedFileAccess access) { IntPtr base_address; MemoryMapImpl.Map (handle, offset, ref size, access, out mmap_handle, out base_address); safe_handle = new SafeMemoryMappedViewHandle (mmap_handle, base_address, size); Initialize (safe_handle, 0, size, ToFileAccess (access)); }
private static SafeMemoryMappedFileHandle CreateOrOpenCore( string mapName, HandleInheritability inheritability, MemoryMappedFileAccess access, MemoryMappedFileOptions options, long capacity) { // Since we don't support mapName != null, CreateOrOpenCore can't // be used to Open an existing map, and thus is identical to CreateCore. return CreateCore(null, mapName, inheritability, access, options, capacity); }
public void ValidAccessLevelCombinations(MemoryMappedFileAccess mapAccess, MemoryMappedFileAccess viewAccess) { const int Capacity = 4096; using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew(null, Capacity, mapAccess)) using (MemoryMappedViewStream s = mmf.CreateViewStream(0, Capacity, viewAccess)) { ValidateMemoryMappedViewStream(s, Capacity, viewAccess); } }
unsafe void CreatePosix (long offset, long size, MemoryMappedFileAccess access) { int offset_diff; MemoryMappedFile.MapPosix (file_handle, offset, ref size, access, out mmap_addr, out offset_diff); handle = new SafeMemoryMappedViewHandle ((IntPtr)((long)mmap_addr + offset_diff), size); Initialize (handle, 0, size, ToFileAccess (access)); }
public Win32MemoryMapPager(string file, long? initialFileSize = null, Win32NativeFileAttributes options = Win32NativeFileAttributes.Normal, Win32NativeFileAccess access = Win32NativeFileAccess.GenericRead | Win32NativeFileAccess.GenericWrite) { Win32NativeMethods.SYSTEM_INFO systemInfo; Win32NativeMethods.GetSystemInfo(out systemInfo); AllocationGranularity = systemInfo.allocationGranularity; _access = access; _memoryMappedFileAccess = _access == Win32NativeFileAccess.GenericRead ? MemoryMappedFileAccess.Read : MemoryMappedFileAccess.ReadWrite; _handle = Win32NativeFileMethods.CreateFile(file, access, Win32NativeFileShare.Read | Win32NativeFileShare.Write | Win32NativeFileShare.Delete, IntPtr.Zero, Win32NativeFileCreationDisposition.OpenAlways, options, IntPtr.Zero); if (_handle.IsInvalid) { int lastWin32ErrorCode = Marshal.GetLastWin32Error(); throw new IOException("Failed to open file storage of Win32MemoryMapPager", new Win32Exception(lastWin32ErrorCode)); } _fileInfo = new FileInfo(file); var streamAccessType = _access == Win32NativeFileAccess.GenericRead ? FileAccess.Read : FileAccess.ReadWrite; _fileStream = new FileStream(_handle, streamAccessType); _totalAllocationSize = _fileInfo.Length; if (_access.HasFlag(Win32NativeFileAccess.GenericWrite) || _access.HasFlag(Win32NativeFileAccess.GenericAll) || _access.HasFlag(Win32NativeFileAccess.FILE_GENERIC_WRITE)) { var fileLength = _fileStream.Length; if (fileLength == 0 && initialFileSize.HasValue) fileLength = initialFileSize.Value; if (_fileStream.Length == 0 || (fileLength % AllocationGranularity != 0)) { fileLength = NearestSizeToAllocationGranularity(fileLength); Win32NativeFileMethods.SetFileLength(_handle, fileLength); } _totalAllocationSize = fileLength; } NumberOfAllocatedPages = _totalAllocationSize / PageSize; PagerState.Release(); PagerState = CreatePagerState(); }
public IMemoryMappedFile CreateOrOpen( string mapName, long capacity, MemoryMappedFileAccess access, MemoryMappedFileOptions options, MemoryMappedFileSecurity memoryMappedFileSecurity, HandleInheritability inheritability) { return new MemoryMappedFileWrapper(MemoryMappedFile.CreateOrOpen( mapName, capacity, access, options, memoryMappedFileSecurity, inheritability)); }
unsafe void CreatePosix (FileStream file, long offset, long size, MemoryMappedFileAccess access) { long fsize = file.Length; if (size == 0 || size > fsize) size = fsize; int offset_diff; MemoryMappedFile.MapPosix (file, offset, size, access, out mmap_addr, out offset_diff, out mmap_size); handle = new SafeMemoryMappedViewHandle ((IntPtr)((long)mmap_addr + offset_diff), size); }
/// <summary> /// Creates and yields a variety of different maps, suitable for a wide range of testing of /// views created from maps. /// </summary> protected IEnumerable<MemoryMappedFile> CreateSampleMaps( int capacity = 4096, MemoryMappedFileAccess access = MemoryMappedFileAccess.ReadWrite, [CallerMemberName]string fileName = null, [CallerLineNumber] int lineNumber = 0) { yield return MemoryMappedFile.CreateNew(null, capacity, access); yield return MemoryMappedFile.CreateFromFile(Path.Combine(TestDirectory, Guid.NewGuid().ToString("N")), FileMode.CreateNew, null, capacity, access); if (MapNamesSupported) { yield return MemoryMappedFile.CreateNew(CreateUniqueMapName(), capacity, access); yield return MemoryMappedFile.CreateFromFile(GetTestFilePath(null, fileName, lineNumber), FileMode.CreateNew, CreateUniqueMapName(), capacity, access); } }
public MemoryMappedFileCommunicator(MemoryMappedFile mappedFile, long offset, long size, MemoryMappedFileAccess access) { MappedFile = mappedFile; view = mappedFile.CreateViewAccessor(offset, size, access); ReadPosition = -1; writePosition = -1; dataToSend = new List<byte[]>(); callback = new SendOrPostCallback(OnDataReceivedInternal); operation = AsyncOperationManager.CreateOperation(null); }
private static unsafe SafeMemoryMappedFileHandle CreateCore( SafeFileHandle fileHandle, string mapName, HandleInheritability inheritability, MemoryMappedFileAccess access, MemoryMappedFileOptions options, long capacity) { if (mapName != null) { throw CreateNamedMapsNotSupportedException(); } return new SafeMemoryMappedFileHandle(fileHandle, inheritability, access, options, capacity); }
internal unsafe static MemoryMappedView Create (IntPtr handle, long offset, long size, MemoryMappedFileAccess access) { IntPtr base_address; IntPtr mmap_handle; MemoryMapImpl.Map (handle, offset, ref size, access, out mmap_handle, out base_address); var safe_handle = new SafeMemoryMappedViewHandle (mmap_handle, base_address, size); // MemoryMapImpl.Map returns a base_address to the offset so MemoryMappedView is initiated // no offset. return new MemoryMappedView (safe_handle, 0, size, access); }
internal static MemoryMappedView CreateView(SafeMemoryMappedFileHandle memMappedFileHandle, MemoryMappedFileAccess access, long offset, long size) { ulong num3; ulong num = (ulong) (offset % ((long) MemoryMappedFile.GetSystemPageAllocationGranularity())); ulong num2 = ((ulong) offset) - num; if (size != 0L) { num3 = ((ulong) size) + num; } else { num3 = 0L; } if ((IntPtr.Size == 4) && (num3 > 0xffffffffL)) { throw new ArgumentOutOfRangeException("size", System.SR.GetString("ArgumentOutOfRange_CapacityLargerThanLogicalAddressSpaceNotAllowed")); } Microsoft.Win32.UnsafeNativeMethods.MEMORYSTATUSEX lpBuffer = new Microsoft.Win32.UnsafeNativeMethods.MEMORYSTATUSEX(); Microsoft.Win32.UnsafeNativeMethods.GlobalMemoryStatusEx(lpBuffer); ulong ullTotalVirtual = lpBuffer.ullTotalVirtual; if (num3 >= ullTotalVirtual) { throw new IOException(System.SR.GetString("IO_NotEnoughMemory")); } uint dwFileOffsetLow = (uint) (num2 & 0xffffffffL); uint dwFileOffsetHigh = (uint) (num2 >> 0x20); SafeMemoryMappedViewHandle address = Microsoft.Win32.UnsafeNativeMethods.MapViewOfFile(memMappedFileHandle, MemoryMappedFile.GetFileMapAccess(access), dwFileOffsetHigh, dwFileOffsetLow, new UIntPtr(num3)); if (address.IsInvalid) { System.IO.__Error.WinIOError(Marshal.GetLastWin32Error(), string.Empty); } Microsoft.Win32.UnsafeNativeMethods.MEMORY_BASIC_INFORMATION buffer = new Microsoft.Win32.UnsafeNativeMethods.MEMORY_BASIC_INFORMATION(); Microsoft.Win32.UnsafeNativeMethods.VirtualQuery(address, ref buffer, (IntPtr) Marshal.SizeOf(buffer)); ulong regionSize = (ulong) buffer.RegionSize; if ((buffer.State & 0x2000) != 0) { Microsoft.Win32.UnsafeNativeMethods.VirtualAlloc(address, (UIntPtr) regionSize, 0x1000, MemoryMappedFile.GetPageAccess(access)); int errorCode = Marshal.GetLastWin32Error(); if (address.IsInvalid) { System.IO.__Error.WinIOError(errorCode, string.Empty); } } if (size == 0L) { size = (long) (regionSize - num); } address.Initialize(((ulong) size) + num); return new MemoryMappedView(address, (long) num, size, access); }
static FileAccess ToFileAccess (MemoryMappedFileAccess access) { switch (access){ case MemoryMappedFileAccess.CopyOnWrite: case MemoryMappedFileAccess.ReadWrite: case MemoryMappedFileAccess.ReadWriteExecute: return FileAccess.ReadWrite; case MemoryMappedFileAccess.Write: return FileAccess.Write; case MemoryMappedFileAccess.ReadExecute: case MemoryMappedFileAccess.Read: default: return FileAccess.Read; } }
public FileSegment(MemoryMappedFile mmf, long offset, long length, MemoryMappedFileAccess access) { _mmf = Exceptions.CheckArgumentNull(mmf, "mmf"); try { _length = length; if (_length == 0) _stream = new MemoryStream(); else _stream = mmf.CreateViewStream(offset, length, access); } catch { _mmf.Dispose(); throw; } }
private static unsafe SafeMemoryMappedFileHandle CreateCore( FileStream fileStream, string mapName, HandleInheritability inheritability, MemoryMappedFileAccess access, MemoryMappedFileOptions options, long capacity) { if (mapName != null) { // TODO: We currently do not support named maps. We could possibly support // named maps in the future by using shm_open / shm_unlink, as we do for // giving internal names to anonymous maps. Issues to work through will include // dealing with permissions, passing information from the creator of the // map to another opener of it, etc. throw CreateNamedMapsNotSupportedException(); } var fileStreamSource = SafeMemoryMappedFileHandle.FileStreamSource.Provided; if (fileStream != null) { // This map is backed by a file. Make sure the file's size is increased to be // at least as big as the requested capacity of the map. if (fileStream.Length < capacity) { fileStream.SetLength(capacity); } } else { // This map is backed by memory-only. With files, multiple views over the same map // will end up being able to share data through the same file-based backing store; // for anonymous maps, we need a similar backing store, or else multiple views would logically // each be their own map and wouldn't share any data. To achieve this, we create a backing object // (either memory or on disk, depending on the system) and use its file descriptor as the file handle. // However, we only do this when the permission is more than read-only. We can't change the size // of an object that has read-only permissions, but we also don't need to worry about sharing // views over a read-only, anonymous, memory-backed map, because the data will never change, so all views // will always see zero and can't change that. In that case, we just use the built-in anonymous support of // the map by leaving fileHandle as null. Interop.libc.MemoryMappedProtections protections = MemoryMappedView.GetProtections(access, forVerification: false); if ((protections & Interop.libc.MemoryMappedProtections.PROT_WRITE) != 0 && capacity > 0) { fileStream = CreateSharedBackingObject(protections, capacity, out mapName, out fileStreamSource); } } return new SafeMemoryMappedFileHandle(mapName, fileStream, fileStreamSource, inheritability, access, options, capacity); }
// // Turns the MemoryMappedFileAccess into the second half of open(2) flags // static OpenFlags ToUnixMode (MemoryMappedFileAccess access) { switch (access){ case MemoryMappedFileAccess.CopyOnWrite: case MemoryMappedFileAccess.ReadWriteExecute: case MemoryMappedFileAccess.ReadWrite: return OpenFlags.O_RDWR; case MemoryMappedFileAccess.Write: return OpenFlags.O_WRONLY; case MemoryMappedFileAccess.ReadExecute: case MemoryMappedFileAccess.Read: default: return OpenFlags.O_RDONLY; } }
public static MemoryMappedFile CreateNew(string mapName, long capacity, MemoryMappedFileAccess access) { return(CreateNew(mapName, capacity, access, MemoryMappedFileOptions.None, null, HandleInheritability.None)); }
internal static IntPtr OpenFile(string path, FileMode mode, string mapName, out long capacity, MemoryMappedFileAccess access, MemoryMappedFileOptions options) { int error = 0; IntPtr res = OpenFileInternal(path, mode, mapName, out capacity, access, options, out error); if (error != 0) { throw CreateException(error, path); } return(res); }
public static MemoryMappedFile CreateOrOpen(string mapName, long capacity, MemoryMappedFileAccess access, MemoryMappedFileOptions options, HandleInheritability inheritability);
extern static int MapInternal(IntPtr handle, long offset, ref long size, MemoryMappedFileAccess access, out IntPtr mmap_handle, out IntPtr base_address);
public bool RunTest() { try { if (File.Exists(s_fileNameTest1)) { File.Delete(s_fileNameTest1); } if (File.Exists(s_fileNameTest2)) { File.Delete(s_fileNameTest2); } String fileText = "Non-empty file for MMF testing."; File.WriteAllText(s_fileNameTest2, fileText); //////////////////////////////////////////////////////////////////////// // CreateFromFile(String) //////////////////////////////////////////////////////////////////////// // [] fileName // null fileName VerifyCreateFromFileException <ArgumentNullException>("Loc001", null); // existing file VerifyCreateFromFile("Loc002", s_fileNameTest2); // nonexistent file if (File.Exists(s_fileNameNonexistent)) { File.Delete(s_fileNameNonexistent); } VerifyCreateFromFileException <FileNotFoundException>("Loc003", s_fileNameNonexistent); // FS open using (FileStream fs = new FileStream(s_fileNameTest2, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)) { VerifyCreateFromFileException <IOException>("Loc004a", s_fileNameTest2); } // same file - not allowed using (MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(s_fileNameTest2)) { VerifyCreateFromFileException <IOException>("Loc004b", s_fileNameTest2); } //////////////////////////////////////////////////////////////////////// // CreateFromFile(String, FileMode) //////////////////////////////////////////////////////////////////////// if (File.Exists(s_fileNameTest1)) { File.Delete(s_fileNameTest1); } if (File.Exists(s_fileNameTest2)) { File.Delete(s_fileNameTest2); } File.WriteAllText(s_fileNameTest2, fileText); // [] fileName // null fileName VerifyCreateFromFileException <ArgumentNullException>("Loc101", null, FileMode.Open); // existing file - open VerifyCreateFromFile("Loc102a", s_fileNameTest2, FileMode.Open); VerifyCreateFromFile("Loc102b", s_fileNameTest2, FileMode.OpenOrCreate); // existing file - create // can't create new since it exists VerifyCreateFromFileException <IOException>("Loc102d", s_fileNameTest2, FileMode.CreateNew); // newly created file - exception with default capacity VerifyCreateFromFileException <ArgumentException>("Loc102c", s_fileNameTest2, FileMode.Create); VerifyCreateFromFileException <ArgumentException>("Loc102f", s_fileNameTest2, FileMode.Truncate); // append not allowed VerifyCreateFromFileException <ArgumentException>("Loc102e", s_fileNameTest2, FileMode.Append); // nonexistent file - error if (File.Exists(s_fileNameNonexistent)) { File.Delete(s_fileNameNonexistent); } VerifyCreateFromFileException <FileNotFoundException>("Loc103a", s_fileNameNonexistent, FileMode.Open); // newly created file - exception with default capacity VerifyCreateFromFileException <ArgumentException>("Loc103b", s_fileNameTest1, FileMode.OpenOrCreate); VerifyCreateFromFileException <ArgumentException>("Loc103c", s_fileNameTest1, FileMode.CreateNew); VerifyCreateFromFileException <ArgumentException>("Loc103d", s_fileNameTest1, FileMode.Create); VerifyCreateFromFileException <ArgumentException>("Loc103e", s_fileNameTest2, FileMode.Truncate); // append not allowed VerifyCreateFromFileException <ArgumentException>("Loc103f", s_fileNameTest2, FileMode.Append); // empty file - exception with default capacity using (FileStream fs = new FileStream(s_fileNameTest1, FileMode.Create)) { } VerifyCreateFromFileException <ArgumentException>("Loc104a", s_fileNameTest1, FileMode.Open); VerifyCreateFromFileException <ArgumentException>("Loc104b", s_fileNameTest1, FileMode.OpenOrCreate); VerifyCreateFromFileException <ArgumentException>("Loc104c", s_fileNameTest1, FileMode.Create); VerifyCreateFromFileException <ArgumentException>("Loc104d", s_fileNameTest1, FileMode.Truncate); // can't create new since it exists VerifyCreateFromFileException <IOException>("Loc104e", s_fileNameTest1, FileMode.CreateNew); // append not allowed VerifyCreateFromFileException <ArgumentException>("Loc104f", s_fileNameTest1, FileMode.Append); // FS open using (FileStream fs = new FileStream(s_fileNameTest2, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)) { VerifyCreateFromFileException <IOException>("Loc105a", s_fileNameTest2, FileMode.Open); } // same file - not allowed File.WriteAllText(s_fileNameTest2, fileText); using (MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(s_fileNameTest2)) { VerifyCreateFromFileException <IOException>("Loc105b", s_fileNameTest2, FileMode.Open); } //////////////////////////////////////////////////////////////////////// // CreateFromFile(String, FileMode, String) //////////////////////////////////////////////////////////////////////// if (File.Exists(s_fileNameTest1)) { File.Delete(s_fileNameTest1); } if (File.Exists(s_fileNameTest2)) { File.Delete(s_fileNameTest2); } File.WriteAllText(s_fileNameTest2, fileText); File.WriteAllText(s_fileNameTest3, fileText + fileText); // [] fileName // null fileName VerifyCreateFromFileException <ArgumentNullException>("Loc201", null, FileMode.Open); // existing file - open VerifyCreateFromFile("Loc202a", s_fileNameTest2, FileMode.Open); VerifyCreateFromFile("Loc202b", s_fileNameTest2, FileMode.OpenOrCreate); // existing file - create // can't create new since it exists VerifyCreateFromFileException <IOException>("Loc202d", s_fileNameTest2, FileMode.CreateNew); // newly created file - exception with default capacity VerifyCreateFromFileException <ArgumentException>("Loc202c", s_fileNameTest2, FileMode.Create); VerifyCreateFromFileException <ArgumentException>("Loc202f", s_fileNameTest2, FileMode.Truncate); // append not allowed VerifyCreateFromFileException <ArgumentException>("Loc202e", s_fileNameTest2, FileMode.Append); // nonexistent file - error if (File.Exists(s_fileNameNonexistent)) { File.Delete(s_fileNameNonexistent); } VerifyCreateFromFileException <FileNotFoundException>("Loc203a", s_fileNameNonexistent, FileMode.Open); // newly created file - exception with default capacity VerifyCreateFromFileException <ArgumentException>("Loc203b", s_fileNameTest1, FileMode.OpenOrCreate); VerifyCreateFromFileException <ArgumentException>("Loc203c", s_fileNameTest1, FileMode.CreateNew); VerifyCreateFromFileException <ArgumentException>("Loc203d", s_fileNameTest1, FileMode.Create); VerifyCreateFromFileException <ArgumentException>("Loc203e", s_fileNameTest2, FileMode.Truncate); // append not allowed VerifyCreateFromFileException <ArgumentException>("Loc203f", s_fileNameTest2, FileMode.Append); // empty file - exception with default capacity using (FileStream fs = new FileStream(s_fileNameTest1, FileMode.Create)) { } VerifyCreateFromFileException <ArgumentException>("Loc204a", s_fileNameTest1, FileMode.Open); VerifyCreateFromFileException <ArgumentException>("Loc204b", s_fileNameTest1, FileMode.OpenOrCreate); VerifyCreateFromFileException <ArgumentException>("Loc204c", s_fileNameTest1, FileMode.Create); VerifyCreateFromFileException <ArgumentException>("Loc204d", s_fileNameTest1, FileMode.Truncate); // can't create new since it exists VerifyCreateFromFileException <IOException>("Loc204e", s_fileNameTest1, FileMode.CreateNew); // append not allowed VerifyCreateFromFileException <ArgumentException>("Loc204f", s_fileNameTest1, FileMode.Append); // FS open using (FileStream fs = new FileStream(s_fileNameTest2, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)) { VerifyCreateFromFileException <IOException>("Loc205a", s_fileNameTest2, FileMode.Open); } // same file - not allowed File.WriteAllText(s_fileNameTest2, fileText); using (MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(s_fileNameTest2)) { VerifyCreateFromFileException <IOException>("Loc205b", s_fileNameTest2, FileMode.Open); } // [] mapName // mapname > 260 chars VerifyCreateFromFile("Loc211", s_fileNameTest2, FileMode.Open, "CreateFromFile2" + new String('a', 1000) + s_uniquifier); // null VerifyCreateFromFile("Loc212", s_fileNameTest2, FileMode.Open, null); // empty string disallowed VerifyCreateFromFileException <ArgumentException>("Loc213", s_fileNameTest2, FileMode.Open, String.Empty); // all whitespace VerifyCreateFromFile("Loc214", s_fileNameTest2, FileMode.Open, "\t \n\u00A0"); // MMF with this mapname already exists if (Interop.IsWindows) // named maps not supported on Unix { using (MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(s_fileNameTest3, FileMode.Open, "map215" + s_uniquifier)) { VerifyCreateFromFileException <IOException>("Loc215", s_fileNameTest2, FileMode.Open, "map215" + s_uniquifier); } } // MMF with this mapname existed, but was closed VerifyCreateFromFile("Loc216", s_fileNameTest2, FileMode.Open, "map215" + s_uniquifier); // "global/" prefix VerifyCreateFromFile("Loc217", s_fileNameTest2, FileMode.Open, "global/CFF_0" + s_uniquifier); // "local/" prefix VerifyCreateFromFile("Loc218", s_fileNameTest2, FileMode.Open, "local/CFF_1" + s_uniquifier); //////////////////////////////////////////////////////////////////////// // CreateFromFile(String, FileMode, String, long) //////////////////////////////////////////////////////////////////////// // [] fileName // null fileName VerifyCreateFromFileException <ArgumentNullException>("Loc301", null, FileMode.Open, "CFF_mapname" + s_uniquifier, 0); // [] capacity // newly created file - exception with default capacity if (File.Exists(s_fileNameTest1)) { File.Delete(s_fileNameTest1); } VerifyCreateFromFileException <ArgumentException>("Loc311", s_fileNameTest1, FileMode.CreateNew, "CFF_mapname211" + s_uniquifier, 0); // newly created file - valid with >0 capacity if (File.Exists(s_fileNameTest1)) { File.Delete(s_fileNameTest1); } VerifyCreateFromFile("Loc312", s_fileNameTest1, FileMode.CreateNew, "CFF_mapname312" + s_uniquifier, 1); // existing file, default capacity VerifyCreateFromFile("Loc313", s_fileNameTest2, FileMode.Open, "CFF_mapname313" + s_uniquifier, 0); // existing file, capacity less than file size File.WriteAllText(s_fileNameTest2, fileText); VerifyCreateFromFileException <ArgumentOutOfRangeException>("Loc314", s_fileNameTest2, FileMode.Open, "CFF_mapname314" + s_uniquifier, 6); // existing file, capacity equal to file size File.WriteAllText(s_fileNameTest2, fileText); VerifyCreateFromFile("Loc315", s_fileNameTest2, FileMode.Open, "CFF_mapname315" + s_uniquifier, fileText.Length); // existing file, capacity greater than file size File.WriteAllText(s_fileNameTest2, fileText); VerifyCreateFromFile("Loc316", s_fileNameTest2, FileMode.Open, "CFF_mapname316" + s_uniquifier, 6000); // negative VerifyCreateFromFileException <ArgumentOutOfRangeException>("Loc317", s_fileNameTest2, FileMode.Open, "CFF_mapname317" + s_uniquifier, -1); // negative VerifyCreateFromFileException <ArgumentOutOfRangeException>("Loc318", s_fileNameTest2, FileMode.Open, "CFF_mapname318" + s_uniquifier, -4096); VerifyCreateFromFileException <IOException>("Loc319b", s_fileNameTest2, FileMode.Open, "CFF_mapname319" + s_uniquifier, Int64.MaxValue); // valid but too large //////////////////////////////////////////////////////////////////////// // CreateFromFile(String, FileMode, String, long, MemoryMappedFileAccess) //////////////////////////////////////////////////////////////////////// // [] capacity // existing file, capacity less than file size, MemoryMappedFileAccess.Read File.WriteAllText(s_fileNameTest2, fileText); VerifyCreateFromFileException <ArgumentOutOfRangeException>("Loc414", s_fileNameTest2, FileMode.Open, "CFF_mapname414" + s_uniquifier, 6, MemoryMappedFileAccess.Read); // existing file, capacity equal to file size, MemoryMappedFileAccess.Read File.WriteAllText(s_fileNameTest2, fileText); VerifyCreateFromFile("Loc415", s_fileNameTest2, FileMode.Open, "CFF_mapname415" + s_uniquifier, fileText.Length, MemoryMappedFileAccess.Read); // existing file, capacity greater than file size, MemoryMappedFileAccess.Read File.WriteAllText(s_fileNameTest2, fileText); VerifyCreateFromFileException <ArgumentException>("Loc416", s_fileNameTest2, FileMode.Open, "CFF_mapname416" + s_uniquifier, 6000, MemoryMappedFileAccess.Read); //////////////////////////////////////////////////////////////////////// // CreateFromFile(FileStream, String, long, MemoryMappedFileAccess, // MemoryMappedFileSecurity, HandleInheritability, bool) //////////////////////////////////////////////////////////////////////// if (File.Exists(s_fileNameTest1)) { File.Delete(s_fileNameTest1); } File.WriteAllText(s_fileNameTest2, fileText); File.WriteAllText(s_fileNameTest3, fileText + fileText); // [] fileStream // null filestream VerifyCreateFromFileException <ArgumentNullException>("Loc401", null, "map401" + s_uniquifier, 4096, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, false); // existing file using (FileStream fs = new FileStream(s_fileNameTest2, FileMode.Open)) { VerifyCreateFromFile("Loc402", fs, "map402" + s_uniquifier, 4096, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, false); } if (Interop.IsWindows) // named maps not supported on Unix { // same FS using (FileStream fs = new FileStream(s_fileNameTest2, FileMode.Open)) { using (MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(fs, "map403a" + s_uniquifier, 8192, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, false)) { VerifyCreateFromFile("Loc403", fs, "map403" + s_uniquifier, 8192, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, false); } } } // closed FS using (FileStream fs = new FileStream(s_fileNameTest2, FileMode.Open)) { fs.Dispose(); VerifyCreateFromFileException <ObjectDisposedException>("Loc404", fs, "map404" + s_uniquifier, 4096, MemoryMappedFileAccess.Read, HandleInheritability.None, false); } // newly created file - exception with default capacity using (FileStream fs = new FileStream(s_fileNameTest1, FileMode.CreateNew)) { VerifyCreateFromFileException <ArgumentException>("Loc405", fs, "map405" + s_uniquifier, 0, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, false); } // empty file - exception with default capacity using (FileStream fs = new FileStream(s_fileNameTest1, FileMode.Open)) { VerifyCreateFromFileException <ArgumentException>("Loc406", fs, "map406" + s_uniquifier, 0, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, false); } // [] mapName // mapname > 260 chars File.WriteAllText(s_fileNameTest2, fileText); using (FileStream fs = new FileStream(s_fileNameTest2, FileMode.Open)) { VerifyCreateFromFile("Loc411", fs, "CreateFromFile" + new String('a', 1000) + s_uniquifier, 4096, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, false); } // null File.WriteAllText(s_fileNameTest2, fileText); using (FileStream fs = new FileStream(s_fileNameTest2, FileMode.Open)) { VerifyCreateFromFile("Loc412", fs, null, 4096, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, false); } // empty string disallowed using (FileStream fs = new FileStream(s_fileNameTest2, FileMode.Open)) { VerifyCreateFromFileException <ArgumentException>("Loc413", fs, String.Empty, 4096, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, false); } // all whitespace File.WriteAllText(s_fileNameTest2, fileText); using (FileStream fs = new FileStream(s_fileNameTest2, FileMode.Open)) { VerifyCreateFromFile("Loc414", fs, "\t \n\u00A0", 4096, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, false); } if (Interop.IsWindows) // named maps not supported on Unix { // MMF with this mapname already exists using (MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(s_fileNameTest3, FileMode.Open, "map415" + s_uniquifier)) { using (FileStream fs2 = new FileStream(s_fileNameTest2, FileMode.Open)) { VerifyCreateFromFileException <IOException>("Loc415", fs2, "map415" + s_uniquifier, 4096, MemoryMappedFileAccess.Read, HandleInheritability.None, false); } } } // MMF with this mapname existed, but was closed using (FileStream fs = new FileStream(s_fileNameTest2, FileMode.Open)) { VerifyCreateFromFile("Loc416", fs, "map415" + s_uniquifier, 4096, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, false); } // "global/" prefix using (FileStream fs = new FileStream(s_fileNameTest2, FileMode.Open)) { VerifyCreateFromFile("Loc417", fs, "global/CFF_2" + s_uniquifier, 4096, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, false); } // "local/" prefix using (FileStream fs = new FileStream(s_fileNameTest2, FileMode.Open)) { VerifyCreateFromFile("Loc418", fs, "local/CFF_3" + s_uniquifier, 4096, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, false); } // [] capacity // newly created file - exception with default capacity if (File.Exists(s_fileNameTest1)) { File.Delete(s_fileNameTest1); } using (FileStream fs = new FileStream(s_fileNameTest1, FileMode.CreateNew)) { VerifyCreateFromFileException <ArgumentException>("Loc421", fs, "CFF_mapname421" + s_uniquifier, 0, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, false); } // newly created file - valid with >0 capacity if (File.Exists(s_fileNameTest1)) { File.Delete(s_fileNameTest1); } using (FileStream fs = new FileStream(s_fileNameTest1, FileMode.CreateNew)) { VerifyCreateFromFile("Loc422", fs, "CFF_mapname422" + s_uniquifier, 1, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, false); } // existing file, default capacity using (FileStream fs = new FileStream(s_fileNameTest2, FileMode.Open)) { VerifyCreateFromFile("Loc423", fs, "CFF_mapname423" + s_uniquifier, 0, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, false); } // existing file, capacity less than file size File.WriteAllText(s_fileNameTest2, fileText); using (FileStream fs = new FileStream(s_fileNameTest2, FileMode.Open)) { VerifyCreateFromFileException <ArgumentOutOfRangeException>("Loc424", fs, "CFF_mapname424" + s_uniquifier, 6, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, false); } // existing file, capacity equal to file size File.WriteAllText(s_fileNameTest2, fileText); using (FileStream fs = new FileStream(s_fileNameTest2, FileMode.Open)) { VerifyCreateFromFile("Loc425", fs, "CFF_mapname425" + s_uniquifier, fileText.Length, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, false); } // existing file, capacity greater than file size File.WriteAllText(s_fileNameTest2, fileText); using (FileStream fs = new FileStream(s_fileNameTest2, FileMode.Open)) { VerifyCreateFromFile("Loc426", fs, "CFF_mapname426" + s_uniquifier, 6000, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, false); } // existing file, capacity greater than file size & access = Read only File.WriteAllText(s_fileNameTest2, fileText); using (FileStream fs = new FileStream(s_fileNameTest2, FileMode.Open)) { VerifyCreateFromFileException <ArgumentException>("Loc426a", fs, "CFF_mapname426a" + s_uniquifier, 6000, MemoryMappedFileAccess.Read, HandleInheritability.None, false); } // negative using (FileStream fs = new FileStream(s_fileNameTest2, FileMode.Open)) { VerifyCreateFromFileException <ArgumentOutOfRangeException>("Loc427", fs, "CFF_mapname427" + s_uniquifier, -1, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, false); } // negative using (FileStream fs = new FileStream(s_fileNameTest2, FileMode.Open)) { VerifyCreateFromFileException <ArgumentOutOfRangeException>("Loc428", fs, "CFF_mapname428" + s_uniquifier, -4096, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, false); } // Int64.MaxValue - cannot exceed local address space using (FileStream fs = new FileStream(s_fileNameTest2, FileMode.Open)) { VerifyCreateFromFileException <IOException>("Loc429", fs, "CFF_mapname429" + s_uniquifier, Int64.MaxValue, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, false); // valid but too large } // [] access // Write is disallowed using (FileStream fs = new FileStream(s_fileNameTest2, FileMode.Open, FileAccess.ReadWrite)) { VerifyCreateFromFileException <ArgumentException>("Loc430", fs, "CFF_mapname430" + s_uniquifier, 0, MemoryMappedFileAccess.Write, HandleInheritability.None, false); } // valid access (filestream is ReadWrite) MemoryMappedFileAccess[] accessList = new MemoryMappedFileAccess[] { MemoryMappedFileAccess.Read, MemoryMappedFileAccess.ReadWrite, MemoryMappedFileAccess.CopyOnWrite, }; foreach (MemoryMappedFileAccess access in accessList) { using (FileStream fs = new FileStream(s_fileNameTest2, FileMode.Open, FileAccess.ReadWrite)) { VerifyCreateFromFile("Loc431_" + access, fs, "CFF_mapname431_" + access + s_uniquifier, 0, access, HandleInheritability.None, false); } } // invalid access (filestream is ReadWrite) accessList = new MemoryMappedFileAccess[] { MemoryMappedFileAccess.ReadExecute, MemoryMappedFileAccess.ReadWriteExecute, }; foreach (MemoryMappedFileAccess access in accessList) { using (FileStream fs = new FileStream(s_fileNameTest2, FileMode.Open, FileAccess.ReadWrite)) { VerifyCreateFromFileException <UnauthorizedAccessException>("Loc432_" + access, fs, "CFF_mapname432_" + access + s_uniquifier, 0, access, HandleInheritability.None, false); } } // valid access (filestream is Read only) accessList = new MemoryMappedFileAccess[] { MemoryMappedFileAccess.Read, MemoryMappedFileAccess.CopyOnWrite, }; foreach (MemoryMappedFileAccess access in accessList) { using (FileStream fs = new FileStream(s_fileNameTest2, FileMode.Open, FileAccess.Read)) { VerifyCreateFromFile("Loc433_" + access, fs, "CFF_mapname433_" + access + s_uniquifier, 0, access, HandleInheritability.None, false); } } // invalid access (filestream was Read only) accessList = new MemoryMappedFileAccess[] { MemoryMappedFileAccess.ReadWrite, MemoryMappedFileAccess.ReadExecute, MemoryMappedFileAccess.ReadWriteExecute, }; foreach (MemoryMappedFileAccess access in accessList) { using (FileStream fs = new FileStream(s_fileNameTest2, FileMode.Open, FileAccess.Read)) { VerifyCreateFromFileException <UnauthorizedAccessException>("Loc434_" + access, fs, "CFF_mapname434_" + access + s_uniquifier, 0, access, HandleInheritability.None, false); } } // invalid access (filestream is Write only) accessList = new MemoryMappedFileAccess[] { MemoryMappedFileAccess.Read, MemoryMappedFileAccess.ReadWrite, MemoryMappedFileAccess.CopyOnWrite, MemoryMappedFileAccess.ReadExecute, MemoryMappedFileAccess.ReadWriteExecute, }; foreach (MemoryMappedFileAccess access in accessList) { using (FileStream fs = new FileStream(s_fileNameTest2, FileMode.Open, FileAccess.Write)) { VerifyCreateFromFileException <UnauthorizedAccessException>("Loc435_" + access, fs, "CFF_mapname435_" + access + s_uniquifier, 0, access, HandleInheritability.None, false); } } // invalid enum value accessList = new MemoryMappedFileAccess[] { (MemoryMappedFileAccess)(-1), (MemoryMappedFileAccess)(6), }; foreach (MemoryMappedFileAccess access in accessList) { using (FileStream fs = new FileStream(s_fileNameTest2, FileMode.Open, FileAccess.ReadWrite)) { VerifyCreateFromFileException <ArgumentOutOfRangeException>("Loc436_" + ((int)access), fs, "CFF_mapname436_" + ((int)access) + s_uniquifier, 0, access, HandleInheritability.None, false); } } // [] inheritability // None using (FileStream fs = new FileStream(s_fileNameTest2, FileMode.Open)) { VerifyCreateFromFile("Loc461", fs, "CFF_mapname461" + s_uniquifier, 0, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, false); } // Inheritable using (FileStream fs = new FileStream(s_fileNameTest2, FileMode.Open)) { VerifyCreateFromFile("Loc462", fs, "CFF_mapname462" + s_uniquifier, 0, MemoryMappedFileAccess.ReadWrite, HandleInheritability.Inheritable, false); } // invalid using (FileStream fs = new FileStream(s_fileNameTest2, FileMode.Open)) { VerifyCreateFromFileException <ArgumentOutOfRangeException>("Loc463", fs, "CFF_mapname463" + s_uniquifier, 0, MemoryMappedFileAccess.ReadWrite, (HandleInheritability)(-1), false); } using (FileStream fs = new FileStream(s_fileNameTest2, FileMode.Open)) { VerifyCreateFromFileException <ArgumentOutOfRangeException>("Loc464", fs, "CFF_mapname464" + s_uniquifier, 0, MemoryMappedFileAccess.ReadWrite, (HandleInheritability)(2), false); } // [] leaveOpen // false using (FileStream fs = new FileStream(s_fileNameTest2, FileMode.Open)) { VerifyCreateFromFile("Loc471", fs, "CFF_mapname471" + s_uniquifier, 0, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, false); } // true using (FileStream fs = new FileStream(s_fileNameTest2, FileMode.Open)) { VerifyCreateFromFile("Loc472", fs, "CFF_mapname472" + s_uniquifier, 0, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, true); } /// END TEST CASES File.Delete(s_fileNameTest1); File.Delete(s_fileNameTest2); File.Delete(s_fileNameTest3); if (iCountErrors == 0) { return(true); } else { Console.WriteLine("Fail: iCountErrors==" + iCountErrors); return(false); } } catch (Exception ex) { Console.WriteLine("ERR999: Unexpected exception in runTest, {0}", ex); return(false); } }
public void VerifyCreateFromFile(String strLoc, FileStream fileStream, String mapName, long capacity, MemoryMappedFileAccess access, HandleInheritability inheritability, bool leaveOpen) { if (mapName != null && Interop.PlatformDetection.OperatingSystem != Interop.OperatingSystem.Windows) { return; } iCountTestcases++; try { using (MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(fileStream, mapName, capacity, access, inheritability, leaveOpen)) { VerifyAccess(strLoc, mmf, access, capacity); VerifyHandleInheritability(strLoc, mmf.SafeMemoryMappedFileHandle, inheritability); } VerifyLeaveOpen(strLoc, fileStream, leaveOpen); } catch (Exception ex) { iCountErrors++; Console.WriteLine("ERROR, {0}: Unexpected exception, {1}", strLoc, ex); } }
public static MemoryMappedFile CreateOrOpen(string mapName, long capacity, MemoryMappedFileAccess access, MemoryMappedFileOptions options, MemoryMappedFileSecurity memoryMappedFileSecurity, HandleInheritability inheritability) { throw new NotImplementedException(); }
public static MemoryMappedFile CreateFromFile(FileStream fileStream, string mapName, long capacity, MemoryMappedFileAccess access, MemoryMappedFileSecurity memoryMappedFileSecurity, HandleInheritability inheritability, bool leaveOpen) { if (fileStream == null) { throw new ArgumentNullException("fileStream"); } if (mapName != null && mapName.Length == 0) { throw new ArgumentException("mapName"); } if ((!MonoUtil.IsUnix && capacity == 0 && fileStream.Length == 0) || (capacity > fileStream.Length)) { throw new ArgumentException("capacity"); } IntPtr handle = MemoryMapImpl.OpenHandle(fileStream.SafeFileHandle.DangerousGetHandle(), mapName, out capacity, access, MemoryMappedFileOptions.None); MemoryMapImpl.ConfigureHandleInheritability(handle, inheritability); return(new MemoryMappedFile() { handle = new SafeMemoryMappedFileHandle(handle, true), // fileAccess = access, // name = mapName, // fileCapacity = capacity, stream = fileStream, keepOpen = leaveOpen }); }
internal static unsafe void MapPosix(int file_handle, long offset, ref long size, MemoryMappedFileAccess access, out IntPtr map_addr, out int offset_diff) { if (pagesize == 0) { pagesize = Syscall.getpagesize(); } Stat buf; Syscall.fstat(file_handle, out buf); long fsize = buf.st_size; if (size == 0 || size > fsize) { size = fsize; } // Align offset long real_offset = offset & ~(pagesize - 1); offset_diff = (int)(offset - real_offset); // FIXME: Need to determine the unix fd for the file, Handle is only // equal to it by accident // // The new API no longer uses FileStream everywhere, but exposes instead // the filename (with one exception), we could move this API to use // file descriptors instead of the FileStream plus its Handle. // map_addr = Syscall.mmap(IntPtr.Zero, (ulong)size, ToUnixProts(access), access == MemoryMappedFileAccess.CopyOnWrite ? MmapFlags.MAP_PRIVATE : MmapFlags.MAP_SHARED, file_handle, real_offset); if (map_addr == (IntPtr)(-1)) { throw new IOException("mmap failed for fd#" + file_handle + "(" + offset + ", " + size + ")"); } }
public static MemoryMappedFile CreateFromFile(string path, FileMode mode, string mapName, long capacity, MemoryMappedFileAccess access) { if (path == null) { throw new ArgumentNullException("path"); } if (path.Length == 0) { throw new ArgumentException("path"); } if (mapName != null && mapName.Length == 0) { throw new ArgumentException("mapName"); } if (mode == FileMode.Append) { throw new ArgumentException("mode"); } if (capacity < 0) { throw new ArgumentOutOfRangeException("capacity"); } IntPtr handle = MemoryMapImpl.OpenFile(path, mode, mapName, out capacity, access, MemoryMappedFileOptions.None); return(new MemoryMappedFile() { handle = new SafeMemoryMappedFileHandle(handle, true), // fileAccess = access, // name = mapName, // fileCapacity = capacity }); }
internal static unsafe IntPtr OpenHandle(IntPtr handle, string mapName, out long capacity, MemoryMappedFileAccess access, MemoryMappedFileOptions options) { CheckString(nameof(mapName), mapName); fixed(char *fmapName = mapName) { int error = 0; IntPtr res = OpenHandleInternal(handle, fmapName, StringLength(mapName), out capacity, access, options, out error); if (error != 0) { throw CreateException(error, "<none>"); } return(res); } }
internal static unsafe IntPtr OpenFile(string path, FileMode mode, string mapName, out long capacity, MemoryMappedFileAccess access, MemoryMappedFileOptions options) { CheckString(nameof(path), path); CheckString(nameof(mapName), mapName); fixed(char *fpath = path, fmapName = mapName) { int error = 0; IntPtr res = OpenFileInternal(fpath, StringLength(path), mode, fmapName, StringLength(mapName), out capacity, access, options, out error); if (error != 0) { throw CreateException(error, path); } return(res); } }
public MemoryMappedViewAccessor CreateViewAccessor(long offset, long size, MemoryMappedFileAccess access) { return(new MemoryMappedViewAccessor(handle, offset, size, access)); }
public MemoryMappedViewAccessor CreateViewAccessor(long offset, long size, MemoryMappedFileAccess access);
public MemoryMappedViewAccessor CreateViewAccessor(long offset, long size, MemoryMappedFileAccess access) { int file_handle = stream != null ? (int)stream.Handle : unix_fd; return(new MemoryMappedViewAccessor(file_handle, offset, size, access)); }
public static MemoryMappedFile CreateOrOpen(string mapName, long capacity, MemoryMappedFileAccess access, MemoryMappedFileOptions options, HandleInheritability inheritability) { return(CreateOrOpen(mapName, capacity, access, options, null, inheritability)); }
public MemoryMappedViewStream CreateViewStream(long offset, long size, MemoryMappedFileAccess access) { return(new MemoryMappedViewStream(stream != null ? (int)stream.Handle : unix_fd, offset, size, access)); }
public static MemoryMappedFile CreateFromFile(string path, FileMode mode, string mapName, long capacity, MemoryMappedFileAccess access);
public static MemoryMappedFile CreateOrOpen(string mapName, long capacity, MemoryMappedFileAccess access) { return(CreateFromFile(mapName, FileMode.OpenOrCreate, mapName, capacity, access)); }
public static MemoryMappedFile CreateFromFile(FileStream fileStream, string mapName, long capacity, MemoryMappedFileAccess access, HandleInheritability inheritability, bool leaveOpen);
public static MemoryMappedFile CreateNew(string mapName, long capacity, MemoryMappedFileAccess access, MemoryMappedFileOptions options, MemoryMappedFileSecurity memoryMappedFileSecurity, HandleInheritability inheritability) { return(CreateFromFile(mapName, FileMode.CreateNew, mapName, capacity, access)); }
static extern IntPtr OpenHandleInternal(IntPtr handle, string mapName, out long capacity, MemoryMappedFileAccess access, MemoryMappedFileOptions options, out int error);
public void VerifyCreateFromFileException <EXCTYPE>(String strLoc, FileStream fileStream, String mapName, long capacity, MemoryMappedFileAccess access, HandleInheritability inheritability, bool leaveOpen) where EXCTYPE : Exception { if (mapName != null && Interop.PlatformDetection.OperatingSystem != Interop.OperatingSystem.Windows) { return; } iCountTestcases++; try { using (MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(fileStream, mapName, capacity, access, inheritability, leaveOpen)) { iCountErrors++; Console.WriteLine("ERROR, {0}: No exception thrown, expected {1}", strLoc, typeof(EXCTYPE)); } } catch (EXCTYPE) { //Console.WriteLine("{0}: Expected, {1}: {2}", strLoc, ex.GetType(), ex.Message); } catch (Exception ex) { iCountErrors++; Console.WriteLine("ERROR, {0}: Unexpected exception, {1}", strLoc, ex); } }
internal static IntPtr OpenHandle(IntPtr handle, string mapName, out long capacity, MemoryMappedFileAccess access, MemoryMappedFileOptions options) { int error = 0; IntPtr res = OpenHandleInternal(handle, mapName, out capacity, access, options, out error); if (error != 0) { throw CreateException(error, "<none>"); } return(res); }
public static MemoryMappedFile CreateOrOpen(string mapName, long capacity, MemoryMappedFileAccess access, MemoryMappedFileOptions options, MemoryMappedFileSecurity memoryMappedFileSecurity, HandleInheritability inheritability) { return(CoreShmCreate(mapName, capacity, access, options, memoryMappedFileSecurity, inheritability, FileMode.OpenOrCreate)); }
public static MemoryMappedFile CreateOrOpen(string mapName, long capacity, MemoryMappedFileAccess access);
static extern unsafe IntPtr OpenFileInternal(char *path, int path_length, FileMode mode, char *mapName, int mapName_length, out long capacity, MemoryMappedFileAccess access, MemoryMappedFileOptions options, out int error);
public static MemoryMappedFile CreateOrOpen(string mapName, long capacity, MemoryMappedFileAccess access) { return(CreateOrOpen(mapName, capacity, access, MemoryMappedFileOptions.DelayAllocatePages, null, HandleInheritability.None)); }
public MemoryMappedViewStream CreateViewStream(long offset, long size, MemoryMappedFileAccess access);
static extern IntPtr OpenFileInternal(string path, FileMode mode, string mapName, out long capacity, MemoryMappedFileAccess access, MemoryMappedFileOptions options, out int error);