]> gitweb.factorcode.org Git - factor.git/blob - basis/io/mmap/mmap.factor
basis: ERROR: changes.
[factor.git] / basis / io / mmap / mmap.factor
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 ;
5 IN: io.mmap
6
7 TUPLE: mapped-file < disposable address handle length ;
8
9 ERROR: bad-mmap-size n ;
10
11 <PRIVATE
12
13 HOOK: (mapped-file-reader) os ( path length -- address handle )
14 HOOK: (mapped-file-r/w) os ( path length -- address handle )
15
16 : prepare-mapped-file ( path quot -- mapped-file path' length )
17     [
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
22
23 PRIVATE>
24
25 : <mapped-file-reader> ( path -- mmap )
26     [ (mapped-file-reader) ] prepare-mapped-file ; inline
27
28 : <mapped-file> ( path -- mmap )
29     [ (mapped-file-r/w) ] prepare-mapped-file ; inline
30
31 : <mapped-array> ( mmap c-type -- direct-array )
32     [ [ address>> ] [ length>> ] bi ] dip
33     [ heap-size /i ] keep
34     <c-direct-array> ; inline
35
36 HOOK: close-mapped-file io-backend ( mmap -- )
37
38 M: mapped-file dispose* ( mmap -- ) close-mapped-file ;
39
40 : with-mapped-file ( path quot -- )
41     [ <mapped-file> ] dip with-disposal ; inline
42
43 : with-mapped-file-reader ( path quot -- )
44     [ <mapped-file-reader> ] dip with-disposal ; inline
45
46 <PRIVATE
47
48 : (with-mapped-array) ( c-type quot -- )
49     [ [ <mapped-array> ] curry ] dip compose with-disposal ; inline
50
51 PRIVATE>
52
53 : with-mapped-array ( path c-type quot -- )
54     [ <mapped-file> ] 2dip (with-mapped-array) ; inline
55
56 : with-mapped-array-reader ( path c-type quot -- )
57     [ <mapped-file-reader> ] 2dip (with-mapped-array) ; inline
58
59 {
60     { [ os unix? ] [ "io.mmap.unix" require ] }
61     { [ os windows? ] [ "io.mmap.windows" require ] }
62 } cond