Saturn Software Mills Visit for more technical article http://www.saturnsoftmills.com
Memory Mapped Files The data stored in a file resides permanently in your computer hard disk. This data is loaded in to main memory (also known as RAM) of your computer before it can be used for any processing. This data transfer from hard disk to RAM takes certain amount of time to load. If the data is large in size the transfer time is noticeable. In 32-bit operating systems, the size of a single data file could be up to 2 GB. But suppose the RAM size is only 512 MB or 1 GB, then the complete data file of 2 GB can not be fitted in to the RAM. In such cases, it is the programmer’s responsibility to read the data in parts. But reading the data in parts will not always give the solution to this problem. To handle such issues, Microsoft has given a very flexible feature of Memory mapped files. Using this technique, you can actually virtually map the data file on the disk and create a view of it as it is stored in RAM. There is no need to load the data file in to RAM. Windows kernel automatically handles the buffering between memory and disk. It can be implemented in the following three steps. First Step : Create a file Object Open the data file using the standard function call CreateFile( ). Its prototype looks like this HANDLE hfile = CreateFile( LPCTSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDistribution, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile ); lpFileName A line pointer to name of the file. dwDesiredAccess Access mode (read/write) dwShareMode Specify the share mode. (whether to share it with other processes or not). lpSecurityAttributes A pointer to security descriptor. dwCreattionDistribution Specify the action required if file exists or do not exists.
Saturn Software Mills Visit for more technical article http://www.saturnsoftmills.com
dwFlagsAndAttributes Specify file attributes and flags. hTemplateFile A handle to the file with attributes to copy. The sample call of this function is shown below. We will assume that we would like to memory map file “mydata.dat”. HANDLE hfile = CreateFile( “mydata.dat”, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY | FILE_FLAG_RANDOM_ACCESS, NULL );
Second Step : Create a file mapping object Once you have create the handle of a open file, now create a map of the open file using the following function call. HANDLE hnd = CreateFileMapping( HANDLE hFile, LPSECURITY_ATTRIBUTES lpFileMappingAttributes, DWORD flProtect, DWORD dwMaximumSizeHigh, DWORD dwMaximumSizeLow, LPCTSTR lpName ); hFile File handle of the file to be mapped. lpFileMappingAttributes Security attributes for the mapping object. flProtect Protection attribute for the file when the file is mapped. dwMaximumSizeHigh High order 32 bits of maximum size of the file mapping object.
Saturn Software Mills Visit for more technical article http://www.saturnsoftmills.com
dwMaximumSizeLow Low order 32 bits of maximum size of the file mapping object. lpName A NULL terminated string to specify the name of the mapping object. The sample code to use this function is as follows. HANDLE hnd = CreateFileMapping( hfile, NULL, PAGE_READONLY, 0, 0, NULL );
Step 3: Create a view of the mapped file Now the final step is to create a view of the mapped file. After this step the returned pointer can straightway be used to access data. The function call is as under. LPVOID *ptr = MapViewOfFile( HANDLE hFileMappingObject, DWORD dwDesiredAccess, DWORD dwFileOffsetHigh, DWORD dwFileOffsetLow, DWORD dwNumberOfBytesToMap ); hFileMappingObject An Open file mapping object handle. dwDesiredAccess Specify the type of file view access, like read only access or read-write access etc. dwFileOffsetHigh High order 32 bit position of the file offset to start the mapping. dwFileOffsetLow Low order 32 bit position of the file offset to start the mapping. dwNumberOfBytesToMap Number of file’s bytes that are mapped. Use zero to map the entire file.
Saturn Software Mills Visit for more technical article http://www.saturnsoftmills.com
The sample code to use this function is as follows. Unsigned char *mapview = (unsigned char*)MapViewOfFile( hnd, FILE_MAP_READ, 0, 0, 0); Now the process of creating a memory mapped file is over. The mapview pointer can be used to access the data from the file assuming that it is stored in an array. So Simple!!!