(import (only memory-mapped-files map-file-to-memory unmap-file-from-memory memory-mapped-file-pointer map/file map/shared prot/read)) (define MMAP-FLAGS (+ map/file map/shared)) ;@siz will NOT be false (define (memory-mapped-buffer loc fd siz chk) ;no size & no buff then byte, size & no buff then 1, else use buff (let ((amt (buffer-byte-size siz chk)) (chunk (make-message-digest-raw-chunk #f 0 0)) (rem siz) (mmap (the (or false (struct mmap)) #f)) ) ; (define (finalize) (unmap-file-from-memory mmap rem)) ; (if (= amt rem) ;then map whole file (begin (set! mmap (map-file-to-memory #f rem prot/read MMAP-FLAGS fd)) (message-digest-raw-chunk-object-set! chunk (memory-mapped-file-pointer mmap)) ;everything actually there - sizeof the allocated buffer! (message-digest-raw-chunk-size-set! chunk rem) (values chunk #f finalize) ) ;else chunking mapping & unmapping unpdater (let ((off 0)) ;FIXME pass fd in message-digest-raw-chunk? (define (updater) (define (reader amt) (when mmap (finalize)) (set! mmap (map-file-to-memory #f amt prot/read MMAP-FLAGS fd off)) (message-digest-raw-chunk-object-set! chunk (memory-mapped-file-pointer mmap)) (message-digest-raw-chunk-size-set! chunk amt) (set! rem (- rem amt)) (set! off (+ off amt)) chunk ) ;`finalize' invoked by caller when done (#f result) (and (not (zero? rem)) (reader (min rem (message-digest-raw-chunk-size chunk)))) ) ; (values chunk updater finalize)) ) ) ) #| ;(assert (and (not (negative? siz)) (or (infinity? siz) (integer? siz))) 'XXX "invalid size" siz) ;-> read-until or (integer?/check siz) -> ;map-file-to-memory 1st arg ptr to alloc'ed buff of chunk-size, not #f ;(null-ptr) - so mapping chunk-size or remainder each on each update call ; (siz = true | buf < siz) ? read-until(fd-mmap) : read-all(fd-mmap) (define (mapped-fileno-buffer loc fd siz buf) ;read fd until EOF using amt sized chunk ;EOF does not only mean read amount = 0 but also errno = 0 ;what about interrupted reads (read-until loc fd (if (true? siz) buf (min siz buf))) ) |#