1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Indent-tabs-mode: NIL -*-
3 ;;; buffer.lisp --- Foreign memory buffers.
5 ;;; Copyright (C) 2006-2008, Stelian Ionescu <sionescu@common-lisp.net>
7 ;;; This code is free software; you can redistribute it and/or
8 ;;; modify it under the terms of the version 2.1 of
9 ;;; the GNU Lesser General Public License as published by
10 ;;; the Free Software Foundation, as clarified by the
11 ;;; preamble found here:
12 ;;; http://opensource.franz.com/preamble.html
14 ;;; This program is distributed in the hope that it will be useful,
15 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;;; GNU General Public License for more details.
19 ;;; You should have received a copy of the GNU Lesser General
20 ;;; Public License along with this library; if not, write to the
21 ;;; Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 ;;; Boston, MA 02110-1301, USA
24 (in-package :io.zeta-streams
)
28 (define-constant +bytes-per-iobuf
+ 4096)
30 ;;; FIXME: make this right
31 ;;; probably not all SIMPLE-ARRAYs are admissible
32 ;;; on all implementations
33 (deftype compatible-lisp-array
()
34 '(simple-array * (*)))
36 (declaim (inline allocate-iobuf iobuf-length iobuf-start-pointer
37 iobuf-end-pointer iobuf-end-space-length
38 iobuf-empty-p iobuf-full-p
39 iobuf-reset iobuf-copy-data-to-start
40 bref
(setf bref
) iobuf-copy
41 iobuf-pop-octet iobuf-push-octet
))
43 (defun make-iobuf (&optional
(size +bytes-per-iobuf
+))
44 (let ((b (%make-iobuf
)))
45 (setf (iobuf-data b
) (foreign-alloc :uint8
:count size
)
49 (defun iobuf-length (iobuf)
50 (declare (type iobuf iobuf
))
54 (defun iobuf-start-pointer (iobuf data-pointer
)
55 (declare (type iobuf iobuf
)
56 (type cffi
:foreign-pointer
))
57 (inc-pointer data-pointer
(iobuf-start iobuf
)))
59 (defun iobuf-end-pointer (iobuf data-pointer
)
60 (declare (type iobuf iobuf
)
61 (type cffi
:foreign-pointer
))
62 (inc-pointer data-pointer
(iobuf-end iobuf
)))
64 (defun iobuf-empty-p (iobuf)
65 (declare (type iobuf iobuf
))
69 (defun iobuf-full-p (iobuf)
70 (declare (type iobuf iobuf
))
74 (defun iobuf-end-space-length (iobuf)
75 (declare (type iobuf iobuf
))
79 (defun iobuf-reset (iobuf)
80 (declare (type iobuf iobuf
))
81 (setf (iobuf-start iobuf
) 0
84 (defun iobuf-copy-data-to-start (iobuf)
85 (declare (type iobuf iobuf
))
86 (cffi:with-pointer-to-vector-data
(ptr (iobuf-data iobuf
))
87 (nix:memmove ptr
(cffi:inc-pointer ptr
(iobuf-start iobuf
))
88 (iobuf-length iobuf
)))
89 (setf (iobuf-end iobuf
) (iobuf-length iobuf
))
90 (setf (iobuf-start iobuf
) 0))
92 ;;; BREF, (SETF BREF) and BUFFER-COPY *DO NOT* check boundaries
93 ;;; that must be done by their callers
94 (defun bref (iobuf index
)
95 (declare (type iobuf iobuf
)
96 (type iobuf-index index
))
97 (aref (iobuf-data iobuf
) index
))
99 (defun (setf bref
) (octet iobuf index
)
100 (declare (type ub8 octet
)
102 (type iobuf-index index
))
103 (setf (aref (iobuf-data iobuf
) index
) octet
))
105 (defun iobuf-pop-octet (iobuf)
106 (declare (type iobuf iobuf
))
107 (let ((start (iobuf-start iobuf
)))
108 (prog1 (bref iobuf start
)
109 (incf (iobuf-start iobuf
)))))
111 (defun iobuf-push-octet (iobuf octet
)
112 (declare (type iobuf iobuf
)
113 (type (unsigned-byte 8) octet
))
114 (let ((end (iobuf-end iobuf
)))
115 (prog1 (setf (bref iobuf end
) octet
)
116 (incf (iobuf-end iobuf
)))))