Added mmap utility and test
authorRalph Ronnquist <ralph.ronnquist@gmail.com>
Tue, 18 Apr 2023 12:20:50 +0000 (22:20 +1000)
committerRalph Ronnquist <ralph.ronnquist@gmail.com>
Tue, 18 Apr 2023 12:20:50 +0000 (22:20 +1000)
lsp-misc/mmap.lsp [new file with mode: 0644]
lsp-misc/test.lsp [new file with mode: 0644]

diff --git a/lsp-misc/mmap.lsp b/lsp-misc/mmap.lsp
new file mode 100644 (file)
index 0000000..639b1c9
--- /dev/null
@@ -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 (file)
index 0000000..94730fa
--- /dev/null
@@ -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)