]> gitweb.factorcode.org Git - factor.git/blob - basis/io/mmap/mmap.factor
Specialized array overhaul
[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: 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 : <mapped-array> ( mmap c-type -- direct-array )
34     [ [ address>> ] [ length>> ] bi ] dip
35     [ heap-size /i ] keep
36     <c-direct-array> ; inline
37
38 HOOK: close-mapped-file io-backend ( mmap -- )
39
40 M: mapped-file dispose* ( mmap -- ) close-mapped-file ;
41
42 : with-mapped-file ( path quot -- )
43     [ <mapped-file> ] dip with-disposal ; inline
44
45 : with-mapped-file-reader ( path quot -- )
46     [ <mapped-file-reader> ] dip with-disposal ; inline
47
48 {
49     { [ os unix? ] [ "io.mmap.unix" require ] }
50     { [ os winnt? ] [ "io.mmap.windows" require ] }
51 } cond