Memory ing File Memory ing file the memory ing file is mapped from a file to a piece of memory. Win32 provides a function that allows applications to map files to a process (createfilemapping ). In this way, the data in the file can be accessed using memory read/write commands, rather than using I/O system functions such as readfile and writefile, thus improving the file access speed. This type of function is most suitable for applications that need to read files and perform syntax analysis on the information contained in the files, such as the color syntax editor and compiler for syntax analysis of input files. After the file is mapped, the application can use memory operations to manipulate the file without having to read, write, and move the file pointer back and forth in the file. Some operations, such as dropping the "read" character, were quite complicated in the past and users had to deal with the buffer refresh issue. After the ing file is introduced, it is much simpler. All the application has to do is reduce the pointer by a value. Another important application of ing files is to support permanently named shared memory. To share memory between two applications, you can create a file and map it in one application, then another application can use this file as the shared memory by opening and ing it. Using memory ing files in VC ++ to process large files (1) Key Words: VC ++ memory ing Reading Tips: this article provides a convenient and practical solution for reading and storing large files, and introduces the specific implementation process based on the relevant program code. Introduction file operations are one of the most basic functions of applications. Both Win32 APIs and MFC provide functions and classes that support file processing. Commonly Used functions include createfile () and writefile () of Win32 APIs () readfile () and cfile class provided by MFC. In general, these functions can meet the requirements of most scenarios, but for some special application fields, the massive storage needs may be dozens of GB, hundreds of GB, or even several TB, it is obviously not feasible to use the common file processing method. Currently, the operations on such large files are generally handled in the memory ing file mode. This article will discuss this core windows programming technology. The memory ing file of the memory ing file is similar to that of the virtual memory. The memory ing file can reserve an address space area and submit the physical memory to this area, the physical memory for memory file ing only comes from a file that already exists on the disk, rather than a system page file. Before operating the file, you must map the file first, it is like loading the entire file from the disk to the memory. It can be seen that when using memory ing files to process files stored on disks, I/O operations on files are no longer required, this means that when processing a file, you do not have to apply for and allocate a cache for the file. All File Cache operations are directly managed by the system, the process of loading file data to the memory, writing back data from the memory to the file, and releasing memory blocks is canceled, this makes memory ing files play an important role in processing large data volumes. In addition, the system in the actual project usually needs to share data among multiple processes. If the data volume is small, the processing method is flexible and changeable. If the shared data capacity is large, therefore, we need to use the memory ing file. In fact, the memory ing file is the most effective solution to data sharing between multiple local processes. Memory ing files are not simple file I/O operations, but actually use the core Programming Technology of Windows-memory management. Therefore, if you want to have a deeper understanding of memory ing files, you must have a clear understanding of the memory management mechanism of the Windows operating system. The knowledge of memory management is very complex, beyond the scope of this article, I will not go into details here. Interested readers can refer to other related books. The following describes how to use a memory ing file: First, you must use the createfile () function to create or open a file kernel object, which identifies the file to be used as a memory ing file on the disk. After createfile () is used to advertise the location of the file image in the physical storage to the operating system, only the path of the image file is specified, and the image length is not specified yet. To specify the size of the physical storage space required by the file ing object, you must use the createfilemapping () function to create a file ing kernel object to inform the System of the file size and file access method. After creating a file ing object, you must retain an address space area for the file data and submit the file data as a physical storage mapped to the region. The mapviewoffile () function maps all or part of the object mapped to the process address space through system management. In this case, the use and processing of memory ing files are basically the same as that of file data normally loaded into the memory. When the memory ing file is used, you also need to perform a series of operations to clear and release resources that have been used. This part is relatively simple. You can use unmapviewoffile () to cancel the image of file data from the address space of the process, and use closehandle () to close the previously created file ing object and file object. When memory ing file-related functions use memory ing files, the APIs used are mainly the previously mentioned functions. The following describes handle createfile (lpctstr lpfilename, DWORD dwdesiredaccess, DWORD dwsharemode, lpsecurity_attributes lpsecurityattributes, DWORD dwcreationdisposition, DWORD watermark, handle htemplatefile); function createfile () is often used to create and open files even in common file operations, when Processing Memory ing files, this function creates/opens a file Kernel Object and returns its handle, when calling this function, you must set the dwdesiredaccess and dw1_mode parameters based on whether data reading and writing and file sharing are required. The parameter settings are incorrect. The Operation will fail. Handle createfilemapping (handle hfile, lpsecurity_attributes lpfilemappingattributes, DWORD flprotect, DWORD dwmaximumsizehigh, DWORD plugin, lptstr lpname); createfilemapping () function creates a file ing kernel object, use the hfile parameter to specify the file handle to be mapped to the process address space (this handle is obtained from the return value of the createfile () function ). Because the physical memory of the memory ing file is actually stored in a file on the disk, rather than the memory allocated from the page file of the system, the system will not take the initiative to reserve the address space area for it, the bucket of the file is not automatically mapped to this region. To enable the system to determine the protection attribute of the page, you need to set the parameter flprotect, protection attributes page_readonly, page_readwrite, and page_writecopy indicate that after the file ing object is mapped, the file data can be read and written. When page_readonly is used, make sure that createfile () adopts the generic_read parameter; page_readwrite requires that createfile () adopts the generic_read | generic_write parameter; as for the property page_writecopy, you only need to ensure that createfile () use either generic_read or generic_write. DWORD parameters dwmaximumsizehigh and dwmaximumsizelow are also very important. The maximum number of bytes of the file is specified. Because these two parameters are 64-bit, the maximum file length supported is 16eb, it can meet almost all the requirements for processing large data files. Lpvoid mapviewoffile (handle hfilemappingobject, DWORD offline, DWORD dwfileoffsethigh, DWORD offline, DWORD offline); mapviewoffile () function maps file data to the address space of the process. The parameter hfilemappingobject is createfilemapping () handle of the returned file image object. The dwdesiredaccess parameter specifies the access method to the file data again, and must also match the protection attribute set by the createfilemapping () function. Although repeated setting of protection attributes seems redundant, the application can effectively control data protection attributes. The mapviewoffile () function allows all or part of the ing files. During ing, you must specify the offset address of the data file and the length to be mapped. The file offset address is specified by a 64-bit value consisting of DWORD parameters dwfileoffsethigh and dwfileoffsetlow, and must be an integer multiple of the distribution granularity of the operating system. For Windows operating systems, the allocation granularity is fixed to 64 KB. Of course, you can also use the following code to dynamically obtain the distribution granularity of the current operating system: system_info sinf; getsysteminfo (& sinf); DWORD dwallocationgranularity = sinf. dwallocationgranularity; The dwnumberofbytestomap parameter specifies the ing length of the data file. Note that for Windows 9x operating systems, if mapviewoffile () you cannot find a large enough area to store the entire file ing object. A null value (null) is returned. However, in Windows 2000, mapviewoffile () you only need to find a region large enough for the necessary view, without considering the size of the entire file ing object. After processing the file mapped to the process address space area, you need to release the file data image using the unmapviewoffile () function. The prototype declaration of this function is as follows: bool unmapviewoffile (lpcvoid lpbaseaddress); the unique parameter lpbaseaddress specifies the base address of the returned region and must be set to the return value of mapviewoffile. After mapviewoffile () is used, a corresponding unmapviewoffile () call is required. Otherwise, the reserved region cannot be released before the process ends. In addition, the file Kernel Object and file ing kernel object have been created by the createfile () and createfilemapping () functions. It is necessary to release the object through closehandle () before the process ends, otherwise, resource leakage may occur. In addition to the required API functions, You must select other auxiliary functions as needed when using memory ing files. For example, when using memory ing files, the system caches the data pages of files at a high speed to improve the speed, and does not immediately update the disk image of the files when processing the file ing view. To solve this problem, you can use the flushviewoffile () function, which forces the system to re-write some or all of the modified data to the disk image, this ensures that all data updates can be saved to the disk in a timely manner. The following example uses a memory ing file to process large files. A specific example is used to describe how to use the memory ing file. The instance receives data from the port and stores the data on the disk in real time. Due to the large amount of data (dozens of GB), the memory ing file is used for processing. The following describes some of the main code in the mainproc of the working thread, Which is started when the thread is running. When data arrives on the port, the event hevent [0], waitformultipleobjects () will be triggered () after the function waits for the event to occur, it saves the received data to the disk. If it stops receiving the event, it will issue the event hevent [1]. the event processing process is responsible for releasing resources and closing files. The specific implementation process of this thread processing function is as follows :...... // Create a file kernel object whose handle is stored in hfilehandle hfile = createfile ("recv1.zip", generic_write | generic_read, file_assist_read, null, create_always, file_flag_sequential_scan, null ); // create a file ing kernel object, and store the handle in hfilemappinghandle hfilemapping = createfilemapping (hfile, null, page_readwrite, 0, 0x4000000, null ); // release the file Kernel Object closehandle (hfile); // set the size, offset, and other parameters _ int64 qwfilesize = 0x4000000 ;__ int64 qwfileoffset = 0; __int64 T = 600 * sinf. Dwallocationgranularity; DWORD dwbytesinblock = 1000 * sinf. dwallocationgranularity; // map file data to the address space of the Process pbyte pbfile = (pbyte) mapviewoffile (hfilemapping, file_map_all_access, (DWORD) (qwfileoffset> 32), (DWORD) (qwfileoffset & 0 xffffffff), dwbytesinblock); While (bloop) {// capture event hevent [0] and event hevent [1] DWORD ret = waitformultipleobjects (2, hevent, false, infinite); ret-= wait_object_0; Switch (RET) {// receives data event triggers Sending case 0: // receive data from the port and save it to the memory ing file nreadlen = syio_read (Port [1], pbfile + qwfileoffset, queuelen); qwfileoffset + = nreadlen; // when the data is fully 60% written, a new if ing view if (qwfileoffset> T) {T = qwfileoffset + 600 * sinf needs to be created to prevent data overflow. response; unmapviewoffile (pbfile); pbfile = (pbyte) mapviewoffile (hfilemapping, encoding, (DWORD) (qwfileoffset> 32), (DWORD) (qwfileoffset & 0 xffffffffff) );} Break; // Case 1: bloop = false when the termination event is triggered; // unmapviewoffile (pbfile) is removed from the address space of the process ); // close the closehandle (hfilemapping); break ;}}... During the process of terminating event triggering, if only the unmapviewoffile () and closehandle () functions are simply executed, the actual file size cannot be correctly identified, that is, if the opened memory ing file is 30 GB, the received data is only 14 GB, so after the above program is executed, the stored file length is still 30 GB. That is to say, after processing, the file must be restored to the actual size through the memory ing file again. The following is the main code to achieve this requirement: // create another file Kernel Object hfile2 = createfile ("recv.zip", generic_write | generic_read, file_assist_read, null, create_always, file_flag_sequential_scan, null ); // create another file ing Kernel Object hfilemapping2 = createfilemapping (hfile2, null, page_readwrite, 0, (DWORD) (qwfileoffset & 0 xffffffff), null) based on the actual data length ); // close the file Kernel Object closehandle (hfile2); // map the file data to the address space of the Process pbfile2 = (pbyte) mapv Iewoffile (hfilemapping2, file_map_all_access, 0, 0, qwfileoffset); // copy data from the original memory ing file to this memory ing file memcpy (pbfile2, pbfile, qwfileoffset); file: // undo the file data image unmapviewoffile (pbfile); unmapviewoffile (pbfile2); // close the file ing object closehandle (hfilemapping); closehandle (hfilemapping2 ); // Delete the temporary file deletefile ("recv1.zip"). Conclusion: The Memory ing file demonstrates good performance when processing large data files, it has obvious advantages over the file processing methods using functions such as cfile, readfile (), and writefile. The code described in this article is compiled by Microsoft Visual C ++ 6.0 under Windows 98
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.