private static MemoryMapping WindowsCreate(string path) { int size; IntPtr mappingHandle = IntPtr.Zero; MemoryMapping mapping = null; using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.ReadWrite)) { size = unchecked ((int)stream.Length); mapping = new MemoryMapping(); mapping._capacity = size; // make sure we don't get interrupted before we save the handle and the pointer: RuntimeHelpers.PrepareConstrainedRegions(); try { } finally { mappingHandle = UnsafeNativeMethods.CreateFileMapping(stream.SafeFileHandle, null, PAGE_READONLY, 0, size, null); if (mappingHandle != IntPtr.Zero) { mapping._pointer = UnsafeNativeMethods.MapViewOfFile(mappingHandle, FILE_MAP_READ, 0, 0, (IntPtr)size); UnsafeNativeMethods.CloseHandle(mappingHandle); } else { } } if (mapping._pointer == null) { int error = Marshal.GetLastWin32Error(); throw new IOException("Unable to create memory map: " + path, error); } } return(mapping); }
private static MemoryMapping WindowsCreate(string path) { int size; IntPtr mappingHandle = IntPtr.Zero; MemoryMapping mapping = null; using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.ReadWrite)) { size = unchecked((int)stream.Length); mapping = new MemoryMapping(); mapping._capacity = size; // make sure we don't get interrupted before we save the handle and the pointer: RuntimeHelpers.PrepareConstrainedRegions(); try { } finally { mappingHandle = UnsafeNativeMethods.CreateFileMapping(stream.SafeFileHandle, null, PAGE_READONLY, 0, size, null); if (mappingHandle != IntPtr.Zero) { mapping._pointer = UnsafeNativeMethods.MapViewOfFile(mappingHandle, FILE_MAP_READ, 0, 0, (IntPtr)size); UnsafeNativeMethods.CloseHandle(mappingHandle); } } if (mapping._pointer == null) { throw new IOException("Unable to create memory map: " + path, Marshal.GetLastWin32Error()); } } return mapping; }
private static MemoryMapping UnixCreate(string path) { #if MONO int size; int fileDescriptor = 0; MemoryMapping mapping = null; // make sure we don't get interrupted before we save the handle and the pointer: RuntimeHelpers.PrepareConstrainedRegions(); try { } finally { fileDescriptor = Syscall.open(path, OpenFlags.O_RDONLY); if (fileDescriptor < 0) { Stat stat; if (Syscall.fstat(fileDescriptor, out stat) >= 0) { size = unchecked ((int)stat.st_size); mapping._capacity = size; mapping._pointer = (byte *)Syscall.mmap(IntPtr.Zero, (ulong)size, MmapProts.PROT_READ, MmapFlags.MAP_SHARED, fileDescriptor, 0); } Syscall.close(fileDescriptor); } } if (mapping._pointer == null) { throw new IOException("Unable to create memory map: " + path, Marshal.GetLastWin32Error()); } return(mapping); #else throw new NotImplementedException(); #endif }
public static MemoryMapping Create(string path) { MemoryMappedFile file = null; MemoryMappedViewAccessor accessor = null; SafeMemoryMappedViewHandle handle = null; MemoryMapping mapping = null; FileStream stream = null; byte * ptr = null; try { stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.ReadWrite); file = MemoryMappedFile.CreateFromFile(stream, null, 0, MemoryMappedFileAccess.Read, null, HandleInheritability.None, true); accessor = file.CreateViewAccessor(0, 0, MemoryMappedFileAccess.Read); mapping = new MemoryMapping(); // we need to make sure that the handle and the acquired pointer get stored to MemoryMapping: RuntimeHelpers.PrepareConstrainedRegions(); try { } finally { handle = accessor.SafeMemoryMappedViewHandle; handle.AcquirePointer(ref ptr); if (ptr == null) { throw new IOException("Cannot create a file mapping"); } mapping._handle = handle; mapping._pointer = ptr; mapping._capacity = accessor.Capacity; } } finally { stream?.Dispose(); accessor?.Dispose(); file?.Dispose(); } return(mapping); }
private static MetadataImport CreateImport(string path) { var file = MemoryMapping.Create(path); return(new MetadataImport(file.GetRange(0, (int)System.Math.Min(file.Capacity, Int32.MaxValue)))); }
public static MemoryMapping Create(string path) { MemoryMappedFile file = null; MemoryMappedViewAccessor accessor = null; SafeMemoryMappedViewHandle handle = null; MemoryMapping mapping = null; FileStream stream = null; byte* ptr = null; try { stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.ReadWrite); file = MemoryMappedFile.CreateFromFile(stream, null, 0, MemoryMappedFileAccess.Read, null, HandleInheritability.None, true); accessor = file.CreateViewAccessor(0, 0, MemoryMappedFileAccess.Read); mapping = new MemoryMapping(); // we need to make sure that the handle and the acquired pointer get stored to MemoryMapping: RuntimeHelpers.PrepareConstrainedRegions(); try { } finally { handle = accessor.SafeMemoryMappedViewHandle; handle.AcquirePointer(ref ptr); if (ptr == null) { throw new IOException("Cannot create a file mapping"); } mapping._handle = handle; mapping._pointer = ptr; mapping._capacity = accessor.Capacity; } } finally { if (stream != null) { stream.Dispose(); } if (accessor != null) { accessor.Dispose(); } if (file != null) { file.Dispose(); } } return mapping; }
private static MemoryMapping UnixCreate(string path) { #if MONO int size; int fileDescriptor = 0; MemoryMapping mapping = null; // make sure we don't get interrupted before we save the handle and the pointer: RuntimeHelpers.PrepareConstrainedRegions(); try { } finally { fileDescriptor = Syscall.open(path, OpenFlags.O_RDONLY); if (fileDescriptor > 0) { Stat stat; if (Syscall.fstat(fileDescriptor, out stat) >= 0) { size = unchecked((int)stat.st_size); mapping = new MemoryMapping { _capacity = size, _pointer = (byte*)Syscall.mmap(IntPtr.Zero, (ulong)size, MmapProts.PROT_READ, MmapFlags.MAP_SHARED, fileDescriptor, 0) }; } Syscall.close(fileDescriptor); } } if (mapping == null || mapping._pointer == null) { throw new IOException("Unable to create memory map: " + path, Marshal.GetLastWin32Error()); } return mapping; #else throw new NotImplementedException(); #endif }