From 63152fb1a40d4d3c8008c83b3ff6bbbc691b016c Mon Sep 17 00:00:00 2001 From: Stelian Ionescu Date: Wed, 7 Jan 2009 01:04:16 +0100 Subject: [PATCH] Use three specialised functions instead of REPLACE-UB8. --- io.streams/zeta/iobuf.lisp | 71 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 55 insertions(+), 16 deletions(-) diff --git a/io.streams/zeta/iobuf.lisp b/io.streams/zeta/iobuf.lisp index d36c2e9..bdf7265 100644 --- a/io.streams/zeta/iobuf.lisp +++ b/io.streams/zeta/iobuf.lisp @@ -16,27 +16,26 @@ ;; almost 128 MB: large enough for a stream buffer, ;; but small enough to fit into a fixnum (deftype iobuf-index () '(unsigned-byte 27)) -(deftype iobuf-length () '(integer 0 #.(expt 2 27))) (deftype iobuf-data-vector () 'ub8-simple-vector) (defstruct (iobuf (:constructor %make-iobuf (data))) - (lock (bt:make-lock "IObuf lock") :read-only t) - (data nil :type iobuf-data-vector :read-only t) - (start 0 :type iobuf-index) - (end 0 :type iobuf-index)) + (lock (bt:make-lock "IObuf lock") :read-only t) + (data nil :type iobuf-data-vector :read-only t) + (start 0 :type iobuf-index) + (end 0 :type iobuf-index)) (defun make-iobuf-data-vector (size) (declare (type iobuf-index size)) (make-array size :element-type 'ub8 :initial-element 0)) -(defun make-iobuf (&optional size) - (check-type size (or null iobuf-index)) - (%make-iobuf (make-iobuf-data-vector (or size +default-iobuf-size+)))) +(defun make-iobuf (&optional (size +default-iobuf-size+)) + (check-type size iobuf-index) + (%make-iobuf (make-iobuf-data-vector size))) (defun iobuf-size (iobuf) (declare (type iobuf iobuf)) - (length (iobuf-data iobuf))) + (the iobuf-index (length (iobuf-data iobuf)))) (defun iobuf-available-octets (iobuf) (declare (type iobuf iobuf)) @@ -99,7 +98,7 @@ (prog1 (setf (bref iobuf end) octet) (setf (iobuf-end iobuf) (1+ end))))) -(defun replace-ub8 (destination source start1 end1 start2 end2) +(defun replace-ub8sv->ub8sv (destination source start1 end1 start2 end2) (declare (type ub8-simple-vector destination source) (type iobuf-index start1 start2 end1 end2)) (let ((nbytes (min (- end1 start1) @@ -109,28 +108,68 @@ :start2 start2 :end2 end2) (values nbytes))) +(defun replace-ub8sv->ub8cv (destination source start1 end1 start2 end2) + (declare (type ub8-simple-vector source) + (type ub8-complex-vector destination) + (type iobuf-index start1 start2 end1 end2)) + (let ((nbytes (min (- end1 start1) + (- end2 start2)))) + (replace destination source + :start1 start1 :end1 end1 + :start2 start2 :end2 end2) + (values nbytes))) + +(defun replace-ub8cv->ub8sv (destination source start1 end1 start2 end2) + (declare (type ub8-complex-vector source) + (type ub8-simple-vector destination) + (type iobuf-index start1 start2 end1 end2)) + (let ((nbytes (min (- end1 start1) + (- end2 start2)))) + (replace destination source + :start1 start1 :end1 end1 + :start2 start2 :end2 end2) + (values nbytes))) + (defun iobuf->vector (iobuf vector start end) (declare (type iobuf iobuf) - (type ub8-simple-vector vector) + (type ub8-vector vector) (type iobuf-index start end)) (when (iobuf-empty-p iobuf) (iobuf-reset iobuf)) (multiple-value-bind (iobuf-data data-start data-end) (iobuf-next-data-zone iobuf) + (declare (type iobuf-index data-start data-end)) (let ((nbytes - (replace-ub8 vector iobuf-data start end data-start data-end))) - (setf (iobuf-start iobuf) (+ data-start nbytes)) + (etypecase vector + (ub8-simple-vector + (replace-ub8sv->ub8sv vector iobuf-data + start end + data-start data-end)) + (ub8-complex-vector + (replace-ub8sv->ub8cv vector iobuf-data + start end + data-start data-end))))) + (setf (iobuf-start iobuf) (+ data-start (the iobuf-index nbytes))) (values nbytes)))) (defun vector->iobuf (iobuf vector start end) (declare (type iobuf iobuf) - (type ub8-simple-vector vector) + (type ub8-vector vector) (type iobuf-index start end)) (when (iobuf-empty-p iobuf) (iobuf-reset iobuf)) (multiple-value-bind (iobuf-data data-start data-end) (iobuf-next-empty-zone iobuf) + (declare (type iobuf-index data-start data-end)) (let ((nbytes - (replace-ub8 iobuf-data vector data-start data-end start end))) - (setf (iobuf-end iobuf) (+ data-start nbytes)) + (etypecase vector + (ub8-simple-vector + (replace-ub8sv->ub8sv iobuf-data vector + data-start data-end + start end)) + (ub8-complex-vector + (replace-ub8cv->ub8sv iobuf-data vector + data-start data-end + start end))))) + (setf (iobuf-end iobuf) (+ data-start (the iobuf-index nbytes))) (values nbytes)))) -- 2.11.4.GIT