Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
;;; -*- 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)
(defconstant *requestsize* 160) ;; Max request size (excluding variable length requests)
;;; 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)
#+(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)))
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
(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)))))
#+clx-ansi-common-lisp
(declare (dynamic-extent #'transform->char->card8))
(write-sequence-card8
buffer boffset data start end #'transform->char->card8))
(write-sequence-card8 buffer boffset data start end #'char->card8)))
;;; Writing sequences of card8's
(defun write-list-card8 (buffer boffset data start end)
(declare (type buffer buffer)
(type list data)
(type array-index boffset start end))
(writing-buffer-chunks card8
((lst (nthcdr start data)))
((type list lst))
(dotimes (j chunk)
(declare (type array-index j))
#-ti (write-card8 j (pop lst)) ;TI Compiler bug
#+ti (setf (aref buffer-bbuf (index+ buffer-boffset j)) (pop lst))
))
nil)
(defun write-list-card8-with-transform (buffer boffset data start end transform)
(declare (type buffer buffer)
(type list data)
(type array-index boffset start end))
(declare (type (function (t) card8) transform)
#+clx-ansi-common-lisp
(dynamic-extent transform)
#+(and lispm (not clx-ansi-common-lisp))
(sys:downward-funarg transform))
(writing-buffer-chunks card8
((lst (nthcdr start data)))
((type list lst))
(dotimes (j chunk)
(declare (type array-index j))
(write-card8 j (funcall transform (pop lst)))))
nil)
;;; Should really write directly from data, instead of into the buffer first
#-lispm
(defun write-simple-array-card8 (buffer boffset data start end)
(declare (type buffer buffer)
(type (simple-array card8 (*)) data)
(type array-index boffset start end))
(with-vector (data (simple-array card8 (*)))
(writing-buffer-chunks card8
((index start (index+ index chunk)))
((type array-index index))
(buffer-replace buffer-bbuf data
buffer-boffset
(index+ buffer-boffset chunk)
index)))
nil)
#-lispm
(defun write-simple-array-card8-with-transform (buffer boffset data start end transform)
(declare (type buffer buffer)
(type (simple-array card8 (*)) data)
(type array-index boffset start end))
(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 (*)))
(writing-buffer-chunks card8
((index start))
((type array-index index))
(dotimes (j chunk)
(declare (type array-index j))
(write-card8 j (funcall transform (aref data index)))
(setq index (index+ index 1)))))
nil)
(defun write-vector-card8 (buffer boffset data start end)
(declare (type buffer buffer)
(type vector data)
(type array-index boffset start end)
(optimize #+cmu(ext:inhibit-warnings 3)))
(with-vector (data vector)
(writing-buffer-chunks card8
((index start))
((type array-index index))
(dotimes (j chunk)
(declare (type array-index j))
(write-card8 j (aref data index))
(setq index (index+ index 1)))))
nil)
(defun write-vector-card8-with-transform (buffer boffset data start end transform)
(declare (type buffer buffer)
(type vector data)
(type array-index boffset start end))
(declare (type (function (t) card8) transform)
#+clx-ansi-common-lisp
(dynamic-extent transform)
#+(and lispm (not clx-ansi-common-lisp))
(sys:downward-funarg transform))
(with-vector (data vector)
(writing-buffer-chunks card8
((index start))
((type array-index index))
(dotimes (j chunk)
(declare (type array-index j))
(write-card8 j (funcall transform (aref data index)))
(setq index (index+ index 1)))))
nil)
(defun write-sequence-card8
(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) card8)) transform)
#+clx-ansi-common-lisp
(dynamic-extent transform)
#+(and lispm (not clx-ansi-common-lisp))
(sys:downward-funarg transform))
(typecase data
(list
(if transform
(write-list-card8-with-transform buffer boffset data start end transform)
(write-list-card8 buffer boffset data start end)))
#-lispm
((simple-array card8 (*))
(if transform
(write-simple-array-card8-with-transform buffer boffset data start end transform)
(write-simple-array-card8 buffer boffset data start end)))
(t
(if transform
(write-vector-card8-with-transform buffer boffset data start end transform)
(write-vector-card8 buffer boffset data start end)))))
;;; For now, perhaps performance it isn't worth doing better?
(defun write-sequence-int8
(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) int8)) transform)
#+clx-ansi-common-lisp
(dynamic-extent transform)
#+(and lispm (not clx-ansi-common-lisp))
(sys:downward-funarg transform))
(if transform
(flet ((transform->int8->card8 (x)
(int8->card8 (the int8 (funcall transform x)))))
#+clx-ansi-common-lisp
(declare (dynamic-extent #'transform->int8->card8))
(write-sequence-card8
buffer boffset data start end #'transform->int8->card8))
(write-sequence-card8 buffer boffset data start end #'int8->card8)))
;;; Writing sequences of card16's
(defun write-list-card16 (buffer boffset data start end)
(declare (type buffer buffer)
(type list data)
(type array-index boffset start end))
(writing-buffer-chunks card16
((lst (nthcdr start data)))
((type list lst))
;; Depends upon the chunks being an even multiple of card16's big
(do ((j 0 (index+ j 2)))
((index>= j chunk))
(declare (type array-index j))
(write-card16 j (pop lst))))
nil)
(defun write-list-card16-with-transform (buffer boffset data start end transform)
(declare (type buffer buffer)
(type list data)
(type array-index boffset start end))
(declare (type (function (t) card16) transform)
#+clx-ansi-common-lisp
(dynamic-extent transform)
#+(and lispm (not clx-ansi-common-lisp))
(sys:downward-funarg transform))
(writing-buffer-chunks card16
((lst (nthcdr start data)))
((type list lst))
;; Depends upon the chunks being an even multiple of card16's big
(do ((j 0 (index+ j 2)))
((index>= j chunk))
(declare (type array-index j))
(write-card16 j (funcall transform (pop lst)))))
nil)
#-lispm
(defun write-simple-array-card16 (buffer boffset data start end)
(declare (type buffer buffer)
(type (simple-array card16 (*)) data)
(type array-index boffset start end))
(with-vector (data (simple-array card16 (*)))
(writing-buffer-chunks card16
((index start))
((type array-index index))
;; Depends upon the chunks being an even multiple of card16's big
(do ((j 0 (index+ j 2)))
((index>= j chunk))
(declare (type array-index j))
(write-card16 j (aref data index))
(setq index (index+ index 1)))
;; overlapping case
(let ((length (floor chunk 2)))
(buffer-replace buffer-wbuf data
buffer-woffset
(index+ buffer-woffset length)
index)
(setq index (index+ index length)))))
nil)
#-lispm
(defun write-simple-array-card16-with-transform (buffer boffset data start end transform)
(declare (type buffer buffer)
(type (simple-array card16 (*)) data)
(type array-index boffset start end))
(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 (*)))
(writing-buffer-chunks card16
((index start))
((type array-index index))
;; Depends upon the chunks being an even multiple of card16's big
(do ((j 0 (index+ j 2)))
((index>= j chunk))
(declare (type array-index j))
(write-card16 j (funcall transform (aref data index)))
(setq index (index+ index 1)))))
nil)
(defun write-vector-card16 (buffer boffset data start end)
(declare (type buffer buffer)
(type vector data)
(type array-index boffset start end)
(optimize #+cmu(ext:inhibit-warnings 3)))
(with-vector (data vector)
(writing-buffer-chunks card16
((index start))
((type array-index index))
;; Depends upon the chunks being an even multiple of card16's big
(do ((j 0 (index+ j 2)))
((index>= j chunk))
(declare (type array-index j))
(write-card16 j (aref data index))
(setq index (index+ index 1)))
;; overlapping case
(let ((length (floor chunk 2)))
(buffer-replace buffer-wbuf data
buffer-woffset
(index+ buffer-woffset length)
index)
(setq index (index+ index length)))))
nil)
(defun write-vector-card16-with-transform (buffer boffset data start end transform)
(declare (type buffer buffer)
(type vector data)
(type array-index boffset start end)
(optimize #+cmu(ext:inhibit-warnings 3)))
(declare (type (function (t) card16) transform)
#+clx-ansi-common-lisp
(dynamic-extent transform)
#+(and lispm (not clx-ansi-common-lisp))
(sys:downward-funarg transform))
(with-vector (data vector)
(writing-buffer-chunks card16
((index start))
((type array-index index))
;; Depends upon the chunks being an even multiple of card16's big
(do ((j 0 (index+ j 2)))
((index>= j chunk))
(declare (type array-index j))
(write-card16 j (funcall transform (aref data index)))
(setq index (index+ index 1)))))
nil)
(defun write-sequence-card16
(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) card16)) transform)
#+clx-ansi-common-lisp
(dynamic-extent transform)
#+(and lispm (not clx-ansi-common-lisp))
(sys:downward-funarg transform))
(typecase data
(list
(if transform
(write-list-card16-with-transform buffer boffset data start end transform)
(write-list-card16 buffer boffset data start end)))
#-lispm
((simple-array card16 (*))
(if transform
(write-simple-array-card16-with-transform buffer boffset data start end transform)
(write-simple-array-card16 buffer boffset data start end)))
(t
(if transform
(write-vector-card16-with-transform buffer boffset data start end transform)
(write-vector-card16 buffer boffset data start end)))))
;;; Writing sequences of int16's
(defun write-list-int16 (buffer boffset data start end)
(declare (type buffer buffer)
(type list data)
(type array-index boffset start end))
(writing-buffer-chunks int16
((lst (nthcdr start data)))
((type list lst))
;; Depends upon the chunks being an even multiple of int16's big
(do ((j 0 (index+ j 2)))
((index>= j chunk))
(declare (type array-index j))
(write-int16 j (pop lst))))
nil)
(defun write-list-int16-with-transform (buffer boffset data start end transform)
(declare (type buffer buffer)
(type list data)
(type array-index boffset start end))
(declare (type (function (t) int16) transform)
#+clx-ansi-common-lisp
(dynamic-extent transform)
#+(and lispm (not clx-ansi-common-lisp))
(sys:downward-funarg transform))
(writing-buffer-chunks int16
((lst (nthcdr start data)))
((type list lst))
;; Depends upon the chunks being an even multiple of int16's big
(do ((j 0 (index+ j 2)))
((index>= j chunk))
(declare (type array-index j))
(write-int16 j (funcall transform (pop lst)))))
nil)
#-lispm
(defun write-simple-array-int16 (buffer boffset data start end)
(declare (type buffer buffer)
(type (simple-array int16 (*)) data)
(type array-index boffset start end))
(with-vector (data (simple-array int16 (*)))
(writing-buffer-chunks int16
((index start))
((type array-index index))
;; Depends upon the chunks being an even multiple of int16's big
(do ((j 0 (index+ j 2)))
((index>= j chunk))
(declare (type array-index j))
(write-int16 j (aref data index))
(setq index (index+ index 1)))
;; overlapping case
(let ((length (floor chunk 2)))
(buffer-replace buffer-wbuf data
buffer-woffset
(index+ buffer-woffset length)
index)
(setq index (index+ index length)))))
nil)
#-lispm
(defun write-simple-array-int16-with-transform (buffer boffset data start end transform)
(declare (type buffer buffer)
(type (simple-array int16 (*)) data)
(type array-index boffset start end))
(declare (type (function (int16) int16) transform)
#+clx-ansi-common-lisp
(dynamic-extent transform)
#+(and lispm (not clx-ansi-common-lisp))
(sys:downward-funarg transform))
(with-vector (data (simple-array int16 (*)))
(writing-buffer-chunks int16
((index start))
((type array-index index))
;; Depends upon the chunks being an even multiple of int16's big
(do ((j 0 (index+ j 2)))
((index>= j chunk))
(declare (type array-index j))
(write-int16 j (funcall transform (aref data index)))
(setq index (index+ index 1)))))
nil)
(defun write-vector-int16 (buffer boffset data start end)
(declare (type buffer buffer)
(type vector data)
(type array-index boffset start end)
(optimize #+cmu(ext:inhibit-warnings 3)))
(with-vector (data vector)
(writing-buffer-chunks int16
((index start))
((type array-index index))
;; Depends upon the chunks being an even multiple of int16's big
(do ((j 0 (index+ j 2)))
((index>= j chunk))
(declare (type array-index j))
(write-int16 j (aref data index))
(setq index (index+ index 1)))
;; overlapping case
(let ((length (floor chunk 2)))
(buffer-replace buffer-wbuf data
buffer-woffset
(index+ buffer-woffset length)
index)
(setq index (index+ index length)))))
nil)
(defun write-vector-int16-with-transform (buffer boffset data start end transform)
(declare (type buffer buffer)
(type vector data)
(type array-index boffset start end)
(optimize #+cmu(ext:inhibit-warnings 3)))
(declare (type (function (t) int16) transform)
#+clx-ansi-common-lisp
(dynamic-extent transform)
#+(and lispm (not clx-ansi-common-lisp))
(sys:downward-funarg transform))
(with-vector (data vector)
(writing-buffer-chunks int16
((index start))
((type array-index index))
;; Depends upon the chunks being an even multiple of int16's big
(do ((j 0 (index+ j 2)))
((index>= j chunk))
(declare (type array-index j))
(write-int16 j (funcall transform (aref data index)))
(setq index (index+ index 1)))))
nil)
(defun write-sequence-int16
(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) int16)) transform)
#+clx-ansi-common-lisp
(dynamic-extent transform)
#+(and lispm (not clx-ansi-common-lisp))
(sys:downward-funarg transform))
(typecase data
(list
(if transform
(write-list-int16-with-transform buffer boffset data start end transform)
(write-list-int16 buffer boffset data start end)))
#-lispm
((simple-array int16 (*))
(if transform
(write-simple-array-int16-with-transform buffer boffset data start end transform)
(write-simple-array-int16 buffer boffset data start end)))
(t
(if transform
(write-vector-int16-with-transform buffer boffset data start end transform)
(write-vector-int16 buffer boffset data start end)))))
;;; Writing sequences of card32's
(defun write-list-card32 (buffer boffset data start end)
(declare (type buffer buffer)
(type list data)
(type array-index boffset start end))
(writing-buffer-chunks card32
((lst (nthcdr start data)))
((type list lst))
;; Depends upon the chunks being an even multiple of card32's big
(do ((j 0 (index+ j 4)))
((index>= j chunk))
(declare (type array-index j))
(write-card32 j (pop lst))))
nil)
(defun write-list-card32-with-transform (buffer boffset data start end transform)
(declare (type buffer buffer)
(type list data)
(type array-index boffset start end))
(declare (type (function (t) card32) transform)
#+clx-ansi-common-lisp
(dynamic-extent transform)
#+(and lispm (not clx-ansi-common-lisp))
(sys:downward-funarg transform))
(writing-buffer-chunks card32
((lst (nthcdr start data)))
((type list lst))
;; Depends upon the chunks being an even multiple of card32's big
(do ((j 0 (index+ j 4)))
((index>= j chunk))
(declare (type array-index j))
(write-card32 j (funcall transform (pop lst)))))
nil)
#-lispm
(defun write-simple-array-card32 (buffer boffset data start end)
(declare (type buffer buffer)
(type (simple-array card32 (*)) data)
(type array-index boffset start end))
(with-vector (data (simple-array card32 (*)))
(writing-buffer-chunks card32
((index start))
((type array-index index))
;; Depends upon the chunks being an even multiple of card32's big
(do ((j 0 (index+ j 4)))
((index>= j chunk))
(declare (type array-index j))
(write-card32 j (aref data index))
(setq index (index+ index 1)))
;; overlapping case
(let ((length (floor chunk 4)))
(buffer-replace buffer-lbuf data
buffer-loffset
(index+ buffer-loffset length)
index)
(setq index (index+ index length)))))
nil)
#-lispm
(defun write-simple-array-card32-with-transform (buffer boffset data start end transform)
(declare (type buffer buffer)
(type (simple-array card32 (*)) data)
(type array-index boffset start end))
(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 (*)))
(writing-buffer-chunks card32
((index start))
((type array-index index))
;; Depends upon the chunks being an even multiple of card32's big
(do ((j 0 (index+ j 4)))
((index>= j chunk))
(declare (type array-index j))
(write-card32 j (funcall transform (aref data index)))
(setq index (index+ index 1)))))
nil)
(defun write-vector-card32 (buffer boffset data start end)
(declare (type buffer buffer)
(type vector data)
(type array-index boffset start end)
(optimize #+cmu(ext:inhibit-warnings 3)))
(with-vector (data vector)
(writing-buffer-chunks card32
((index start))
((type array-index index))
;; Depends upon the chunks being an even multiple of card32's big
(do ((j 0 (index+ j 4)))
((index>= j chunk))
(declare (type array-index j))
(write-card32 j (aref data index))
(setq index (index+ index 1)))
;; overlapping case
(let ((length (floor chunk 4)))
(buffer-replace buffer-lbuf data
buffer-loffset
(index+ buffer-loffset length)
index)
(setq index (index+ index length)))))
nil)
(defun write-vector-card32-with-transform (buffer boffset data start end transform)
(declare (type buffer buffer)
(type vector data)
(type array-index boffset start end)
(optimize #+cmu(ext:inhibit-warnings 3)))
(declare (type (function (t) card32) transform)
#+clx-ansi-common-lisp
(dynamic-extent transform)
#+(and lispm (not clx-ansi-common-lisp))
(sys:downward-funarg transform))
(with-vector (data vector)
(writing-buffer-chunks card32
((index start))
((type array-index index))
;; Depends upon the chunks being an even multiple of card32's big
(do ((j 0 (index+ j 4)))
((index>= j chunk))
(declare (type array-index j))
(write-card32 j (funcall transform (aref data index)))
(setq index (index+ index 1)))))
nil)
(defun write-sequence-card32
(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) card32)) transform)
#+clx-ansi-common-lisp
(dynamic-extent transform)
#+(and lispm (not clx-ansi-common-lisp))
(sys:downward-funarg transform))
(typecase data
(list
(if transform
(write-list-card32-with-transform buffer boffset data start end transform)
(write-list-card32 buffer boffset data start end)))
#-lispm
((simple-array card32 (*))
(if transform
(write-simple-array-card32-with-transform buffer boffset data start end transform)
(write-simple-array-card32 buffer boffset data start end)))
(t
(if transform
(write-vector-card32-with-transform buffer boffset data start end transform)
(write-vector-card32 buffer boffset data start end)))))
;;; For now, perhaps performance it isn't worth doing better?
(defun write-sequence-int32
(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) int32)) transform)
#+clx-ansi-common-lisp
(dynamic-extent transform)
#+(and lispm (not clx-ansi-common-lisp))
(sys:downward-funarg transform))
(if transform
(flet ((transform->int32->card32 (x)
(int32->card32 (the int32 (funcall transform x)))))
#+clx-ansi-common-lisp
(declare (dynamic-extent #'transform->int32->card32))
(write-sequence-card32
buffer boffset data start end #'transform->int32->card32))
(write-sequence-card32 buffer boffset data start end #'int32->card32)))
(defun read-bitvector256 (buffer-bbuf boffset data)
(declare (type buffer-bytes buffer-bbuf)
(type array-index boffset)
(type (or null (simple-bit-vector 256)) data))
(let ((result (or data (make-array 256 :element-type 'bit :initial-element 0))))
(declare (type (simple-bit-vector 256) result))
(do ((i (index+ boffset 1) (index+ i 1)) ;; Skip first byte
(j 8 (index+ j 8)))
((index>= j 256))
(declare (type array-index i j))
(do ((byte (aref-card8 buffer-bbuf i) (index-ash byte -1))
(k j (index+ k 1)))
((zerop byte)
(when data ;; Clear uninitialized bits in data
(do ((end (index+ j 8)))
((index= k end))
(declare (type array-index end))
(setf (aref result k) 0)
(index-incf k))))
(declare (type array-index k)
(type card8 byte))
(setf (aref result k) (the bit (logand byte 1)))))
result))
(defun write-bitvector256 (buffer boffset map)
(declare (type buffer buffer)
(type array-index boffset)
(type (simple-array bit (*)) map))
(with-buffer-output (buffer :index boffset :sizes 8)
(do* ((i (index+ buffer-boffset 1) (index+ i 1)) ; Skip first byte
(j 8 (index+ j 8)))
((index>= j 256))
(declare (type array-index i j))
(do ((byte 0)
(bit (index+ j 7) (index- bit 1)))
((index< bit j)
(aset-card8 byte buffer-bbuf i))
(declare (type array-index bit)
(type card8 byte))
(setq byte (the card8 (logior (the card8 (ash byte 1)) (aref map bit))))))))
;;; Writing sequences of char2b's
(defun write-list-char2b (buffer boffset data start end)
(declare (type buffer buffer)
(type list data)
(type array-index boffset start end))
(writing-buffer-chunks card16
((lst (nthcdr start data)))
((type list lst))
(do ((j 0 (index+ j 2)))
((index>= j (1- chunk)) (setf chunk j))
(declare (type array-index j))
(write-char2b j (pop lst))))
nil)
(defun write-list-char2b-with-transform (buffer boffset data start end transform)
(declare (type buffer buffer)
(type list data)
(type array-index boffset start end))
(declare (type (function (t) card16) transform)
#+clx-ansi-common-lisp
(dynamic-extent transform)
#+(and lispm (not clx-ansi-common-lisp))
(sys:downward-funarg transform))
(writing-buffer-chunks card16
((lst (nthcdr start data)))
((type list lst))
(do ((j 0 (index+ j 2)))
((index>= j (1- chunk)) (setf chunk j))
(declare (type array-index j))
(write-char2b j (funcall transform (pop lst)))))
nil)
#-lispm
(defun write-simple-array-char2b (buffer boffset data start end)
(declare (type buffer buffer)
(type (simple-array card16 (*)) data)
(type array-index boffset start end))
(with-vector (data (simple-array card16 (*)))
(writing-buffer-chunks card16
((index start))
((type array-index index))
(do ((j 0 (index+ j 2)))
((index>= j (1- chunk)) (setf chunk j))
(declare (type array-index j))
(write-char2b j (aref data index))
(setq index (index+ index 1)))))
nil)
#-lispm
(defun write-simple-array-char2b-with-transform (buffer boffset data start end transform)
(declare (type buffer buffer)
(type (simple-array card16 (*)) data)
(type array-index boffset start end))
(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 (*)))
(writing-buffer-chunks card16
((index start))
((type array-index index))
(do ((j 0 (index+ j 2)))
((index>= j (1- chunk)) (setf chunk j))
(declare (type array-index j))
(write-char2b j (funcall transform (aref data index)))
(setq index (index+ index 1)))))
nil)
(defun write-vector-char2b (buffer boffset data start end)
(declare (type buffer buffer)
(type vector data)
(type array-index boffset start end)
(optimize #+cmu(ext:inhibit-warnings 3)))
(with-vector (data vector)
(writing-buffer-chunks card16
((index start))
((type array-index index))
(do ((j 0 (index+ j 2)))
((index>= j (1- chunk)) (setf chunk j))
(declare (type array-index j))
(write-char2b j (aref data index))
(setq index (index+ index 1)))))
nil)
(defun write-vector-char2b-with-transform (buffer boffset data start end transform)
(declare (type buffer buffer)
(type vector data)
(type array-index boffset start end)
(optimize #+cmu(ext:inhibit-warnings 3)))
(declare (type (function (t) card16) transform)
#+clx-ansi-common-lisp
(dynamic-extent transform)
#+(and lispm (not clx-ansi-common-lisp))
(sys:downward-funarg transform))
(with-vector (data vector)
(writing-buffer-chunks card16
((index start))
((type array-index index))
(do ((j 0 (index+ j 2)))
((index>= j (1- chunk)) (setf chunk j))
(declare (type array-index j))
(write-char2b j (funcall transform (aref data index)))
(setq index (index+ index 1)))))
nil)
(defun write-sequence-char2b
(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) card16)) transform)
#+clx-ansi-common-lisp
(dynamic-extent transform)
#+(and lispm (not clx-ansi-common-lisp))
(sys:downward-funarg transform))
(typecase data
(list
(if transform
(write-list-char2b-with-transform buffer boffset data start end transform)
(write-list-char2b buffer boffset data start end)))
#-lispm
((simple-array card16 (*))
(if transform
(write-simple-array-char2b-with-transform buffer boffset data start end transform)
(write-simple-array-char2b buffer boffset data start end)))
(t
(if transform
(write-vector-char2b-with-transform buffer boffset data start end transform)
(write-vector-char2b buffer boffset data start end)))))