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
8 TUPLE: mapped-file < disposable address handle length ;
10 ERROR: bad-mmap-size n ;
14 HOOK: (mapped-file-reader) os ( path length -- address handle )
15 HOOK: (mapped-file-r/w) os ( path length -- address handle )
17 : prepare-mapped-file ( path quot -- mapped-file path' length )
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
26 : <mapped-file-reader> ( path -- mmap )
27 [ (mapped-file-reader) ] prepare-mapped-file ; inline
29 : <mapped-file> ( path -- mmap )
30 [ (mapped-file-r/w) ] prepare-mapped-file ; inline
32 : <mapped-array> ( mmap c-type -- direct-array )
33 [ [ address>> ] [ length>> ] bi ] dip
35 <c-direct-array> ; inline
37 HOOK: close-mapped-file io-backend ( mmap -- )
39 M: mapped-file dispose* close-mapped-file ;
41 : with-mapped-file ( path quot -- )
42 [ <mapped-file> ] dip with-disposal ; inline
44 : with-mapped-file-reader ( path quot -- )
45 [ <mapped-file-reader> ] dip with-disposal ; inline
49 : (with-mapped-array) ( c-type quot -- )
50 [ [ <mapped-array> ] curry ] dip compose with-disposal ; inline
54 : with-mapped-array ( path c-type quot -- )
55 [ <mapped-file> ] 2dip (with-mapped-array) ; inline
57 : with-mapped-array-reader ( path c-type quot -- )
58 [ <mapped-file-reader> ] 2dip (with-mapped-array) ; inline
60 USE-UNIX: io.mmap.unix
61 USE-WINDOWS: io.mmap.windows