mmap now throws an understandable exception upon trying to mmap a zero length file. fix a bug with calling |dispose on an integer if mmap failed on unix

db4
Doug Coleman 2009-02-26 17:34:26 -06:00
parent bda8b2dda6
commit dc370e56ab
3 changed files with 28 additions and 3 deletions

View File

@ -9,3 +9,23 @@ IN: io.mmap.tests
[ 5 ] [ "mmap-test-file.txt" temp-file [ length ] with-mapped-char-file ] unit-test
[ "22345" ] [ "mmap-test-file.txt" temp-file ascii file-contents ] unit-test
[ "mmap-test-file.txt" temp-file delete-file ] ignore-errors
[ ]
[ "mmap-empty-file.txt" temp-file touch-file ] unit-test
! Test for leaking resources bug on Unix
[ ]
[
100000 [
[
"mmap-empty-file.txt" temp-file [
drop
] with-mapped-file
] [ dup bad-mmap-size? [ drop ] [ rethrow ] if ] recover
] times
"asdf" "mmap-asdf-file.txt" temp-file [ ascii set-file-contents ] keep [
drop
] with-mapped-file
] unit-test

View File

@ -2,15 +2,20 @@
! See http://factorcode.org/license.txt for BSD license.
USING: continuations destructors io.files io.files.info
io.backend kernel quotations system alien alien.accessors
accessors system vocabs.loader combinators alien.c-types ;
accessors system vocabs.loader combinators alien.c-types
math ;
IN: io.mmap
TUPLE: mapped-file address handle length disposed ;
HOOK: (mapped-file) os ( path length -- address handle )
ERROR: bad-mmap-size path size ;
: <mapped-file> ( path -- mmap )
[ normalize-path ] [ file-info size>> ] bi [ (mapped-file) ] keep
[ normalize-path ] [ file-info size>> ] bi
dup 0 <= [ bad-mmap-size ] when
[ (mapped-file) ] keep
f mapped-file boa ;
HOOK: close-mapped-file io-backend ( mmap -- )

View File

@ -9,7 +9,7 @@ IN: io.mmap.unix
:: mmap-open ( path length prot flags -- alien fd )
[
f length prot flags
path open-r/w |dispose
path open-r/w [ <fd> |dispose drop ] keep
[ 0 mmap dup MAP_FAILED = [ (io-error) ] when ] keep
] with-destructors ;