shareable-vectors.txt 1.5 KB

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