Windows Memory ing File

Source: Internet
Author: User
Tags rounds

Windows provides three methods for memory management:


Virtual memory, which is most suitable for managing large objects or arrays of structures.


Memory ing files are most suitable for managing large data streams (usually from files) and sharing data among multiple processes running on a single computer.

• Memory stack, which is most suitable for managing a large number of small objects.

Memory ing File

Correlation between memory ing files and data viewsMemory ing files supported by page files Use a memory ing file to share data between processesThe memory ing file can be used for three different purposes

• The system uses memory ing files to load and execute. EXE and DLL files. This greatly saves the page file space and the time required for the application to start and run.

• You can use a memory ing file to access data files on the disk. This eliminates the need to perform I/O operations on files and cache file content.

• Memory ing files can be used to allow multiple processes running on the same computer to share data with each other. Windows does provide other methods for data communication between processes, but these methods are implemented using memory ing files, this makes the memory ing file the most effective way for multiple processes on a single computer to communicate with each other.

Use memory to map data files

To use a memory ing file, follow these steps:

1) Create or open a file kernel object to identify the file on the disk that you want to use as the memory ing file.

2) create a file ing kernel object to tell the system the file size and how you plan to access the file.

3) Let the system map all or part of the file ing object to your process address space.

When using the memory ing file, you must perform the following steps to clear it:

1) Tell the system to cancel the image mapped to the kernel object from the address space of your process.

2) disable the file ing kernel object.

3) close the file kernel object.

The following describes the procedure in detail.

Step 1: Create or open a file Kernel Object

Handle createfile (

Pcstr pszfilename,

DWORD dwdesiredaccess,

DWORD dw1_mode,

Psecurity_attributes PSA,

DWORD dwcreationdisposition,

DWORD dwflagsandattributes,

Handle htemplatefile );

Value of dwdesiredaccess

Value

Description

0

Cannot read or write file content. If you only want to obtain the object attributes, set 0.

Generic_read

Data can be read from files.

Generic_write

Data can be written to a file.

Generic_read | generic_write

You can read data from a file or write data to a file.

Dwsharemode Value

Value

Description

0

Any attempt to open the file will fail.

File_pai_read

Other attempts to open the file using generic_write will fail.

File_pai_write

Other attempts to open the file using generic_read will fail.

File_pai_read file_pai_write |

Other attempts to open the file will be successful.

Step 2: create a file ing Kernel Object

Calling the createfilemapping function tells the system how much physical storage is required for the file ing object.

Handle createfilemapping (

Handle hfile,

Psecurity_attributes PSA,

DWORD fdwprotect,

DWORD dwmaximumsizehigh,

DWORD dwmaximumsizelow,

Pctstr pszname );

The first parameter: hfile is used to identify the file handle that you want to map to the process address space. This handle is returned by the previously called createfile function.

The second parameter: the PSA parameter is a pointer to the security_attributes structure of the file ing kernel object. Generally, the passed value is null (it provides default security features, and the returned handle cannot be inherited ).

Third parameter: The fdwprotect parameter enables you to set these protection attributes. In most cases, you can set one of the three Protection attributes listed in the following table.

Partial protection attributes set using the fdwprotect Parameter

Protection attributes

Description

Page_readonly

When a file ing object is mapped, the file data can be read. Generic_read must have been passed to the createfile Function

Page_readwrite

When a file ing object is mapped, the data of the file can be read and written. Generic_read | generic_write must have been passed to the creat efile

Page_writecopy

When a file ing object is mapped, the data of the file can be read and written. If data is written, the private copy of the page is created. Generic_read or generic_write must have been passed to createfile

In addition to the above page protection attributes, there are also four section protection attributes

The first protection attribute in section is sec_nocache, which tells the system that no memory ing page of the file is put into the cache. Therefore, when writing data to this file, the system will update the file data on the disk more frequently. For Device Driver developers, applications are generally not used.

The second protection attribute in section is sec_image, which tells the system that the file you map is a portable executable (PE) file image. When the system maps the file to the address space of your process, the system needs to view the content of the file to determine which protection attributes are assigned to each page of the file image. For example,
The Code Section (. text) of the PE file is usually mapped using the page _ execute_read attribute, while
File data section (. Data)
You can map the attributes of page_readw rite. If the set attribute is s e c _ I m a G E, the system is notified to map the file image and set the corresponding page protection attribute.

The last two protection attributes are sec_reserve and sec_commit. They are two mutex attributes. These two flags are meaningful only when you create file ing objects supported by system page files. The sec_commit flag enables createfilemapping to submit memory from the system's page files. If neither flag is set, the result is the same.

Parameters 4 and 5: dwmaximumsizehigh and dwmaximumsizelow indicate the maximum number of bytes of the file.

The last parameter is pszname: it is a string ending with 0 and is used to assign a name to the ing object of the file. This name is used to map objects with other process shared files.

Step 3: map the file data to the address space of the process

Submit the file data as physical storage mapped to the region.

Pvoid mapviewoffile (

Handle hfilemappingobject,

DWORD dwdesiredaccess,

DWORD dwfileoffsethigh,

DWORD dwfileoffsetlow,

Size_t dwnumberofbytestomap );

The first parameter:HFilemappingobject is used to identify the handle of the file ing object, which is returned by calling the createfilemapping or openfilemapping function.

The second parameter: dwdesiredaccess is used to identify how to access the data. You can set one of the four values listed in the following table.

Value

Description

File_map_write

You can read and write file data. The createfilemapping function must be called by passing the page_readwrite flag.

File_map_read

You can read file data. The createfilemapping function can be called by passing any of the following protection attributes: page_readonly, page _ readwrite, or page_writecopy

File_map_all_acces s

Same as file_map_write

File_map_copy

You can read and write file data. If file data is written, you can create a private copy of the page. In Windows 2000, The createilemapping function can be called by any of the Protection attributes such as page_readonly, page_readwrite, and page_writecopy. In Windows 98, createfilemapping must be called using page_writecopy.

(When a file is mapped to the address space of your process, you do not have to map the entire file at once. Instead, you can map only a small part of the file to the address space. This part of the file mapped to the address space of the process is called a view .)

The third four parameters are dwfileofsethigh and dwfileofsetlow. Specify which byte should be mapped as the first byte in the view.

Fifth parameter: How many bytes of dwnumberofbytestomap need to be mapped to the address space. If the value is set to 0, the system will try to map the View starting from the specified displacement in the file to the end of the entire file to the address space.

Step 4: remove the file data image from the address space of the process

When you no longer need to retain the file data mapped to the process address space area, you can call the following function to release it:

Bool unmapviewoffile (pvoid pvbaseaddress );

Parameter: pvbaseaddress is returned by the mapviewoffile function.

Note: If this function is not called, the reserved region will not be released before the process stops running. When mapviewoffile is called, The system always retains a new region in your process address space, and all previously reserved regions will not be released.

To increase the speed, the system caches the data page of the file and does not immediately update the disk image of the file when operating on the ing view of the file. If you need to ensure that your update is written to the disk, you can force the system to re-write part or all of the modified data to the disk image by calling the flushviewoffile function:

Bool flushviewoffile (

Pvoid pvaddress,

Size_t dwnumberofbytestoflush );

The first parameter is a byte address of the view contained in the memory ing file. This function rounds the address you passed here into a page boundary value.

The second parameter indicates the number of bytes you want to refresh. The system rounds up the number so that the total number of bytes is an integer of the page. If you call the flushviewoffile function without modifying any data, this function only returns data without writing any information to the disk.

Step 5 and Step 6: Disable the file ing object and file object

Use the closehandle function to close the corresponding object.

Close these objects when the Code starts to run:

Handle hfile = createfile (...);

Handle hfilemapping = createfilemapping (hfile ,...);

Closehandle (hfile );

Pvoid pvfile = mapviewoffile (hfilemapping ,...);

Closehandle (hfilemapping );

// Use the memory-mapped file.

Unmapviewoffile (pvfile );

Example: (download vs2008 code)

//------------------------------------------------------------

// File name: 17_filemapping2.cpp

// Creator:
Fanwe

// Mail: fangyukuan@gmail.com

// Creation Time:

// Function description:
Memory ing Data File

//

//------------------------------------------------------------

# Include
"Stdafx. H"

# Include
"Windows. H"

# Include
<Iostream>

Using
Namespace STD;

Int
_ Tmain (INT argc,
_ Tchar * argv [])

{

// Open the file that we want to map.

// On the C drive, create a kuan.txt file and write the content

Handle
Hfile =: createfile (L "C :\\ kuan.txt ",

Generic_read |
Generic_write,

0,

Null,

Open_always,

File_attribute_normal,

Null );

// Create a file-mapping object for the file.

Handle
Hfilemapping =: createfilemapping (hfile,

Null,

Page_writecopy,

0, 0,

Null );

Pbyte
Pbfile = (pbyte): mapviewoffile (hfilemapping,
File_map_copy, 0, 0, 0 );

Cout <
Pbfile <Endl;

: Unmapviewoffile (pbfile );

: Closehandle (hfilemapping );

: Closehandle (hfile );

Return 0;

}

Address: http://www.cnblogs.com/fangyukuan/archive/2010/09/09/1822216.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.