-(deftype-method translate-to-alien vector (type-spec vector &optional copy)
- (declare (ignore copy))
- (destructuring-bind (element-type &optional (length '*))
- (cdr (type-expand-to 'vector type-spec))
- (let ((element-to-alien (translate-to-alien element-type 'element :copy))
- (element-size (size-of element-type)))
- `(let ((vector ,vector))
- (let ((c-vector
- (allocate-memory
- ,(if (eq length '*)
- `(* ,element-size (length vector))
- (* element-size length)))))
- (dotimes (i ,(if (eq length '*) '(length vector) length) c-vector)
- (setf
- (,(sap-ref-fname element-type) c-vector (* i ,element-size))
- ,(translate-to-alien element-type '(svref vector i) :copy))))))))
-
-(deftype-method cleanup-alien vector (type-spec sap &optional copied)
- (declare (ignore type-spec copied))
- ;; The individual elements also have to be cleaned up to avoid memory leaks,
- ;; but this is currently not possible because we can't always tell the
- ;; length of the vector
- `(deallocate-memory ,sap))
+(defmethod to-alien-form (list (type (eql 'gslist)) &rest args)
+ (declare (ignore type))
+ (destructuring-bind (element-type) args
+ `(make-sglist ',element-type ,list)))
+
+(defmethod to-alien-function ((type (eql 'gslist)) &rest args)
+ (declare (ignore type))
+ (destructuring-bind (element-type) args
+ #'(lambda (list)
+ (make-gslist element-type list))))
+
+(defmethod from-alien-form (gslist (type (eql 'gslist)) &rest args)
+ (declare (ignore type))
+ (destructuring-bind (element-type) args
+ `(let ((gslist ,gslist))
+ (unwind-protect
+ (map-glist 'list #'identity gslist ',element-type)
+ (gslist-free gslist)))))
+
+(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)
+ (gslist-free gslist)))))
+
+(defmethod cleanup-form (list (type (eql 'gslist)) &rest args)
+ (declare (ignore type args))
+ `(gslist-free ,list))
+
+(defmethod cleanup-function ((type (eql 'gslist)) &rest args)
+ (declare (ignore type args))
+ #'gslist-free)
+
+
+
+;;; 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)))
+ (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))
+ 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))))))
+
+
+(defmethod alien-type ((type (eql 'vector)) &rest args)
+ (declare (ignore type args))
+ (alien-type 'pointer))
+
+(defmethod size-of ((type (eql 'vector)) &rest args)
+ (declare (ignore type args))
+ (size-of 'pointer))
+
+(defmethod to-alien-form (vector (type (eql 'vector)) &rest args)
+ (declare (ignore type))
+ (destructuring-bind (element-type &optional (length '*)) args
+ (if (eq length '*)
+ `(let* ((vector ,vector)
+ (location (sap+
+ (allocate-memory (+ ,+size-of-int+
+ (* ,(size-of element-type)
+ (length vector))))
+ ,+size-of-int+)))
+ (make-c-vector ',element-type (length vector) vector location)
+ (setf (sap-ref-32 location ,(- +size-of-int+)) (length vector))
+ location)
+ `(make-c-vector ',element-type ,length ,vector))))
+
+(defmethod from-alien-form (location (type (eql 'vector)) &rest args)
+ (declare (ignore type))
+ (destructuring-bind (element-type &optional (length '*)) args
+ (if (eq length '*)
+ (error "Can't use vector of variable size as return type")
+ `(map-c-vector 'vector #'identity ',element-type ',length ,location))))
+
+(defmethod cleanup-form (location (type (eql 'vector)) &rest args)
+ (declare (ignore type))
+ (destructuring-bind (element-type &optional (length '*)) args
+ `(let* ((location ,location)
+ (length ,(if (eq length '*)
+ `(sap-ref-32 location ,(- +size-of-int+))
+ length)))
+ (loop
+ with destroy = (destroy-function ',element-type)
+ for i from 0 below length
+ as offset = 0 then (+ offset ,(size-of element-type))
+ do (funcall destroy location offset))
+ (deallocate-memory ,(if (eq length '*)
+ `(sap+ location ,(- +size-of-int+))
+ 'location)))))