Skip to content
Snippets Groups Projects
buffer.lisp 64.2 KiB
Newer Older
dan's avatar
dan committed
;;; -*- Mode: LISP; Syntax: Common-lisp; Package: XLIB; Base: 10; Lowercase: Yes -*-

;;; This file contains definitions for the BUFFER object for Common-Lisp X
;;; windows version 11

;;;
;;;			 TEXAS INSTRUMENTS INCORPORATED
;;;				  P.O. BOX 2909
;;;			       AUSTIN, TEXAS 78769
;;;
;;; Copyright (C) 1987 Texas Instruments Incorporated.
;;;
;;; Permission is granted to any individual or institution to use, copy, modify,
;;; and distribute this software, provided that this complete copyright and
;;; permission notice is maintained, intact, in all copies and supporting
;;; documentation.
;;;
;;; Texas Instruments Incorporated provides this software "as is" without
;;; express or implied warranty.
;;;

;; A few notes:
;;
;;  1. The BUFFER implements a two-way buffered byte / half-word
;;     / word stream.  Hooks are left for implementing this with a
;;     shared memory buffer, or with effenciency hooks to the network
;;     code.
;;
;;  2. The BUFFER object uses overlapping displaced arrays for
;;     inserting and removing bytes half-words and words.
;;
;;  3. The BYTE component of these arrays is written to a STREAM
;;     associated with the BUFFER.  The stream has its own buffer.
;;     This may be made more efficient by using the Zetalisp
;;     :Send-Output-Buffer operation.
;;
;;  4. The BUFFER object is INCLUDED in the DISPLAY object.
;;     This was done to reduce access time when sending requests,
;;     while maintaing some code modularity.
;;     Several buffer functions are duplicated (with-buffer,
;;     buffer-force-output, close-buffer) to keep the naming
;;     conventions consistent.
;;
;;  5. A nother layer of software is built on top of this for generating
;;     both client and server interface routines, given a specification
;;     of the protocol. (see the INTERFACE file)
;;
;;  6. Care is taken to leave the buffer pointer (buffer-bbuf) set to
;;     a point after a complete request.  This is to ensure that a partial
;;     request won't be left after aborts (e.g. control-abort on a lispm).

(in-package :xlib)

csr21's avatar
csr21 committed
(defconstant +requestsize+ 160) ;; Max request size (excluding variable length requests)
dan's avatar
dan committed

;;; This is here instead of in bufmac so that with-display can be
;;; compiled without macros and bufmac being loaded.

(defmacro with-buffer ((buffer &key timeout inline)
		       &body body &environment env)
  ;; This macro is for use in a multi-process environment.  It provides
  ;; exclusive access to the local buffer object for request generation and
  ;; reply processing.
  `(macrolet ((with-buffer ((buffer &key timeout) &body body)
		;; Speedup hack for lexically nested with-buffers
		`(progn
		   (progn ,buffer ,@(and timeout `(,timeout)) nil)
		   ,@body)))
     ,(if (and (null inline) (macroexpand '(use-closures) env))
	  `(flet ((.with-buffer-body. () ,@body))
	     #+clx-ansi-common-lisp
	     (declare (dynamic-extent #'.with-buffer-body.))
	     (with-buffer-function ,buffer ,timeout #'.with-buffer-body.))
	(let ((buf (if (or (symbolp buffer) (constantp buffer))
		       buffer
		     '.buffer.)))
	  `(let (,@(unless (eq buf buffer) `((,buf ,buffer))))
	     ,@(unless (eq buf buffer) `((declare (type buffer ,buf))))
	     ,(declare-bufmac)
	     (when (buffer-dead ,buf)
	       (x-error 'closed-display :display ,buf))
	     (holding-lock ((buffer-lock ,buf) ,buf "CLX Display Lock"
			    ,@(and timeout `(:timeout ,timeout)))
	       ,@body))))))

(defun with-buffer-function (buffer timeout function)
  (declare (type display buffer)
	   (type (or null number) timeout)
	   (type function function)
	   #+clx-ansi-common-lisp
	   (dynamic-extent function)
csr21's avatar
csr21 committed
	   ;; FIXME: This is probably more a bug in SBCL (logged as
	   ;; bug #243)
	   (ignorable timeout)
dan's avatar
dan committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
	   #+(and lispm (not clx-ansi-common-lisp))
	   (sys:downward-funarg function))
  (with-buffer (buffer :timeout timeout :inline t)
    (funcall function)))

;;; The following are here instead of in bufmac so that event-case can
;;; be compiled without macros and bufmac being loaded.

(defmacro read-card8 (byte-index)
  `(aref-card8 buffer-bbuf (index+ buffer-boffset ,byte-index)))

(defmacro read-int8 (byte-index)
  `(aref-int8 buffer-bbuf (index+ buffer-boffset ,byte-index)))

(defmacro read-card16 (byte-index)
  #+clx-overlapping-arrays
  `(aref-card16 buffer-wbuf (index+ buffer-woffset (index-ash ,byte-index -1)))
  #-clx-overlapping-arrays
  `(aref-card16 buffer-bbuf (index+ buffer-boffset ,byte-index)))

(defmacro read-int16 (byte-index)
  #+clx-overlapping-arrays
  `(aref-int16 buffer-wbuf (index+ buffer-woffset (index-ash ,byte-index -1)))
  #-clx-overlapping-arrays
  `(aref-int16 buffer-bbuf (index+ buffer-boffset ,byte-index)))

(defmacro read-card32 (byte-index)
  #+clx-overlapping-arrays
  `(aref-card32 buffer-lbuf (index+ buffer-loffset (index-ash ,byte-index -2)))
  #-clx-overlapping-arrays
  `(aref-card32 buffer-bbuf (index+ buffer-boffset ,byte-index)))

(defmacro read-int32 (byte-index)
  #+clx-overlapping-arrays
  `(aref-int32 buffer-lbuf (index+ buffer-loffset (index-ash ,byte-index -2)))
  #-clx-overlapping-arrays
  `(aref-int32 buffer-bbuf (index+ buffer-boffset ,byte-index)))

(defmacro read-card29 (byte-index)
  #+clx-overlapping-arrays
  `(aref-card29 buffer-lbuf (index+ buffer-loffset (index-ash ,byte-index -2)))
  #-clx-overlapping-arrays
  `(aref-card29 buffer-bbuf (index+ buffer-boffset ,byte-index)))

(defmacro event-code (reply-buffer)
  ;; The reply-buffer structure is used for events.
  ;; The size slot is used for the event code.
  `(reply-size ,reply-buffer))

(defmacro reading-event ((event &rest options) &body body)
  (declare (arglist (buffer &key sizes) &body body))
  ;; BODY may contain calls to (READ32 &optional index) etc.
  ;; These calls will read from the input buffer at byte
  ;; offset INDEX.  If INDEX is not supplied, then the next
  ;; word, half-word or byte is returned.
  `(with-buffer-input (,event ,@options) ,@body))

(defmacro with-buffer-input ((reply-buffer &key display (sizes '(8 16 32)) index)
			     &body body)
  (unless (listp sizes) (setq sizes (list sizes)))
  ;; 160 is a special hack for client-message-events
  (when (set-difference sizes '(0 8 16 32 160 256))
    (error "Illegal sizes in ~a" sizes))
  `(let ((%reply-buffer ,reply-buffer)
	 ,@(and display `((%buffer ,display))))
     (declare (type reply-buffer %reply-buffer)
	      ,@(and display '((type display %buffer))))
     ,(declare-bufmac)
     ,@(and display '(%buffer))
     (let* ((buffer-boffset (the array-index ,(or index 0)))
	    #-clx-overlapping-arrays
	    (buffer-bbuf (reply-ibuf8 %reply-buffer))
	    #+clx-overlapping-arrays
	    ,@(append
		(when (member 8 sizes)
		  `((buffer-bbuf (reply-ibuf8 %reply-buffer))))
		(when (or (member 16 sizes) (member 160 sizes))
		  `((buffer-woffset (index-ash buffer-boffset -1))
		    (buffer-wbuf (reply-ibuf16 %reply-buffer))))
		(when (member 32 sizes)
		  `((buffer-loffset (index-ash buffer-boffset -2))
		    (buffer-lbuf (reply-ibuf32 %reply-buffer))))))
       (declare (type array-index buffer-boffset))
       #-clx-overlapping-arrays
       (declare (type buffer-bytes buffer-bbuf))
       #+clx-overlapping-arrays
       ,@(append
	   (when (member 8 sizes)
	     '((declare (type buffer-bytes buffer-bbuf))))
	   (when (member 16 sizes)
	     '((declare (type array-index buffer-woffset))
	       (declare (type buffer-words buffer-wbuf))))
	   (when (member 32 sizes)
	     '((declare (type array-index buffer-loffset))
	       (declare (type buffer-longs buffer-lbuf)))))
       buffer-boffset
       #-clx-overlapping-arrays
       buffer-bbuf
       #+clx-overlapping-arrays
       ,@(append
	   (when (member 8  sizes) '(buffer-bbuf))
	   (when (member 16 sizes) '(buffer-woffset buffer-wbuf))
	   (when (member 32 sizes) '(buffer-loffset buffer-lbuf)))
       #+clx-overlapping-arrays
       (macrolet ((%buffer-sizes () ',sizes))
	 ,@body)
       #-clx-overlapping-arrays
       ,@body)))

(defun make-buffer (output-size constructor &rest options)
  (declare (dynamic-extent options))
  ;; Output-Size is the output-buffer size in bytes.
  (let ((byte-output (make-array output-size :element-type 'card8
				 :initial-element 0)))
    (apply constructor
	   :size output-size
	   :obuf8 byte-output
	   #+clx-overlapping-arrays
	   :obuf16
	   #+clx-overlapping-arrays
	   (make-array (index-ash output-size -1)
		       :element-type 'overlap16
		       :displaced-to byte-output)
	   #+clx-overlapping-arrays
	   :obuf32
	   #+clx-overlapping-arrays
	   (make-array (index-ash output-size -2)
		       :element-type 'overlap32
		       :displaced-to byte-output)
	   options))) 

(defun make-reply-buffer (size)
  ;; Size is the buffer size in bytes
  (let ((byte-input (make-array size :element-type 'card8
				:initial-element 0)))
    (make-reply-buffer-internal
      :size size
      :ibuf8 byte-input
      #+clx-overlapping-arrays
      :ibuf16
      #+clx-overlapping-arrays
      (make-array (index-ash size -1)
		  :element-type 'overlap16
		  :displaced-to byte-input)
      #+clx-overlapping-arrays
      :ibuf32
      #+clx-overlapping-arrays
      (make-array (index-ash size -2)
		  :element-type 'overlap32
		  :displaced-to byte-input))))

(defun buffer-ensure-size (buffer size)
  (declare (type buffer buffer)
	   (type array-index size))
  (when (index> size (buffer-size buffer))
    (with-buffer (buffer)
      (buffer-flush buffer)
      (let* ((new-buffer-size (index-ash 1 (integer-length (index1- size))))
	     (new-buffer (make-array new-buffer-size :element-type 'card8
				     :initial-element 0)))
	(setf (buffer-obuf8 buffer) new-buffer)
	#+clx-overlapping-arrays
	(setf (buffer-obuf16 buffer)
	      (make-array (index-ash new-buffer-size -1)
			  :element-type 'overlap16
			  :displaced-to new-buffer)
	      (buffer-obuf32 buffer)
	      (make-array (index-ash new-buffer-size -2)
			  :element-type 'overlap32
			  :displaced-to new-buffer))))))

(defun buffer-pad-request (buffer pad)
  (declare (type buffer buffer)
	   (type array-index pad))
  (unless (index-zerop pad)
    (when (index> (index+ (buffer-boffset buffer) pad)
		  (buffer-size buffer))
      (buffer-flush buffer))
    (incf (buffer-boffset buffer) pad)
    (unless (index-zerop (index-mod (buffer-boffset buffer) 4))
      (buffer-flush buffer))))

(declaim (inline buffer-new-request-number))

(defun buffer-new-request-number (buffer)
  (declare (type buffer buffer))
  (setf (buffer-request-number buffer)
	(ldb (byte 16 0) (1+ (buffer-request-number buffer)))))

(defun with-buffer-request-function (display gc-force request-function)
  (declare (type display display)
	   (type (or null gcontext) gc-force))
  (declare (type function request-function)
	   #+clx-ansi-common-lisp
	   (dynamic-extent request-function)
	   #+(and lispm (not clx-ansi-common-lisp))
	   (sys:downward-funarg request-function))
  (with-buffer (display :inline t)
    (multiple-value-prog1
      (progn
	(when gc-force (force-gcontext-changes-internal gc-force))
	(without-aborts (funcall request-function display)))
      (display-invoke-after-function display))))

(defun with-buffer-request-function-nolock (display gc-force request-function)
  (declare (type display display)
	   (type (or null gcontext) gc-force))
  (declare (type function request-function)
	   #+clx-ansi-common-lisp
	   (dynamic-extent request-function)
	   #+(and lispm (not clx-ansi-common-lisp))
	   (sys:downward-funarg request-function))
  (multiple-value-prog1
    (progn
      (when gc-force (force-gcontext-changes-internal gc-force))
      (without-aborts (funcall request-function display)))
    (display-invoke-after-function display)))

(defstruct (pending-command (:copier nil) (:predicate nil))
  (sequence 0 :type card16)
  (reply-buffer nil :type (or null reply-buffer))
  (process nil)
  (next nil #-explorer :type #-explorer (or null pending-command)))

(defun with-buffer-request-and-reply-function
       (display multiple-reply request-function reply-function)
  (declare (type display display)
	   (type generalized-boolean multiple-reply))
  (declare (type function request-function reply-function)
	   #+clx-ansi-common-lisp
	   (dynamic-extent request-function reply-function)
	   #+(and lispm (not clx-ansi-common-lisp))
	   (sys:downward-funarg request-function reply-function))
  (let ((pending-command nil)
	(reply-buffer nil))
    (declare (type (or null pending-command) pending-command)
	     (type (or null reply-buffer) reply-buffer))
    (unwind-protect
	(progn 
	  (with-buffer (display :inline t)
	    (setq pending-command (start-pending-command display))
	    (without-aborts (funcall request-function display))
	    (buffer-force-output display)
	    (display-invoke-after-function display))
	  (cond (multiple-reply
		 (loop
		   (setq reply-buffer (read-reply display pending-command))
		   (when (funcall reply-function display reply-buffer) (return nil))
		   (deallocate-reply-buffer (shiftf reply-buffer nil))))
		(t
		 (setq reply-buffer (read-reply display pending-command))
		 (funcall reply-function display reply-buffer))))
      (when reply-buffer (deallocate-reply-buffer reply-buffer))
      (when pending-command (stop-pending-command display pending-command)))))

;;
;; Buffer stream operations
;;

(defun buffer-write (vector buffer start end)
  ;; Write out VECTOR from START to END into BUFFER
  ;; Internal function, MUST BE CALLED FROM WITHIN WITH-BUFFER
  (declare (type buffer buffer)
	   (type array-index start end))
  (when (buffer-dead buffer)
    (x-error 'closed-display :display buffer))
  (wrap-buf-output (buffer)
    (funcall (buffer-write-function buffer) vector buffer start end))
  nil)

(defun buffer-flush (buffer)
  ;; Write the buffer contents to the server stream - doesn't force-output the stream
  ;; Internal function, MUST BE CALLED FROM WITHIN WITH-BUFFER
  (declare (type buffer buffer))
  (unless (buffer-flush-inhibit buffer)
    (let ((boffset (buffer-boffset buffer)))
      (declare (type array-index boffset))
      (when (index-plusp boffset)
	(buffer-write (buffer-obuf8 buffer) buffer 0 boffset)
	(setf (buffer-boffset buffer) 0)
	(setf (buffer-last-request buffer) nil))))
  nil)

(defmacro with-buffer-flush-inhibited ((buffer) &body body)
  (let ((buf (if (or (symbolp buffer) (constantp buffer)) buffer '.buffer.)))
    `(let* (,@(and (not (eq buf buffer)) `((,buf ,buffer)))
	    (.saved-buffer-flush-inhibit. (buffer-flush-inhibit ,buf)))
       (unwind-protect
	   (progn
	     (setf (buffer-flush-inhibit ,buf) t)
	     ,@body)
	 (setf (buffer-flush-inhibit ,buf) .saved-buffer-flush-inhibit.)))))

(defun buffer-force-output (buffer)
  ;; Output is normally buffered, this forces any buffered output to the server.
  (declare (type buffer buffer))
  (when (buffer-dead buffer)
    (x-error 'closed-display :display buffer))
  (buffer-flush buffer)
  (wrap-buf-output (buffer)
    (without-aborts
      (funcall (buffer-force-output-function buffer) buffer)))
  nil)

(defun close-buffer (buffer &key abort)
  ;; Close the host connection in BUFFER
  (declare (type buffer buffer))
  (unless (null (buffer-output-stream buffer))
    (wrap-buf-output (buffer)
      (funcall (buffer-close-function buffer) buffer :abort abort))
    (setf (buffer-dead buffer) t)
    ;; Zap pointers to the streams, to ensure they're GC'd
    (setf (buffer-output-stream buffer) nil)
    (setf (buffer-input-stream buffer) nil)
    )
  nil)

(defun buffer-input  (buffer vector start end &optional timeout)
  ;; Read into VECTOR from the buffer stream
  ;; Timeout, when non-nil, is in seconds
  ;; Returns non-nil if EOF encountered
  ;; Returns :TIMEOUT when timeout exceeded
  (declare (type buffer buffer)
	   (type vector vector)
	   (type array-index start end)
	   (type (or null number) timeout))
  (declare (clx-values eof-p))
  (when (buffer-dead buffer)
    (x-error 'closed-display :display buffer))
  (unless (= start end)
    (let ((result
	    (wrap-buf-input (buffer)
	      (funcall (buffer-input-function buffer)
		       buffer vector start end timeout))))
      (unless (or (null result) (eq result :timeout))
	(close-buffer buffer))
      result)))

(defun buffer-input-wait  (buffer timeout)
  ;; Timeout, when non-nil, is in seconds
  ;; Returns non-nil if EOF encountered
  ;; Returns :TIMEOUT when timeout exceeded
  (declare (type buffer buffer)
	   (type (or null number) timeout))
  (declare (clx-values timeout))
  (when (buffer-dead buffer)
    (x-error 'closed-display :display buffer))
  (let ((result
	  (wrap-buf-input (buffer)
	    (funcall (buffer-input-wait-function buffer)
		     buffer timeout))))
    (unless (or (null result) (eq result :timeout))
      (close-buffer buffer))
    result))

(defun buffer-listen (buffer)
  ;; Returns T if there is input available for the buffer. This should never
  ;; block, so it can be called from the scheduler.
  (declare (type buffer buffer))
  (declare (clx-values input-available))
  (or (not (null (buffer-dead buffer)))
      (wrap-buf-input (buffer)
	(funcall (buffer-listen-function buffer) buffer))))

;;; Reading sequences of strings

;;; a list of pascal-strings with card8 lengths, no padding in between
;;; can't use read-sequence-char
(defun read-sequence-string (buffer-bbuf length nitems result-type
			     &optional (buffer-boffset 0))
  (declare (type buffer-bytes buffer-bbuf)
	   (type array-index length nitems buffer-boffset))
  length
  (with-vector (buffer-bbuf buffer-bytes)
    (let ((result (make-sequence result-type nitems)))
      (do* ((index 0 (index+ index 1 string-length))
	    (count 0 (index1+ count))
	    (string-length 0)
	    (string ""))
	   ((index>= count nitems)
	    result)
	(declare (type array-index index count string-length)
		 (type string string))
	(setq string-length (read-card8 index)
	      string (make-sequence 'string string-length))
	(do ((i (index1+ index) (index1+ i))
	     (j 0 (index1+ j)))
	    ((index>= j string-length)
	     (setf (elt result count) string))
	  (declare (type array-index i j))
	  (setf (aref string j) (card8->char (read-card8 i))))))))

;;; Reading sequences of chars

(defun read-sequence-char (reply-buffer result-type nitems &optional transform data
			   (start 0) (index 0))
  (declare (type reply-buffer reply-buffer)
	   (type t result-type) ;; CL type
	   (type array-index nitems start index)
	   (type (or null sequence) data))
  (declare (type (or null (function (character) t)) transform)
	   #+clx-ansi-common-lisp
	   (dynamic-extent transform)
	   #+(and lispm (not clx-ansi-common-lisp))
	   (sys:downward-funarg transform))
  (if transform 
      (flet ((card8->char->transform (v)
	       (declare (type card8 v))
	       (funcall transform (card8->char v))))
	#+clx-ansi-common-lisp
	(declare (dynamic-extent #'card8->char->transform))
	(read-sequence-card8
	  reply-buffer result-type nitems #'card8->char->transform
	  data start index))
    (read-sequence-card8
      reply-buffer result-type nitems #'card8->char
      data start index)))

;;; Reading sequences of card8's

(defun read-list-card8 (reply-buffer nitems data start index)
  (declare (type reply-buffer reply-buffer)
	   (type array-index nitems start index)
	   (type list data))
  (with-buffer-input (reply-buffer :sizes (8) :index index)
    (do* ((j nitems (index- j 1))
	  (lst (nthcdr start data)  (cdr lst))
	  (index 0 (index+ index 1)))
	 ((index-zerop j))
      (declare (type array-index j index)
	       (list lst))
      (setf (car lst) (read-card8 index)))))

(defun read-list-card8-with-transform (reply-buffer nitems data transform start index)
  (declare (type reply-buffer reply-buffer)
	   (type array-index nitems start index)
	   (type list data))
  (declare (type (function (card8) t) transform)
	   #+clx-ansi-common-lisp
	   (dynamic-extent transform)
	   #+(and lispm (not clx-ansi-common-lisp))
	   (sys:downward-funarg transform))
  (with-buffer-input (reply-buffer :sizes (8) :index index)
    (do* ((j nitems (index- j 1))
	  (lst (nthcdr start data) (cdr lst))
	  (index 0 (index+ index 1)))
	 ((index-zerop j))
      (declare (type array-index j index)
	       (list lst))
      (setf (car lst) (funcall transform (read-card8 index))))))

#-lispm
(defun read-simple-array-card8 (reply-buffer nitems data start index)
  (declare (type reply-buffer reply-buffer)
	   (type array-index nitems start index)
	   (type (simple-array card8 (*)) data))
  (with-vector (data (simple-array card8 (*)))
    (with-buffer-input (reply-buffer :sizes (8))
      (buffer-replace data buffer-bbuf start (index+ start nitems) index))))

#-lispm
(defun read-simple-array-card8-with-transform (reply-buffer nitems data transform start index)
  (declare (type reply-buffer reply-buffer)
	   (type array-index nitems start index)
	   (type (simple-array card8 (*)) data))
  (declare (type (function (card8) card8) transform)
	   #+clx-ansi-common-lisp
	   (dynamic-extent transform)
	   #+(and lispm (not clx-ansi-common-lisp))
	   (sys:downward-funarg transform))
  (with-vector (data (simple-array card8 (*)))
    (with-buffer-input (reply-buffer :sizes (8) :index index)
      (do* ((j start (index+ j 1))
	    (end (index+ start nitems))
	    (index 0 (index+ index 1)))
	   ((index>= j end))
	(declare (type array-index j end index))
	(setf (aref data j) (the card8 (funcall transform (read-card8 index))))))))

(defun read-vector-card8 (reply-buffer nitems data start index)
  (declare (type reply-buffer reply-buffer)
	   (type array-index nitems start index)
	   (type vector data)
	   (optimize #+cmu(ext:inhibit-warnings 3)))
  (with-vector (data vector)
    (with-buffer-input (reply-buffer :sizes (8) :index index)
      (do* ((j start (index+ j 1))
	    (end (index+ start nitems))
	    (index 0 (index+ index 1)))
	   ((index>= j end))
	(declare (type array-index j end index))
	(setf (aref data j) (read-card8 index))))))

(defun read-vector-card8-with-transform (reply-buffer nitems data transform start index)
  (declare (type reply-buffer reply-buffer)
	   (type array-index nitems start index)
	   (type vector data)
	   (optimize #+cmu(ext:inhibit-warnings 3)))
  (declare (type (function (card8) t) transform)
	   #+clx-ansi-common-lisp
	   (dynamic-extent transform)
	   #+(and lispm (not clx-ansi-common-lisp))
	   (sys:downward-funarg transform))
  (with-vector (data vector)
    (with-buffer-input (reply-buffer :sizes (8) :index index)
      (do* ((j start (index+ j 1))
	    (end (index+ start nitems))
	    (index 0 (index+ index 1)))
	   ((index>= j end))
	(declare (type array-index j end index))
	(setf (aref data j) (funcall transform (read-card8 index)))))))

(defun read-sequence-card8 (reply-buffer result-type nitems &optional transform data
			    (start 0) (index 0))
  (declare (type reply-buffer reply-buffer)
	   (type t result-type) ;; CL type
	   (type array-index nitems start index)
	   (type (or null sequence) data))
  (declare (type (or null (function (card8) t)) transform)
	   #+clx-ansi-common-lisp
	   (dynamic-extent transform)
	   #+(and lispm (not clx-ansi-common-lisp))
	   (sys:downward-funarg transform))
  (let ((result (or data (make-sequence result-type nitems))))
    (typecase result
      (list
	(if transform 
	    (read-list-card8-with-transform
	      reply-buffer nitems result transform start index)
	  (read-list-card8 reply-buffer nitems result start index)))
      #-lispm
      ((simple-array card8 (*))
       (if transform 
	   (read-simple-array-card8-with-transform
	     reply-buffer nitems result transform start index)
	 (read-simple-array-card8 reply-buffer nitems result start index)))
      (t
	(if transform 
	    (read-vector-card8-with-transform
	      reply-buffer nitems result transform start index)
	  (read-vector-card8 reply-buffer nitems result start index))))
    result))

;;; For now, perhaps performance it isn't worth doing better?

(defun read-sequence-int8 (reply-buffer result-type nitems &optional transform data
			   (start 0) (index 0))
  (declare (type reply-buffer reply-buffer)
	   (type t result-type) ;; CL type
	   (type array-index nitems start index)
	   (type (or null sequence) data))
  (declare (type (or null (function (int8) t)) transform)
	   #+clx-ansi-common-lisp
	   (dynamic-extent transform)
	   #+(and lispm (not clx-ansi-common-lisp))
	   (sys:downward-funarg transform))
  (if transform 
      (flet ((card8->int8->transform (v)
	       (declare (type card8 v))
	       (funcall transform (card8->int8 v))))
	#+clx-ansi-common-lisp
	(declare (dynamic-extent #'card8->int8->transform))
	(read-sequence-card8
	  reply-buffer result-type nitems #'card8->int8->transform
	  data start index))
    (read-sequence-card8
      reply-buffer result-type nitems #'card8->int8
      data start index)))

;;; Reading sequences of card16's

(defun read-list-card16 (reply-buffer nitems data start index)
  (declare (type reply-buffer reply-buffer)
	   (type array-index nitems start index)
	   (type list data))
  (with-buffer-input (reply-buffer :sizes (16) :index index)
    (do* ((j nitems (index- j 1))
	  (lst (nthcdr start data) (cdr lst))
	  (index 0 (index+ index 2)))
	 ((index-zerop j))
      (declare (type array-index j index)
	       (list lst))
      (setf (car lst) (read-card16 index)))))

(defun read-list-card16-with-transform (reply-buffer nitems data transform start index)
  (declare (type reply-buffer reply-buffer)
	   (type array-index nitems start index)
	   (type list data))
  (declare (type (function (card16) t) transform)
	   #+clx-ansi-common-lisp
	   (dynamic-extent transform)
	   #+(and lispm (not clx-ansi-common-lisp))
	   (sys:downward-funarg transform))
  (with-buffer-input (reply-buffer :sizes (16) :index index)
    (do* ((j nitems (index- j 1))
	  (lst (nthcdr start data) (cdr lst))
	  (index 0 (index+ index 2)))
	 ((index-zerop j))
      (declare (type array-index j index)
	       (list lst))
      (setf (car lst) (funcall transform (read-card16 index))))))

#-lispm
(defun read-simple-array-card16 (reply-buffer nitems data start index)
  (declare (type reply-buffer reply-buffer)
	   (type array-index nitems start index)
	   (type (simple-array card16 (*)) data))
  (with-vector (data (simple-array card16 (*)))
    (with-buffer-input (reply-buffer :sizes (16) :index index)
      #-clx-overlapping-arrays
      (do* ((j start (index+ j 1))
	    (end (index+ start nitems))
	    (index 0 (index+ index 2)))
	   ((index>= j end))
	(declare (type array-index j end index))
	(setf (aref data j) (the card16 (read-card16 index))))
      #+clx-overlapping-arrays
      (buffer-replace data buffer-wbuf start (index+ start nitems) (index-floor index 2)))))

#-lispm
(defun read-simple-array-card16-with-transform (reply-buffer nitems data transform start index)
  (declare (type reply-buffer reply-buffer)
	   (type array-index nitems start index)
	   (type (simple-array card16 (*)) data))
  (declare (type (function (card16) card16) transform)
	   #+clx-ansi-common-lisp
	   (dynamic-extent transform)
	   #+(and lispm (not clx-ansi-common-lisp))
	   (sys:downward-funarg transform))
  (with-vector (data (simple-array card16 (*)))
    (with-buffer-input (reply-buffer :sizes (16) :index index)
      (do* ((j start (index+ j 1))
	    (end (index+ start nitems))
	    (index 0 (index+ index 2)))
	   ((index>= j end))
	(declare (type array-index j end index))
	(setf (aref data j) (the card16 (funcall transform (read-card16 index))))))))

(defun read-vector-card16 (reply-buffer nitems data start index)
  (declare (type reply-buffer reply-buffer)
	   (type array-index nitems start index)
	   (type vector data)
	   (optimize #+cmu(ext:inhibit-warnings 3)))
  (with-vector (data vector)
    (with-buffer-input (reply-buffer :sizes (16) :index index)
      #-clx-overlapping-arrays
      (do* ((j start (index+ j 1))
	    (end (index+ start nitems))
	    (index 0 (index+ index 2)))
	   ((index>= j end))
	(declare (type array-index j end index))
	(setf (aref data j) (read-card16 index)))
      #+clx-overlapping-arrays
      (buffer-replace data buffer-wbuf start (index+ start nitems) (index-floor index 2)))))

(defun read-vector-card16-with-transform (reply-buffer nitems data transform start index)
  (declare (type reply-buffer reply-buffer)
	   (type array-index nitems start index)
	   (type vector data)
	   (optimize #+cmu(ext:inhibit-warnings 3)))
  (declare (type (function (card16) t) transform)
	   #+clx-ansi-common-lisp
	   (dynamic-extent transform)
	   #+(and lispm (not clx-ansi-common-lisp))
	   (sys:downward-funarg transform))
  (with-vector (data vector)
    (with-buffer-input (reply-buffer :sizes (16) :index index)
      (do* ((j start (index+ j 1))
	    (end (index+ start nitems))
	    (index 0 (index+ index 2)))
	   ((index>= j end))
	(declare (type array-index j end index))
	(setf (aref data j) (funcall transform (read-card16 index)))))))

(defun read-sequence-card16 (reply-buffer result-type nitems &optional transform data
			     (start 0) (index 0))
  (declare (type reply-buffer reply-buffer)
	   (type t result-type) ;; CL type
	   (type array-index nitems start index)
	   (type (or null sequence) data))
  (declare (type (or null (function (card16) t)) transform)
	   #+clx-ansi-common-lisp
	   (dynamic-extent transform)
	   #+(and lispm (not clx-ansi-common-lisp))
	   (sys:downward-funarg transform))
  (let ((result (or data (make-sequence result-type nitems))))
    (typecase result
      (list
	(if transform 
	    (read-list-card16-with-transform reply-buffer nitems result transform start index)
	  (read-list-card16 reply-buffer nitems result start index)))
      #-lispm
      ((simple-array card16 (*))
       (if transform 
	   (read-simple-array-card16-with-transform
	     reply-buffer nitems result transform start index)
	 (read-simple-array-card16 reply-buffer nitems result start index)))
      (t
	(if transform 
	    (read-vector-card16-with-transform
	      reply-buffer nitems result transform start index)
	  (read-vector-card16 reply-buffer nitems result start index))))
    result))
  
;;; For now, perhaps performance it isn't worth doing better?

(defun read-sequence-int16 (reply-buffer result-type nitems &optional transform data
			    (start 0) (index 0))
  (declare (type reply-buffer reply-buffer)
	   (type t result-type) ;; CL type
	   (type array-index nitems start index)
	   (type (or null sequence) data))
  (declare (type (or null (function (int16) t)) transform)
	   #+clx-ansi-common-lisp
	   (dynamic-extent transform)
	   #+(and lispm (not clx-ansi-common-lisp))
	   (sys:downward-funarg transform))
  (if transform 
      (flet ((card16->int16->transform (v)
	       (declare (type card16 v))
	       (funcall transform (card16->int16 v))))
	#+clx-ansi-common-lisp
	(declare (dynamic-extent #'card16->int16->transform))
	(read-sequence-card16
	  reply-buffer result-type nitems #'card16->int16->transform
	  data start index))
    (read-sequence-card16
      reply-buffer result-type nitems #'card16->int16
      data start index)))

;;; Reading sequences of card32's

(defun read-list-card32 (reply-buffer nitems data start index)
  (declare (type reply-buffer reply-buffer)
	   (type array-index nitems start index)
	   (type list data))
  (with-buffer-input (reply-buffer :sizes (32) :index index)
    (do* ((j nitems (index- j 1))
	  (lst (nthcdr start data) (cdr lst))
	  (index 0 (index+ index 4)))
	 ((index-zerop j))
      (declare (type array-index j index)
	       (list lst))
      (setf (car lst) (read-card32 index)))))

(defun read-list-card32-with-transform (reply-buffer nitems data transform start index)
  (declare (type reply-buffer reply-buffer)
	   (type array-index nitems start index)
	   (type list data))
  (declare (type (function (card32) t) transform)
	   #+clx-ansi-common-lisp
	   (dynamic-extent transform)
	   #+(and lispm (not clx-ansi-common-lisp))
	   (sys:downward-funarg transform))
  (with-buffer-input (reply-buffer :sizes (32) :index index)
    (do* ((j nitems (index- j 1))
	  (lst (nthcdr start data) (cdr lst))
	  (index 0 (index+ index 4)))
	 ((index-zerop j))
      (declare (type array-index j index)
	       (list lst))
      (setf (car lst) (funcall transform (read-card32 index))))))

#-lispm
(defun read-simple-array-card32 (reply-buffer nitems data start index)
  (declare (type reply-buffer reply-buffer)
	   (type array-index nitems start index)
	   (type (simple-array card32 (*)) data))
  (with-vector (data (simple-array card32 (*)))
    (with-buffer-input (reply-buffer :sizes (32) :index index)
      #-clx-overlapping-arrays
      (do* ((j start (index+ j 1))
	    (end (index+ start nitems))
	    (index 0 (index+ index 4)))
	   ((index>= j end))
	(declare (type array-index j end index))
	(setf (aref data j) (the card32 (read-card32 index))))
      #+clx-overlapping-arrays
      (buffer-replace data buffer-lbuf start (index+ start nitems) (index-floor index 4)))))

#-lispm
(defun read-simple-array-card32-with-transform (reply-buffer nitems data transform start index)
  (declare (type reply-buffer reply-buffer)
	   (type array-index nitems start index)
	   (type (simple-array card32 (*)) data))
  (declare (type (function (card32) card32) transform)
	   #+clx-ansi-common-lisp
	   (dynamic-extent transform)
	   #+(and lispm (not clx-ansi-common-lisp))
	   (sys:downward-funarg transform))
  (with-vector (data (simple-array card32 (*)))
    (with-buffer-input (reply-buffer :sizes (32) :index index)
      (do* ((j start (index+ j 1))
	    (end (index+ start nitems))
	    (index 0 (index+ index 4)))
	   ((index>= j end))
	(declare (type array-index j end index))
	(setf (aref data j) (the card32 (funcall transform (read-card32 index))))))))

(defun read-vector-card32 (reply-buffer nitems data start index)
  (declare (type reply-buffer reply-buffer)
	   (type array-index nitems start index)
	   (type vector data)
	   (optimize #+cmu(ext:inhibit-warnings 3)))
  (with-vector (data vector)
    (with-buffer-input (reply-buffer :sizes (32) :index index)
      #-clx-overlapping-arrays
      (do* ((j start (index+ j 1))
	    (end (index+ start nitems))
	    (index 0 (index+ index 4)))
	   ((index>= j end))
	(declare (type array-index j end index))
	(setf (aref data j) (read-card32 index)))
      #+clx-overlapping-arrays
      (buffer-replace data buffer-lbuf start (index+ start nitems) (index-floor index 4)))))

(defun read-vector-card32-with-transform (reply-buffer nitems data transform start index)
  (declare (type reply-buffer reply-buffer)
	   (type array-index nitems start index)
	   (type vector data)
	   (optimize #+cmu(ext:inhibit-warnings 3)))
  (declare (type (function (card32) t) transform)
	   #+clx-ansi-common-lisp
	   (dynamic-extent transform)
	   #+(and lispm (not clx-ansi-common-lisp))
	   (sys:downward-funarg transform))
  (with-vector (data vector)
    (with-buffer-input (reply-buffer :sizes (32) :index index)
      (do* ((j start (index+ j 1))
	    (end (index+ start nitems))
	    (index 0 (index+ index 4)))
	   ((index>= j end))
	(declare (type array-index j end index))
	(setf (aref data j) (funcall transform (read-card32 index)))))))

(defun read-sequence-card32 (reply-buffer result-type nitems &optional transform data
			     (start 0) (index 0))
  (declare (type reply-buffer reply-buffer)
	   (type t result-type) ;; CL type
	   (type array-index nitems start index)
	   (type (or null sequence) data))
  (declare (type (or null (function (card32) t)) transform)
	   #+clx-ansi-common-lisp
	   (dynamic-extent transform)
	   #+(and lispm (not clx-ansi-common-lisp))
	   (sys:downward-funarg transform))
  (let ((result (or data (make-sequence result-type nitems))))
    (typecase result
      (list
	(if transform 
	    (read-list-card32-with-transform reply-buffer nitems result transform start index)
	  (read-list-card32 reply-buffer nitems result start index)))
      #-lispm
      ((simple-array card32 (*))
       (if transform 
	   (read-simple-array-card32-with-transform
	     reply-buffer nitems result transform start index)
	 (read-simple-array-card32 reply-buffer nitems result start index)))
      (t
	(if transform 
	    (read-vector-card32-with-transform
	      reply-buffer nitems result transform start index)
	  (read-vector-card32 reply-buffer nitems result start index))))
    result))

;;; For now, perhaps performance it isn't worth doing better?

(defun read-sequence-int32 (reply-buffer result-type nitems &optional transform data
			    (start 0) (index 0))
  (declare (type reply-buffer reply-buffer)
	   (type t result-type) ;; CL type
	   (type array-index nitems start index)
	   (type (or null sequence) data))
  (declare (type (or null (function (int32) t)) transform)
	   #+clx-ansi-common-lisp
	   (dynamic-extent transform)
	   #+(and lispm (not clx-ansi-common-lisp))
	   (sys:downward-funarg transform))
  (if transform 
      (flet ((card32->int32->transform (v)
	       (declare (type card32 v))
	       (funcall transform (card32->int32 v))))
	#+clx-ansi-common-lisp
	(declare (dynamic-extent #'card32->int32->transform))
	(read-sequence-card32
	  reply-buffer result-type nitems #'card32->int32->transform
	  data start index))
    (read-sequence-card32
      reply-buffer result-type nitems #'card32->int32
      data start index)))

;;; Writing sequences of chars

(defun write-sequence-char
       (buffer boffset data &optional (start 0) (end (length data)) transform)
  (declare (type buffer buffer)
	   (type sequence data)
	   (type array-index boffset start end))
  (declare (type (or null (function (t) character)) transform)
	   #+clx-ansi-common-lisp
	   (dynamic-extent transform)
	   #+(and lispm (not clx-ansi-common-lisp))
	   (sys:downward-funarg transform))
  (if transform 
      (flet ((transform->char->card8 (x)
	       (char->card8 (the character (funcall transform x)))))