sb-win32: offer low-level bindings for file mapping functions
authorAnton Kovalenko <anton@sw4me.com>
Thu, 7 Oct 2010 00:37:07 +0000 (04:37 +0400)
committerDavid Lichteblau <david@lichteblau.com>
Wed, 10 Aug 2011 18:04:33 +0000 (20:04 +0200)
  Includes functions offered by Windows that are similar in
  spirit to mmap.

  Currently these are low-level FFI versions only, and no attempt
  is made to export them from sb-posix under the name mmap.  In
  sb-posix, only a wrapper for msync and definitions of several
  constants is offered, as needed for sb-simple-streams.

Thanks to Anton Kovalenko.

Also take this opportunity to sort the sb-win32 package
definition as a flat list.

contrib/sb-posix/interface.lisp
package-data-list.lisp-expr
src/code/win32.lisp
src/runtime/win32-os.c

index 4669aaf..f8eef1f 100644 (file)
@@ -442,8 +442,29 @@ not supported."
  (define-call "munmap" int minusp
    (start sb-sys:system-area-pointer) (length unsigned))
 
+#-win32
 (define-call "msync" int minusp
   (addr sb-sys:system-area-pointer) (length unsigned) (flags int)))
+#+win32
+(progn
+  ;; No attempt is made to offer a full mmap-like interface on Windows.
+  ;; It would be possible to do so (and has been done by AK on his
+  ;; branch), but the use case is unclear to me.  However, the following
+  ;; definitions are needed to keep existing code in sb-simple-streams
+  ;; running. --DFL
+  (defconstant PROT-READ #x02)
+  (defconstant PROT-WRITE #x04)
+  (defconstant PROT-EXEC #x10)
+  (defconstant PROT-NONE 0)
+  (defconstant MAP-SHARED 0)
+  (defconstant MAP-PRIVATE 1)
+  (defconstant MS-ASYNC nil)
+  (defconstant MS-SYNC nil)
+  (export                            ;export on the fly like define-call
+   (defun msync (address length flags)
+     (declare (ignore flags))
+     (when (zerop (sb-win32:flush-view-of-file address length))
+       (sb-win32::win32-error "FlushViewOfFile")))))
 
 ;;; mlockall, munlockall
 (define-call "mlockall" int minusp (flags int))
index 8b80bf2..112d88e 100644 (file)
@@ -2835,10 +2835,27 @@ structure representations"
 SBCL itself"
       :use ("CL" "SB!ALIEN" "SB!EXT" "SB!INT" "SB!SYS")
       :export ("BOOL"
-               "DWORD" "FD-CLEAR-INPUT" "FD-LISTEN"
-               "FLUSH-CONSOLE-INPUT-BUFFER" "FORMAT-MESSAGE"
-               "GET-LAST-ERROR" "GET-OSFHANDLE" "HANDLE"
-               "HANDLE-CLEAR-INPUT" "HANDLE-LISTEN" "INT-PTR"
-               "INVALID-HANDLE" "MILLISLEEP" "PEEK-CONSOLE-INPUT"
-               "PEEK-NAMED-PIPE" "READ-FILE" "WRITE-FILE"
-               "WITH-PROCESS-TIMES" "GET-VERSION-EX")))
+               "CLOSE-HANDLE"
+               "CREATE-FILE-MAPPING"
+               "DWORD"
+               "FD-CLEAR-INPUT"
+               "FD-LISTEN"
+               "FLUSH-CONSOLE-INPUT-BUFFER"
+               "FLUSH-VIEW-OF-FILE"
+               "FORMAT-MESSAGE"
+               "GET-LAST-ERROR"
+               "GET-OSFHANDLE"
+               "GET-VERSION-EX"
+               "HANDLE"
+               "HANDLE-CLEAR-INPUT"
+               "HANDLE-LISTEN"
+               "INT-PTR"
+               "INVALID-HANDLE"
+               "MAP-VIEW-OF-FILE"
+               "MILLISLEEP"
+               "PEEK-CONSOLE-INPUT"
+               "PEEK-NAMED-PIPE"
+               "READ-FILE"
+               "UNMAP-VIEW-OF-FILE"
+               "WRITE-FILE"
+               "WITH-PROCESS-TIMES")))
index c970412..7481fc5 100644 (file)
@@ -637,3 +637,33 @@ UNIX epoch: January 1st 1970."
           (setf aname (make-system-buffer length))
           (alien-funcall afunc aname (addr length))))
       (cast-and-free aname))))
+
+;; File mapping support routines
+(define-alien-routine (#!+sb-unicode "CreateFileMappingW"
+                       #!-sb-unicode "CreateFileMappingA"
+                       create-file-mapping)
+    handle
+  (handle handle)
+  (security-attributes (* t))
+  (protection dword)
+  (maximum-size-high dword)
+  (maximum-size-low dword)
+  (name (c-string #!+sb-unicode #!+sb-unicode :external-format :ucs-2)))
+
+(define-alien-routine ("MapViewOfFile" map-view-of-file)
+    system-area-pointer
+  (file-mapping handle)
+  (desired-access dword)
+  (offset-high dword)
+  (offset-low dword)
+  (size dword))
+
+(define-alien-routine ("UnmapViewOfFile" unmap-view-of-file) bool
+  (address (* t)))
+
+(define-alien-routine ("FlushViewOfFile" flush-view-of-file) bool
+  (address (* t))
+  (length dword))
+
+(define-alien-routine ("CloseHandle" close-handle) bool
+  (handle handle))
index a767461..406f011 100644 (file)
@@ -573,8 +573,12 @@ void scratch(void)
     strerror(42);
     write(0, 0, 0);
     RtlUnwind(0, 0, 0, 0);
+    MapViewOfFile(0,0,0,0,0);
+    UnmapViewOfFile(0);
+    FlushViewOfFile(0,0);
     #ifndef LISP_FEATURE_SB_UNICODE
       CreateDirectoryA(0,0);
+      CreateFileMappingA(0,0,0,0,0,0);
       GetComputerNameA(0, 0);
       GetCurrentDirectoryA(0,0);
       GetEnvironmentVariableA(0, 0, 0);
@@ -585,6 +589,7 @@ void scratch(void)
       SetEnvironmentVariableA(0, 0);
     #else
       CreateDirectoryW(0,0);
+      CreateFileMappingW(0,0,0,0,0,0);
       FormatMessageW(0, 0, 0, 0, 0, 0, 0);
       GetComputerNameW(0, 0);
       GetCurrentDirectoryW(0,0);