Clean up buffer code.
[iolib/alendvai.git] / io.streams / zeta / buffer.lisp
blob755685ecf7dfa4668621add2003e145f01799dec
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; indent-tabs-mode: nil -*-
2 ;;;
3 ;;; --- Foreign memory buffers.
4 ;;;
6 (in-package :io.zeta-streams)
8 ;;;; Foreign Buffers
10 (define-constant +bytes-per-iobuf+ 4096)
12 (defun make-iobuf (&optional (size +bytes-per-iobuf+))
13 (let ((b (%make-iobuf)))
14 (setf (iobuf-data b) (make-array size :element-type 'ub8)
15 (iobuf-size b) size)
16 (values b)))
18 (defun iobuf-length (iobuf)
19 (declare (type iobuf iobuf))
20 (- (iobuf-end iobuf)
21 (iobuf-start iobuf)))
23 (defun iobuf-empty-p (iobuf)
24 (declare (type iobuf iobuf))
25 (= (iobuf-end iobuf)
26 (iobuf-start iobuf)))
28 (defun iobuf-full-p (iobuf)
29 (declare (type iobuf iobuf))
30 (= (iobuf-end iobuf)
31 (iobuf-size iobuf)))
33 (defun iobuf-end-space-length (iobuf)
34 (declare (type iobuf iobuf))
35 (- (iobuf-size iobuf)
36 (iobuf-end iobuf)))
38 (defun iobuf-reset (iobuf)
39 (declare (type iobuf iobuf))
40 (setf (iobuf-start iobuf) 0
41 (iobuf-end iobuf) 0))
44 ;;;
45 ;;; UNSAFE functions which *DO NOT* check boundaries
46 ;;; that must be done by their callers
47 ;;;
49 (defun bref (iobuf index)
50 (declare (type iobuf iobuf)
51 (type iobuf-index index))
52 (aref (iobuf-data iobuf) index))
54 (defun (setf bref) (octet iobuf index)
55 (declare (type ub8 octet)
56 (type iobuf iobuf)
57 (type iobuf-index index))
58 (setf (aref (iobuf-data iobuf) index) octet))
60 (defun iobuf-pop-octet (iobuf)
61 (declare (type iobuf iobuf))
62 (let ((start (iobuf-start iobuf)))
63 (prog1 (bref iobuf start)
64 (incf (iobuf-start iobuf)))))
66 (defun iobuf-push-octet (iobuf octet)
67 (declare (type iobuf iobuf)
68 (type (unsigned-byte 8) octet))
69 (let ((end (iobuf-end iobuf)))
70 (prog1 (setf (bref iobuf end) octet)
71 (incf (iobuf-end iobuf)))))