The C language implements the memory mapping mechanism for big data files

  • 2020-05-10 18:36:06
  • OfStack

The C language implements the memory mapping mechanism for big data files

Abstract

This article focuses on the implementation of memory mapping mechanism for files with large amounts of data.

1. Memory mapping

Memory mapped file, is the mapping from 1 file to 1 block of memory. Win32 provides functions that allow applications to map files to one process (CreateFileMapping). Memory mapped files have some similarities with virtual memory, through memory mapping file can keep one address space area, physical memory to the region at the same time, the memory mapping file of physical storage from 1 already exists in the file on disk, and before the operations on the document must first be mapped to a file. Using memory-mapped files to process files stored on disk eliminates the need to perform I/O operations on files, making memory-mapped files useful for processing files with large amounts of data.

2. Implement


#include "stdafx.h" 
#include "windows.h" 
#include <iostream> 
#include <fstream> 
 
using namespace std; 
  
 
int main(int argc, _TCHAR* argv[]){ 
 
  //// Open the file that we want to map. 
  HANDLE hFile = ::CreateFile(L"C:/Users/Joe WANG/Desktop/Data.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); 
 
  // Now map the file 
  PCHAR pbFile = (PCHAR)::MapViewOfFile(hFileMapping, FILE_MAP_COPY, 0, 0, 0); 
  
  // Print result 
  printf("%s\n", ((string)pbFile).c_str());  
 
  // Close all file handle 
  ::UnmapViewOfFile(pbFile); 
  ::CloseHandle(hFileMapping); 
  ::CloseHandle(hFile); 
  
  return 0; 
} 


Related articles: