private MemoryAccessor(MemoryMappedFile memoryMappedFile, bool usingDefaultData = false) { UsingDefaultData = usingDefaultData; MemoryMappedFile = memoryMappedFile; using (var viewAccessor = MemoryMappedFile?.CreateViewAccessor()) { var bytesCheck = new byte[] { 0x50, 0x53, 0x2D, 0x58, 0x20, 0x45, 0x58, 0x45 }; byte[] bytes = new byte[8]; if (viewAccessor?.ReadArray(0, bytes, 0, 8) != 8) { MemoryMappedFile?.Dispose(); throw new Exception("This file has the wrong header size returning early."); } if (bytesCheck.Where((t, i) => bytes[i] != t).Any()) { MemoryMappedFile?.Dispose(); throw new Exception("This file has the wrong header returning early."); } MemoryMappedFilesToBeDisposed.Add(MemoryMappedFile); IsOpen = true; GetData(viewAccessor); } }
/// <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)); } }
static void Initialize(string identifier, out Mutex mutex, out MemoryMappedFile file, out long offset, out Data data) { data = new Data { Identifier = identifier, Initialized = true, InUse = true, OwnerProcessId = Process.GetCurrentProcess().Id, ItemsInQueue = int.MaxValue, MaximumItems = 0, Message = null }; Data empty = new Data { Identifier = string.Empty, Initialized = false, InUse = false }; int SIZE = Marshal.SizeOf(data); bool isNew; try { mutex = new Mutex(true, MUTEX_NAME, out isNew); } catch (UnauthorizedAccessException) { mutex = Mutex.OpenExisting(MUTEX_NAME); isNew = false; } offset = 0; if (!isNew && !mutex.WaitOne(LOCK_TIMEOUT)) { mutex.Dispose(); throw new InvalidOperationException("Unable to initialize inter-process communication"); } file = null; try { if (isNew) { file = MemoryMappedFile.CreateNew(MMF_NAME, (MAX_ITEMS + 2) * SIZE, MemoryMappedFileAccess.ReadWrite, MemoryMappedFileOptions.None, null, System.IO.HandleInheritability.None); if (identifier != null) { using (var accessor = file.CreateViewAccessor(offset, SIZE * 2)) { accessor.Write(0, ref data); // Zero the following entry accessor.Write(SIZE, ref empty); } } else { using (var accessor = file.CreateViewAccessor(offset, SIZE)) { // Zero the first entry accessor.Write(0, ref empty); } } } else { file = MemoryMappedFile.OpenExisting(MMF_NAME, MemoryMappedFileRights.ReadWrite); if (identifier != null) { bool succeeded = false; for (int index = 0; index < MAX_ITEMS; ++index) { Data existing; offset = index * SIZE; using (var accessor = file.CreateViewAccessor(offset, SIZE * 2)) { accessor.Read(0, out existing); if (!existing.Initialized) { accessor.Write(0, ref data); // This wasn't initialized, so zero the following entry accessor.Write(SIZE, ref empty); succeeded = true; break; } else if (!existing.InUse) { accessor.Write(0, ref data); // Initialized is true, so the next entry is zeroed already succeeded = true; break; } else if (existing.Identifier == identifier) { throw new IdentifierInUseException("Identifier is already in use"); } } } if (!succeeded) { throw new InvalidOperationException("Out of space for entries"); } } } } catch { mutex.ReleaseMutex(); mutex.Dispose(); mutex = null; if (file != null) { file.Dispose(); file = null; } throw; } finally { if (mutex != null) { mutex.ReleaseMutex(); } } }
public Dictionary <string, List <string> > BaseSearch(string[] searchKeyWords) { FileInfo[] Files = GetFileNamesInsideTheDirectory(); string[] files = new string[Files.Length]; for (int t = 0; t < Files.Length; t++) { files[t] = Files[t].Name; long fileLen = new FileInfo(files[t]).Length; lastBytePos = fileLen; using (MemoryMappedFile mapFile = MemoryMappedFile.CreateFromFile(files[t], FileMode.OpenOrCreate)) { var view = mapFile.CreateViewAccessor(); for (long i = fileLen - 1; i >= 0; i--) { try { byte b = view.ReadByte(i); lastBytePos = i; switch (b) { default: lastReadByteWasLF = false; currentLine.Insert(0, b); break; case 13: //check for carriage return if (lastReadByteWasLF) { linesRead = AddLastByteWasCReturn(currentLine, lines, linesRead); } lastReadByteWasLF = false; break; case 10: //check for new line lastReadByteWasLF = true; currentLine.Insert(0, b); break; } if (linesToRead == linesRead) { break; } } catch { lastReadByteWasLF = false; currentLine.Insert(0, (byte)'?'); badPositions.Insert(0, i); } } } if (linesToRead > linesRead) { linesRead = InsertElementsToOutput(currentLine, lines, linesRead); } foreach (var s in lines) { if (files[t] != null) { input[files[t]] = Convert.ToString(s); } } lines.Clear(); currentLine.Clear(); output.Clear(); } foreach (var s in searchKeyWords) { output.Add(s, null); List <string> temp = new List <string>(); for (int u = 0; u < Files.Length; u++) { if (input[files[u]].Contains(s)) { temp.Add(files[u]); } } output[s] = temp; } return(output); }
public static void UnloadModule(object sender, EventArgs e) { // Claim the current hits array and reset it to prevent double-counting scenarios. var hitsArray = Interlocked.Exchange(ref HitsArray, new int[HitsArray.Length]); // The same module can be unloaded multiple times in the same process via different app domains. // Use a global mutex to ensure no concurrent access. using (var mutex = new Mutex(true, HitsMemoryMapName + "_Mutex", out bool createdNew)) { if (!createdNew) { mutex.WaitOne(); } MemoryMappedFile memoryMap = null; try { try { memoryMap = MemoryMappedFile.OpenExisting(HitsMemoryMapName); } catch (PlatformNotSupportedException) { memoryMap = MemoryMappedFile.CreateFromFile(HitsFilePath, FileMode.Open, null, (HitsArray.Length + HitsResultHeaderSize) * sizeof(int)); } // Tally hit counts from all threads in memory mapped area var accessor = memoryMap.CreateViewAccessor(); using (var buffer = accessor.SafeMemoryMappedViewHandle) { unsafe { byte *pointer = null; buffer.AcquirePointer(ref pointer); try { var intPointer = (int *)pointer; // Signal back to coverage analysis that we've started transferring hit counts. // Use interlocked here to ensure a memory barrier before the Coverage class reads // the shared data. Interlocked.Increment(ref *(intPointer + HitsResultUnloadStarted)); for (var i = 0; i < hitsArray.Length; i++) { var count = hitsArray[i]; // By only modifying the memory map pages where there have been hits // unnecessary allocation of all-zero pages is avoided. if (count > 0) { var hitLocationArrayOffset = intPointer + i + HitsResultHeaderSize; // No need to use Interlocked here since the mutex ensures only one thread updates // the shared memory map. *hitLocationArrayOffset += count; } } // Signal back to coverage analysis that all hit counts were successfully tallied. Interlocked.Increment(ref *(intPointer + HitsResultUnloadFinished)); } finally { buffer.ReleasePointer(); } } } } finally { mutex.ReleaseMutex(); memoryMap?.Dispose(); } } }
public void StreamAndRandomAccessReadWriteMemoryMappedProjectedFile() { string filename = @"Test_EPF_WorkingDirectoryTests\StreamAndRandomAccessReadWriteMemoryMappedProjectedFile.cs"; string fileVirtualPath = this.Enlistment.GetVirtualPathTo(filename); StringBuilder contentsBuilder = new StringBuilder(); // Length of the Byte-order-mark that will be at the start of the memory mapped file. // See https://msdn.microsoft.com/en-us/library/windows/desktop/dd374101(v=vs.85).aspx int bomOffset = 3; using (MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(fileVirtualPath)) { // The text length of StreamAndRandomAccessReadWriteMemoryMappedProjectedFile.cs was determined // outside of this test so that the test would not hydrate the file before we access via MemoryMappedFile int fileTextLength = 13762; int size = bomOffset + fileTextLength; int streamAccessWriteOffset = 64; int randomAccessWriteOffset = 128; string newStreamAccessContent = "**NEW_STREAM_CONTENT**"; string newRandomAccessConents = "&&NEW_RANDOM_CONTENT&&"; // Read (and modify) contents using stream accessor using (MemoryMappedViewStream streamAccessor = mmf.CreateViewStream(offset: 0, size: size)) { streamAccessor.CanRead.ShouldEqual(true); streamAccessor.CanWrite.ShouldEqual(true); for (int i = 0; i < size; ++i) { contentsBuilder.Append((char)streamAccessor.ReadByte()); } // Reset to the start of the stream (which will place the streamAccessor at offset in the memory file) streamAccessor.Seek(streamAccessWriteOffset, SeekOrigin.Begin); byte[] newContentBuffer = Encoding.ASCII.GetBytes(newStreamAccessContent); streamAccessor.Write(newContentBuffer, 0, newStreamAccessContent.Length); for (int i = 0; i < newStreamAccessContent.Length; ++i) { contentsBuilder[streamAccessWriteOffset + i] = newStreamAccessContent[i]; } } // Read (and modify) contents using random accessor using (MemoryMappedViewAccessor randomAccessor = mmf.CreateViewAccessor(offset: 0, size: size)) { randomAccessor.CanRead.ShouldEqual(true); randomAccessor.CanWrite.ShouldEqual(true); // Confirm the random accessor reads the same content that was read (and written) by the stream // accessor for (int i = 0; i < size; ++i) { ((char)randomAccessor.ReadByte(i)).ShouldEqual(contentsBuilder[i]); } // Write some new content for (int i = 0; i < newRandomAccessConents.Length; ++i) { // Convert to byte before writing rather than writing as char, because char version will write a 16-bit // unicode char randomAccessor.Write(i + randomAccessWriteOffset, Convert.ToByte(newRandomAccessConents[i])); ((char)randomAccessor.ReadByte(i + randomAccessWriteOffset)).ShouldEqual(newRandomAccessConents[i]); } for (int i = 0; i < newRandomAccessConents.Length; ++i) { contentsBuilder[randomAccessWriteOffset + i] = newRandomAccessConents[i]; } } // Verify the file one more time with a stream accessor using (MemoryMappedViewStream streamAccessor = mmf.CreateViewStream(offset: 0, size: size)) { for (int i = 0; i < size; ++i) { streamAccessor.ReadByte().ShouldEqual(contentsBuilder[i]); } } } // Remove the BOM before comparing with the contents of the file on disk contentsBuilder.Remove(0, bomOffset); // Confirm the new contents was written to the file fileVirtualPath.ShouldBeAFile(this.fileSystem).WithContents(contentsBuilder.ToString()); }
private void CreateOrUpdateBitmap(bool isPopup, Rect dirtyRect, IntPtr buffer, int width, int height, Image image, ref Size currentSize, ref MemoryMappedFile mappedFile, ref MemoryMappedViewAccessor viewAccessor) { if (image.Dispatcher.HasShutdownStarted) { return; } var createNewBitmap = false; lock (lockObject) { int pixels = width * height; int numberOfBytes = pixels * BytesPerPixel; createNewBitmap = mappedFile == null || currentSize.Height != height || currentSize.Width != width; if (createNewBitmap) { //If the MemoryMappedFile is smaller than we need then create a larger one //If it's larger then we need then rather than going through the costly expense of //allocating a new one we'll just use the old one and only access the number of bytes we require. if (viewAccessor == null || viewAccessor.Capacity < numberOfBytes) { ReleaseMemoryMappedView(ref mappedFile, ref viewAccessor); mappedFile = MemoryMappedFile.CreateNew(null, numberOfBytes, MemoryMappedFileAccess.ReadWrite); viewAccessor = mappedFile.CreateViewAccessor(); } currentSize.Height = height; currentSize.Width = width; } //TODO: Performance analysis to determine which is the fastest memory copy function //NativeMethodWrapper.CopyMemoryUsingHandle(viewAccessor.SafeMemoryMappedViewHandle.DangerousGetHandle(), buffer, numberOfBytes); CopyMemory(viewAccessor.SafeMemoryMappedViewHandle.DangerousGetHandle(), buffer, (uint)numberOfBytes); //Take a reference to the backBufferHandle, once we're on the UI thread we need to check if it's still valid var backBufferHandle = mappedFile.SafeMemoryMappedFileHandle; image.Dispatcher.BeginInvoke((Action)(() => { lock (lockObject) { if (backBufferHandle.IsClosed || backBufferHandle.IsInvalid) { return; } if (createNewBitmap) { if (image.Source != null) { image.Source = null; GC.Collect(1); } var stride = width * BytesPerPixel; var bitmap = (InteropBitmap)Imaging.CreateBitmapSourceFromMemorySection(backBufferHandle.DangerousGetHandle(), width, height, PixelFormat, stride, 0); image.Source = bitmap; } else { if (image.Source != null) { var sourceRect = new Int32Rect(dirtyRect.X, dirtyRect.Y, dirtyRect.Width, dirtyRect.Height); var bitmap = (InteropBitmap)image.Source; bitmap.Invalidate(sourceRect); } } } }), dispatcherPriority); } }
public void OnNext(string path) { string filename = Path.GetFileNameWithoutExtension(path); FileInfo fi = new FileInfo(path); Mutex m = new Mutex(false, "m" + filename); filename += Convert.ToString(objects.Count); MemoryMappedFile mappedFile = null; m.WaitOne(); try { mappedFile = MemoryMappedFile.CreateFromFile(path, FileMode.Open, filename, 0); } catch (IOException) { return; } MemoryMappedViewAccessor accessor = mappedFile.CreateViewAccessor(); uint offsetPE = accessor.ReadUInt32(60); // Считываем сдвиг заголовка PE AVScanObject obj = null; // Проверяем размер файла if (offsetPE <= accessor.Capacity) { byte[] peSignature = new byte[4]; accessor.ReadArray(offsetPE, peSignature, 0, 4); // Считываем сигнатуру if (Encoding.ASCII.GetString(peSignature) == "PE\0\0") // Для PE-файла должно быть равно PE\0\0 { ushort magic = accessor.ReadUInt16(offsetPE + 24); // Считываем поле magic определяющее вид PE-заголовка uint peHeaderSize = 0; if (magic == 267) { peHeaderSize = 248; // Для PE32 } else { peHeaderSize = 264; // Для PE32+ } uint sectionsNum = accessor.ReadUInt16(offsetPE + 6); // Считываем количество секций uint pFirstSection = offsetPE + peHeaderSize; for (uint i = 0; i < sectionsNum; i++) { uint pCurSection = pFirstSection + i * 40; uint characteristic = accessor.ReadUInt32(pCurSection + 36); uint executeFlag = 0x20000000; // Проверяем содержимое поля характеристики // Eсли сегмент исполняемый, то создаем AVScanObject с этим сегментом if ((characteristic & executeFlag) == executeFlag) { uint pDataStart = accessor.ReadUInt32(pCurSection + 20); uint pDataLength = accessor.ReadUInt32(pCurSection + 16); MemoryMappedViewAccessor sectionAccessor = mappedFile.CreateViewAccessor(pDataStart, pDataLength, MemoryMappedFileAccess.Read); obj = new AVScanObject(sectionAccessor, mappedFile, path); } } } } m.ReleaseMutex(); // По окончанию проверки файла, добавляем в список obj, который, если файл не исполняемый равен null objects.Add(obj); foreach (IObserver <AVScanObject> observer in observers) // И оповещаем всех слушателей { observer.OnNext(obj); } }
/// <summary> /// Gets the current state of the mod loader. /// </summary> public ReloadedLoaderState GetState() { using var accessor = _memoryMappedFile.CreateViewAccessor(); accessor.Read(0, out ReloadedLoaderState state); return(state); }
public LibsnesApi(string exePath) { //make sure we've checked this exe for OKness.. the dry run should keep us from freezing up or crashing weirdly if the external process isnt correct if (!okExes.Contains(exePath)) { bool ok = DryRun(exePath); if (!ok) { throw new InvalidOperationException(string.Format("Couldn't launch {0} to run SNES core. Not sure why this would have happened. Try redownloading BizHawk first.", Path.GetFileName(exePath))); } okExes.Add(exePath); } InstanceName = "libsneshawk_" + Guid.NewGuid().ToString(); #if DEBUG //use this to get a debug console with libsnes output InstanceName = "console-" + InstanceName; #endif var pipeName = InstanceName; mmf = MemoryMappedFile.CreateNew(pipeName, 1024 * 1024); mmva = mmf.CreateViewAccessor(); mmva.SafeMemoryMappedViewHandle.AcquirePointer(ref mmvaPtr); pipe = new NamedPipeServerStream(pipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.None, 1024 * 1024, 1024); //slim chance this might be useful sometimes: //http://stackoverflow.com/questions/2590334/creating-a-cross-process-eventwaithandle //create an event for the child process to monitor with a watchdog, to make sure it terminates when the emuhawk process terminates. //NOTE: this is alarming! for some reason .net releases this event when it gets finalized, instead of when i (dont) dispose it. bool createdNew; watchdogEvent = new System.Threading.EventWaitHandle(false, System.Threading.EventResetMode.AutoReset, InstanceName + "-event", out createdNew); process = new Process(); process.StartInfo.WorkingDirectory = Path.GetDirectoryName(exePath); process.StartInfo.FileName = exePath; process.StartInfo.Arguments = pipeName; process.StartInfo.ErrorDialog = true; process.Start(); //TODO - start a thread to wait for process to exit and gracefully handle errors? how about the pipe? pipe.WaitForConnection(); rbuf = new IPCRingBuffer(); wbuf = new IPCRingBuffer(); rbuf.Allocate(1024); wbuf.Allocate(1024); rbufstr = new IPCRingBufferStream(rbuf); wbufstr = new IPCRingBufferStream(wbuf); rstream = new SwitcherStream(); wstream = new SwitcherStream(); rstream.SetCurrStream(pipe); wstream.SetCurrStream(pipe); brPipe = new BinaryReader(rstream); bwPipe = new BinaryWriter(wstream); WritePipeMessage(eMessage.eMessage_SetBuffer); bwPipe.Write(1); WritePipeString(rbuf.Id); WritePipeMessage(eMessage.eMessage_SetBuffer); bwPipe.Write(0); WritePipeString(wbuf.Id); bwPipe.Flush(); }
// Проверка таблицы с индексами static void Main5(string[] args) { Console.WriteLine("Start"); string path = @"..\..\..\Databases\"; string filename = path + "table.pac"; Random rnd = new Random(3333); PaCell table = new PaCell(new PTypeSequence(new PType(PTypeEnumeration.integer)), filename, false); DateTime tt0 = DateTime.Now; int nvalues = 1000000000; bool toload = false; if (toload) { table.Clear(); table.Fill(new object[0]); for (int i = 0; i < nvalues; i++) { table.Root.AppendElement(i); } table.Flush(); Console.WriteLine("Load ok. duration={0}", (DateTime.Now - tt0).Ticks / 10000L); tt0 = DateTime.Now; return; } int portion = 100000; for (int i = 0; i < portion; i++) { int ind = rnd.Next(nvalues - 1); var v = table.Root.Element(ind).Get(); } Console.WriteLine("Test1 ok. duration={0}", (DateTime.Now - tt0).Ticks / 10000L); tt0 = DateTime.Now; table.Close(); System.IO.Stream fs = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read); System.IO.BinaryReader br = new System.IO.BinaryReader(fs); for (int i = 0; i < portion; i++) { int ind = rnd.Next(nvalues - 1); long off = 40 + (long)ind * 4; fs.Position = off; int v = br.ReadInt32(); } fs.Close(); Console.WriteLine("Test2 ok. duration={0}", (DateTime.Now - tt0).Ticks / 10000L); tt0 = DateTime.Now; using (MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(filename)) { for (int i = 0; i < portion; i++) { int ind = rnd.Next(nvalues - 1); long off = 40 + (long)ind * 4; using (var accessor = mmf.CreateViewAccessor(off, 4)) { int v = accessor.ReadInt32(0); } } Console.WriteLine("Test3 ok. duration={0}", (DateTime.Now - tt0).Ticks / 10000L); tt0 = DateTime.Now; using (var accessor = mmf.CreateViewAccessor(0, (long)nvalues * 4 + 40)) { tt0 = DateTime.Now; for (int i = 0; i < portion; i++) { int ind = rnd.Next(nvalues - 1); long off = 40 + (long)ind * 4; //using (var accessor = mmf.CreateViewAccessor(off, 4)) //{ // int v = accessor.ReadInt32(0); //} int v = accessor.ReadInt32(off); } } } Console.WriteLine("Test4 ok. duration={0}", (DateTime.Now - tt0).Ticks / 10000L); tt0 = DateTime.Now; }
public MappedMemory(string sharedMemoryName) { memory = MemoryMappedFile.CreateOrOpen(sharedMemoryName, Marshal.SizeOf(typeof(TRootObject))); view = memory.CreateViewAccessor(); }
public MemoryMappedSpan(MemoryMappedFile mf, int length) { this.Length = length; this.acc = mf.CreateViewAccessor(0, length, MemoryMappedFileAccess.Read); this.acc.SafeMemoryMappedViewHandle.AcquirePointer(ref dptr); }
public static void write_file_MMF <T>(T[] data, string path_folder, string file_name) { if (!path_folder.Contains(':')) { path_folder = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\" + path_folder; } if (!Directory.Exists(path_folder)) { Directory.CreateDirectory(path_folder); } MemoryMappedFileSecurity mSec = new MemoryMappedFileSecurity(); mSec.AddAccessRule(new AccessRule <MemoryMappedFileRights>(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MemoryMappedFileRights.FullControl, AccessControlType.Allow)); string file = path_folder + @"\" + file_name + ".mmf"; file = file.Replace("\\\\", "\\"); Task.Factory.StartNew((object obj) => { Tuple <string, T[]> rs = (obj as Tuple <string, T[]>); string v_file = rs.Item1; T[] v_data = rs.Item2; int buffer_size = 0; try { using (MemoryStream stream = new MemoryStream()) { BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(stream, v_data); byte[] bw = stream.ToArray(); buffer_size = bw.Length; long file_size = 0; if (File.Exists(v_file)) { FileInfo fi = new FileInfo(v_file); file_size = fi.Length; } long capacity = buffer_size; if (capacity < file_size) { capacity = file_size; } using (MemoryMappedFile w = MemoryMappedFile.CreateFromFile(v_file, FileMode.OpenOrCreate, null, capacity)) { using (MemoryMappedViewAccessor mmfWriter = w.CreateViewAccessor(0, capacity)) { mmfWriter.WriteArray <byte>(0, bw, 0, buffer_size); } } } } catch (Exception ex) { string msg = ex.Message; } }, new Tuple <string, T[]>(file, data)); }//end function
public static void write_MMF <T>(T item, string path_folder, string file_name) where T : struct { if (!path_folder.Contains(':')) { path_folder = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\" + path_folder; } if (!Directory.Exists(path_folder)) { Directory.CreateDirectory(path_folder); } MemoryMappedFileSecurity mSec = new MemoryMappedFileSecurity(); mSec.AddAccessRule(new AccessRule <MemoryMappedFileRights>(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MemoryMappedFileRights.FullControl, AccessControlType.Allow)); string filePath = path_folder + @"\" + file_name + ".mmf"; filePath = filePath.Replace("\\\\", "\\"); int buffer_size = 0; try { using (MemoryStream stream = new MemoryStream()) { int objsize = Marshal.SizeOf(typeof(T)); byte[] bw = new byte[objsize]; IntPtr buff = Marshal.AllocHGlobal(objsize); Marshal.StructureToPtr(item, buff, true); Marshal.Copy(buff, bw, 0, objsize); Marshal.FreeHGlobal(buff); buffer_size = bw.Length; long file_size = 0; if (File.Exists(filePath)) { FileInfo fi = new FileInfo(filePath); file_size = fi.Length; } long capacity = buffer_size; if (capacity < file_size) { capacity = file_size; } using (MemoryMappedFile w = MemoryMappedFile.CreateFromFile(filePath, FileMode.OpenOrCreate, null, capacity)) { using (MemoryMappedViewAccessor mmfWriter = w.CreateViewAccessor(0, capacity)) { mmfWriter.WriteArray <byte>(0, bw, 0, buffer_size); } } } } catch (Exception ex) { string msg = ex.Message; } }//end function
private void CreateOrUpdateBitmap(bool isPopup, Rect dirtyRect, IntPtr buffer, int width, int height, Image image, ref Size currentSize, ref MemoryMappedFile mappedFile, ref MemoryMappedViewAccessor viewAccessor) { bool createNewBitmap = false; if (image.Dispatcher.HasShutdownStarted) { return; } lock (lockObject) { int pixels = width * height; int numberOfBytes = pixels * BytesPerPixel; createNewBitmap = mappedFile == null || currentSize.Height != height || currentSize.Width != width; if (createNewBitmap) { ReleaseMemoryMappedView(ref mappedFile, ref viewAccessor); mappedFile = MemoryMappedFile.CreateNew(null, numberOfBytes, MemoryMappedFileAccess.ReadWrite); viewAccessor = mappedFile.CreateViewAccessor(); currentSize.Height = height; currentSize.Width = width; } //TODO: Performance analysis to determine which is the fastest memory copy function //NativeMethodWrapper.CopyMemoryUsingHandle(viewAccessor.SafeMemoryMappedViewHandle.DangerousGetHandle(), buffer, numberOfBytes); CopyMemory(viewAccessor.SafeMemoryMappedViewHandle.DangerousGetHandle(), buffer, (uint)numberOfBytes); //Take a reference to the sourceBuffer that's used to update our WritableBitmap, //once we're on the UI thread we need to check if it's still valid var sourceBuffer = viewAccessor.SafeMemoryMappedViewHandle; image.Dispatcher.BeginInvoke((Action)(() => { lock (lockObject) { if (sourceBuffer.IsClosed || sourceBuffer.IsInvalid) { return; } if (createNewBitmap) { if (image.Source != null) { image.Source = null; GC.Collect(1); } image.Source = new WriteableBitmap(width, height, dpiX, dpiY, PixelFormat, null); } var stride = width * BytesPerPixel; var noOfBytes = stride * height; var bitmap = (WriteableBitmap)image.Source; //By default we'll only update the dirty rect, for those that run into a MILERR_WIN32ERROR Exception (#2035) //it's desirably to either upgrade to a newer .Net version (only client runtime needs to be installed, not compiled //against a newer version. Or invalidate the whole bitmap if (invalidateDirtyRect) { // Update the dirty region var sourceRect = new Int32Rect(dirtyRect.X, dirtyRect.Y, dirtyRect.Width, dirtyRect.Height); bitmap.Lock(); bitmap.WritePixels(sourceRect, sourceBuffer.DangerousGetHandle(), noOfBytes, stride, dirtyRect.X, dirtyRect.Y); bitmap.Unlock(); } else { // Update whole bitmap var sourceRect = new Int32Rect(0, 0, width, height); bitmap.Lock(); bitmap.WritePixels(sourceRect, sourceBuffer.DangerousGetHandle(), noOfBytes, stride); bitmap.Unlock(); } } }), dispatcherPriority); } }
private MemoryBuffer(MemoryMappedFile mappedFile, long offset, long size) { this.mappedFile = mappedFile; mappedViewAccessor = mappedFile.CreateViewAccessor(offset, size, MemoryMappedFileAccess.Read); BufferStart = (byte *)mappedViewAccessor.SafeMemoryMappedViewHandle.DangerousGetHandle(); }
public void Open(MemoryMappedFile mmf, long offset, long size) { this.accessor = mmf.CreateViewAccessor(offset, size, MemoryMappedFileAccess.Read); }
void Main(IntPtr intPtr, IsolatedStorageFile isolatedStorageFile, MemoryMappedFile memoryMappedFile, MemoryMappedFileSecurity memoryMappedFileSecurity, Stream stream) { // Any static method call on File or Directory will raise File.Exists(""); // Noncompliant {{Make sure this file handling is safe here.}} // ^^^^^^^^^^^^^^^ File.Create(""); // Noncompliant File.Delete(""); // Noncompliant File.ReadLines(""); // Noncompliant Directory.Exists(""); // Noncompliant Directory.Delete(""); // Noncompliant Directory.EnumerateFiles(""); // Noncompliant // Any FileInfo or DirectoryInfo creation var fileInfo = new FileInfo(""); // Noncompliant {{Make sure this file handling is safe here.}} // ^^^^^^^^^^^^^^^^ var dirInfo = new DirectoryInfo(""); // Noncompliant // Calls to extern CreateFile SafeFileHandle handle; handle = CreateFile("", 0, 0, intPtr, 0, 0, intPtr); // Noncompliant handle = CreateFile(); // Compliant, not extern // Creation of SafeFileHandle handle = new SafeFileHandle(IntPtr.Zero, false); // Noncompliant // All constructors of FileStream FileStream fileStream; fileStream = new FileStream(IntPtr.Zero, FileAccess.Read); // Noncompliant fileStream = new FileStream(handle, FileAccess.Read); // Compliant, created from SafeFileHandle, which should be already reported fileStream = new FileStream("", FileMode.Append); // Noncompliant fileStream = new FileStream(IntPtr.Zero, FileAccess.Read, true); // Noncompliant fileStream = new FileStream(handle, FileAccess.Read, 0); // Compliant, created from SafeFileHandle, which should be already reported fileStream = new FileStream("", FileMode.Append, FileAccess.Read); // Noncompliant fileStream = new FileStream(IntPtr.Zero, FileAccess.Read, true, 0); // Noncompliant fileStream = new FileStream(handle, FileAccess.Read, 0, true); // Compliant, created from SafeFileHandle, which should be already reported fileStream = new FileStream("", FileMode.Append, FileAccess.Read, FileShare.Read); // Noncompliant fileStream = new FileStream(IntPtr.Zero, FileAccess.Read, true, 0, true); // Noncompliant fileStream = new FileStream("", FileMode.Append, FileAccess.Read, FileShare.Read, 0); // Noncompliant fileStream = new FileStream("", FileMode.Append, FileAccess.Read, FileShare.Read, 0, true); // Noncompliant fileStream = new FileStream("", FileMode.Append, FileAccess.Read, FileShare.Read, 0, FileOptions.Asynchronous); // Noncompliant fileStream = new FileStream("", FileMode.Append, FileSystemRights.Read, FileShare.Read, 0, FileOptions.Asynchronous); // Noncompliant fileStream = new FileStream("", FileMode.Append, FileSystemRights.Read, FileShare.Read, 0, FileOptions.Asynchronous, new FileSecurity()); // Noncompliant // All constructors of StreamWriter, whos first argument is string StreamWriter streamWriter; streamWriter = new StreamWriter(stream); streamWriter = new StreamWriter(""); // Noncompliant streamWriter = new StreamWriter(stream, Encoding.Unicode); streamWriter = new StreamWriter("", true); // Noncompliant streamWriter = new StreamWriter(stream, Encoding.Unicode, 0); streamWriter = new StreamWriter("", true, Encoding.Unicode); // Noncompliant streamWriter = new StreamWriter(stream, Encoding.Unicode, 0, true); streamWriter = new StreamWriter("", true, Encoding.Unicode, 0); // Noncompliant // All constructors of StreamReader, whos first argument is string StreamReader streamReader; streamReader = new StreamReader(stream); streamReader = new StreamReader(""); // Noncompliant streamReader = new StreamReader(stream, true); streamReader = new StreamReader(stream, Encoding.Unicode); streamReader = new StreamReader("", true); // Noncompliant streamReader = new StreamReader("", Encoding.Unicode); // Noncompliant streamReader = new StreamReader(stream, Encoding.Unicode, true); streamReader = new StreamReader("", Encoding.Unicode, true); // Noncompliant streamReader = new StreamReader(stream, Encoding.Unicode, true, 0); streamReader = new StreamReader("", Encoding.Unicode, true, 0); // Noncompliant streamReader = new StreamReader(stream, Encoding.Unicode, true, 0, true); Path.GetTempFileName(); // Noncompliant Path.GetTempPath(); // Noncompliant FileSecurity fileSecurity; fileSecurity = new FileSecurity(); fileSecurity = new FileSecurity("", AccessControlSections.All); // Noncompliant // All static methods of ZipFile (all methods are static!) ZipFile.CreateFromDirectory("", ""); // Noncompliant ZipFile.CreateFromDirectory("", "", CompressionLevel.Fastest, true); // Noncompliant ZipFile.CreateFromDirectory("", "", CompressionLevel.Fastest, true, Encoding.Unicode); // Noncompliant ZipFile.Open("", ZipArchiveMode.Read); // Noncompliant ZipFile.Open("", ZipArchiveMode.Read, Encoding.Unicode); // Noncompliant ZipFile.OpenRead(""); // Noncompliant ZipFile.ExtractToDirectory("", ""); // Noncompliant ZipFile.ExtractToDirectory("", "", Encoding.Unicode); // Noncompliant // All static methods of IsolatedStorageFile IsolatedStorageFile.GetMachineStoreForApplication(); // Noncompliant IsolatedStorageFile.GetMachineStoreForAssembly(); // Noncompliant IsolatedStorageFile.GetMachineStoreForDomain(); // Noncompliant IsolatedStorageFile.GetStore(IsolatedStorageScope.Application, typeof(Program), typeof(Program)); // Noncompliant IsolatedStorageFile.GetStore(IsolatedStorageScope.Application, new Evidence(), typeof(Program), new Evidence(), typeof(Program)); // Noncompliant IsolatedStorageFile.GetStore(IsolatedStorageScope.Application, typeof(Program)); // Noncompliant IsolatedStorageFile.GetStore(IsolatedStorageScope.Application, null); // Noncompliant IsolatedStorageFile.GetStore(IsolatedStorageScope.Application, null, null); // Noncompliant IsolatedStorageFile.GetUserStoreForApplication(); // Noncompliant IsolatedStorageFile.GetUserStoreForAssembly(); // Noncompliant IsolatedStorageFile.GetUserStoreForDomain(); // Noncompliant IsolatedStorageFile.GetUserStoreForSite(); // Noncompliant isolatedStorageFile.CopyFile("", ""); // Compliant, not static // All constructors of IsolatedStorageFileStream IsolatedStorageFileStream isolatedStorageFileStream; isolatedStorageFileStream = new IsolatedStorageFileStream("", FileMode.Append); // Noncompliant isolatedStorageFileStream = new IsolatedStorageFileStream("", FileMode.Append, FileAccess.Read); // Noncompliant isolatedStorageFileStream = new IsolatedStorageFileStream("", FileMode.Append, isolatedStorageFile); // Noncompliant isolatedStorageFileStream = new IsolatedStorageFileStream("", FileMode.Append, FileAccess.Read, FileShare.Read); // Noncompliant isolatedStorageFileStream = new IsolatedStorageFileStream("", FileMode.Append, FileAccess.Read, isolatedStorageFile); // Noncompliant isolatedStorageFileStream = new IsolatedStorageFileStream("", FileMode.Append, FileAccess.Read, FileShare.Read, 0); // Noncompliant isolatedStorageFileStream = new IsolatedStorageFileStream("", FileMode.Append, FileAccess.Read, FileShare.Read, isolatedStorageFile); // Noncompliant isolatedStorageFileStream = new IsolatedStorageFileStream("", FileMode.Append, FileAccess.Read, FileShare.Read, 0, isolatedStorageFile); // Noncompliant // All static methods that start with Create* on MemoryMappedFile MemoryMappedFile.CreateFromFile(""); // Noncompliant MemoryMappedFile.CreateFromFile("", FileMode.Append); // Noncompliant MemoryMappedFile.CreateFromFile("", FileMode.Append, ""); // Noncompliant MemoryMappedFile.CreateFromFile("", FileMode.Append, "", 0); // Noncompliant MemoryMappedFile.CreateFromFile("", FileMode.Append, "", 0, MemoryMappedFileAccess.CopyOnWrite); // Noncompliant MemoryMappedFile.CreateFromFile(fileStream, "", 0, MemoryMappedFileAccess.CopyOnWrite, memoryMappedFileSecurity, HandleInheritability.Inheritable, true); // Noncompliant MemoryMappedFile.CreateNew("", 0); // Noncompliant MemoryMappedFile.CreateNew("", 0, MemoryMappedFileAccess.CopyOnWrite); // Noncompliant MemoryMappedFile.CreateNew("", 0, MemoryMappedFileAccess.CopyOnWrite, MemoryMappedFileOptions.DelayAllocatePages, memoryMappedFileSecurity, HandleInheritability.Inheritable); // Noncompliant MemoryMappedFile.CreateOrOpen("", 0); // Noncompliant MemoryMappedFile.CreateOrOpen("", 0, MemoryMappedFileAccess.CopyOnWrite); // Noncompliant MemoryMappedFile.CreateOrOpen("", 0, MemoryMappedFileAccess.CopyOnWrite, MemoryMappedFileOptions.DelayAllocatePages, memoryMappedFileSecurity, HandleInheritability.Inheritable); // Noncompliant memoryMappedFile.CreateViewAccessor(0, 0); // Compliant, not static // All static methods that start with Open* on MemoryMappedFile MemoryMappedFile.OpenExisting(""); // Noncompliant MemoryMappedFile.OpenExisting("", MemoryMappedFileRights.CopyOnWrite); // Noncompliant MemoryMappedFile.OpenExisting("", MemoryMappedFileRights.CopyOnWrite, HandleInheritability.Inheritable); // Noncompliant memoryMappedFile.GetAccessControl(); // Compliant, not static, not starting with Open or Create }
/// <summary></summary> /// <param name="mmf"></param> /// <param name="block"></param> /// <returns></returns> public static UnmanagedMemoryAccessor CreateAccessor(this MemoryMappedFile mmf, Block block) { return(mmf.CreateViewAccessor(block.Position, block.Size)); }
/// <summary> /// 引用内存映射文件 /// </summary> /// <summary> /// 用于访问内存映射文件的存取对象 /// </summary> /// <summary> /// 创建内存映射文件 /// </summary> private int CreateMemoryMapFile(object arg0)//(string srcFile, int num) { MemoryMappedFile memoryFile = null; //MemoryMappedViewAccessor accessor1;// accessor; file_to_thread ft = (file_to_thread)arg0; string srcFile = ft.filename; try { FileInfo fi = new FileInfo(srcFile); // 单位:字节 // total size in bytes Int64 size = fi.Length; Int64 minSize = 1024 * 1024 * 100; Int64 minSize2 = 1024 * 1024 * 400; Int64 maxSize = 1024 * 1024 * 100; currentFileName = "正在处理第" + (ft.filenum + 1) + "个文件:" + srcFile.Substring(srcFile.LastIndexOf('\\') + 1) + ",文件大小:" + size / 1024 / 1024 + "MB"; bw.ReportProgress(ft.filenum); Console.WriteLine("处理的文件:" + srcFile + "。文件序号:" + ft.filenum.ToString() + "。文件线程编号:" + ft.threadnum.ToString() + "。文件大小:" + size); //Int64 proSize = 0; //Int64 fileToRead = size;//文件总的大小 memoryFile = MemoryMappedFile.CreateFromFile(srcFile, FileMode.Open, "fileencrypt" + ft.filenum.ToString(), size); // 四个线程处理,平均分成四份 if (size <= minSize) { exe_to_thread et; et.accessor = memoryFile.CreateViewAccessor(0, size); et.ft_threadnum = ft.threadnum; et.size = size; et.et_threadnum = 0; var result = Task.Run(() => EncryptDecrypt0(et)); Console.WriteLine("主线程不会阻塞,它会继续执行"); Console.WriteLine("子任务的返回值分别为:{0}", result.Result); currentFileName = "第" + (ft.filenum + 1) + "个文件:" + srcFile.Substring(srcFile.LastIndexOf('\\') + 1) + ",文件大小:" + size / 1024 / 1024 + "MB,当前进度:" + 100 + "%"; bw.ReportProgress(ft.filenum); et.accessor.Dispose(); } else if (size > minSize && size <= minSize2) { Int64 proSize = size / 4; exe_to_thread[] ets = new exe_to_thread[4]; ets[0].accessor = memoryFile.CreateViewAccessor(0, proSize); ets[1].accessor = memoryFile.CreateViewAccessor(proSize, proSize); ets[2].accessor = memoryFile.CreateViewAccessor(2 * proSize, proSize); ets[3].accessor = memoryFile.CreateViewAccessor(3 * proSize, size - 3 * proSize); ets[0].ft_threadnum = ft.threadnum; ets[0].et_threadnum = 0; ets[0].size = proSize; ets[1].ft_threadnum = ft.threadnum; ets[1].et_threadnum = 1; ets[1].size = proSize; ets[2].ft_threadnum = ft.threadnum; ets[2].et_threadnum = 2; ets[2].size = proSize; ets[3].ft_threadnum = ft.threadnum; ets[3].et_threadnum = 3; ets[3].size = size - 3 * proSize; var parentTask = new Task <int[]>(() => { //开启多个子任务 var results = new int[4]; //创建子任务,并将子任务的值赋给results变量,并通过TaskCreationOptions.AttachedToParent,将其关联到父任务,如果不指定,该任务将独立于父任务单独执行 //这里有个奇怪的问题,只能使用new Task的方式去创建关联到父任务的子任务,因为Task.Run没有提供这个方法,可以通过扩展方法解决这个问题 new Task(() => results[0] = EncryptDecrypt0(ets[0]), TaskCreationOptions.AttachedToParent).Start(); new Task(() => results[1] = EncryptDecrypt1(ets[1]), TaskCreationOptions.AttachedToParent).Start(); new Task(() => results[2] = EncryptDecrypt2(ets[2]), TaskCreationOptions.AttachedToParent).Start(); new Task(() => results[3] = EncryptDecrypt3(ets[3]), TaskCreationOptions.AttachedToParent).Start(); return(results); }); parentTask.Start(); Console.WriteLine("主线程不会阻塞,它会继续执行"); foreach (var re in parentTask.Result) { Console.WriteLine("子任务的返回值分别为:{0}", re); } currentFileName = "第" + (ft.filenum + 1) + "个文件:" + srcFile.Substring(srcFile.LastIndexOf('\\') + 1) + ",文件大小:" + size / 1024 / 1024 + "MB,当前进度:" + 100 + "%"; bw.ReportProgress(ft.filenum); ets[0].accessor.Dispose(); ets[1].accessor.Dispose(); ets[2].accessor.Dispose(); ets[3].accessor.Dispose(); } else { Int64 total_count = size / maxSize; if (size % maxSize != 0) { total_count++; } Int64 counts = 0; Int64 remainSize = size; // 同时分配的不超过500MB,每次分配100M while ((total_count - counts) > 0) { Int64 temp_count; Int64 temp_size = 0;; if ((total_count - counts) >= 4) { temp_count = 4; } else { temp_count = (total_count - counts); } exe_to_thread[] ets = new exe_to_thread[temp_count]; if (temp_count == 4) { ets[0].accessor = memoryFile.CreateViewAccessor(counts * maxSize, maxSize); ets[1].accessor = memoryFile.CreateViewAccessor((counts + 1) * maxSize, maxSize); ets[2].accessor = memoryFile.CreateViewAccessor((counts + 2) * maxSize, maxSize); if ((remainSize - 3 * maxSize) >= maxSize) { temp_size = maxSize; } else { temp_size = remainSize - 3 * maxSize; } ets[3].accessor = memoryFile.CreateViewAccessor((counts + 3) * maxSize, temp_size); ets[0].ft_threadnum = ft.threadnum; ets[0].et_threadnum = 0; ets[0].size = maxSize; ets[1].ft_threadnum = ft.threadnum; ets[1].et_threadnum = 1; ets[1].size = maxSize; ets[2].ft_threadnum = ft.threadnum; ets[2].et_threadnum = 2; ets[2].size = maxSize; ets[3].ft_threadnum = ft.threadnum; ets[3].et_threadnum = 3; ets[3].size = temp_size; var parentTask = new Task <int[]>(() => { //开启多个子任务 var results = new int[temp_count]; //创建子任务,并将子任务的值赋给results变量,并通过TaskCreationOptions.AttachedToParent,将其关联到父任务,如果不指定,该任务将独立于父任务单独执行 //这里有个奇怪的问题,只能使用new Task的方式去创建关联到父任务的子任务,因为Task.Run没有提供这个方法,可以通过扩展方法解决这个问题 new Task(() => results[0] = EncryptDecrypt0(ets[0]), TaskCreationOptions.AttachedToParent).Start(); new Task(() => results[1] = EncryptDecrypt1(ets[1]), TaskCreationOptions.AttachedToParent).Start(); new Task(() => results[2] = EncryptDecrypt2(ets[2]), TaskCreationOptions.AttachedToParent).Start(); new Task(() => results[3] = EncryptDecrypt3(ets[3]), TaskCreationOptions.AttachedToParent).Start(); return(results); }); parentTask.Start(); Console.WriteLine("主线程不会阻塞,它会继续执行"); foreach (var re in parentTask.Result) { Console.WriteLine("子任务的返回值分别为:{0}", re); } ets[0].accessor.Dispose(); ets[1].accessor.Dispose(); ets[2].accessor.Dispose(); ets[3].accessor.Dispose(); } else if (temp_count == 3) { ets[0].accessor = memoryFile.CreateViewAccessor(counts * maxSize, maxSize); ets[1].accessor = memoryFile.CreateViewAccessor((counts + 1) * maxSize, maxSize); if ((remainSize - 2 * maxSize) >= maxSize) { temp_size = maxSize; } else { temp_size = remainSize - 2 * maxSize; } ets[2].accessor = memoryFile.CreateViewAccessor((counts + 2) * maxSize, temp_size); ets[0].ft_threadnum = ft.threadnum; ets[0].et_threadnum = 0; ets[0].size = maxSize; ets[1].ft_threadnum = ft.threadnum; ets[1].et_threadnum = 1; ets[1].size = maxSize; ets[2].ft_threadnum = ft.threadnum; ets[2].et_threadnum = 2; ets[2].size = temp_size; var parentTask = new Task <int[]>(() => { //开启多个子任务 var results = new int[temp_count]; //创建子任务,并将子任务的值赋给results变量,并通过TaskCreationOptions.AttachedToParent,将其关联到父任务,如果不指定,该任务将独立于父任务单独执行 //这里有个奇怪的问题,只能使用new Task的方式去创建关联到父任务的子任务,因为Task.Run没有提供这个方法,可以通过扩展方法解决这个问题 new Task(() => results[0] = EncryptDecrypt0(ets[0]), TaskCreationOptions.AttachedToParent).Start(); new Task(() => results[1] = EncryptDecrypt1(ets[1]), TaskCreationOptions.AttachedToParent).Start(); new Task(() => results[2] = EncryptDecrypt2(ets[2]), TaskCreationOptions.AttachedToParent).Start(); return(results); }); parentTask.Start(); Console.WriteLine("主线程不会阻塞,它会继续执行"); foreach (var re in parentTask.Result) { Console.WriteLine("子任务的返回值分别为:{0}", re); } ets[0].accessor.Dispose(); ets[1].accessor.Dispose(); ets[2].accessor.Dispose(); } else if (temp_count == 2) { ets[0].accessor = memoryFile.CreateViewAccessor(counts * maxSize, maxSize); if ((remainSize - maxSize) >= maxSize) { temp_size = maxSize; } else { temp_size = remainSize - maxSize; } ets[1].accessor = memoryFile.CreateViewAccessor((counts + 1) * maxSize, temp_size); ets[0].ft_threadnum = ft.threadnum; ets[0].et_threadnum = 0; ets[0].size = maxSize; ets[1].ft_threadnum = ft.threadnum; ets[1].et_threadnum = 1; ets[1].size = temp_size; var parentTask = new Task <int[]>(() => { //开启多个子任务 var results = new int[temp_count]; //创建子任务,并将子任务的值赋给results变量,并通过TaskCreationOptions.AttachedToParent,将其关联到父任务,如果不指定,该任务将独立于父任务单独执行 //这里有个奇怪的问题,只能使用new Task的方式去创建关联到父任务的子任务,因为Task.Run没有提供这个方法,可以通过扩展方法解决这个问题 new Task(() => results[0] = EncryptDecrypt0(ets[0]), TaskCreationOptions.AttachedToParent).Start(); new Task(() => results[1] = EncryptDecrypt1(ets[1]), TaskCreationOptions.AttachedToParent).Start(); return(results); }); parentTask.Start(); Console.WriteLine("主线程不会阻塞,它会继续执行"); foreach (var re in parentTask.Result) { Console.WriteLine("子任务的返回值分别为:{0}", re); } ets[0].accessor.Dispose(); ets[1].accessor.Dispose(); } else if (temp_count == 1) { if ((remainSize - maxSize) >= maxSize) { temp_size = maxSize; } else { temp_size = remainSize;// - maxSize; } ets[0].accessor = memoryFile.CreateViewAccessor(counts * maxSize, temp_size); ets[0].ft_threadnum = ft.threadnum; ets[0].et_threadnum = 0; ets[0].size = temp_size; var parentTask = new Task <int[]>(() => { //开启多个子任务 var results = new int[temp_count]; //创建子任务,并将子任务的值赋给results变量,并通过TaskCreationOptions.AttachedToParent,将其关联到父任务,如果不指定,该任务将独立于父任务单独执行 //这里有个奇怪的问题,只能使用new Task的方式去创建关联到父任务的子任务,因为Task.Run没有提供这个方法,可以通过扩展方法解决这个问题 new Task(() => results[0] = EncryptDecrypt0(ets[0]), TaskCreationOptions.AttachedToParent).Start(); return(results); }); parentTask.Start(); Console.WriteLine("主线程不会阻塞,它会继续执行"); foreach (var re in parentTask.Result) { Console.WriteLine("子任务的返回值分别为:{0}", re); } ets[0].accessor.Dispose(); } remainSize = remainSize - (temp_count - 1) * maxSize - temp_size; currentFileName = "第" + (ft.filenum + 1) + "个文件:" + srcFile.Substring(srcFile.LastIndexOf('\\') + 1) + ",文件大小:" + size / 1024 / 1024 + "MB,当前进度:" + (size - remainSize) * 100 / size + "%";; bw.ReportProgress(ft.filenum); counts += temp_count; } } Console.WriteLine("文件:" + srcFile + "处理完成"); memoryFile.Dispose(); } catch (Exception ex) { throw; } return(ft.threadnum); }
public ViewWrap(MemoryMappedFile mmf) { _va = mmf.CreateViewAccessor(); }
/// <summary> /// Get a pagesized block that contains the data from the byte offset specified /// </summary> /// <param name="FileOffset">byte offset of long aligned page block</param> /// <param name="block">to be filled on return optionally</param> /// <param name="DataRead">signals success</param> /// <returns>long value from fileoffset</returns> public long GetPageFromFileOffset(long FileOffset, ref long[] block, ref bool DataRead) { var rv = 0L; DataRead = false; var NewMapViewSize = MapViewSize; var CheckBase = FileOffset / MapViewSize; var NewMapViewBase = CheckBase * MapViewSize; if (FileOffset > FileSize) { return(0); } var AbsOffset = FileOffset - NewMapViewBase; var BlockOffset = AbsOffset & ~(PAGE_SIZE - 1); try { if (NewMapViewBase != MapViewBase) { cntInAccessor++; if (NewMapViewBase + MapViewSize > FileSize) { NewMapViewSize = FileSize - NewMapViewBase; } else { NewMapViewSize = MapViewSize; } mappedAccess = mappedFile.CreateViewAccessor( NewMapViewBase, NewMapViewSize, MemoryMappedFileAccess.Read); MapViewBase = NewMapViewBase; } else { cntOutAccsor++; } if (block != null) { var copy_len = block.Length; if (BlockOffset + (block.Length * 8) > NewMapViewSize) { copy_len = (int)((NewMapViewSize - BlockOffset) / 8); } UnsafeHelp.ReadBytes(mappedAccess, BlockOffset, ref block, copy_len); rv = block[((AbsOffset >> 3) & 0x1ff)]; } // FIX: ReadInt64 uses byte address so when we use it must adjust, check for other callers // assumptions since we changed this from array<long>[] maybe expecting old behavior, however // caller from getpageforphysaddr passes valid block usually so that's the main one from V2P else { rv = mappedAccess.ReadInt64(BlockOffset | (AbsOffset & 0x1ff)); } DataRead = true; } catch (Exception ex) { throw new MemoryMapWindowFailedException("Unable to map or read memory offset", ex); } return(rv); }
/// <summary> /// Creates a new or opens an existing shared memory buffer with the name of <see cref="Name"/> depending on the value of <see cref="IsOwnerOfSharedMemory"/>. /// </summary> /// <exception cref="System.IO.IOException">Trying to create a new shared memory buffer with a duplicate name as buffer owner.</exception> /// <exception cref="System.IO.FileNotFoundException">Trying to open a new shared memory buffer that does not exist as a consumer of existing buffer.</exception> /// <exception cref="System.ArgumentOutOfRangeException">Trying to create a new shared memory buffer with a size larger than the logical addressable space.</exception> /// <returns>`true` if the memory was successfully mapped. Otherwise, `false`.</returns> /// <remarks> /// If <see cref="IsOwnerOfSharedMemory"/> is `true`, the shared memory buffer will be created. Opening will fail in this case if the shared memory already /// exists. Otherwise, if <see cref="IsOwnerOfSharedMemory"/> is `false`, the shared memory buffer will be opened, which will fail if it doesn't already exist. /// </remarks> protected bool Open() { Close(); try { // Attempts to create or open the shared memory with a name of this.Name if (IsOwnerOfSharedMemory) { // Create a new shared memory mapping Mmf = MemoryMappedFile.CreateNew(Name, SharedMemorySize); // Create a view to the entire region of the shared memory View = Mmf.CreateViewAccessor(0, SharedMemorySize, MemoryMappedFileAccess.ReadWrite); View.SafeMemoryMappedViewHandle.AcquirePointer(ref ViewPtr); Header = (SharedBufferHeader *)(ViewPtr + HeaderOffset); BufferStartPtr = ViewPtr + BufferOffset; // Initialise the header InitializeHeader(); } else { // Open an existing shared memory mapping Mmf = MemoryMappedFile.OpenExisting(Name); // Retrieve the header from the shared memory in order to initialise the correct size using (var headerView = Mmf.CreateViewAccessor(0, HeaderOffset + Polyfill.GetMarshalSizeOf <SharedBufferHeader>(), MemoryMappedFileAccess.Read)) { byte *headerPtr = null; headerView.SafeMemoryMappedViewHandle.AcquirePointer(ref headerPtr); var header = (SharedBufferHeader *)(headerPtr + HeaderOffset); BufferSize = header->SharedMemorySize - Polyfill.GetMarshalSizeOf <SharedBufferHeader>(); headerView.SafeMemoryMappedViewHandle.ReleasePointer(); } // Create a view to the entire region of the shared memory View = Mmf.CreateViewAccessor(0, SharedMemorySize, MemoryMappedFileAccess.ReadWrite); View.SafeMemoryMappedViewHandle.AcquirePointer(ref ViewPtr); Header = (SharedBufferHeader *)(ViewPtr + HeaderOffset); BufferStartPtr = ViewPtr + HeaderOffset + Polyfill.GetMarshalSizeOf <SharedBufferHeader>(); } } catch { Close(); throw; } // Complete any additional open logic try { if (!DoOpen()) { Close(); return(false); } else { return(true); } } catch { Close(); throw; } }
public HydraSpoof(int controllerCount) { this.controllerCount = controllerCount; memoryMappedFile = MemoryMappedFile.CreateOrOpen("SixenseEmulatedData", Marshal.SizeOf(typeof(EmulatedData)) * controllerCount); accessor = memoryMappedFile.CreateViewAccessor(); }
public MmapDefault(CodeContext /*!*/ context, int fileno, long length, string tagname = null, int access = ACCESS_WRITE, long offset = 0) { switch (access) { case ACCESS_READ: _fileAccess = MemoryMappedFileAccess.Read; break; case ACCESS_WRITE: _fileAccess = MemoryMappedFileAccess.ReadWrite; break; case ACCESS_COPY: _fileAccess = MemoryMappedFileAccess.CopyOnWrite; break; default: throw PythonOps.ValueError("mmap invalid access parameter"); } if (length < 0) { throw PythonOps.OverflowError("memory mapped size must be positive"); } if (offset < 0) { throw PythonOps.OverflowError("memory mapped offset must be positive"); } if (length > SysModule.maxsize) { throw PythonOps.OverflowError("cannot fit 'long' into an index-sized integer"); } // CPython only allows offsets that are a multiple of ALLOCATIONGRANULARITY if (offset % ALLOCATIONGRANULARITY != 0) { throw WindowsError(PythonExceptions._OSError.ERROR_MAPPED_ALIGNMENT); } // .NET throws on an empty tagname, but CPython treats it as null. _mapName = tagname == "" ? null : tagname; if (fileno == -1 || fileno == 0) { // Map anonymous memory that is not tied to a file. // Note: CPython seems to allow 0 as a file descriptor even though it represents stdin. _offset = 0; // offset is ignored without an underlying file _sourceStream = null; // work around the .NET bug whereby CreateOrOpen throws on a null mapName if (_mapName == null) { _mapName = Guid.NewGuid().ToString(); } _file = MemoryMappedFile.CreateOrOpen(_mapName, length, _fileAccess); } else { // Memory-map an actual file _offset = offset; PythonContext pContext = context.LanguageContext; if (pContext.FileManager.TryGetFileFromId(pContext, fileno, out PythonIOModule.FileIO fileio)) { if ((_sourceStream = fileio._readStream as FileStream) == null) { throw WindowsError(PythonExceptions._OSError.ERROR_INVALID_HANDLE); } } else { throw PythonOps.OSError(PythonExceptions._OSError.ERROR_INVALID_BLOCK, "Bad file descriptor"); } if (_fileAccess == MemoryMappedFileAccess.ReadWrite && !_sourceStream.CanWrite) { throw WindowsError(PythonExceptions._OSError.ERROR_ACCESS_DENIED); } if (length == 0) { length = _sourceStream.Length; if (length == 0) { throw PythonOps.ValueError("cannot mmap an empty file"); } if (_offset >= length) { throw PythonOps.ValueError("mmap offset is greater than file size"); } length -= _offset; } long capacity = checked (_offset + length); // Enlarge the file as needed. if (capacity > _sourceStream.Length) { if (_sourceStream.CanWrite) { _sourceStream.SetLength(capacity); } else { throw WindowsError(PythonExceptions._OSError.ERROR_NOT_ENOUGH_MEMORY); } } _file = CreateFromFile( _sourceStream, _mapName, _sourceStream.Length, _fileAccess, HandleInheritability.None, true); } try { _view = _file.CreateViewAccessor(_offset, length, _fileAccess); } catch { _file.Dispose(); _file = null; throw; } _position = 0L; }
public MemoryMappedViewReader(long fileLength, MemoryMappedFile mmap) { ContentLength = fileLength; _mmap = mmap; _view = mmap.CreateViewAccessor(); }
public void resize(long newsize) { using (new MmapLocker(this)) { if (_fileAccess != MemoryMappedFileAccess.ReadWrite) { throw PythonOps.TypeError("mmap can't resize a readonly or copy-on-write memory map."); } if (_sourceStream == null) { // resizing is not supported without an underlying file throw WindowsError(PythonExceptions._OSError.ERROR_INVALID_PARAMETER); } if (newsize == 0) { // resizing to an empty mapped region is not allowed throw WindowsError(_offset == 0 ? PythonExceptions._OSError.ERROR_ACCESS_DENIED : PythonExceptions._OSError.ERROR_FILE_INVALID ); } if (_view.Capacity == newsize) { // resizing to the same size return; } long capacity = checked (_offset + newsize); try { _view.Flush(); _view.Dispose(); _file.Dispose(); var leaveOpen = true; if (!_sourceStream.CanWrite) { _sourceStream = new FileStream(_sourceStream.Name, FileMode.OpenOrCreate, FileAccess.ReadWrite); leaveOpen = false; } // Resize the file as needed. if (capacity != _sourceStream.Length) { _sourceStream.SetLength(capacity); } _file = CreateFromFile( _sourceStream, _mapName, _sourceStream.Length, _fileAccess, HandleInheritability.None, leaveOpen); _view = _file.CreateViewAccessor(_offset, newsize, _fileAccess); } catch { close(); throw; } } }
/// <summary>Creates and validates a view accessor and a view stream from the map.</summary> /// <param name="mmf">The map.</param> /// <param name="capacity">The capacity to use when creating the view.</param> /// <param name="access">The access to use when creating the view.</param> private static void CreateAndValidateViews(MemoryMappedFile mmf, long capacity, MemoryMappedFileAccess access) { using (MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor(0, capacity, access)) { ValidateMemoryMappedViewAccessor(accessor, capacity, access); } using (MemoryMappedViewStream stream = mmf.CreateViewStream(0, capacity, access)) { ValidateMemoryMappedViewStream(stream, capacity, access); } }
private void CopyD3D9CPUTextureThread() { int sharedMemID = 0; while (hCopyEvent.WaitOne()) { hCopyEvent.Reset(); copyReadySignal.Reset(); if (bKillThread) { break; } int nextSharedMemID = sharedMemID == 0 ? 1 : 0; int copyTex = curCPUTexture; IntPtr data = pCopyData; if (copyTex < NUM_BUFFERS && data != IntPtr.Zero) { try { Monitor.Enter(dataMutexes[copyTex]); int lastRendered = -1; bool locked = false; try { locked = Monitor.TryEnter(textureMutexes[sharedMemID], 0); if (locked) { lastRendered = sharedMemID; } else { locked = Monitor.TryEnter(textureMutexes[nextSharedMemID], 0); if (locked) { lastRendered = nextSharedMemID; } } if (lastRendered != -1) { try { var bpp = copyData.pitch / copyData.width; var size = copyData.height * copyData.pitch; var bdata = new byte[size]; Marshal.Copy(data, bdata, 0, size); using (var stream = sharedMem.CreateViewAccessor()) { this.copyData.lastRendered = lastRendered; // stream.Seek(0, SeekOrigin.Begin); stream.Write(0, ref copyData); stream.WriteArray(Marshal.SizeOf(typeof(CopyData)), bdata, 0, bdata.Length); } copyReadySignal.Set(); } catch (Exception ex) { DebugMessage(ex.ToString()); } } } finally { if (locked) { Monitor.Exit(textureMutexes[lastRendered]); } } } finally { Monitor.Exit(dataMutexes[copyTex]); } } sharedMemID = nextSharedMemID; } }
private Task StartSpeedControllerAsync() { return(Task.Factory.StartNew(async() => { _logger.LogInformation($"任务 {Id} 速度控制器启动"); bool @break = false; MemoryMappedFile mmf = MmfSignal ? MemoryMappedFile.CreateFromFile(Id, FileMode.OpenOrCreate, null, 4, MemoryMappedFileAccess.ReadWrite) : null; using (var accessor = mmf?.CreateViewAccessor()) { accessor?.Write(0, false); accessor?.Flush(); while (!@break) { Thread.Sleep(_speedControllerInterval); switch (Status) { case Status.Running: { try { var requests = _scheduler.Dequeue(Id, _dequeueBatchCount); foreach (var request in requests) { OnDownloading?.Invoke(request); } if (requests.Length > 0) { await _downloadService.EnqueueRequests(Id, requests); } } catch (Exception e) { _logger.LogError($"速度控制器运转失败: {e}"); } break; } case Status.Paused: { _logger.LogInformation($"任务 {Id} 速度控制器暂停"); break; } case Status.Exiting: case Status.Exited: { @break = true; break; } } if (accessor != null && accessor.ReadBoolean(0)) { Exit(); break; } } } _logger.LogInformation($"任务 {Id} 速度控制器退出"); })); }
static private void QuickSortInt64(MemoryMappedFile memoryMap, Int64 left, Int64 right) { Int64 middleElement = 0; Int64 indexLeftElement = left; Int64 indexRightElement = right; Int64 indexMiddle = (left >> 1) + (right >> 1); using (var accessor_middle = memoryMap.CreateViewAccessor(indexMiddle << 3, sizeof(Int64))) { middleElement = accessor_middle.ReadInt64(0); } while (indexLeftElement <= indexRightElement) { Int64 leftElement = Int64.MinValue; using (var accessor = memoryMap.CreateViewAccessor(indexLeftElement << 3, sizeof(Int64))) { leftElement = accessor.ReadInt64(0); } while (leftElement < middleElement) { ++indexLeftElement; using (var accessor = memoryMap.CreateViewAccessor(indexLeftElement << 3, sizeof(Int64))) { leftElement = accessor.ReadInt64(0); } } Int64 rightElement = Int64.MaxValue; using (var accessor = memoryMap.CreateViewAccessor(indexRightElement << 3, sizeof(Int64))) { rightElement = accessor.ReadInt64(0); } while (rightElement > middleElement) { --indexRightElement; using (var accessor = memoryMap.CreateViewAccessor(indexRightElement << 3, sizeof(Int64))) { rightElement = accessor.ReadInt64(0); } } if (indexLeftElement <= indexRightElement) { using (var accessor = memoryMap.CreateViewAccessor((indexLeftElement) << 3, sizeof(Int64))) { accessor.Write(0, rightElement); } using (var accessor = memoryMap.CreateViewAccessor(indexRightElement << 3, sizeof(Int64))) { accessor.Write(0, leftElement); } ++indexLeftElement; --indexRightElement; } } if (indexLeftElement < right) { QuickSortInt64(memoryMap, indexLeftElement, right); } if (left < indexRightElement) { QuickSortInt64(memoryMap, left, indexRightElement); } }