Debian Codegraph @

ext/vport/vport.scm

raw
1 ;;;
2 ;;; gauche.vport - virtual port
3 ;;;
4 ;;; Copyright (c) 2004-2024 Shiro Kawai <[email protected]>
5 ;;;
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
9 ;;;
10 ;;; 1. Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
12 ;;;
13 ;;; 2. Redistributions in binary form must reproduce the above copyright
14 ;;; notice, this list of conditions and the following disclaimer in the
15 ;;; documentation and/or other materials provided with the distribution.
16 ;;;
17 ;;; 3. Neither the name of the authors nor the names of its contributors
18 ;;; may be used to endorse or promote products derived from this
19 ;;; software without specific prior written permission.
20 ;;;
21 ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 ;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 ;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 ;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 ;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 ;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 ;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 ;;;
33
34 (define-module gauche.vport
35 (use gauche.uvector)
36 (use gauche.generator)
37 (use util.match)
38 (export <virtual-input-port>
39 <virtual-output-port>
40 <virtual-io-port>
41 <buffered-input-port>
42 <buffered-output-port>
43 open-input-uvector
44 open-input-bytevector
45 open-output-uvector get-output-uvector
46 open-output-bytevector get-output-bytevector
47 open-input-limited-length-port
48 open-input-char-list open-input-byte-list get-remaining-input-list
49 open-input-char-generator open-input-byte-generator
50 get-remaining-input-generator
51 open-output-char-accumulator open-output-byte-accumulator
52 open-output-accumulator
53 ))
54 (select-module gauche.vport)
55
56 (autoload data.queue make-mtqueue enqueue/wait! queue-push/wait! dequeue/wait!)
57 (autoload gauche.threads make-thread thread-start!)
58
59 (dynamic-load "gauche--vport")
60
61 ;;=======================================================
62 ;; A port backed up by an uniform vector
63 ;;
64
65 (define (open-input-uvector uvector)
66 (let* ([src (if (u8vector? uvector)
67 uvector
68 (uvector-alias <u8vector> uvector))]
69 [index 0]
70 [len (u8vector-length src)])
71 (define (filler buf)
72 (if (>= index len)
73 #f
74 (let1 req (u8vector-length buf)
75 (if (>= req (- len index))
76 (rlet1 count (- len index)
77 (u8vector-copy! buf 0 src index)
78 (inc! index count))
79 (begin
80 (u8vector-copy! buf 0 src index (+ index req))
81 (inc! index req)
82 req)))))
83 (define (seeker offset whence)
84 (cond [(= whence SEEK_SET)
85 (set! index (clamp offset 0 len))]
86 [(= whence SEEK_CUR)
87 (set! index (clamp (+ index offset) 0 len))]
88 [(= whence SEEK_END)
89 (set! index (clamp (+ len offset) 0 len))])
90 index)
91 (make <buffered-input-port> :fill filler :seek seeker)))
92
93 (define (open-input-bytevector bv) ; R7RS
94 (assume-type bv <u8vector>)
95 (open-input-uvector bv))
96
97 ;; For output uvector, we keep backing storage info in port attributes so that
98 ;; we can retrieve it by get-output-uvector.
99 ;; The info is #(<buffer> <max-index> <target-class>), where <buffer> is
100 ;; u8vector (possibly aliased), <max-index> is the maximum index during
101 ;; write operation, and <target-class> is the original u8vector class.
102 (define (%make-uvector-storage uvector)
103 (vector (if (u8vector? uvector) uvector (uvector-alias <u8vector> uvector))
104 0 (class-of uvector)))
105
106 ;; Ensure the backing storage can contain at least next-index octets.
107 ;; We adopt allocate-and-copy strategy. Although this is amortized O(1),
108 ;; it may not be ideal for our GC; using chained chunks may behave better.
109 ;; Let's see.
110 (define (%extend-uvector-storage! storage next-index)
111 (let* ([cur-buffer (vector-ref storage 0)]
112 [cur-size (uvector-size cur-buffer)])
113 (when (> next-index cur-size)
114 (let* ([next-size (ash 1 (integer-length (- next-index 1)))] ;round up
115 [next-buffer (make-uvector <u8vector> next-size)])
116 (u8vector-copy! next-buffer 0 cur-buffer)
117 (set! (vector-ref storage 0) next-buffer)))))
118
119 (define (%storage-buffer storage) (vector-ref storage 0))
120 (define (%storage-size storage) (uvector-size (%storage-buffer storage)))
121 (define (%storage-get-uvector storage)
122 (match-let1 #(buffer max-index class) storage
123 (let ([aligned-index (if (memv class (list <u8vector> <s8vector>))
124 max-index
125 (let1 align (uvector-class-element-size class)
126 (* (quotient max-index align) align)))])
127 (uvector-alias class buffer 0 aligned-index))))
128 (define (%storage-max-index storage) (vector-ref storage 1))
129
130 (define (%update-storage-max-index! storage max-index)
131 (update! (vector-ref storage 1) (cut max <> max-index)))
132
133 (define (open-output-uvector :optional (uvector #f) :key (extendable #f))
134 ;; If uvector isn't provided, extendable is #t.
135 ;; If uvector is provided, extendable defaults to #f.
136 ;; This is for the backward compatibility, though confusing.
137 (let* ([extendable (if uvector extendable #t)]
138 [uvector (or uvector '#u8())]
139 [storage (%make-uvector-storage uvector)]
140 [index 0])
141 (define (set-index! val)
142 (%update-storage-max-index! storage val)
143 (set! index val))
144 (define (flusher buf flush?)
145 (rlet1 req (u8vector-length buf)
146 (if (> req (- (%storage-size storage) index))
147 ;; buffer is too small. if we're extendable, realloc the buffer.
148 ;; otherwise we just discard the excess data.
149 (if extendable
150 (begin
151 (%extend-uvector-storage! storage (+ index req))
152 (flusher buf flush?))
153 (let1 count (- (%storage-size storage) index)
154 (u8vector-copy! (%storage-buffer storage) index buf 0 count)
155 (set-index! (+ index count))))
156 (begin ; normal path
157 (u8vector-copy! (%storage-buffer storage) index buf 0 req)
158 (set-index! (+ index req))))))
159 (define (seeker offset whence)
160 (define (do-seek new-index)
161 (when extendable (%extend-uvector-storage! storage new-index))
162 (set-index! (clamp new-index 0 (%storage-size storage))))
163 (cond [(= whence SEEK_SET) (do-seek offset)]
164 [(= whence SEEK_CUR) (do-seek (+ index offset))]
165 [(= whence SEEK_END)
166 ;; the meaning of 'end' differ if the storage is extendable or not.
167 ;; if it's extendable, the rightmost end of the data ever written
168 ;; is the 'end' whence; if fixed sized storage, end is the
169 ;; end of the given storage.
170 (if extendable
171 (do-seek (+ (%storage-max-index storage) offset))
172 (do-seek (+ (%storage-size storage) offset)))])
173 index)
174 (rlet1 port (make <buffered-output-port> :flush flusher :seek seeker)
175 (port-attribute-set! port 'uvector-port-backing-storage storage))))
176
177 (define (get-output-uvector port :key (shared #f))
178 (with-port-locking port
179 (^[]
180 (and-let* ([storage
181 (port-attribute-ref port 'uvector-port-backing-storage #f)])
182 (flush port) ; ensure buffered output is written to the storage
183 (let1 vec (%storage-get-uvector storage)
184 (if shared
185 vec
186 (uvector-copy vec)))))))
187
188 (define (open-output-bytevector) (open-output-uvector)) ;R7RS
189 (define (get-output-bytevector port)
190 (or (get-output-uvector port)
191 (error "get-output-bytevector needs a output uvector port, but got:"
192 port)))
193
194 ;;=======================================================
195 ;; A port with limited-length input/output
196 ;;
197
198 (define (open-input-limited-length-port source limit
199 :key (limit-reached #f)
200 (eof-reached #f) (closed #f))
201 (let ([nrest limit]
202 [eof #f])
203 (define (filler buf)
204 (if (or (<= nrest 0) eof)
205 (if limit-reached (limit-reached buf) 0)
206 (let* ([len (u8vector-length buf)]
207 [nread (read-block! buf source 0 (min nrest len))])
208 (cond [(eof-object? nread)
209 (set! eof #t)
210 (if eof-reached (eof-reached buf) 0)]
211 [else
212 (dec! nrest nread)
213 nread]))))
214 (define (closer)
215 (when closed (closed)))
216 (make <buffered-input-port> :fill filler :close closer)))
217
218 ;;=======================================================
219 ;; A port read from list of chars or bytes
220 ;;
221
222 (define (open-input-char-list lis)
223 (define (getc)
224 (if (null? lis)
225 (eof-object)
226 (rlet1 c (pop! lis)
227 (unless (char? c)
228 (error "input-char-list: source contains non-char value:" c)))))
229 (rlet1 p (make <virtual-input-port> :getc getc)
230 (define (getlist)
231 (let1 cc ((with-module gauche.internal %port-ungotten-chars) p)
232 (append cc lis)))
233 (port-attribute-set! p 'get-input-list getlist)))
234
235 (define (open-input-byte-list lis)
236 (define (getb)
237 (if (null? lis)
238 (eof-object)
239 (rlet1 b (pop! lis)
240 (unless (and (integer? b) (<= 0 b 255))
241 (error "input-byte-list: source contains other than bytes:" b)))))
242 (rlet1 p (make <virtual-input-port> :getb getb)
243 (define (getlist)
244 (let ([bb ((with-module gauche.internal %port-ungotten-bytes) p)]
245 [cc ((with-module gauche.internal %port-ungotten-chars) p)])
246 (concatenate `(,@(map %char->bytes cc) ,bb ,lis))))
247 (port-attribute-set! p 'get-input-list getlist)))
248
249 (define (get-remaining-input-list p)
250 (if-let1 getlist (port-attribute-ref p 'get-input-list)
251 (getlist)
252 '()))
253
254 ;; Common utility used to convert ungotten char to byte list
255 (define (%char->bytes c)
256 (let1 b (char->integer c)
257 (if (< b #x80)
258 (list b)
259 ;; this is inefficient, but we assume it's a rare path
260 (u8vector->list (string->u8vector (string c))))))
261
262
263 ;;=======================================================
264 ;; A port read from generators
265 ;;
266
267 (define (open-input-char-generator gen)
268 (define (getc)
269 (rlet1 c (gen)
270 (unless (or (char? c) (eof-object? c))
271 (error "input-char-generator: source yields non-char value:" c))))
272 (rlet1 p (make <virtual-input-port> :getc getc)
273 (define (getlist)
274 (let1 cc ((with-module gauche.internal %port-ungotten-chars) p)
275 (%gprepend cc gen)))
276 (port-attribute-set! p 'get-input-generator getlist)))
277
278 (define (open-input-byte-generator gen)
279 (define (getb)
280 (rlet1 b (gen)
281 (unless (or (eof-object? b) (and (integer? b) (<= 0 b 255)))
282 (error "input-byte-generator: source yields other than bytes:" b))))
283 (rlet1 p (make <virtual-input-port> :getb getb)
284 (define (getgen)
285 (let ([bb ((with-module gauche.internal %port-ungotten-bytes) p)]
286 [cc ((with-module gauche.internal %port-ungotten-chars) p)])
287 (%gprepend (append (append-map %char->bytes cc) bb) gen)))
288 (port-attribute-set! p 'get-input-generator getgen)))
289
290 (define (get-remaining-input-generator p)
291 (if-let1 getgen (port-attribute-ref p 'get-input-generator)
292 (getgen)
293 eof-object))
294
295 (define (%gprepend lis gen) (gunfold null? car cdr lis (^_ gen)))
296
297 ;;=======================================================
298 ;; A port write to accumulator
299 ;;
300
301 (define (open-output-char-accumulator acc)
302 (define (putc c) (acc c))
303 (define (puts s) (string-for-each acc s))
304 (define (close) (acc (eof-object)))
305 (make <virtual-output-port> :putc putc :puts puts :close close))
306
307 (define (open-output-byte-accumulator acc)
308 (define (putb b) (acc b))
309 (define (close) (acc (eof-object)))
310 (make <virtual-output-port> :putb putb :close close))
311
312 (define (open-output-accumulator acc)
313 (define (putb b) (acc b))
314 (define (putc c) (acc c))
315 (define (puts s) (acc s))
316 (define (close) (acc (eof-object)))
317 (make <virtual-output-port> :putb putb :putc putc :puts puts :close close))