<div dir="ltr"><div><div>My first cut at this makes me think that the translate-from-foreign and w-f-o should trade places. Here's a selected macroexpansion of GSLL's cx-add:<br><br>(LET ()<br>  (DEFUN CX-ADD (C1 C2)<br>

    (DECLARE)<br>    (LET ((#:CRETURN<br>           (WITH-FOREIGN-OBJECT (#:G1705 '(:STRUCT GRID:COMPLEX-DOUBLE-C))<br>             (TRANSLATE-INTO-FOREIGN-MEMORY C1<br>                                            #<GRID::COMPLEX-DOUBLE-TYPE<br>

                                              GRID:COMPLEX-DOUBLE-C><br>                                            #:G1705)<br>             (WITH-FOREIGN-OBJECT (#:G1706 '(:STRUCT GRID:COMPLEX-DOUBLE-C))<br>               (TRANSLATE-INTO-FOREIGN-MEMORY C2<br>

                                              #<GRID::COMPLEX-DOUBLE-TYPE<br>                                                GRID:COMPLEX-DOUBLE-C><br>                                              #:G1706)<br>               (TRANSLATE-FROM-FOREIGN   ;;;   <=========== this should be after the next two lines<br>

                (WITH-FOREIGN-OBJECTS ((CFFI::ARGVALUES :POINTER 2)<br>                                       (CFFI::RESULT '(:STRUCT GRID:COMPLEX-DOUBLE-C)))<br>                  (LOOP :FOR CFFI::ARG :IN (LIST #:G1705 #:G1706)<br>

                        :FOR<br>                        COUNT :FROM 0<br>                        :DO (SETF (MEM-AREF CFFI::ARGVALUES :POINTER COUNT) CFFI::ARG))<br>                  (CFFI::CALL<br>                   (CFFI::PREPARE-FUNCTION "gsl_complex_add"<br>

                                           '(:STRUCT GRID:COMPLEX-DOUBLE-C)<br>                                           '((:STRUCT GRID:COMPLEX-DOUBLE-C)<br>                                             (:STRUCT GRID:COMPLEX-DOUBLE-C))<br>

                                           ':DEFAULT-ABI)<br>                   (FOREIGN-SYMBOL-POINTER "gsl_complex_add") CFFI::RESULT CFFI::ARGVALUES)<br>                  CFFI::RESULT)<br>                #<GRID::COMPLEX-DOUBLE-TYPE GRID:COMPLEX-DOUBLE-C>)))))<br>

      #:CRETURN))<br>  (MAP-NAME 'CX-ADD "gsl_complex_add")<br>  (EXPORT 'CX-ADD))<br><br></div><div>So that means some mashing around the other definitions, as the translate-from-foreign comes in outside the ffcall-body-libffi. As to why it works in SBCL (and maybe CCL): um, luck? <br>

</div><br></div>Liam<br><div><div><br></div></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Apr 18, 2014 at 2:01 PM, Cyrus Harmon <span dir="ltr"><<a href="mailto:ch-lisp@bobobeach.com" target="_blank">ch-lisp@bobobeach.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
So I’ve been trying to get FSBV working on ABCL and I’ve run into a problem with returning structs. The problem is that we allocate memory on the stack for the return struct, successfully call the foreign function, writing the return value into the appropriate place, then we free the memory and return a pointer to the now defunct memory location.<br>


<br>
I’m curious as to how this is supposed to work in other implementations. It obviously does work in, at least, SBCL, so I must be doing something wrong. But looking at the ffcall-body-libffi function it looks like we setup the foreign memory for RESULT in with-foreign-objects and then if we have a translatable-foreign-type, we return RESULT, which of course gets freed by with-foreign-objects. What am I missing here?<br>


<br>
thanks,<br>
<br>
Cyrus<br>
<br>
<br>
(defun ffcall-body-libffi<br>
    (function symbols return-type argument-types &optional pointerp (abi :default-abi))<br>
  "A body of foreign-funcall calling the libffi function #'call (ffi_call)."<br>
  (let ((number-of-arguments (length argument-types)))<br>
    `(with-foreign-objects<br>
         ((argvalues :pointer ,number-of-arguments)<br>
          ,@(unless (eql return-type :void)<br>
              `((result ',return-type))))<br>
       (loop :for arg :in (list ,@symbols)<br>
             :for count :from 0<br>
             :do (setf (mem-aref argvalues :pointer count) arg))<br>
       (call<br>
        (prepare-function ,function ',return-type ',argument-types ',abi)<br>
        ,(if pointerp<br>
             function<br>
             `(foreign-symbol-pointer ,function))<br>
        ,(if (eql return-type :void) '(null-pointer) 'result)<br>
        argvalues)<br>
       ,(if (eql return-type :void)<br>
            '(values)<br>
            (if (typep (parse-type return-type) 'translatable-foreign-type)<br>
                ;; just return the pointer so that expand-from-foreign<br>
                ;; can apply translate-from-foreign<br>
                'result<br>
                ;; built-in types won't be translated by<br>
                ;; expand-from-foreign, we have to do it here<br>
                `(mem-aref result ',return-type))))))<br>
_______________________________________________<br>
Cffi-devel mailing list<br>
<a href="mailto:Cffi-devel@common-lisp.net">Cffi-devel@common-lisp.net</a><br>
<a href="http://common-lisp.net/cgi-bin/mailman/listinfo/cffi-devel" target="_blank">http://common-lisp.net/cgi-bin/mailman/listinfo/cffi-devel</a><br>
</blockquote></div><br></div>