]> gitweb.factorcode.org Git - factor.git/blob - basis/io/mmap/mmap-docs.factor
io.mmap: fix obsolete tests and get code to inline better
[factor.git] / basis / io / mmap / mmap-docs.factor
1 USING: help.markup help.syntax alien math continuations
2 destructors 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 ARTICLE: "io.mmap.arrays" "Working with memory-mapped data"
37 "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:"
38 { $subsection <mapped-array> }
39 "The appropriate specialized array type must first be generated with " { $link POSTPONE: SPECIALIZED-ARRAY: } "."
40 $nl
41 "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." ;
42
43 ARTICLE: "io.mmap.examples" "Memory-mapped file examples"
44 "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:"
45 { $code
46     "USING: alien.c-types grouping io.mmap sequences" "specialized-arrays ;"
47     "SPECIALIZED-ARRAY: char"
48     ""
49     "\"mydata.dat\" ["
50     "    char <mapped-array> 4 <sliced-groups>"
51     "    [ reverse-here ] change-each"
52     "] with-mapped-file"
53 }
54 "Normalize a file containing packed quadrupes of floats:"
55 { $code
56     "USING: kernel io.mmap math.vectors math.vectors.simd" "sequences specialized-arrays ;"
57     "SIMD: float"
58     "SPECIALIZED-ARRAY: float-4"
59     ""
60     "\"mydata.dat\" ["
61     "    float-4 <mapped-array>"
62     "    [ normalize ] change-each"
63     "] with-mapped-file"
64 } ;
65
66 ARTICLE: "io.mmap" "Memory-mapped files"
67 "The " { $vocab-link "io.mmap" } " vocabulary implements support for memory-mapped files."
68 { $subsection <mapped-file> }
69 "Memory-mapped files are disposable and can be closed with " { $link dispose } " or " { $link with-disposal } ". A utility combinator which wraps the above:"
70 { $subsection with-mapped-file }
71 "Instances of " { $link mapped-file } " don't support any interesting operations in themselves. There are two facilities for accessing their contents:"
72 { $subsection "io.mmap.arrays" }
73 { $subsection "io.mmap.examples" } ;
74
75 ABOUT: "io.mmap"