1 ! Copyright (C) 2007, 2008 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.loader combinators alien.c-types
9 TUPLE: mapped-file < disposable address handle length ;
11 HOOK: (mapped-file-reader) os ( path length -- address handle )
12 HOOK: (mapped-file-r/w) os ( path length -- address handle )
14 ERROR: bad-mmap-size n ;
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 ;
30 : <mapped-file> ( path -- mmap )
31 [ (mapped-file-r/w) ] prepare-mapped-file ;
33 HOOK: close-mapped-file io-backend ( mmap -- )
35 M: mapped-file dispose* ( mmap -- ) close-mapped-file ;
37 : with-mapped-file ( path quot -- )
38 [ <mapped-file> ] dip with-disposal ; inline
40 : with-mapped-file-reader ( path quot -- )
41 [ <mapped-file-reader> ] dip with-disposal ; inline
44 { [ os unix? ] [ "io.mmap.unix" require ] }
45 { [ os winnt? ] [ "io.mmap.windows" require ] }