]> gitweb.factorcode.org Git - factor.git/blob - basis/io/mmap/mmap.factor
factor: more top level forms.
[factor.git] / basis / io / mmap / mmap.factor
1 ! Copyright (C) 2007, 2009 Doug Coleman, Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types alien.data combinators
4 destructors io.backend io.files.info kernel math system vocabs
5 vocabs.platforms ;
6 IN: io.mmap
7
8 TUPLE: mapped-file < disposable address handle length ;
9
10 ERROR: bad-mmap-size n ;
11
12 <PRIVATE
13
14 HOOK: (mapped-file-reader) os ( path length -- address handle )
15 HOOK: (mapped-file-r/w) os ( path length -- address handle )
16
17 : prepare-mapped-file ( path quot -- mapped-file path' length )
18     [
19         [ normalize-path ] [ file-info size>> ] bi
20         [ dup 0 <= [ bad-mmap-size ] [ 2drop ] if ]
21         [ nip mapped-file new-disposable swap >>length ]
22     ] dip 2tri [ >>address ] [ >>handle ] bi* ; inline
23
24 PRIVATE>
25
26 : <mapped-file-reader> ( path -- mmap )
27     [ (mapped-file-reader) ] prepare-mapped-file ; inline
28
29 : <mapped-file> ( path -- mmap )
30     [ (mapped-file-r/w) ] prepare-mapped-file ; inline
31
32 : <mapped-array> ( mmap c-type -- direct-array )
33     [ [ address>> ] [ length>> ] bi ] dip
34     [ heap-size /i ] keep
35     <c-direct-array> ; inline
36
37 HOOK: close-mapped-file io-backend ( mmap -- )
38
39 M: mapped-file dispose* close-mapped-file ;
40
41 : with-mapped-file ( path quot -- )
42     [ <mapped-file> ] dip with-disposal ; inline
43
44 : with-mapped-file-reader ( path quot -- )
45     [ <mapped-file-reader> ] dip with-disposal ; inline
46
47 <PRIVATE
48
49 : (with-mapped-array) ( c-type quot -- )
50     [ [ <mapped-array> ] curry ] dip compose with-disposal ; inline
51
52 PRIVATE>
53
54 : with-mapped-array ( path c-type quot -- )
55     [ <mapped-file> ] 2dip (with-mapped-array) ; inline
56
57 : with-mapped-array-reader ( path c-type quot -- )
58     [ <mapped-file-reader> ] 2dip (with-mapped-array) ; inline
59
60 USE-UNIX: io.mmap.unix
61 USE-WINDOWS: io.mmap.windows