Tag: offset data represents param memory mapped. NET math until REM
Recently has been worrying about file memory mapping, the whole two weeks have been tossing this thing. The 64-bit system and the 32-bit system are also calculated for the high and low bits of memory. Good trouble. It's still not done.
Accidental discovery of. NET 4.0 on MSDN added the memory file mapping. NET class Library: It seems to be a lot of convenience. It's much easier than calling Windows APIs directly in C #. So
This must be a decisive record of ... The project is to be used immediately, in order to enhance the efficiency of memory data exchange. This one... Must be AH.
Task |
The method or property used |
MemoryMappedFile object, represents a persisted memory-mapped file from a file on disk. "Data-guid=" 84b3db52f3f2718b52 57afe65060e898 "Get that represents a persistent memory-mapped file from a file on disk; memorymappedfile object. |
memorymappedfile. CreateFromFile method. "Data-guid=" c29cef5cde3162298b9bc85667764173 "> memorymappedfile . createfromfile method. |
MemoryMappedFile object, represents a non-persisted memory-mapped file (not associated with a file on disk). "Data-gui D= "d2b6c25bccb8d94ebb82cf72d24d922e" > Gets a that represents a non-persistent memory-mapped file (not associated with a file on disk); memorymappedfile object. |
memorymappedfile . CreateNew method. "Data-guid=" 4d950d0d7be269266cadc4691c5a77de "> memorymappedfile . createnew method. -or- memorymappedfile. Createoropen method. "Data-guid=" a02507c676095b137903b4d139c4506c "> memorymappedfile . createoropen method. |
MemoryMappedFile object of an existing memory-mapped file (either persisted or non-persisted). "Data-guid=" 2eb29815b9d38d 5575c762a34a2f6a7f "To get the of an existing memory-mapped file (persistent or non-persistent); memorymappedfile object. |
memorymappedfile. OpenExisting method. "Data-guid=" 931b496a3516abff5a06e4f9e269a1a5 "> memorymappedfile . openexisting method. |
Unmanagedmemorystream object for a sequentially accessed view to the memory-mapped file. "Data-guid=" e6d1a01155453800829d bcac5493b88b "" Gets the of the view for the sequential access to the memory-mapped file; unmanagedmemorystream object. |
memorymappedfile. Createviewstream method. "Data-guid=" 1d3fbcb428a5c648bdf4f16d4d3cc613 "> memorymappedfile . createviewstream method. |
Gets the Unmanagedmemoryaccessor object for the random-access view of the memory-mapped file . |
MemoryMappedFile. Createviewaccessor method. |
Gets the Safememorymappedviewhandle object to use for unmanaged code . |
MemoryMappedFile. The Safememorymappedfilehandle property. Or Memorymappedviewaccessor. The Safememorymappedviewhandle property. Or Memorymappedviewstream. The Safememorymappedviewhandle property. |
Defer memory allocation until the view is created (non-persistent files only). (To determine the current system page size, use environment.) The Systempagesize property. ) |
With memorymappedfileoptions. The CreateNew method of the Delayallocatepages value . Or The Memorymappedfileoptions enumeration is used as the Createoropen method of the parameter . |
Persistent File Memory mapping:
The CreateFromFile method creates a memory-mapped file based on an existing file on disk.
1 using System;
2 using System.IO;
3 using System.IO.MemoryMappedFiles;
4 using System.Runtime.InteropServices;
5
6 Class Program
7 {
8 static void Main (string[] args)
9 {
Ten long offset = 0x10000000; Megabytes
One long length = 0x20000000; Megabytes
12
//Create the memory-mapped file.
using (var mmf = Memorymappedfile.createfromfile (@ "C:\ExtremelyLargeImage.data", FileMode.Open, "ImgA")
15 {
+//Create a random access view, from the 256th megabyte (the offset)
From/to the 768th megabyte (the offset plus length).
The using (var accessor = mmf. Createviewaccessor (offset, length))
19 {
int colorsize = marshal.sizeof (typeof (MyColor));
MyColor color;
22
Changes to the view.
(Long i = 0; i < length; i + = colorsize)
25 {
Accessor. Read (i, out color);
Color. Brighten (10);
Accessor. Write (i, ref color);
29}
30}
31}
32}
33}
34
public struct MyColor
36 {
Panax Notoginseng Short Red;
a short Green;
a short Blue;
-Public short Alpha;
41
The view Brigher.
public void brighten (short value)
44 {
Red = (short) math.min (short). MaxValue, (int) Red + value);
Math.min Green = (short). MaxValue, (int) Green + value);
Blue = (short) math.min (short). MaxValue, (int) Blue + value);
Math.min Alpha = (short). MaxValue, (int) Alpha + value);
49}
50}
Non-persistent file memory mappings:
The CreateNew and Createoropen methods create a memory-mapped file that is not mapped to an existing file on disk.
1 using System;
2 using System.IO;
3 using System.IO.MemoryMappedFiles;
4 using System.Threading;
5
6 Class Program
7 {
8//Process A:
9 static void Main (string[] args)
10 {
One using (memorymappedfile MMF = memorymappedfile.createnew ("TestMap", 10000))
12 {
BOOL mutexcreated;
The Mutex mutex = new Mutex (true, "Testmapmutex", out mutexcreated);
The using (Memorymappedviewstream stream = mmf. Createviewstream ())
16 {
BinaryWriter writer = new BinaryWriter (stream);
Writer. Write (1);
19}
Mutex. ReleaseMutex ();
21st
Console.WriteLine ("Start Process B and press ENTER to continue.");
Console.ReadLine ();
24
Console.WriteLine ("Start Process C and press ENTER to continue.");
Console.ReadLine ();
27
Mutex. WaitOne ();
The using (Memorymappedviewstream stream = mmf. Createviewstream ())
30 {
BinaryReader reader = new BinaryReader (stream);
Console.WriteLine ("Process A says: {0}", reader. Readboolean ());
Console.WriteLine ("Process B says: {0}", reader. Readboolean ());
Console.WriteLine ("Process C says: {0}", reader. Readboolean ());
35}
The mutex. ReleaseMutex ();
37}
38}
The. NET framework comes with a file memory mapping class