py-cvs-rel2_1 (Rev 1.2) merge
[python/dscho.git] / Doc / lib / libmmap.tex
blob8d04ededb32209edabccf18d14841c2126861a83
1 \section{\module{mmap} ---
2 Memory-mapped file support}
4 \declaremodule{builtin}{mmap}
5 \modulesynopsis{Interface to memory-mapped files for Unix and Windows.}
7 Memory-mapped file objects behave like both mutable strings and like
8 file objects. You can use mmap objects in most places where strings
9 are expected; for example, you can use the \module{re} module to
10 search through a memory-mapped file. Since they're mutable, you can
11 change a single character by doing \code{obj[\var{index}] = 'a'}, or
12 change a substring by assigning to a slice:
13 \code{obj[\var{i1}:\var{i2}] = '...'}. You can also read and write
14 data starting at the current file position, and \method{seek()}
15 through the file to different positions.
17 A memory-mapped file is created by the following function, which is
18 different on Unix and on Windows.
20 \begin{funcdesc}{mmap}{fileno, length\optional{, tagname}}
21 \strong{(Windows version)} Maps \var{length} bytes from the file
22 specified by the file handle \var{fileno}, and returns a mmap object.
23 If \var{length} is \code{0}, the maximum length of the map will be the
24 current size of the file when \function{mmap()} is called.
25 If you wish to map an existing Python file object, use its
26 \method{fileno()} method to obtain the correct value for the
27 \var{fileno} parameter. The file must be opened for update.
29 \var{tagname}, if specified and not \code{None}, is a string giving a
30 tag name for the mapping. Windows allows you to have many different
31 mappings against the same file. If you specify the name of an
32 existing tag, that tag is opened, otherwise a new tag of this name is
33 created. If this parameter is omitted or \code{None}, the mapping is
34 created without a name. Avoiding the use of the tag parameter will
35 assist in keeping your code portable between \UNIX{} and Windows.
36 \end{funcdesc}
38 \begin{funcdesc}{mmap}{fileno, size\optional{, flags, prot}}
39 \strong{(\UNIX{} version)} Maps \var{length} bytes from the file
40 specified by the file handle \var{fileno}, and returns a mmap object.
41 If you wish to map an existing Python file object, use its
42 \method{fileno()} method to obtain the correct value for the
43 \var{fileno} parameter. The file must be opened for update.
45 \var{flags} specifies the nature of the mapping.
46 \constant{MAP_PRIVATE} creates a private copy-on-write mapping, so
47 changes to the contents of the mmap object will be private to this
48 process, and \constant{MAP_SHARED} creates a mapping that's shared
49 with all other processes mapping the same areas of the file.
50 The default value is \constant{MAP_SHARED}.
52 \var{prot}, if specified, gives the desired memory protection; the two
53 most useful values are \constant{PROT_READ} and \constant{PROT_WRITE},
54 to specify that the pages may be read or written.
55 \var{prot} defaults to \constant{PROT_READ | PROT_WRITE}.
56 \end{funcdesc}
58 Memory-mapped file objects support the following methods:
61 \begin{methoddesc}{close}{}
62 Close the file. Subsequent calls to other methods of the object
63 will result in an exception being raised.
64 \end{methoddesc}
66 \begin{methoddesc}{find}{string\optional{, start}}
67 Returns the lowest index in the object where the substring
68 \var{string} is found. Returns \code{-1} on failure. \var{start} is
69 the index at which the search begins, and defaults to zero.
70 \end{methoddesc}
72 \begin{methoddesc}{flush}{\optional{offset, size}}
73 Flushes changes made to the in-memory copy of a file back to disk.
74 Without use of this call there is no guarantee that changes are
75 written back before the object is destroyed. If \var{offset} and
76 \var{size} are specified, only changes to the given range of bytes
77 will be flushed to disk; otherwise, the whole extent of the mapping is
78 flushed.
79 \end{methoddesc}
81 \begin{methoddesc}{move}{\var{dest}, \var{src}, \var{count}}
82 Copy the \var{count} bytes starting at offset \var{src}
83 to the destination index \var{dest}.
84 \end{methoddesc}
86 \begin{methoddesc}{read}{\var{num}}
87 Return a string containing up to \var{num} bytes starting from the
88 current file position; the file position is updated to point after the
89 bytes that were returned.
90 \end{methoddesc}
92 \begin{methoddesc}{read_byte}{}
93 Returns a string of length 1 containing the character at the current
94 file position, and advances the file position by 1.
95 \end{methoddesc}
97 \begin{methoddesc}{readline}{}
98 Returns a single line, starting at the current file position and up to
99 the next newline.
100 \end{methoddesc}
102 \begin{methoddesc}{resize}{\var{newsize}}
103 \end{methoddesc}
105 \begin{methoddesc}{seek}{pos\optional{, whence}}
106 Set the file's current position.
107 \var{whence} argument is optional and defaults to \code{0} (absolute
108 file positioning); other values are \code{1} (seek relative to the
109 current position) and \code{2} (seek relative to the file's end).
110 \end{methoddesc}
112 \begin{methoddesc}{size}{}
113 Return the length of the file, which can be larger than the size
114 of the memory-mapped area.
115 \end{methoddesc}
117 \begin{methoddesc}{tell}{}
118 Returns the current position of the file pointer.
119 \end{methoddesc}
121 \begin{methoddesc}{write}{\var{string}}
122 Write the bytes in \var{string} into memory at the current position of
123 the file pointer; the file position is updated to point after the
124 bytes that were written.
125 \end{methoddesc}
127 \begin{methoddesc}{write_byte}{\var{byte}}
128 Write the single-character string \var{byte} into memory at the
129 current position of the file pointer; the file position is advanced by
130 \code{1}.
131 \end{methoddesc}