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 ;
7 TUPLE: mapped-file < disposable address handle length ;
9 ERROR: bad-mmap-size n ;
13 HOOK: (mapped-file-reader) os ( path length -- address handle )
14 HOOK: (mapped-file-r/w) os ( path length -- address handle )
16 : prepare-mapped-file ( path quot -- mapped-file path' length )
18 [ normalize-path ] [ file-info size>> ] bi
19 [ dup 0 <= [ throw-bad-mmap-size ] [ 2drop ] if ]
20 [ nip mapped-file new-disposable swap >>length ]
21 ] dip 2tri [ >>address ] [ >>handle ] bi* ; inline
25 : <mapped-file-reader> ( path -- mmap )
26 [ (mapped-file-reader) ] prepare-mapped-file ; inline
28 : <mapped-file> ( path -- mmap )
29 [ (mapped-file-r/w) ] prepare-mapped-file ; inline
31 : <mapped-array> ( mmap c-type -- direct-array )
32 [ [ address>> ] [ length>> ] bi ] dip
34 <c-direct-array> ; inline
36 HOOK: close-mapped-file io-backend ( mmap -- )
38 M: mapped-file dispose* ( mmap -- ) close-mapped-file ;
40 : with-mapped-file ( path quot -- )
41 [ <mapped-file> ] dip with-disposal ; inline
43 : with-mapped-file-reader ( path quot -- )
44 [ <mapped-file-reader> ] dip with-disposal ; inline
48 : (with-mapped-array) ( c-type quot -- )
49 [ [ <mapped-array> ] curry ] dip compose with-disposal ; inline
53 : with-mapped-array ( path c-type quot -- )
54 [ <mapped-file> ] 2dip (with-mapped-array) ; inline
56 : with-mapped-array-reader ( path c-type quot -- )
57 [ <mapped-file-reader> ] 2dip (with-mapped-array) ; inline
60 { [ os unix? ] [ "io.mmap.unix" require ] }
61 { [ os windows? ] [ "io.mmap.windows" require ] }