1 ! Copyright (C) 2007, 2009 Doug Coleman, Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: continuations destructors io.files io.files.info
4 io.backend kernel quotations system alien alien.accessors
5 accessors vocabs combinators alien.c-types alien.data
9 TUPLE: mapped-file < disposable address handle length ;
11 ERROR: bad-mmap-size n ;
15 HOOK: (mapped-file-reader) os ( path length -- address handle )
16 HOOK: (mapped-file-r/w) os ( path length -- address handle )
18 : prepare-mapped-file ( path quot -- mapped-file path' length )
20 [ normalize-path ] [ file-info size>> ] bi
21 [ dup 0 <= [ bad-mmap-size ] [ 2drop ] if ]
22 [ nip mapped-file new-disposable swap >>length ]
23 ] dip 2tri [ >>address ] [ >>handle ] bi* ; inline
27 : <mapped-file-reader> ( path -- mmap )
28 [ (mapped-file-reader) ] prepare-mapped-file ; inline
30 : <mapped-file> ( path -- mmap )
31 [ (mapped-file-r/w) ] prepare-mapped-file ; inline
33 : <mapped-array> ( mmap c-type -- direct-array )
34 [ [ address>> ] [ length>> ] bi ] dip
36 <c-direct-array> ; inline
38 HOOK: close-mapped-file io-backend ( mmap -- )
40 M: mapped-file dispose* ( mmap -- ) close-mapped-file ;
42 : with-mapped-file ( path quot -- )
43 [ <mapped-file> ] dip with-disposal ; inline
45 : with-mapped-file-reader ( path quot -- )
46 [ <mapped-file-reader> ] dip with-disposal ; inline
50 : (with-mapped-array) ( c-type quot -- )
51 [ [ <mapped-array> ] curry ] dip compose with-disposal ; inline
55 : with-mapped-array ( path c-type quot -- )
56 [ <mapped-file> ] 2dip (with-mapped-array) ; inline
58 : with-mapped-array-reader ( path c-type quot -- )
59 [ <mapped-file-reader> ] 2dip (with-mapped-array) ; inline
62 { [ os unix? ] [ "io.mmap.unix" require ] }
63 { [ os windows? ] [ "io.mmap.windows" require ] }