]> gitweb.factorcode.org Git - factor.git/blob - basis/io/mmap/mmap-docs.factor
factor: trim using lists
[factor.git] / basis / io / mmap / mmap-docs.factor
1 USING: alien alien.c-types destructors help.markup help.syntax
2 quotations specialized-arrays ;
3 IN: io.mmap
4
5 HELP: mapped-file
6 { $class-description "The class of memory-mapped files, opened by " { $link <mapped-file> } " and closed by " { $link close-mapped-file } ". The following two slots are of interest to users:"
7     { $list
8         { { $snippet "length" } " - the length of the mapped file area, in bytes" }
9         { { $snippet "address" } " - an " { $link alien } " pointing at the file's memory area" }
10     }
11 } ;
12
13 HELP: <mapped-file>
14 { $values { "path" "a pathname string" } { "mmap" mapped-file } }
15 { $contract "Opens a file and maps its contents into memory. The length is permitted to exceed the length of the file on disk, in which case the remaining space is padded with zero bytes." }
16 { $notes "You must call " { $link dispose } " when you are finished working with the returned object, to reclaim resources. The " { $link with-mapped-file } " provides an abstraction which can close the mapped file for you." }
17 { $errors "Throws an error if a memory mapping could not be established." } ;
18
19 HELP: with-mapped-file
20 { $values { "path" "a pathname string" } { "quot" { $quotation ( mmap -- ) } } }
21 { $contract "Opens a file for read/write access and maps its contents into memory, passing the " { $link mapped-file } " instance to the quotation. The mapped file is disposed of when the quotation returns, or if an error is thrown." }
22 { $notes "This is a low-level word, because " { $link mapped-file } " objects simply expose their base address and length. Most applications should use " { $link "io.mmap.arrays" } " instead." }
23 { $errors "Throws an error if a memory mapping could not be established." } ;
24
25 HELP: with-mapped-file-reader
26 { $values { "path" "a pathname string" } { "quot" { $quotation ( mmap -- ) } } }
27 { $contract "Opens a file for read-only access and maps its contents into memory, passing the " { $link mapped-file } " instance to the quotation. The mapped file is disposed of when the quotation returns, or if an error is thrown." }
28 { $notes "This is a low-level word, because " { $link mapped-file } " objects simply expose their base address and length. See " { $link "io.mmap.arrays" } " for a discussion of how to access data in a mapped file." }
29 { $errors "Throws an error if a memory mapping could not be established." } ;
30
31 HELP: close-mapped-file
32 { $values { "mmap" mapped-file } }
33 { $contract "Releases system resources associated with the mapped file. This word should not be called by user code; use " { $link dispose } " instead." }
34 { $errors "Throws an error if a memory mapping could not be established." } ;
35
36 HELP: <mapped-file-reader>
37 { $values { "path" "a pathname string" } { "mmap" mapped-file } }
38 { $contract "Opens a file for reading only and maps its contents into memory. The length is permitted to exceed the length of the file on disk, in which case the remaining space is padded with zero bytes." }
39 { $notes "You must call " { $link dispose } " when you are finished working with the returned object, to reclaim resources. The " { $link with-mapped-file } " provides an abstraction which can close the mapped file for you." }
40 { $errors "Throws an error if a memory mapping could not be established." } ;
41
42 HELP: with-mapped-array
43 { $values
44     { "path" "a pathname string" } { "c-type" c-type } { "quot" quotation }
45 }
46 { $description "Memory-maps a file for reading and writing, wrapping it in a specialized array with the given element type. The mapped file is disposed of when the quotation returns, or if an error is thrown." }
47 { $examples
48     { $unchecked-example
49         "USING: alien.c-types io.mmap prettyprint specialized-arrays ;"
50         "SPECIALIZED-ARRAY: uint"
51 "resource:LICENSE.txt\" uint [
52     [ . ] each
53 ] with-mapped-array"
54         ""
55     }
56 }
57 { $errors "Throws an error if a memory mapping could not be established." } ;
58
59 HELP: with-mapped-array-reader
60 { $values
61     { "path" "a pathname string" } { "c-type" c-type } { "quot" quotation }
62 }
63 { $description "Memory-maps a file for reading as a mapped-array of the given c-type. The mapped file is disposed of when the quotation returns, or if an error is thrown." }
64 { $errors "Throws an error if a memory mapping could not be established." } ;
65
66 ARTICLE: "io.mmap.arrays" "Working with memory-mapped data"
67 "The " { $link <mapped-file> } " word returns an instance of " { $link mapped-file } ", which doesn't directly support the sequence protocol. Instead, it needs to be wrapped in a specialized array of the appropriate C type:"
68 { $subsections <mapped-array> }
69 "Additionally, files may be opened with two combinators which take a c-type as input:"
70 { $subsections with-mapped-array with-mapped-array-reader }
71 "The appropriate specialized array type must first be generated with " { $link POSTPONE: SPECIALIZED-ARRAY: } "."
72 $nl
73 "Data can also be read and written from the " { $link mapped-file } " by applying low-level alien words to the " { $slot "address" } " slot. This approach is not recommended, though, since in most cases the compiler will generate efficient code for specialized array usage. See " { $link "reading-writing-memory" } " for a description of low-level memory access primitives." ;
74
75 ARTICLE: "io.mmap.examples" "Memory-mapped file examples"
76 "Convert a file of 4-byte cells from little to big endian or vice versa, by directly mapping it into memory and operating on it with sequence words:"
77 { $code
78     "USING: alien.c-types grouping io.mmap sequences" "specialized-arrays ;"
79     "SPECIALIZED-ARRAY: char"
80     ""
81     "\"mydata.dat\" char ["
82     "    4 <groups>"
83     "    [ reverse! drop ] each"
84     "] with-mapped-array"
85 }
86 "Normalize a file containing packed quadruples of floats:"
87 { $code
88     "USING: kernel io.mmap math.vectors math.vectors.simd" "sequences specialized-arrays ;"
89     "SPECIALIZED-ARRAY: float-4"
90     ""
91     "\"mydata.dat\" float-4 ["
92     "    [ normalize ] map! drop"
93     "] with-mapped-array"
94 } ;
95
96 ARTICLE: "io.mmap" "Memory-mapped files"
97 "The " { $vocab-link "io.mmap" } " vocabulary implements support for memory-mapped files."
98 { $subsections <mapped-file> }
99 "Memory-mapped files are disposable and can be closed with " { $link dispose } " or " { $link with-disposal } "." $nl
100 "Utility combinators which wrap the above:"
101 { $subsections with-mapped-file
102     with-mapped-file-reader
103     with-mapped-array
104     with-mapped-array-reader }
105 "Instances of " { $link mapped-file } " don't support any interesting operations in themselves. There are two facilities for accessing their contents:"
106 { $subsections
107     "io.mmap.arrays"
108     "io.mmap.examples"
109 } ;
110
111 ABOUT: "io.mmap"