-(defmethod copy-from-alien-form (c-vector (type (eql 'null-terminated-vector)) &rest args)
- (declare (ignore type))
- (destructuring-bind (element-type) args
- `(map-0-vector 'vector #'identity ,c-vector ',element-type)))
-
-(defmethod cleanup-form (location (type (eql 'null-terminated-vector)) &rest args)
- (declare (ignore type))
- (destructuring-bind (element-type) args
- `(destroy-0-vector ,location ',element-type)))
-
-(defmethod writer-function ((type (eql 'null-terminated-vector)) &rest args)
- (declare (ignore type))
- (destructuring-bind (element-type) args
- (unless (eq (alien-type element-type) (alien-type 'pointer))
- (error "Elements in null-terminated vectors need to be of pointer types"))
- #'(lambda (vector location &optional (offset 0))
- (setf
- (sap-ref-sap location offset)
- (make-0-vector element-type vector)))))
-
-(defmethod reader-function ((type (eql 'null-terminated-vector)) &rest args)
- (declare (ignore type))
- (destructuring-bind (element-type) args
- (unless (eq (alien-type element-type) (alien-type 'pointer))
- (error "Elements in null-terminated vectors need to be of pointer types"))
- #'(lambda (location &optional (offset 0) weak-p)
- (declare (ignore weak-p))
- (unless (null-pointer-p (sap-ref-sap location offset))
- (map-0-vector 'vector #'identity (sap-ref-sap location offset)
- element-type)))))
-
-(defmethod destroy-function ((type (eql 'null-terminated-vector)) &rest args)
- (declare (ignore type))
- (destructuring-bind (element-type) args
- (unless (eq (alien-type element-type) (alien-type 'pointer))
- (error "Elements in null-terminated vectors need to be of pointer types"))
- #'(lambda (location &optional (offset 0))
- (unless (null-pointer-p (sap-ref-sap location offset))
- (destroy-0-vector
- (sap-ref-sap location offset) element-type)
- (setf (sap-ref-sap location offset) (make-pointer 0))))))
-
-(defmethod unbound-value ((type (eql 'null-terminated-vector)) &rest args)
- (declare (ignore type args))
- (values t nil))
-
-
-;;; Counted vector
-
-(defun make-counted-vector (type content)
- (let* ((size-of-type (size-of type))
- (length (length content))
- (location
- (allocate-memory (+ +size-of-int+ (* size-of-type length)))))
- (setf (sap-ref-32 location 0) length)
- (make-c-vector type length content (sap+ location +size-of-int+))))
-
-(defun map-counted-vector (seqtype function location element-type)
- (let ((length (sap-ref-32 location 0)))
- (map-c-vector
- seqtype function (sap+ location +size-of-int+)
- element-type length)))
-
-(defun destroy-counted-vector (location element-type)
- (loop
- with destroy = (destroy-function element-type)
- with element-size = (size-of element-type)
- for i from 0 below (sap-ref-32 location 0)
- as offset = +size-of-int+ then (+ offset element-size)
- do (funcall destroy location offset))
- (deallocate-memory location))
-
-
-(deftype counted-vector (element-type) `(vector ,element-type))
-
-(defmethod alien-type ((type (eql 'counted-vector)) &rest args)
- (declare (ignore type args))
- (alien-type 'pointer))
-
-(defmethod size-of ((type (eql 'counted-vector)) &rest args)
- (declare (ignore type args))
- (size-of 'pointer))
-
-(defmethod to-alien-form (vector (type (eql 'counted-vector)) &rest args)
- (declare (ignore type))
- (destructuring-bind (element-type) args
- `(make-counted-vector ',element-type ,vector)))
-
-(defmethod from-alien-form (c-vector (type (eql 'counted-vector)) &rest args)
- (declare (ignore type))
- (destructuring-bind (element-type) args
- `(let ((c-vector ,c-vector))
- (prog1
- (map-counted-vector 'vector #'identity c-vector ',element-type)
- (destroy-counted-vector c-vector ',element-type)))))
-
-(defmethod copy-from-alien-form (c-vector (type (eql 'counted-vector)) &rest args)
- (declare (ignore type))
- (destructuring-bind (element-type) args
- `(map-counted-vector 'vector #'identity ,c-vector ',element-type)))
-
-(defmethod copy-from-alien-function ((type (eql 'counted-vector)) &rest args)
- (declare (ignore type))
- (destructuring-bind (element-type) args
- #'(lambda (c-vector)
- (map-counted-vector 'vector #'identity c-vector element-type))))
-
-(defmethod cleanup-form (location (type (eql 'counted-vector)) &rest args)
- (declare (ignore type))
- (destructuring-bind (element-type) args
- `(destroy-counted-vector ,location ',element-type)))
-
-(defmethod writer-function ((type (eql 'counted-vector)) &rest args)
- (declare (ignore type))
- (destructuring-bind (element-type) args
- #'(lambda (vector location &optional (offset 0))
- (setf
- (sap-ref-sap location offset)
- (make-counted-vector element-type vector)))))
-
-(defmethod reader-function ((type (eql 'counted-vector)) &rest args)
- (declare (ignore type))
- (destructuring-bind (element-type) args
- #'(lambda (location &optional (offset 0) weak-p)
- (declare (ignore weak-p))
- (unless (null-pointer-p (sap-ref-sap location offset))
- (map-counted-vector 'vector #'identity
- (sap-ref-sap location offset) element-type)))))
-
-(defmethod destroy-function ((type (eql 'counted-vector)) &rest args)
- (declare (ignore type))
- (destructuring-bind (element-type) args
+(define-type-method alien-arg-wrapper ((type gslist) var list style form &optional copy-in-p)
+ (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
+ (cond
+ ((and (in-arg-p style) (not (out-arg-p style)))
+ `(with-pointer (,var (make-gslist ',element-type ,list ,(not copy-in-p)))
+ (unwind-protect
+ ,form
+ ,(unless copy-in-p
+ `(destroy-gslist ,var ',element-type t)))))
+ ((and (in-arg-p style) (out-arg-p style))
+ (let ((gslist (make-symbol "GSLIST")))
+ `(with-pointer (,gslist (make-gslist ',element-type ,list ,(not copy-in-p)))
+ (with-pointer (,var ,gslist)
+ (unwind-protect
+ ,form
+ ,(unless copy-in-p
+ `(destroy-gslist ,gslist ',element-type t)))))))
+ ((and (out-arg-p style) (not (in-arg-p style)))
+ `(with-pointer (,var)
+ ,form)))))
+
+(define-type-method to-alien-form ((type gslist) list &optional copy-p)
+ (declare (ignore copy-p))
+ (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
+ `(make-gslist ',element-type ,list)))
+
+(define-type-method to-alien-function ((type gslist) &optional copy-p)
+ (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
+ (values
+ #'(lambda (list)
+ (make-gslist element-type list (not copy-p)))
+ (unless copy-p
+ #'(lambda (list gslist)
+ (declare (ignore list))
+ (destroy-gslist gslist element-type t))))))
+
+(define-type-method from-alien-form ((type gslist) form &key (ref :free))
+ (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
+ `(let ((gslist ,form))
+ (unwind-protect
+ (map-glist 'list #'identity gslist ',element-type
+ ,(ecase ref (:free :get) ((:static :temp) :peek) (:copy :read)))
+ ,(when (eq ref :free)
+ `(destroy-gslist gslist ',element-type))))))
+
+(define-type-method from-alien-function ((type gslist) &key (ref :free))
+ (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
+ (ecase ref
+ (:free
+ #'(lambda (glist)
+ (prog1
+ (map-glist 'list #'identity glist element-type :get)
+ (gslist-free glist))))
+ (:copy
+ #'(lambda (glist)
+ (map-glist 'list #'identity glist element-type :read)))
+ ((:static :temp)
+ #'(lambda (glist)
+ (map-glist 'list #'identity glist element-type :peek))))))
+
+(define-type-method writer-function ((type gslist) &key temp inlined)
+ (assert-not-inlined type inlined)
+ (destructuring-bind (element-type) (rest (type-expand-to 'gslist type))
+ (let ((element-writer (writer-function element-type :temp temp)))
+ #'(lambda (list location &optional (offset 0))
+ (setf
+ (ref-pointer location offset)
+ (make-gslist element-writer list))))))
+
+(define-type-method reader-function ((type gslist) &key (ref :read) inlined)
+ (assert-not-inlined type inlined)
+ (let ((element-type (second (type-expand-to 'gslist type))))
+ (ecase ref
+ ((:read :peek)
+ #'(lambda (location &optional (offset 0))
+ (unless (null-pointer-p (ref-pointer location offset))
+ (map-glist 'list #'identity (ref-pointer location offset) element-type ref))))
+ (:get
+ #'(lambda (location &optional (offset 0))
+ (unless (null-pointer-p (ref-pointer location offset))
+ (prog1
+ (map-glist 'list #'identity (ref-pointer location offset) element-type :get)
+ (gslist-free (ref-pointer location offset))
+ (setf (ref-pointer location offset) (make-pointer 0)))))))))
+
+(define-type-method destroy-function ((type gslist) &key temp inlined)
+ (assert-not-inlined type inlined)
+ (let ((element-type (second (type-expand-to 'gslist type))))