Simplify (and robustify) regular PACKing
[sbcl.git] / src / code / unix.lisp
index 9572a22..8b4de36 100644 (file)
@@ -203,33 +203,6 @@ corresponds to NAME, or NIL if there is none."
             (values #!-win32 fd #!+win32 (sb!win32::duplicate-and-unwrap-fd fd)
                     (octets-to-string template-buffer)))))))
 \f
-;;;; timebits.h
-
-;; A time value that is accurate to the nearest
-;; microsecond but also has a range of years.
-;; CLH: Note that tv-usec used to be a time-t, but that this seems
-;; problematic on Darwin x86-64 (and wrong). Trying suseconds-t.
-#!-(or win32 openbsd netbsd)
-(define-alien-type nil
-  (struct timeval
-          (tv-sec time-t)           ; seconds
-          (tv-usec suseconds-t)))   ; and microseconds
-
-;; The above definition doesn't work on 64-bit OpenBSD platforms.
-;; Both tv_sec and tv_usec are declared as long instead of time_t, and
-;; time_t is a typedef for int.
-#!+(or openbsd netbsd)
-(define-alien-type nil
-  (struct timeval
-          (tv-sec long)             ; seconds
-          (tv-usec long)))          ; and microseconds
-
-#!+win32
-(define-alien-type nil
-  (struct timeval
-          (tv-sec time-t)           ; seconds
-          (tv-usec long)))          ; and microseconds
-\f
 ;;;; resourcebits.h
 
 (defconstant rusage_self 0) ; the calling process
@@ -410,11 +383,10 @@ corresponds to NAME, or NIL if there is none."
   ;; helpful, either, as Solaris doesn't export PATH_MAX from
   ;; unistd.h.
   ;;
-  ;; FIXME: The (,stub,) nastiness produces an error message about a
-  ;; comma not inside a backquote. This error has absolutely nothing
-  ;; to do with the actual meaning of the error (and little to do with
-  ;; its location, either).
-  #!-(or linux openbsd freebsd netbsd sunos osf1 darwin hpux win32) (,stub,)
+  ;; Signal an error at compile-time, since it's needed for the
+  ;; runtime to start up
+  #!-(or linux openbsd freebsd netbsd sunos osf1 darwin hpux win32)
+  #.(error "POSIX-GETCWD is not implemented.")
   #!+(or linux openbsd freebsd netbsd sunos osf1 darwin hpux win32)
   (or (newcharstar-string (alien-funcall (extern-alien "getcwd"
                                                        (function (* char)
@@ -565,10 +537,10 @@ avoiding atexit(3) hooks, etc. Otherwise exit(2) is called."
 #!-win32
 (defun unix-fast-getrusage (who)
   (declare (values (member t)
-                   (unsigned-byte 31) (integer 0 1000000)
-                   (unsigned-byte 31) (integer 0 1000000)))
+                   unsigned-byte fixnum
+                   unsigned-byte fixnum))
   (with-alien ((usage (struct rusage)))
-    (syscall* ("getrusage" int (* (struct rusage)))
+    (syscall* ("sb_getrusage" int (* (struct rusage)))
               (values t
                       (slot (slot usage 'ru-utime) 'tv-sec)
                       (slot (slot usage 'ru-utime) 'tv-usec)
@@ -584,7 +556,7 @@ avoiding atexit(3) hooks, etc. Otherwise exit(2) is called."
 #!-win32
 (defun unix-getrusage (who)
   (with-alien ((usage (struct rusage)))
-    (syscall ("getrusage" int (* (struct rusage)))
+    (syscall ("sb_getrusage" int (* (struct rusage)))
               (values t
                       (+ (* (slot (slot usage 'ru-utime) 'tv-sec) 1000000)
                          (slot (slot usage 'ru-utime) 'tv-usec))
@@ -665,7 +637,7 @@ avoiding atexit(3) hooks, etc. Otherwise exit(2) is called."
   `(let ((,n (if (< 0 ,n fd-setsize)
                  ,n
                  (error "Cannot select(2) on ~D: above FD_SETSIZE limit."
-                        (1- num-descriptors)))))
+                        (1- ,n)))))
      (declare (type (integer 0 #.fd-setsize) ,n))
      ,@body))
 
@@ -682,7 +654,7 @@ avoiding atexit(3) hooks, etc. Otherwise exit(2) is called."
            (type (or null (unsigned-byte 31)) timeout-secs timeout-usecs))
   (with-fd-setsize (num-descriptors)
     (flet ((select (tv-sap)
-             (int-syscall ("select" int (* (struct fd-set)) (* (struct fd-set))
+             (int-syscall ("sb_select" int (* (struct fd-set)) (* (struct fd-set))
                                     (* (struct fd-set)) (* (struct timeval)))
                           num-descriptors read-fds write-fds exception-fds
                           tv-sap)))
@@ -698,28 +670,29 @@ avoiding atexit(3) hooks, etc. Otherwise exit(2) is called."
 
 ;;; UNIX-SELECT accepts sets of file descriptors and waits for an event
 ;;; to happen on one of them or to time out.
-(defmacro num-to-fd-set (fdset num)
-  `(if (fixnump ,num)
-       (progn
-         (setf (deref (slot ,fdset 'fds-bits) 0) ,num)
-         ,@(loop for index upfrom 1 below (/ fd-setsize
-                                             sb!vm:n-machine-word-bits)
-             collect `(setf (deref (slot ,fdset 'fds-bits) ,index) 0)))
-       (progn
-         ,@(loop for index upfrom 0 below (/ fd-setsize
-                                             sb!vm:n-machine-word-bits)
-             collect `(setf (deref (slot ,fdset 'fds-bits) ,index)
-                            (ldb (byte sb!vm:n-machine-word-bits
-                                       ,(* index sb!vm:n-machine-word-bits))
-                                 ,num))))))
-
-(defmacro fd-set-to-num (nfds fdset)
-  `(if (<= ,nfds sb!vm:n-machine-word-bits)
-       (deref (slot ,fdset 'fds-bits) 0)
-       (+ ,@(loop for index upfrom 0 below (/ fd-setsize
-                                              sb!vm:n-machine-word-bits)
-              collect `(ash (deref (slot ,fdset 'fds-bits) ,index)
-                            ,(* index sb!vm:n-machine-word-bits))))))
+(declaim (inline num-to-fd-set fd-set-to-num))
+(defun num-to-fd-set (fdset num)
+  (typecase num
+    (fixnum
+     (setf (deref (slot fdset 'fds-bits) 0) num)
+     (loop for index from 1 below (/ fd-setsize
+                                     sb!vm:n-machine-word-bits)
+           do (setf (deref (slot fdset 'fds-bits) index) 0)))
+    (t
+     (loop for index from 0 below (/ fd-setsize
+                                     sb!vm:n-machine-word-bits)
+           do (setf (deref (slot fdset 'fds-bits) index)
+                    (ldb (byte sb!vm:n-machine-word-bits
+                               (* index sb!vm:n-machine-word-bits))
+                         num))))))
+
+(defun fd-set-to-num (nfds fdset)
+  (if (<= nfds sb!vm:n-machine-word-bits)
+      (deref (slot fdset 'fds-bits) 0)
+      (loop for index below (/ fd-setsize
+                               sb!vm:n-machine-word-bits)
+            sum (ash (deref (slot fdset 'fds-bits) index)
+                     (* index sb!vm:n-machine-word-bits)))))
 
 ;;; Examine the sets of descriptors passed as arguments to see whether
 ;;; they are ready for reading and writing. See the UNIX Programmer's
@@ -729,7 +702,7 @@ avoiding atexit(3) hooks, etc. Otherwise exit(2) is called."
            (type unsigned-byte rdfds wrfds xpfds)
            (type (or (unsigned-byte 31) null) to-secs)
            (type (unsigned-byte 31) to-usecs)
-           (optimize (speed 3) (safety 0) (inhibit-warnings 3)))
+           (optimize (speed 3) (safety 0)))
   (with-fd-setsize (nfds)
     (with-alien ((tv (struct timeval))
                  (rdf (struct fd-set))
@@ -747,7 +720,7 @@ avoiding atexit(3) hooks, etc. Otherwise exit(2) is called."
                    `(if (zerop ,lispvar)
                         (int-sap 0)
                         (alien-sap (addr ,alienvar)))))
-        (syscall ("select" int (* (struct fd-set)) (* (struct fd-set))
+        (syscall ("sb_select" int (* (struct fd-set)) (* (struct fd-set))
                            (* (struct fd-set)) (* (struct timeval)))
                  (values result
                          (fd-set-to-num nfds rdf)
@@ -756,38 +729,33 @@ avoiding atexit(3) hooks, etc. Otherwise exit(2) is called."
                  nfds (frob rdfds rdf) (frob wrfds wrf) (frob xpfds xpf)
                  (if to-secs (alien-sap (addr tv)) (int-sap 0)))))))
 
-;;; Lisp-side implmentations of FD_FOO macros. Abandon all hope who enters
-;;; here...
-;;;
-(defmacro fd-set (offset fd-set)
-  (with-unique-names (word bit)
-    `(multiple-value-bind (,word ,bit) (floor ,offset
-                                              sb!vm:n-machine-word-bits)
-       (setf (deref (slot ,fd-set 'fds-bits) ,word)
-             (logior (truly-the (unsigned-byte #.sb!vm:n-machine-word-bits)
-                                (ash 1 ,bit))
-                     (deref (slot ,fd-set 'fds-bits) ,word))))))
-
-(defmacro fd-clr (offset fd-set)
-  (with-unique-names (word bit)
-    `(multiple-value-bind (,word ,bit) (floor ,offset
-                                              sb!vm:n-machine-word-bits)
-       (setf (deref (slot ,fd-set 'fds-bits) ,word)
-             (logand (deref (slot ,fd-set 'fds-bits) ,word)
-                     (sb!kernel:word-logical-not
-                      (truly-the (unsigned-byte #.sb!vm:n-machine-word-bits)
-                                 (ash 1 ,bit))))))))
-
-(defmacro fd-isset (offset fd-set)
-  (with-unique-names (word bit)
-    `(multiple-value-bind (,word ,bit) (floor ,offset
-                                              sb!vm:n-machine-word-bits)
-       (logbitp ,bit (deref (slot ,fd-set 'fds-bits) ,word)))))
-
-(defmacro fd-zero (fd-set)
-  `(progn
-     ,@(loop for index upfrom 0 below (/ fd-setsize sb!vm:n-machine-word-bits)
-         collect `(setf (deref (slot ,fd-set 'fds-bits) ,index) 0))))
+;;; Lisp-side implmentations of FD_FOO macros.
+(declaim (inline fd-set fd-clr fd-isset fd-zero))
+(defun fd-set (offset fd-set)
+  (multiple-value-bind (word bit) (floor offset
+                                            sb!vm:n-machine-word-bits)
+     (setf (deref (slot fd-set 'fds-bits) word)
+           (logior (truly-the (unsigned-byte #.sb!vm:n-machine-word-bits)
+                              (ash 1 bit))
+                   (deref (slot fd-set 'fds-bits) word)))))
+
+(defun fd-clr (offset fd-set)
+  (multiple-value-bind (word bit) (floor offset
+                                         sb!vm:n-machine-word-bits)
+    (setf (deref (slot fd-set 'fds-bits) word)
+          (logand (deref (slot fd-set 'fds-bits) word)
+                  (sb!kernel:word-logical-not
+                   (truly-the (unsigned-byte #.sb!vm:n-machine-word-bits)
+                              (ash 1 bit)))))))
+
+(defun fd-isset (offset fd-set)
+  (multiple-value-bind (word bit) (floor offset
+                                         sb!vm:n-machine-word-bits)
+     (logbitp bit (deref (slot fd-set 'fds-bits) word))))
+
+(defun fd-zero (fd-set)
+  (loop for index below (/ fd-setsize sb!vm:n-machine-word-bits)
+        do (setf (deref (slot fd-set 'fds-bits) index) 0)))
 
 #!-os-provides-poll
 (defun unix-simple-poll (fd direction to-msec)
@@ -938,23 +906,6 @@ avoiding atexit(3) hooks, etc. Otherwise exit(2) is called."
 \f
 ;;;; time.h
 
-;; the POSIX.4 structure for a time value. This is like a "struct
-;; timeval" but has nanoseconds instead of microseconds.
-#!-(or openbsd netbsd)
-(define-alien-type nil
-    (struct timespec
-            (tv-sec long)   ; seconds
-            (tv-nsec long))) ; nanoseconds
-
-;; Just as with struct timeval, 64-bit OpenBSD has problems with the
-;; above definition.  tv_sec is declared as time_t instead of long,
-;; and time_t is a typedef for int.
-#!+(or openbsd netbsd)
-(define-alien-type nil
-    (struct timespec
-            (tv-sec time-t)  ; seconds
-            (tv-nsec long))) ; nanoseconds
-
 ;; used by other time functions
 (define-alien-type nil
     (struct tm
@@ -983,7 +934,7 @@ avoiding atexit(3) hooks, etc. Otherwise exit(2) is called."
           (slot req 'tv-nsec) nsecs)
     (loop while (and (eql sb!unix:eintr
                           (nth-value 1
-                                     (int-syscall ("nanosleep" (* (struct timespec))
+                                     (int-syscall ("sb_nanosleep" (* (struct timespec))
                                                                (* (struct timespec)))
                                                   (addr req) (addr rem))))
                      ;; KLUDGE: On Darwin, if an interrupt cases nanosleep to
@@ -1046,14 +997,14 @@ avoiding atexit(3) hooks, etc. Otherwise exit(2) is called."
    T, it-interval-secs, it-interval-usec, it-value-secs, it-value-usec."
   (declare (type (member :real :virtual :profile) which)
            (values t
-                   (unsigned-byte 29) (mod 1000000)
-                   (unsigned-byte 29) (mod 1000000)))
+                   unsigned-byte (mod 1000000)
+                   unsigned-byte (mod 1000000)))
   (let ((which (ecase which
                  (:real itimer-real)
                  (:virtual itimer-virtual)
                  (:profile itimer-prof))))
     (with-alien ((itv (struct itimerval)))
-      (syscall* ("getitimer" int (* (struct itimerval)))
+      (syscall* ("sb_getitimer" int (* (struct itimerval)))
                 (values t
                         (slot (slot itv 'it-interval) 'tv-sec)
                         (slot (slot itv 'it-interval) 'tv-usec)
@@ -1072,11 +1023,11 @@ avoiding atexit(3) hooks, etc. Otherwise exit(2) is called."
    unix-setitimer returns the old contents of the INTERVAL and VALUE
    slots as in unix-getitimer."
   (declare (type (member :real :virtual :profile) which)
-           (type (unsigned-byte 29) int-secs val-secs)
+           (type unsigned-byte int-secs val-secs)
            (type (integer 0 (1000000)) int-usec val-usec)
            (values t
-                   (unsigned-byte 29) (mod 1000000)
-                   (unsigned-byte 29) (mod 1000000)))
+                   unsigned-byte (mod 1000000)
+                   unsigned-byte (mod 1000000)))
   (let ((which (ecase which
                  (:real itimer-real)
                  (:virtual itimer-virtual)
@@ -1087,7 +1038,7 @@ avoiding atexit(3) hooks, etc. Otherwise exit(2) is called."
             (slot (slot itvn 'it-interval) 'tv-usec) int-usec
             (slot (slot itvn 'it-value   ) 'tv-sec ) val-secs
             (slot (slot itvn 'it-value   ) 'tv-usec) val-usec)
-      (syscall* ("setitimer" int (* (struct timeval))(* (struct timeval)))
+      (syscall* ("sb_setitimer" int (* (struct timeval))(* (struct timeval)))
                 (values t
                         (slot (slot itvo 'it-interval) 'tv-sec)
                         (slot (slot itvo 'it-interval) 'tv-usec)
@@ -1115,23 +1066,23 @@ avoiding atexit(3) hooks, etc. Otherwise exit(2) is called."
   (defun get-time-of-day ()
     "Return the number of seconds and microseconds since the beginning of
 the UNIX epoch (January 1st 1970.)"
-    #!+darwin
+    #!+(or darwin netbsd)
     (with-alien ((tv (struct timeval)))
       ;; CLH: FIXME! This seems to be a MacOS bug, but on x86-64/darwin,
       ;; gettimeofday occasionally fails. passing in a null pointer for the
       ;; timezone struct seems to work around the problem. NS notes: Darwin
       ;; manpage says the timezone is not used anymore in their implementation
       ;; at all.
-      (syscall* ("gettimeofday" (* (struct timeval))
+      (syscall* ("sb_gettimeofday" (* (struct timeval))
                                 (* (struct timezone)))
                 (values (slot tv 'tv-sec)
                         (slot tv 'tv-usec))
                 (addr tv)
                 nil))
-    #!-(and x86-64 darwin)
+    #!-(or darwin netbsd)
     (with-alien ((tv (struct timeval))
                  (tz (struct timezone)))
-      (syscall* ("gettimeofday" (* (struct timeval))
+      (syscall* ("sb_gettimeofday" (* (struct timeval))
                                 (* (struct timezone)))
                 (values (slot tv 'tv-sec)
                         (slot tv 'tv-usec))
@@ -1143,7 +1094,7 @@ the UNIX epoch (January 1st 1970.)"
 
   (defun system-real-time-values ()
     (multiple-value-bind (sec usec) (get-time-of-day)
-      (declare (type (unsigned-byte 32) sec usec))
+      (declare (type unsigned-byte sec) (type (unsigned-byte 31) usec))
       (values sec (truncate usec micro-seconds-per-internal-time-unit))))
 
   ;; There are two optimizations here that actually matter (on 32-bit
@@ -1168,7 +1119,7 @@ the UNIX epoch (January 1st 1970.)"
         (c-sec 0)
         (c-msec 0)
         (now 0))
-    (declare (type (unsigned-byte 32) e-sec c-sec)
+    (declare (type unsigned-byte e-sec c-sec)
              (type fixnum e-msec c-msec)
              (type unsigned-byte now))
     (defun reinit-internal-real-time ()
@@ -1204,12 +1155,12 @@ the UNIX epoch (January 1st 1970.)"
     (multiple-value-bind (ignore utime-sec utime-usec stime-sec stime-usec)
         (unix-fast-getrusage rusage_self)
       (declare (ignore ignore)
-               (type (unsigned-byte 31) utime-sec stime-sec)
+               (type unsigned-byte utime-sec stime-sec)
                ;; (Classic CMU CL had these (MOD 1000000) instead, but
                ;; at least in Linux 2.2.12, the type doesn't seem to
                ;; be documented anywhere and the observed behavior is
                ;; to sometimes return 1000000 exactly.)
-               (type (integer 0 1000000) utime-usec stime-usec))
+               (type fixnum utime-usec stime-usec))
       (let ((result (+ (* (+ utime-sec stime-sec)
                           sb!xc:internal-time-units-per-second)
                        (floor (+ utime-usec