1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; indent-tabs-mode: nil -*-
6 (in-package :io.zeta-streams
)
8 (declaim (optimize speed
))
10 ;;;-------------------------------------------------------------------------
12 ;;;-------------------------------------------------------------------------
14 (define-constant +default-iobuf-size
+ (* 8 1024))
16 ;; almost 128 MB: large enough for a stream buffer,
17 ;; but small enough to fit into a fixnum
18 (deftype iobuf-index
() '(unsigned-byte 27))
19 (deftype iobuf-length
() '(integer 0 #.
(expt 2 27)))
21 (deftype iobuf-data-vector
() 'ub8-simple-vector
)
23 (defstruct (iobuf (:constructor %make-iobuf
(data)))
24 (lock (bt:make-lock
"IObuf lock") :read-only t
)
25 (data nil
:type iobuf-data-vector
:read-only t
)
26 (start 0 :type iobuf-index
)
27 (end 0 :type iobuf-index
))
29 (defun make-iobuf-data-vector (size)
30 (declare (type iobuf-index size
))
31 (make-array size
:element-type
'ub8
:initial-element
0))
33 (defun make-iobuf (&optional size
)
34 (check-type size
(or null iobuf-index
))
35 (%make-iobuf
(make-iobuf-data-vector (or size
+default-iobuf-size
+))))
37 (defun iobuf-size (iobuf)
38 (declare (type iobuf iobuf
))
39 (length (iobuf-data iobuf
)))
41 (defun iobuf-available-octets (iobuf)
42 (declare (type iobuf iobuf
))
46 (defun iobuf-empty-p (iobuf)
47 (declare (type iobuf iobuf
))
48 (zerop (iobuf-available-octets iobuf
)))
50 (defun iobuf-full-p (iobuf)
51 (declare (type iobuf iobuf
))
55 (defun iobuf-reset (iobuf)
56 (declare (type iobuf iobuf
))
57 (setf (iobuf-start iobuf
) 0
60 (defun iobuf-next-data-zone (iobuf)
61 (declare (type iobuf iobuf
))
62 (values (iobuf-data iobuf
)
66 (defun iobuf-next-empty-zone (iobuf)
67 (declare (type iobuf iobuf
))
68 (values (iobuf-data iobuf
)
73 ;;;-------------------------------------------------------------------------
74 ;;; UNSAFE functions which *DO NOT* check boundaries
75 ;;; that must be done by their callers
76 ;;;-------------------------------------------------------------------------
78 (defun bref (iobuf index
)
79 (declare (type iobuf iobuf
)
80 (type iobuf-index index
))
81 (aref (iobuf-data iobuf
) index
))
83 (defun (setf bref
) (octet iobuf index
)
84 (declare (type ub8 octet
)
86 (type iobuf-index index
))
87 (setf (aref (iobuf-data iobuf
) index
) octet
))
89 (defun iobuf-pop-octet (iobuf)
90 (declare (type iobuf iobuf
))
91 (let ((start (iobuf-start iobuf
)))
92 (prog1 (bref iobuf start
)
93 (setf (iobuf-start iobuf
) (1+ start
)))))
95 (defun iobuf-push-octet (iobuf octet
)
96 (declare (type iobuf iobuf
)
98 (let ((end (iobuf-end iobuf
)))
99 (prog1 (setf (bref iobuf end
) octet
)
100 (setf (iobuf-end iobuf
) (1+ end
)))))
102 (defun replace-ub8 (destination source start1 end1 start2 end2
)
103 (declare (type ub8-simple-vector destination source
)
104 (type iobuf-index start1 start2 end1 end2
))
105 (let ((nbytes (min (- end1 start1
)
107 (replace destination source
108 :start1 start1
:end1 end1
109 :start2 start2
:end2 end2
)
112 (defun iobuf->vector
(iobuf vector start end
)
113 (declare (type iobuf iobuf
)
114 (type ub8-simple-vector vector
)
115 (type iobuf-index start end
))
116 (when (iobuf-empty-p iobuf
)
118 (multiple-value-bind (iobuf-data data-start data-end
)
119 (iobuf-next-data-zone iobuf
)
121 (replace-ub8 vector iobuf-data start end data-start data-end
)))
122 (setf (iobuf-start iobuf
) (+ data-start nbytes
))
125 (defun vector->iobuf
(iobuf vector start end
)
126 (declare (type iobuf iobuf
)
127 (type ub8-simple-vector vector
)
128 (type iobuf-index start end
))
129 (when (iobuf-empty-p iobuf
)
131 (multiple-value-bind (iobuf-data data-start data-end
)
132 (iobuf-next-empty-zone iobuf
)
134 (replace-ub8 iobuf-data vector data-start data-end start end
)))
135 (setf (iobuf-end iobuf
) (+ data-start nbytes
))