File mapping has two other notable limitations.
A file mapping cannot be expanded. You need to know the maximum size
when creating the file mapping, and it may be difficult or impossible to
determine this size.
•There is no way to allocate memory within a mapped
memory region without
creating your own memory management functions. It would be convenient if
there were a way to specify a file mapping and a pointer returned by
and obtain a heap handle.
Summary: File Mapping
Here is the standard sequence required by file mapping.
1. Open the file. Be certain that it has
2. If the file is new, set its length either with
(step 3
below) or by using
followed by .
3. Map the file with
or .
4. Create one or more views with
5. Access the file through memory references. If necessary, change the mapped
regions with
and .
6. On completion, perform, in order,
, for the
mapping handle, and
for the file handle.
Example: Sequential File Processing with Mapped Files
The program (Program 2–4) illustrates sequential file processing by convert-
ing ASCII files to Unicode, doubling the file length. This is an ideal application for
memory-mapped files because the most natural way to convert the data is to
process it one character at a time without being concerned with file I/O. Progr
5–3 simply maps the input file and the output file—first computing the output file
length by doubling the input file length—and converts the characters one at a
This example clearly illustrates the trade-off between the file mapping com-
plexity required to initialize the program and the resulting processing s
This complexity may not seem worthwhile given the simplicity of a simple file I/O
implementation, but there is a significant performance advantage. Appendix C
shows that the memory-mapped version can be considerably faster than the file
access versions for NTFS files, so the complexity is worthwhile. The book’s Web
site contains additional performance studies; the highlights are summarized here.
Memory-mapping performance improvements apply only to Windows NT and
the NTFS.
Compared with the best sequential file processing techniques, the
performance improvements can be 3:1 or gre
The performance advantage disappears for larger files. In this example, as the
input fi le size approa ches about one-third of the physical memory size, normal
sequential scanning is preferable. The mapping performance degrades at this
point since the input file fills one-third of the memory and the output file,
which is twice as long, fills the other two-thirds, forcing parts of the output
files to be flushed to disk. Thus, on a 192MB system, mapping performance
degenerates for input files longer than 60MB. Most file processing deals with
smaller files and can take advantage of file mapping.
Program 5–3 shows only the function
. The main program is the
same as for Program 2–4.
Program 5–3
File Conversion with Memory Mapping

Get Windows System Programming Third Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.