From: Ralph Ronnquist Date: Tue, 18 Apr 2023 12:20:50 +0000 (+1000) Subject: Added mmap utility and test X-Git-Url: https://git.rrq.au/?a=commitdiff_plain;h=99e9ef6c571e7181bdedc4986242b2e50bdd51ac;p=rrq%2Flsp-utils.git Added mmap utility and test --- diff --git a/lsp-misc/mmap.lsp b/lsp-misc/mmap.lsp new file mode 100644 index 0000000..639b1c9 --- /dev/null +++ b/lsp-misc/mmap.lsp @@ -0,0 +1,47 @@ +;; 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))) diff --git a/lsp-misc/test.lsp b/lsp-misc/test.lsp new file mode 100644 index 0000000..94730fa --- /dev/null +++ b/lsp-misc/test.lsp @@ -0,0 +1,8 @@ +(load "mmap.lsp") + +(println (symbols MMap)) +(println MMap:%len) + +(println (setf X (MMap "mmap.lsp"))) +(println (get-string (:%base X))) +(:Unmap X)