Processing Files

Q. Are there any additional ways to simplify file processing?

Yes, there are. In particular, these are memory-mapped files. Byte-by-byte reading is not always convenient . Naturally, there is another way of approaching this problem. It is possible to allocate a large enough buffer in memory, to read the file into that buffer, and then to work With that file as with a large array. Memory-mapped files are similar to this; however, in this case, the system carries out most of the job automatically. The most advantageous point here is that the memory-mapped file can be used by several processes. Thus, this mechanism provides another method of interprocess information exchange. However, memory-mapped files have one considerable drawback. After the file is mapped, its size cannot be increased. In this case, the following approach must be used: predict the new file size , and map it to a larger memory area. Actually, the file is not loaded into the memory directly. Instead, it is implemented as mapping to a certain area of the virtual memory (see Chapter 19).

Note 

An interesting fect is that the program loaded into the memory is implemented as a memory-mapped file. Thus, individual parts of the program are loaded into the physical memory only when they are accessed (see Chapter 19).

Operations over memory-mapped files are implemented according to the following algorithm:

  1. Open or create a file using the CreateFile function. This function, as you know, returns the descriptor of the opened file.

  2. Create a mapped file using the CreateFileMapping function. This function is the one that defines the mapping size. As with the previous function, it also returns the descriptor. However, this time, it is the descriptor of the mapped file.

  3. Copy the file or part of it into the mapping area that you have just created using the MapViewOfFile function. This function returns the pointer to the starting position of the area where the file will be located. After that, you can do whatever you need with the mapped file.

  4. If desired, you can write the memory area into the file using the FlushviewOfFile function. Naturally, only that memory area will be flushed to the disk that you reserved when calling the CreateFileMapping function. You can save the file to the disk using the well-known WriteFile function.

  5. Before closing the mapped file, you must invalidate its pointer. This can be achieved using the UnmapViewOfFile function.

  6. Close both handles. First, close the handle returned by the CreateFiieMapping function; then, close the handle created by the CreateFile function.

As you can see, the algorithm of working with mapped files is easy. Consider the new functions encountered for the first time in this book.

The CreateFileMapping function returns the descriptor of the mapped file.

  • First parameterThe descriptor of the opened file.

  • Second parameterThe access attribute, usually assumed to be zero.

  • Third parameterCan take any of the following values and must be compatible to the file sharing mode: PAGE_READONLY , PAGE_WRITECOPY , and PAGE_READWRITE . As you can see, this attribute defines the protection of the file being mapped and must not contradict the attribute of the file opened using the createFile function.

  • Fourth parameterThe most significant part (of the 64-bit value) of the mapped file size.

  • Fifth parameterThe least significant part of the mapped file size (the size may not necessarily match the file size). If both parameters are equal to zero, the size is assumed to be equal to the size of the opened file (the first parameter).

  • Sixth parameterThe name of the mapped file. Is required only if the mapped file will be used by several processes. In this case, a repeated call to the CreateFileMapping function by other processes with the same name specified would return the descriptor of the existing mapped file instead of creating a new file.

The MapviewOfFile function returns the pointer to the memory area where the mapped file or part of the mapped file resides.

  • First parameterThe descriptor returned by the CreateFileMapping function.

  • Second parameterDefines the operation that you are going to carry out. For example, FILE_MAP_READ stands for reading only, and FILE_MAP_WRITE is for reading and writing.

  • Third parameterThe most significant part (of the 64-bit value) of the offset in the file, from-which the data will begin to be copied into the memory.

  • Fourth parameterThe least significant part of the offset in the file, from which data will begin to be copied.

  • Fifth parameterIf you want to copy the entire file, set the last three parameters to zero.

The following are the parameters of the FlushViewOfFile function:

  • First parameterSpecifies the memory area that has to be flushed into a file.

  • Second parameterDefines the number of bytes to be written.

The UnmapViewOfFile function has one parameter:

  • First parameterDescriptor of the mapped file.

Well, that's all that is required to understand the theory of mapped files. Because this material is simple for programming, I won't provide any examples to illustrate it. I hope that if you were patient enough to read this book to this point, you will easily write you own programs using materials of this section.



The Assembly Programming Master Book
The Assembly Programming Master Book
ISBN: 8170088178
EAN: 2147483647
Year: 2004
Pages: 140
Authors: Vlad Pirogov

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net