- (destructuring-bind (element-type) args
- `(let ((gslist ,gslist))
- (unwind-protect
- (map-glist 'list #'identity gslist ',element-type)
- (destroy-gslist gslist ',element-type)))))
-
-(defmethod from-alien-function ((type (eql 'gslist)) &rest args)
- (declare (ignore type))
- (destructuring-bind (element-type) args
- #'(lambda (gslist)
- (unwind-protect
- (map-glist 'list #'identity gslist element-type)
- (destroy-gslist gslist element-type)))))
-
-(defmethod copy-from-alien-form (gslist (type (eql 'gslist)) &rest args)
- (declare (ignore type))
- (destructuring-bind (element-type) args
- `(map-glist 'list #'identity ,gslist ',element-type)))
-
-(defmethod copy-from-alien-function ((type (eql 'gslist)) &rest args)
- (declare (ignore type))
- (destructuring-bind (element-type) args
- #'(lambda (gslist)
- (map-glist 'list #'identity gslist element-type))))
-
-(defmethod cleanup-form (gslist (type (eql 'gslist)) &rest args)
- (declare (ignore type))
- (destructuring-bind (element-type) args
- `(destroy-gslist ,gslist ',element-type)))
-
-(defmethod cleanup-function ((type (eql 'gslist)) &rest args)
- (declare (ignore type))
- (destructuring-bind (element-type) args
- #'(lambda (gslist)
- (destroy-gslist gslist element-type))))
-
-(defmethod writer-function ((type (eql 'gslist)) &rest args)
- (declare (ignore type))
- (destructuring-bind (element-type) args
- #'(lambda (list location &optional (offset 0))
- (setf
- (sap-ref-sap location offset)
- (make-gslist element-type list)))))
-
-(defmethod reader-function ((type (eql 'gslist)) &rest args)
- (declare (ignore type))
- (destructuring-bind (element-type) args
- #'(lambda (location &optional (offset 0))
- (unless (null-pointer-p (sap-ref-sap location offset))
- (map-glist 'list #'identity (sap-ref-sap location offset) element-type)))))
-
-(defmethod destroy-function ((type (eql 'gslist)) &rest args)
- (declare (ignore type))
- (destructuring-bind (element-type) args
- #'(lambda (location &optional (offset 0))
- (unless (null-pointer-p (sap-ref-sap location offset))
- (destroy-gslist (sap-ref-sap location offset) element-type)
- (setf (sap-ref-sap location offset) (make-pointer 0))))))
-
-
-;;; Vector
-
-(defun make-c-vector (type length &optional content location)
- (let* ((size-of-type (size-of type))
- (location (or location (allocate-memory (* size-of-type length))))
- (writer (writer-function type)))
- (etypecase content
- (vector
- (loop
- for element across content
- for i from 0 below length
- as offset = 0 then (+ offset size-of-type)
- do (funcall writer element location offset)))
- (list
- (loop
- for element in content
- for i from 0 below length
- as offset = 0 then (+ offset size-of-type)
- do (funcall writer element location offset))))
- location))
-
-
-(defun map-c-vector (seqtype function location element-type length)
- (let ((reader (reader-function element-type))
- (size-of-element (size-of element-type)))
- (case seqtype
- ((nil)
- (loop
- for i from 0 below length
- as offset = 0 then (+ offset size-of-element)
- do (funcall function (funcall reader location offset))))
- (list
- (loop
- for i from 0 below length
- as offset = 0 then (+ offset size-of-element)
- collect (funcall function (funcall reader location offset))))
- (t
- (loop
- with sequence = (make-sequence seqtype length)
- for i from 0 below length
- as offset = 0 then (+ offset size-of-element)
- do (setf
- (elt sequence i)
- (funcall function (funcall reader location offset)))
- finally (return sequence))))))
-
-
-(defun destroy-c-vector (location element-type length)
- (loop
- with destroy = (destroy-function element-type)
- with element-size = (size-of element-type)
- for i from 0 below length
- as offset = 0 then (+ offset element-size)
- do (funcall destroy location offset))
- (deallocate-memory location))