]> gitweb.factorcode.org Git - factor.git/blob - basis/io/mmap/mmap.factor
Disposables are now registered in a global disposables set. To take advantage of...
[factor.git] / basis / io / mmap / mmap.factor
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
6 math ;
7 IN: io.mmap
8
9 TUPLE: mapped-file < disposable address handle length ;
10
11 HOOK: (mapped-file-reader) os ( path length -- address handle )
12 HOOK: (mapped-file-r/w) os ( path length -- address handle )
13
14 ERROR: bad-mmap-size n ;
15
16 <PRIVATE
17
18 : prepare-mapped-file ( path quot -- mapped-file path' length )
19     [
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
24
25 PRIVATE>
26
27 : <mapped-file-reader> ( path -- mmap )
28     [ (mapped-file-reader) ] prepare-mapped-file ;
29
30 : <mapped-file> ( path -- mmap )
31     [ (mapped-file-r/w) ] prepare-mapped-file ;
32
33 HOOK: close-mapped-file io-backend ( mmap -- )
34
35 M: mapped-file dispose* ( mmap -- ) close-mapped-file ;
36
37 : with-mapped-file ( path quot -- )
38     [ <mapped-file> ] dip with-disposal ; inline
39
40 : with-mapped-file-reader ( path quot -- )
41     [ <mapped-file-reader> ] dip with-disposal ; inline
42
43 {
44     { [ os unix? ] [ "io.mmap.unix" require ] }
45     { [ os winnt? ] [ "io.mmap.windows" require ] }
46 } cond