;; This newlisp module implements mmap access to file regions. ;; (unless foop (load "foop.lsp")) (context 'MAIN:MMap) (FOOP base len prot flags fd offset) (constant 'LIBC (exists file? '("/lib/x86_64-linux-gnu/libc.so.6"))) (unless LIBC (die "cannot find libc")) (import LIBC "mmap" "void*" "void*" ; void *addr "long" ; size_t length "int" ; int prot "int" ; int flags "int" ; int fd "long" ; off_t offset ) (import LIBC "munmap" "int" "void*" ; void *addr "long" ; size_t length ) (import LIBC "lseek" "long" "int" ; int fd "long" ; off_t offset, "int" ; int whence ) (constant 'PROT_READ 0x01 'MAP_SHARED 0x01 'SEEK_END 2 ) ;; Creates a new mapping in the virtual address space of the calling ;; process. (define (MMap:MMap FD LENGTH (OFFSET 0)) (when (string? FD) (setf FD (open FD "r"))) (let ((D (list (context) nil LENGTH 0 0 FD OFFSET))) (when (< LENGTH) (setf (D 2) (lseek FD 0 SEEK_END))) (setf (D 1) (mmap 0 (D 2) PROT_READ MAP_SHARED FD OFFSET)) D)) (define (Unmap) (munmap (%base) (%len)))