/
shareable-vectors.txt
44 lines (32 loc) · 1.5 KB
/
shareable-vectors.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# Shareable Byte Vectors
Function: make-shareable-byte-vector size
Create a vector of element type (UNSIGNED-BYTE 8) suitable for passing
to WITH-POINTER-TO-VECTOR-DATA.
;; Minimal implementation:
(defun make-shareable-byte-vector (size)
(make-array size :element-type '(unsigned-byte 8)))
Macro: with-pointer-to-vector-data (ptr-var vector) &body body
Bind PTR-VAR to a pointer to the data contained in a shareable byte
vector.
VECTOR must be a shareable vector created by MAKE-SHAREABLE-BYTE-VECTOR.
PTR-VAR may point directly into the Lisp vector data, or it may point
to a temporary block of foreign memory which will be copied to and
from VECTOR.
Both the pointer object in PTR-VAR and the memory it points to have
dynamic extent. The results are undefined if foreign code attempts to
access this memory outside this dynamic contour.
The implementation must guarantee the memory pointed to by PTR-VAR
will not be moved during the dynamic contour of this operator, either
by creating the vector in a static area or temporarily disabling the
garbage collector.
;; Minimal (copying) implementation:
(defmacro with-pointer-to-vector-data ((ptr-var vector) &body body)
(let ((vector-var (gensym))
(size-var (gensym)))
`(let* ((,vector-var ,vector)
(,size-var (length ,vector-var)))
(with-foreign-ptr (,ptr-var ,size-var)
(mem-write-vector ,vector-var ,ptr :uint8)
(prog1
(progn ,@body)
(mem-read-vector ,vector-var ,ptr-var :uint8 ,size-var))))))